-
Notifications
You must be signed in to change notification settings - Fork 2.5k
295 lines (262 loc) · 10 KB
/
follow-merge-upstream-repo-sync.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
name: 'Follow Merge: Upstream repo sync'
on:
repository_dispatch:
types:
- upstream_repo_update
concurrency:
group: ${{ github.workflow }}-${{ github.event.client_payload.branch_name }}
env:
RELEASE_BRANCH_PREFIX: "ls-release/"
DOCS_TARGET_DIR: "docs/source/tags/"
UPSTREAM_REPO_WORKDIR: "upstream"
jobs:
open:
name: Sync PR
if: |
github.event.client_payload.event_action == 'opened' ||
github.event.client_payload.event_action == 'synchronize' ||
github.event.client_payload.event_action == 'merged'
runs-on: ubuntu-latest
steps:
- uses: hmarr/debug-action@v3.0.0
- name: Details
id: details
shell: bash
env:
REPO_NAME: "${{ github.event.client_payload.repo_name }}"
run: |
set -xeuo pipefail
case "${REPO_NAME}" in
*/label-studio-sdk)
echo "poetry=true" >> "${GITHUB_OUTPUT}"
;;
*)
echo "::error::Repository ${REPO_NAME} is not supported"
exit 1
;;
esac
- name: Checkout Actions Hub
uses: actions/checkout@v4
with:
token: ${{ secrets.GIT_PAT }}
repository: HumanSignal/actions-hub
path: ./.github/actions-hub
- name: Parse Follow Merge dispatch event payload
uses: ./.github/actions-hub/actions/follow-merge-parse-payload
id: fm
- name: Find or Create branch
uses: ./.github/actions-hub/actions/github-find-or-create-branch
id: get-branch
with:
github_token: ${{ secrets.GIT_PAT }}
branch_name: "${{ steps.fm.outputs.branch_name }}"
- name: Checkout repo
uses: actions/checkout@v4
if: steps.fm.outputs.repo_name && steps.fm.outputs.branch_name
with:
token: ${{ secrets.GIT_PAT }}
fetch-depth: 0
ref: ${{ steps.get-branch.outputs.branch_name }}
path: "${{ env.UPSTREAM_REPO_WORKDIR }}"
- name: Git Configure
uses: ./.github/actions-hub/actions/git-configure
with:
username: ${{ steps.fm.outputs.author_username }}
email: ${{ steps.fm.outputs.author_email }}
# Merge
- name: Git Merge
id: merge
continue-on-error: true
uses: ./.github/actions-hub/actions/git-merge
with:
base_branch: ${{ steps.get-branch.outputs.base_branch_name }}
head_branch: ${{ steps.get-branch.outputs.branch_name }}
our_files: "pyproject.toml poetry.lock web"
working_directory: "${{ env.UPSTREAM_REPO_WORKDIR }}"
# POETRY
- name: "Install poetry"
if: steps.details.outputs.poetry
run: pipx install poetry
- name: "Set up Python"
id: setup_python
if: steps.details.outputs.poetry
uses: actions/setup-python@v5
with:
python-version: '3.12'
cache: 'poetry'
- name: "Poetry: Commit and Push"
if: steps.details.outputs.poetry
shell: bash
working-directory: "${{ env.UPSTREAM_REPO_WORKDIR }}"
run: |
set -xeuo pipefail
poetry add "https://github.com/${{ steps.fm.outputs.repo_name }}/archive/${{ steps.fm.outputs.commit_sha }}.zip" --lock
git add pyproject.toml poetry.lock
git status -s
git commit --allow-empty -m '[submodules] Bump ${{ steps.fm.outputs.repo_name }} version' -m 'Workflow run: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}'
git push origin HEAD
- name: Find or Create PR
uses: ./.github/actions-hub/actions/github-find-or-create-pull-request
id: get-pr
with:
github_token: ${{ secrets.GIT_PAT }}
branch_name: "${{ steps.get-branch.outputs.branch_name }}"
title: "${{ steps.fm.outputs.title }}"
description: |
Hi @${{ steps.fm.outputs.actor }}!
This PR was automaticaly generated via Follow Merge.
Please ensure that all linked upstream Pull Requests are merged before proceeding with this one.
- name: Add PR Reviewers
uses: ./.github/actions-hub/actions/github-add-pull-request-reviewers
continue-on-error: true
with:
github_token: ${{ secrets.GIT_PAT }}
pullrequest_number: "${{ steps.get-pr.outputs.number }}"
reviewers: "${{ steps.fm.outputs.actor }}"
- name: Link PR
uses: ./.github/actions-hub/actions/github-link-upstream-pull-request
continue-on-error: true
with:
github_token: ${{ secrets.GIT_PAT }}
pullrequest_number: "${{ steps.get-pr.outputs.number }}"
upstream_pullrequest_link: "${{ steps.fm.outputs.pr_html_url }}"
- name: Convert to ready for review
if: steps.fm.outputs.event_action == 'merged'
id: ready-for-review-pr
shell: bash
env:
GIT_PAT: ${{ secrets.GIT_PAT }}
run: |
echo "$GIT_PAT" | gh auth login --with-token
gh api graphql -F id='${{ steps.get-pr.outputs.node_id }}' -f query='
mutation($id: ID!) {
markPullRequestReadyForReview(input: { pullRequestId: $id }) {
pullRequest {
id
}
}
}
'
- name: Enable AutoMerge
if: github.event.client_payload.event_action == 'merged'
continue-on-error: true
shell: bash
env:
GIT_PAT: ${{ secrets.GIT_PAT }}
run: |
echo "$GIT_PAT" | gh auth login --with-token
gh api graphql -f pull='${{ steps.get-pr.outputs.node_id }}' -f query='
mutation($pull: ID!) {
enablePullRequestAutoMerge(input: {pullRequestId: $pull, mergeMethod: SQUASH}) {
pullRequest {
id
number
}
}
}'
- name: Notify on failure
uses: ./.github/actions-hub/actions/github-create-comment
if: failure()
with:
github_token: ${{ secrets.GIT_PAT }}
repository: "${{ steps.fm.outputs.repo_name }}"
issue_number: "${{ steps.fm.outputs.pr_number }}"
body: |
Follow Merge downstream workflow has been failed.
> [Workflow run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})
others:
name: Other actions with PR
if: |
github.event.client_payload.event_action == 'converted_to_draft' ||
github.event.client_payload.event_action == 'ready_for_review' ||
github.event.client_payload.event_action == 'closed'
runs-on: ubuntu-latest
steps:
- uses: hmarr/debug-action@v3.0.0
- name: Get PR
uses: actions/github-script@v7
id: get-pr
with:
github-token: ${{ secrets.GIT_PAT }}
script: |
const {repo, owner} = context.repo;
const branchName = '${{ github.event.client_payload.branch_name }}';
const branchNameLowerCase = branchName.toLowerCase();
const {data: listPullsResponse} = await github.rest.pulls.list({
owner,
repo,
head: `${owner}:${branchName}`,
per_page: 1
});
const {data: listPullsResponseLowerCase} = await github.rest.pulls.list({
owner,
repo,
head: `${owner}:${branchNameLowerCase}`,
per_page: 1
});
if (listPullsResponse.length !== 0) {
console.log(`Found PR for branch '${branchName}'`)
core.setOutput("branch-name", branchName);
return listPullsResponse
} else if (listPullsResponseLowerCase.length !== 0) {
console.log(`Found PR for branch '${branchNameLowerCase}'`)
core.setOutput("branch-name", branchNameLowerCase);
return listPullsResponseLowerCase
} else {
console.log(`PR for branch '${branchNameLowerCase}' is not created yet`)
core.setOutput("branch-name", branchNameLowerCase);
return listPullsResponseLowerCase
}
- name: Close PR
if: github.event.client_payload.event_action == 'closed'
id: close-pr
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GIT_PAT }}
script: |
const { repo, owner } = context.repo;
const listPullsResponse = ${{ steps.get-pr.outputs.result }}
for (let pr of listPullsResponse ) {
core.info(`Closing ${ pr.html_url }`)
github.rest.pulls.update({
owner,
repo,
pull_number: pr.number,
state: 'close'
});
}
- name: Convert to draft
if: github.event.client_payload.event_action == 'converted_to_draft'
id: convert-pr-to-draft
shell: bash
env:
GIT_PAT: ${{ secrets.GIT_PAT }}
run: |
echo "$GIT_PAT" | gh auth login --with-token
gh api graphql -F id='${{ fromJson(steps.get-pr.outputs.result)[0].node_id }}' -f query='
mutation($id: ID!) {
convertPullRequestToDraft(input: { pullRequestId: $id }) {
pullRequest {
id
isDraft
}
}
}
'
- name: Convert to ready for review
if: github.event.client_payload.event_action == 'ready_for_review'
id: ready-for-review-pr
shell: bash
env:
GIT_PAT: ${{ secrets.GIT_PAT }}
run: |
echo "$GIT_PAT" | gh auth login --with-token
gh api graphql -F id='${{ fromJson(steps.get-pr.outputs.result)[0].node_id }}' -f query='
mutation($id: ID!) {
markPullRequestReadyForReview(input: { pullRequestId: $id }) {
pullRequest {
id
}
}
}
'