|
| 1 | +name: "Update upstream dependency" |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + secrets: |
| 6 | + ENZYMEAD_BOT_ID: |
| 7 | + required: true |
| 8 | + ENZYMEAD_BOT_PRIVATE_KEY: |
| 9 | + required: true |
| 10 | + inputs: |
| 11 | + upstream_repo: |
| 12 | + description: 'The GitHub repository of the upstream dependency' |
| 13 | + required: true |
| 14 | + type: 'string' |
| 15 | + upstream_commit: |
| 16 | + description: 'The commit of the upstream dependency to update to (optional)' |
| 17 | + required: false |
| 18 | + type: 'string' |
| 19 | + default: '' |
| 20 | + variable_name: |
| 21 | + description: 'The name of the Bazel variable referencing the commit to update' |
| 22 | + required: true |
| 23 | + type: 'string' |
| 24 | + base_branch: |
| 25 | + description: 'Name of the base branch against which to open the PR (optional, defaults to main)' |
| 26 | + required: false |
| 27 | + type: 'string' |
| 28 | + default: 'main' |
| 29 | + |
| 30 | +jobs: |
| 31 | + pr-update-dep: |
| 32 | + name: 'Update dependency' |
| 33 | + runs-on: ubuntu-latest |
| 34 | + timeout-minutes: 10 |
| 35 | + |
| 36 | + steps: |
| 37 | + - uses: actions/create-github-app-token@v2 |
| 38 | + id: generate_token |
| 39 | + with: |
| 40 | + app-id: "${{ secrets.ENZYMEAD_BOT_ID }}" |
| 41 | + private-key: "${{ secrets.ENZYMEAD_BOT_PRIVATE_KEY }}" |
| 42 | + - uses: actions/checkout@v5 |
| 43 | + - name: Set branch name |
| 44 | + run: | |
| 45 | + echo "BRANCH_NAME=update-$(echo ${{ inputs.upstream_repo }} | tr '/A-Z' '-a-z')" >> "${GITHUB_ENV}" |
| 46 | + - name: Prepare commit message |
| 47 | + run: | |
| 48 | + echo "COMMIT_MSG_PREFIX=Update ${{ inputs.upstream_repo }} to commit " >> "${GITHUB_ENV}" |
| 49 | + - name: Get upstream commit |
| 50 | + shell: bash |
| 51 | + run: | |
| 52 | + OLD_UPSTREAM_COMMIT=$(grep '${{ inputs.variable_name }} *= *".*"' workspace.bzl | cut -d= -f2 | tr -d '" ') |
| 53 | +
|
| 54 | + if [[ -n '${{ inputs.upstream_commit }}' ]]; then |
| 55 | + NEW_UPSTREAM_COMMIT="${{ inputs.upstream_commit }}" |
| 56 | + else |
| 57 | + NEW_UPSTREAM_COMMIT="$(git ls-remote https://github.com/${{ inputs.upstream_repo }}.git main | awk '{print $1}')" |
| 58 | + fi |
| 59 | +
|
| 60 | + echo "OLD_UPSTREAM_COMMIT=${OLD_UPSTREAM_COMMIT}" >> "${GITHUB_ENV}" |
| 61 | + echo "NEW_UPSTREAM_COMMIT=${NEW_UPSTREAM_COMMIT}" >> "${GITHUB_ENV}" |
| 62 | + echo "DIFF_MSG=Diff: https://github.com/${{ inputs.upstream_repo }}/compare/${OLD_UPSTREAM_COMMIT}...${NEW_UPSTREAM_COMMIT}" >> "${GITHUB_ENV}" |
| 63 | + - name: Modify upstream commit |
| 64 | + run: | |
| 65 | + sed -i 's/${{ inputs.variable_name }} *= *".*"/${{ inputs.variable_name }} = "${{ env.NEW_UPSTREAM_COMMIT }}"/' workspace.bzl |
| 66 | + - name: Decide whether to open the pull request |
| 67 | + id: decide |
| 68 | + run: | |
| 69 | + # Fetch latest commit on the branch |
| 70 | + LATEST_COMMIT_MESSAGE="$(curl -s -H 'Authorization: token ${{ secrets.GITHUB_TOKEN }}' 'https://api.github.com/repos/${{ github.repository }}/commits/${{ env.BRANCH_NAME }}' | jq -r '.commit.message')" |
| 71 | + echo "Latest commit message on the branch '${{ env.BRANCH_NAME }}' is:" |
| 72 | + echo "----------" |
| 73 | + echo "${LATEST_COMMIT_MESSAGE}" |
| 74 | + echo "----------" |
| 75 | + if [[ "${LATEST_COMMIT_MESSAGE}" == "null" || "${LATEST_COMMIT_MESSAGE}" == "${{ env.COMMIT_MSG_PREFIX }}"* ]]; then |
| 76 | + # Open the PR only if the target branch doesn't exist or latest commit message |
| 77 | + # starts with the expected prefix. |
| 78 | + echo "We're going to open the pull request..." |
| 79 | + echo "should_pr=true" >> "${GITHUB_OUTPUT}" |
| 80 | + else |
| 81 | + # Otherwise there are other commits to the branch and we don't want to |
| 82 | + # override them! |
| 83 | + echo "There seem to be extra commits, we won't open the pull request" |
| 84 | + echo "should_pr=false" >> "${GITHUB_OUTPUT}" |
| 85 | + fi |
| 86 | + - name: Create Pull Request |
| 87 | + if: ${{ steps.decide.outputs.should_pr == 'true' }} |
| 88 | + uses: peter-evans/create-pull-request@v7 |
| 89 | + with: |
| 90 | + token: ${{ steps.generate_token.outputs.token }} |
| 91 | + commit-message: | |
| 92 | + ${{ env.COMMIT_MSG_PREFIX }}${{ env.NEW_UPSTREAM_COMMIT }} |
| 93 | +
|
| 94 | + ${{ env.DIFF_MSG }} |
| 95 | + body: ${{ env.DIFF_MSG }} |
| 96 | + base: ${{ inputs.base_branch }} |
| 97 | + branch: ${{ env.BRANCH_NAME }} |
| 98 | + title: '${{ env.COMMIT_MSG_PREFIX }}${{ env.NEW_UPSTREAM_COMMIT }}' |
| 99 | + delete-branch: true |
| 100 | + draft: false |
| 101 | + author: 'enzymead-bot[bot] <238314553+enzymead-bot[bot]@users.noreply.github.com>' |
| 102 | + labels: 'dependencies' |
0 commit comments