Skip to content

Commit 6d64a0c

Browse files
mosabuaebyhr
authored andcommitted
Add github milestone labeling
1 parent 7ef5915 commit 6d64a0c

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

.github/bin/retry

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
x() {
6+
echo "+ $*" >&2
7+
"$@"
8+
}
9+
10+
max_retry_time_seconds=$(( 30 * 60 ))
11+
retry_delay_seconds=10
12+
13+
END=$(( $(date +%s) + ${max_retry_time_seconds} ))
14+
15+
while (( $(date +%s) < $END )); do
16+
x "$@" && exit 0
17+
sleep "${retry_delay_seconds}"
18+
done
19+
20+
echo "$0: retrying [$*] timed out" >&2
21+
exit 1

.github/workflows/milestone.yml

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: milestone
2+
3+
on:
4+
push:
5+
branches: [main]
6+
7+
defaults:
8+
run:
9+
shell: bash --noprofile --norc -euo pipefail {0}
10+
11+
jobs:
12+
set-milestone:
13+
if: github.repository_owner == 'trinodb'
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v4
18+
- name: Get milestone from pom.xml
19+
run: |
20+
.github/bin/retry ./mvnw -v
21+
MILESTONE_NUMBER="$(./mvnw help:evaluate -Dexpression=project.version -q -DforceStdout | cut -d- -f1)"
22+
echo "Setting PR milestone to ${MILESTONE_NUMBER}"
23+
echo "MILESTONE_NUMBER=${MILESTONE_NUMBER}" >> $GITHUB_ENV
24+
- name: Set milestone to PR
25+
uses: actions/github-script@v7
26+
with:
27+
github-token: ${{secrets.GITHUB_TOKEN}}
28+
script: |
29+
// Get pull request number from commit sha
30+
// 'listPullRequestsAssociatedWithCommit()' lists the merged pull request
31+
// https://docs.github.com/en/rest/reference/repos#list-pull-requests-associated-with-a-commit
32+
// and https://octokit.github.io/rest.js/v19#repos-list-pull-requests-associated-with-commit
33+
const pr_response = await github.rest.repos.listPullRequestsAssociatedWithCommit({
34+
owner: context.repo.owner,
35+
repo: context.repo.repo,
36+
commit_sha: context.sha
37+
})
38+
if (pr_response.data.length === 0) {
39+
console.log('Pull request not found for commit', context.sha)
40+
return
41+
}
42+
if (pr_response.data.length > 1) {
43+
console.log(pr_response.data)
44+
throw 'Expected 1 pull request but found: ' + pr_response.data.length
45+
}
46+
const pr_number = pr_response.data[0].number
47+
console.log('Found pull request', pr_number, 'for commit', context.sha)
48+
49+
// Get milestone
50+
const {
51+
MILESTONE_NUMBER
52+
} = process.env
53+
console.log('Milestone number to set', MILESTONE_NUMBER)
54+
55+
// Find milestone with matching title
56+
// See https://octokit.github.io/rest.js/v19#pagination
57+
const milestones = await github.paginate(github.rest.issues.listMilestones, {
58+
owner: context.repo.owner,
59+
repo: context.repo.repo,
60+
state: 'all'
61+
})
62+
let milestone = milestones.find(milestone => milestone.title === MILESTONE_NUMBER)
63+
console.log('Found milestone with title', MILESTONE_NUMBER, milestone)
64+
65+
// Create new milestone if it doesn't exist
66+
if (!milestone) {
67+
const create_response = await github.rest.issues.createMilestone({
68+
owner: context.repo.owner,
69+
repo: context.repo.repo,
70+
title: MILESTONE_NUMBER
71+
})
72+
milestone = create_response.data
73+
console.log('Created new milestone with title', MILESTONE_NUMBER, milestone)
74+
}
75+
76+
// Set milestone to PR
77+
await github.rest.issues.update({
78+
owner: context.repo.owner,
79+
repo: context.repo.repo,
80+
milestone: milestone.number,
81+
issue_number: pr_number
82+
})
83+
console.log('Added PR', pr_number, 'to milestone', MILESTONE_NUMBER)

0 commit comments

Comments
 (0)