|
| 1 | +# This workflow is centrally managed in https://github.com/asyncapi/.github/ |
| 2 | +# Don't make changes to this file in this repo as they will be overwritten with changes made to the same file in above mentioned repo |
| 3 | + |
| 4 | +# This workflow will run on every comment with /update or /u. And will create merge-commits for the PR. |
| 5 | +# This also works with forks, not only with branches in the same repository/organization. |
| 6 | +# Currently, does not work with forks in different organizations. |
| 7 | + |
| 8 | +# This workflow will be distributed to all repositories in the AsyncAPI organization |
| 9 | + |
| 10 | +name: Update PR branches from fork |
| 11 | + |
| 12 | +on: |
| 13 | + issue_comment: |
| 14 | + types: [created] |
| 15 | + |
| 16 | +jobs: |
| 17 | + update-pr: |
| 18 | + if: > |
| 19 | + startsWith(github.repository, 'asyncapi/') && |
| 20 | + github.event.issue.pull_request && |
| 21 | + github.event.issue.state != 'closed' && ( |
| 22 | + contains(github.event.comment.body, '/update') || |
| 23 | + contains(github.event.comment.body, '/u') |
| 24 | + ) |
| 25 | + runs-on: ubuntu-latest |
| 26 | + steps: |
| 27 | + - name: Get Pull Request Details |
| 28 | + id: pr |
| 29 | + uses: actions/github-script@v7 |
| 30 | + with: |
| 31 | + github-token: ${{ secrets.GH_TOKEN || secrets.GITHUB_TOKEN }} |
| 32 | + previews: 'merge-info-preview' # https://docs.github.com/en/graphql/overview/schema-previews#merge-info-preview-more-detailed-information-about-a-pull-requests-merge-state-preview |
| 33 | + script: | |
| 34 | + const prNumber = context.payload.issue.number; |
| 35 | + core.debug(`PR Number: ${prNumber}`); |
| 36 | + const { data: pr } = await github.rest.pulls.get({ |
| 37 | + owner: context.repo.owner, |
| 38 | + repo: context.repo.repo, |
| 39 | + pull_number: prNumber |
| 40 | + }); |
| 41 | +
|
| 42 | + // If the PR has conflicts, we don't want to update it |
| 43 | + const updateable = ['behind', 'blocked', 'unknown', 'draft', 'clean'].includes(pr.mergeable_state); |
| 44 | + console.log(`PR #${prNumber} is ${pr.mergeable_state} and is ${updateable ? 'updateable' : 'not updateable'}`); |
| 45 | + core.setOutput('updateable', updateable); |
| 46 | +
|
| 47 | + core.debug(`Updating PR #${prNumber} with head ${pr.head.sha}`); |
| 48 | +
|
| 49 | + return { |
| 50 | + id: pr.node_id, |
| 51 | + number: prNumber, |
| 52 | + head: pr.head.sha, |
| 53 | + } |
| 54 | + - name: Update the Pull Request |
| 55 | + if: steps.pr.outputs.updateable == 'true' |
| 56 | + uses: actions/github-script@v7 |
| 57 | + with: |
| 58 | + github-token: ${{ secrets.GH_TOKEN || secrets.GITHUB_TOKEN }} |
| 59 | + script: | |
| 60 | + const mutation = `mutation update($input: UpdatePullRequestBranchInput!) { |
| 61 | + updatePullRequestBranch(input: $input) { |
| 62 | + pullRequest { |
| 63 | + mergeable |
| 64 | + } |
| 65 | + } |
| 66 | + }`; |
| 67 | +
|
| 68 | + const pr_details = ${{ steps.pr.outputs.result }}; |
| 69 | +
|
| 70 | + try { |
| 71 | + const { data } = await github.graphql(mutation, { |
| 72 | + input: { |
| 73 | + pullRequestId: pr_details.id, |
| 74 | + expectedHeadOid: pr_details.head, |
| 75 | + } |
| 76 | + }); |
| 77 | + } catch (GraphQLError) { |
| 78 | + core.debug(GraphQLError); |
| 79 | + if ( |
| 80 | + GraphQLError.name === 'GraphqlResponseError' && |
| 81 | + GraphQLError.errors.some( |
| 82 | + error => error.type === 'FORBIDDEN' || error.type === 'UNAUTHORIZED' |
| 83 | + ) |
| 84 | + ) { |
| 85 | + // Add comment to PR if the bot doesn't have permissions to update the PR |
| 86 | + const comment = `Hi @${context.actor}. Update of PR has failed. It can be due to one of the following reasons: |
| 87 | + - I don't have permissions to update this PR. To update your fork with upstream using bot you need to enable [Allow edits from maintainers](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork) option in the PR. |
| 88 | + - The fork is located in an organization, not under your personal profile. No solution for that. You are on your own with manual update. |
| 89 | + - There may be a conflict in the PR. Please resolve the conflict and try again.`; |
| 90 | +
|
| 91 | + await github.rest.issues.createComment({ |
| 92 | + owner: context.repo.owner, |
| 93 | + repo: context.repo.repo, |
| 94 | + issue_number: context.issue.number, |
| 95 | + body: comment |
| 96 | + }); |
| 97 | +
|
| 98 | + core.setFailed('Bot does not have permissions to update the PR'); |
| 99 | + } else { |
| 100 | + core.setFailed(GraphQLError.message); |
| 101 | + } |
| 102 | + } |
0 commit comments