Skip to content

Commit 023514e

Browse files
ci: PLT-527: Add SDK Follow Merge
1 parent 6d44db8 commit 023514e

File tree

1 file changed

+304
-0
lines changed

1 file changed

+304
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,304 @@
1+
name: 'Follow Merge: Upstream repo sync'
2+
3+
on:
4+
repository_dispatch:
5+
types:
6+
- upstream_repo_update
7+
8+
concurrency:
9+
group: ${{ github.workflow }}-${{ github.event.client_payload.branch_name }}
10+
11+
env:
12+
RELEASE_BRANCH_PREFIX: "lse-release/"
13+
NODE: "18"
14+
YARN: "1.22"
15+
UPSTREAM_REPO_WORKDIR: "upstream"
16+
17+
jobs:
18+
open:
19+
name: Sync PR
20+
if: |
21+
github.event.client_payload.event_action == 'opened' ||
22+
github.event.client_payload.event_action == 'synchronize' ||
23+
github.event.client_payload.event_action == 'merged'
24+
runs-on: ubuntu-22.04
25+
steps:
26+
- uses: hmarr/[email protected]
27+
28+
- name: Details
29+
id: details
30+
shell: bash
31+
env:
32+
REPO_NAME: "${{ github.event.client_payload.repo_name }}"
33+
run: |
34+
set -xeuo pipefail
35+
36+
case "${REPO_NAME}" in
37+
*/label-studio-sdk)
38+
echo "poetry=true" >> $GITHUB_OUTPUT
39+
echo "poetry_group=default" >> $GITHUB_OUTPUT
40+
;;
41+
*)
42+
echo "::error::Repository ${REPO_NAME} is not supported"
43+
exit 1
44+
;;
45+
esac
46+
47+
- name: Checkout Actions Hub
48+
uses: actions/checkout@v4
49+
with:
50+
token: ${{ secrets.GIT_PAT }}
51+
repository: HumanSignal/actions-hub
52+
path: ./.github/actions-hub
53+
54+
- name: Parse Follow Merge dispatch event payload
55+
uses: ./.github/actions-hub/actions/follow-merge-parse-payload
56+
id: fm
57+
58+
- name: Find or Create branch
59+
uses: ./.github/actions-hub/actions/github-find-or-create-branch
60+
id: get-branch
61+
with:
62+
github_token: ${{ secrets.GIT_PAT }}
63+
branch_name: "${{ steps.fm.outputs.branch_name }}"
64+
65+
- name: Checkout repo
66+
uses: actions/checkout@v4
67+
if: steps.fm.outputs.repo_name && steps.fm.outputs.branch_name
68+
with:
69+
token: ${{ secrets.GIT_PAT }}
70+
fetch-depth: 0
71+
ref: ${{ steps.get-branch.outputs.branch_name }}
72+
path: "${{ env.UPSTREAM_REPO_WORKDIR }}"
73+
74+
- name: Git Configure
75+
uses: ./.github/actions-hub/actions/git-configure
76+
with:
77+
username: ${{ steps.fm.outputs.author_username }}
78+
email: ${{ steps.fm.outputs.author_email }}
79+
80+
- name: Git Merge
81+
id: merge
82+
continue-on-error: true
83+
uses: ./.github/actions-hub/actions/git-merge
84+
with:
85+
base_branch: ${{ steps.get-branch.outputs.base_name }}
86+
head_branch: ${{ steps.get-branch.outputs.branch_name }}
87+
our_files: "pyproject.toml poetry.lock web/package.json web/yarn.lock"
88+
working_directory: "${{ env.UPSTREAM_REPO_WORKDIR }}"
89+
90+
- name: "Poetry: Set up"
91+
if: steps.details.outputs.poetry
92+
uses: Gr1N/setup-poetry@v9
93+
94+
- name: Commit submodule
95+
shell: bash
96+
working-directory: "${{ env.UPSTREAM_REPO_WORKDIR }}"
97+
run: |
98+
set -xeuo pipefail
99+
100+
poetry add "https://github.com/${{ steps.fm.outputs.repo_name }}/archive/${{ steps.fm.outputs.commit_sha }}.zip" --lock
101+
102+
git diff
103+
git add -A
104+
git status -s
105+
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 }}'
106+
107+
- name: Update Poetry Lock File
108+
continue-on-error: true
109+
working-directory: "${{ env.UPSTREAM_REPO_WORKDIR }}"
110+
run: |
111+
set -xeuo pipefail
112+
113+
commit_message="Update Poetry lock file"
114+
commit_message_workflow_link='Workflow run: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}'
115+
116+
poetry lock --no-update
117+
118+
git add poetry.lock
119+
git commit -m "${commit_message}" -m "${commit_message_workflow_link}"
120+
121+
- name: Git Push
122+
uses: ./.github/actions-hub/actions/git-push
123+
with:
124+
working_directory: "${{ env.UPSTREAM_REPO_WORKDIR }}"
125+
126+
- name: Find or Create PR
127+
uses: ./.github/actions-hub/actions/github-find-or-create-pull-request
128+
id: get-pr
129+
with:
130+
github_token: ${{ secrets.GIT_PAT }}
131+
branch_name: "${{ steps.get-branch.outputs.branch_name }}"
132+
title: "${{ steps.fm.outputs.title }}"
133+
description: |
134+
Hi @${{ steps.fm.outputs.actor }}!
135+
136+
This PR was automaticaly generated via Follow Merge.
137+
Please ensure that all linked upstream Pull Requests are merged before proceeding with this one.
138+
139+
- name: Add PR Reviewers
140+
uses: ./.github/actions-hub/actions/github-add-pull-request-reviewers
141+
continue-on-error: true
142+
with:
143+
github_token: ${{ secrets.GIT_PAT }}
144+
pullrequest_number: "${{ steps.get-pr.outputs.number }}"
145+
reviewers: "${{ steps.fm.outputs.actor }}"
146+
147+
- name: Link PR
148+
uses: ./.github/actions-hub/actions/github-link-upstream-pull-request
149+
continue-on-error: true
150+
with:
151+
github_token: ${{ secrets.GIT_PAT }}
152+
pullrequest_number: "${{ steps.get-pr.outputs.number }}"
153+
upstream_pullrequest_link: "${{ steps.fm.outputs.pr_html_url }}"
154+
155+
156+
- name: Convert to ready for review
157+
if: github.event.client_payload.event_action == 'merged'
158+
id: ready-for-review-pr
159+
shell: bash
160+
env:
161+
GIT_PAT: ${{ secrets.GIT_PAT }}
162+
run: |
163+
echo "$GIT_PAT" | gh auth login --with-token
164+
gh api graphql -F id='${{ steps.get-pr.outputs.node_id }}' -f query='
165+
mutation($id: ID!) {
166+
markPullRequestReadyForReview(input: { pullRequestId: $id }) {
167+
pullRequest {
168+
id
169+
}
170+
}
171+
}
172+
'
173+
174+
- name: Enable AutoMerge
175+
id: enable-pr-automerge
176+
if: github.event.client_payload.event_action == 'merged'
177+
shell: bash
178+
env:
179+
GIT_PAT: ${{ secrets.GIT_PAT }}
180+
run: |
181+
echo "$GIT_PAT" | gh auth login --with-token
182+
gh api graphql -f pull='${{ steps.get-pr.outputs.node_id }}' -f query='
183+
mutation($pull: ID!) {
184+
enablePullRequestAutoMerge(input: {pullRequestId: $pull, mergeMethod: SQUASH}) {
185+
pullRequest {
186+
id
187+
number
188+
}
189+
}
190+
}'
191+
192+
- name: Notify on failure
193+
uses: ./.github/actions-hub/actions/github-create-comment
194+
if: failure()
195+
with:
196+
github_token: ${{ secrets.GIT_PAT }}
197+
repository: "${{ steps.fm.outputs.repo_name }}"
198+
issue_number: "${{ steps.fm.outputs.pr_number }}"
199+
body: |
200+
Follow Merge downstream workflow has been failed.
201+
> [Workflow run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})
202+
203+
204+
others:
205+
name: Other actions with PR
206+
if: |
207+
github.event.client_payload.event_action == 'converted_to_draft' ||
208+
github.event.client_payload.event_action == 'ready_for_review' ||
209+
github.event.client_payload.event_action == 'closed'
210+
runs-on: ubuntu-latest
211+
steps:
212+
- uses: hmarr/[email protected]
213+
214+
- name: Get PR
215+
uses: actions/github-script@v7
216+
id: get-pr
217+
with:
218+
github-token: ${{ secrets.GIT_PAT }}
219+
script: |
220+
const {repo, owner} = context.repo;
221+
const branchName = '${{ github.event.client_payload.branch_name }}';
222+
const branchNameLowerCase = branchName.toLowerCase();
223+
const {data: listPullsResponse} = await github.rest.pulls.list({
224+
owner,
225+
repo,
226+
head: `${owner}:${branchName}`,
227+
per_page: 1
228+
});
229+
const {data: listPullsResponseLowerCase} = await github.rest.pulls.list({
230+
owner,
231+
repo,
232+
head: `${owner}:${branchNameLowerCase}`,
233+
per_page: 1
234+
});
235+
236+
if (listPullsResponse.length !== 0) {
237+
console.log(`Found PR for branch '${branchName}'`)
238+
core.setOutput("branch-name", branchName);
239+
return listPullsResponse
240+
} else if (listPullsResponseLowerCase.length !== 0) {
241+
console.log(`Found PR for branch '${branchNameLowerCase}'`)
242+
core.setOutput("branch-name", branchNameLowerCase);
243+
return listPullsResponseLowerCase
244+
} else {
245+
console.log(`PR for branch '${branchNameLowerCase}' is not created yet`)
246+
core.setOutput("branch-name", branchNameLowerCase);
247+
return listPullsResponseLowerCase
248+
}
249+
250+
- name: Close PR
251+
if: github.event.client_payload.event_action == 'closed'
252+
id: close-pr
253+
uses: actions/github-script@v7
254+
with:
255+
github-token: ${{ secrets.GIT_PAT }}
256+
script: |
257+
const { repo, owner } = context.repo;
258+
const listPullsResponse = ${{ steps.get-pr.outputs.result }}
259+
for (let pr of listPullsResponse ) {
260+
core.info(`Closing ${ pr.html_url }`)
261+
github.rest.pulls.update({
262+
owner,
263+
repo,
264+
pull_number: pr.number,
265+
state: 'close'
266+
});
267+
}
268+
269+
- name: Convert to draft
270+
if: github.event.client_payload.event_action == 'converted_to_draft'
271+
id: convert-pr-to-draft
272+
shell: bash
273+
env:
274+
GIT_PAT: ${{ secrets.GIT_PAT }}
275+
run: |
276+
echo "$GIT_PAT" | gh auth login --with-token
277+
gh api graphql -F id='${{ fromJson(steps.get-pr.outputs.result)[0].node_id }}' -f query='
278+
mutation($id: ID!) {
279+
convertPullRequestToDraft(input: { pullRequestId: $id }) {
280+
pullRequest {
281+
id
282+
isDraft
283+
}
284+
}
285+
}
286+
'
287+
288+
- name: Convert to ready for review
289+
if: github.event.client_payload.event_action == 'ready_for_review'
290+
id: ready-for-review-pr
291+
shell: bash
292+
env:
293+
GIT_PAT: ${{ secrets.GIT_PAT }}
294+
run: |
295+
echo "$GIT_PAT" | gh auth login --with-token
296+
gh api graphql -F id='${{ fromJson(steps.get-pr.outputs.result)[0].node_id }}' -f query='
297+
mutation($id: ID!) {
298+
markPullRequestReadyForReview(input: { pullRequestId: $id }) {
299+
pullRequest {
300+
id
301+
}
302+
}
303+
}
304+
'

0 commit comments

Comments
 (0)