---
name: Updating the SDK

on:
  workflow_dispatch:
  push:
    branches:
      - master
      - develop

jobs:
  updating_SDK:
    name: Updating the SDK
    runs-on: ubuntu-latest

    steps:
      - name: Clone
        uses: actions/checkout@v3
        with:
          # by default the action uses fetch-depth = 1, which creates
          # shallow repositories from which we can't push
          fetch-depth: 0
          submodules: recursive
          # needed, else the push inside the action will use default credentials
          # instead of provided ones
          persist-credentials: false

      - name: Build new SDK
        run: ./tools/build_sdk.sh

      - name: Extract branch name
        shell: bash
        run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
        id: extract_branch

      - name: Commit & push changes in the SDK if any
        uses: ./.github/actions/commit-changes
        with:
          name: 'ldg-github-ci'
          directory: ethereum-plugin-sdk
          dst_branch: ${{ steps.extract_branch.outputs.branch }}
          message: "[update] Branch ${{ steps.extract_branch.outputs.branch }} | Commit ${GITHUB_SHA}"
          secret: ${{ secrets.CI_BOT_TOKEN }}
          repository: LedgerHQ/ethereum-plugin-sdk

      - name: Create SDK update pull request
        uses: peter-evans/create-pull-request@v4
        with:
          branch: sdk/update-submodule
          delete-branch: true
          title: Update the SDK submodule
          reviewers: apailler-ledger
