Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Update

Update #2353

Workflow file for this run

# Ingest repository_dispatch events and trigger appropriate workflows
name: Listener
on:
repository_dispatch:
types: [Open, Update, Merge, Close, Draft]
jobs:
open-update:
if: ${{ github.event.action == 'Open' || github.event.action == 'Update' }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Pass branch name to env
run: |
BRANCH_NAME=${{ github.event.client_payload.branchName }}
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:
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: |
gh pr create -B main $(if ${{ github.event.client_payload.draftPr }} ; then echo "-d" ; fi) -t '${{ github.event.client_payload.branchName }}' -b 'Update API specs from upstream api-specs repository. Opened By: @${{ github.event.client_payload.author }}'
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:
GITHUB_TOKEN: ${{ secrets.DX_GITHUB_TOKEN }}
# Comment on PR
- name: Comment on Corresponding Repo
uses: actions/github-script@v6
with:
github-token: ${{ secrets.DX_GITHUB_TOKEN }}
script: |
github.rest.issues.createComment({
issue_number: '${{ github.event.client_payload.prNumber }}',
owner: 'Bandwidth',
repo: '${{ github.event.client_payload.originRepo }}',
body: 'Preview site: http://${{ env.BRANCH_NAME }}.staging-dev.bandwidth.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' }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ github.event.client_payload.branchName }}
- 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:
GITHUB_TOKEN: ${{ secrets.DX_GITHUB_TOKEN }}
- name: Check Diff
run: |
echo "========== modified files =========="
git diff --name-only ${{ github.base_ref }}
echo "========== check paths of modified files =========="
git diff --name-only ${{ github.base_ref }} > 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/[email protected]
with:
repoName: api-docs
prNumber: ${{ env.PR_NUMBER }}
token: ${{ secrets.DX_GITHUB_TOKEN }}
- uses: actions/github-script@v6
if: failure()
with:
github-token: ${{ secrets.DX_GITHUB_TOKEN }}
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:
- name: Checkout
uses: actions/checkout@v3
# Close the corresponding PR
# - 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:
GITHUB_TOKEN: ${{ secrets.DX_GITHUB_TOKEN }}
# Comment on PR
- name: Comment on Corresponding Repo
uses: actions/github-script@v6
with:
github-token: ${{ secrets.DX_GITHUB_TOKEN }}
script: |
github.rest.issues.createComment({
issue_number: '${{ env.PR_NUMBER }}',
owner: 'Bandwidth',
repo: 'api-docs',
body: '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' }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Check and Update Draft Status
run: |
PR_STATE=$(hub pr list -h ${{ github.event.client_payload.branchName }} -f %pS)
if ${{ github.event.client_payload.draftPr }}
then
if [ "$PR_STATE" == "open" ]
then
id=$(gh pr view ${{ github.event.client_payload.branchName }} --json id -q '.id')
number=$(gh pr view ${{ github.event.client_payload.branchName }} --json number -q '.number')
MUTATION='
mutation($id: ID!) {
convertPullRequestToDraft(input: { pullRequestId: $id }) {
pullRequest {
id
number
isDraft
}
}
}
'
gh api graphql -F id="${id}" -f query="${MUTATION}" >/dev/null
fi
else
if [ "$PR_STATE" == "draft" ]
then
gh pr ready ${{ github.event.client_payload.branchName }}
fi
fi
env:
GITHUB_TOKEN: ${{ secrets.DX_GITHUB_TOKEN }}