This repository has been archived by the owner on Nov 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
174 lines (155 loc) · 6.52 KB
/
listener.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# 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 }}