From 5926fdf0685fc536324d1029afddc54876498956 Mon Sep 17 00:00:00 2001 From: AJ Rice <53190766+ajrice6713@users.noreply.github.com> Date: Fri, 18 Nov 2022 16:08:36 -0500 Subject: [PATCH 1/2] Merge PR automatically when dispatch `Merge` event received --- .github/workflows/listener.yml | 81 +++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 36 deletions(-) diff --git a/.github/workflows/listener.yml b/.github/workflows/listener.yml index 7943c53af..90b33cd4c 100644 --- a/.github/workflows/listener.yml +++ b/.github/workflows/listener.yml @@ -1,12 +1,12 @@ -# Ingest repository_dispatch events and trigger appropriate workflows +# Ingest repository_dispatch events and trigger appropriate workflows name: Listener on: repository_dispatch: types: [Open, Update, Merge, Close, Draft] -jobs: - open-update: +jobs: + open-update: if: ${{ github.event.action == 'Open' || github.event.action == 'Update' }} runs-on: ubuntu-latest steps: @@ -18,90 +18,99 @@ jobs: BRANCH_NAME=$(echo "$BRANCH_NAME" | tr '[:upper:]' '[:lower:]') BRANCH_NAME=$(echo "$BRANCH_NAME" | sed -e "s/[^a-z0-9]/-/g") echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV - + - uses: ./.github/actions/replace_specs_snippets id: replace-specs-snippets - with: + with: branch-name: ${{ github.event.client_payload.branchName }} username: ${{ secrets.DX_GITHUB_USERNAME }} token: ${{ secrets.DX_GITHUB_TOKEN }} origin-repo: ${{ github.event.client_payload.originRepo }} author: ${{ github.event.client_payload.author }} - + - name: Open Pull Request if: steps.replace-specs-snippets.outputs.needs_pr run: | hub pull-request --base Bandwidth:main $(if ${{ github.event.client_payload.draftPr }} ; then echo "-d" ; fi) -m '${{ github.event.client_payload.branchName }}' -m 'Update API specs from upstream api-specs repository. Opened By: @${{ github.event.client_payload.author }}' - env: + env: GITHUB_TOKEN: ${{ secrets.DX_GITHUB_TOKEN }} - + - name: Output PR Number run: | PR_NUMBER=$(hub pr list -h ${{ github.event.client_payload.branchName }} -f %I) echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV - env: + env: GITHUB_TOKEN: ${{ secrets.DX_GITHUB_TOKEN }} - name: Comment on Corresponding Repo uses: ./.github/actions/add_pr_comment - with: + with: username: ${{ secrets.DX_GITHUB_USERNAME }} token: ${{ secrets.DX_GITHUB_TOKEN }} repo-name: ${{ github.event.client_payload.originRepo }} pr-number: ${{ github.event.client_payload.prNumber }} message: 'Preview site: http://bw-api-docs-${{ env.BRANCH_NAME }}.s3-website-us-east-1.amazonaws.com/\nPlease note that it may take a couple minutes for your preview site to become available.\n\nSee the corresponding PR opened on the docsite repository (no action required):\nhttps://github.com/Bandwidth/api-docs/pull/${{ env.PR_NUMBER }}' - merge: - if: ${{ github.event.action == 'Merge' }} + merge: + if: ${{ github.event.action == 'Merge' }} runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - + - name: Set PR number as env variable run: | echo "PR_NUMBER=$(hub pr list -h ${{ github.event.client_payload.branchName }} -f %I)" >> $GITHUB_ENV - env: + env: GITHUB_TOKEN: ${{ secrets.DX_GITHUB_TOKEN }} - - - uses: ./.github/actions/add_pr_comment - with: - username: ${{ secrets.DX_GITHUB_USERNAME }} + + - uses: bandwidth/automerge-pr-action@v1.0.0 + with: + repoName: api-docs + prNumber: ${{ env.PR_NUMBER }} token: ${{ secrets.DX_GITHUB_TOKEN }} - repo-name: api-docs - pr-number: ${{ env.PR_NUMBER }} - message: 'Corresponding Pull Request on [${{ github.event.client_payload.originRepo }}](https://github.com/Bandwidth/${{ github.event.client_payload.originRepo }}/pull/${{ github.event.client_payload.prNumber }}) was merged.' - close: - if: ${{ github.event.action == 'Close' }} + - uses: actions/github-script@v6 + if: failure() + with: + script: | + github.rest.issues.createComment({ + issue_number: ${{ env.PR_NUMBER }}, + owner: context.repo.owner, + repo: context.repo.repo, + body: 'Failed to auto-merge this PR. Check workflow logs for more information' + }) + + close: + if: ${{ github.event.action == 'Close' }} runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - + # Close the cooresponding PR - # - run: | + # - run: | # PR_NUMBER=$(hub pr list -h ${{ github.event.client_payload.branchName }} -f %I) # hub issue update $PR_NUMBER -s closed - + - name: Set PR number as env variable run: | echo "PR_NUMBER=$(hub pr list -h ${{ github.event.client_payload.branchName }} -f %I)" >> $GITHUB_ENV - env: + env: GITHUB_TOKEN: ${{ secrets.DX_GITHUB_TOKEN }} - + - uses: ./.github/actions/add_pr_comment - with: + with: username: ${{ secrets.DX_GITHUB_USERNAME }} token: ${{ secrets.DX_GITHUB_TOKEN }} repo-name: api-docs pr-number: ${{ env.PR_NUMBER }} message: 'Corresponding Pull Request on [${{ github.event.client_payload.originRepo }}](https://github.com/Bandwidth/${{ github.event.client_payload.originRepo }}/pull/${{ github.event.client_payload.prNumber }}) was closed.' - - change_draft_status: - if: ${{ github.event.action == 'Draft' }} + + change_draft_status: + if: ${{ github.event.action == 'Draft' }} runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - + - name: Check and Update Draft Status run: | PR_STATE=$(hub pr list -h ${{ github.event.client_payload.branchName }} -f %pS) @@ -123,12 +132,12 @@ jobs: } ' gh api graphql -F id="${id}" -f query="${MUTATION}" >/dev/null - fi + fi else if [ "$PR_STATE" == "draft" ] then gh pr ready ${{ github.event.client_payload.branchName }} - fi + fi fi - env: + env: GITHUB_TOKEN: ${{ secrets.DX_GITHUB_TOKEN }} From ffb4de1f2ddde324f7532970862ecd7cf37137c8 Mon Sep 17 00:00:00 2001 From: AJ Rice <53190766+ajrice6713@users.noreply.github.com> Date: Tue, 6 Dec 2022 12:09:15 -0500 Subject: [PATCH 2/2] Add logic to fail the run if non-spec files have been changed --- .github/workflows/listener.yml | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/.github/workflows/listener.yml b/.github/workflows/listener.yml index 90b33cd4c..962cc1208 100644 --- a/.github/workflows/listener.yml +++ b/.github/workflows/listener.yml @@ -55,7 +55,9 @@ jobs: if: ${{ github.event.action == 'Merge' }} runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 + with: + fetch-depth: '2' - name: Set PR number as env variable run: | @@ -63,6 +65,21 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.DX_GITHUB_TOKEN }} + - name: Check Diff + run: | + echo "========== modified files ==========" + git diff --name-only HEAD^ HEAD + + echo "========== check paths of modified files ==========" + git diff --name-only HEAD^ HEAD > files.txt + while IFS= read -r file + do + if [[ $file != site/specs/* ]]; then + echo "$file is not an API spec file. Can't automerge this PR." + exit 1 + fi + done < files.txt + - uses: bandwidth/automerge-pr-action@v1.0.0 with: repoName: api-docs