-
Notifications
You must be signed in to change notification settings - Fork 43
81 lines (72 loc) · 3.41 KB
/
Copy pathsync-milestone.yml
File metadata and controls
81 lines (72 loc) · 3.41 KB
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
name: Sync Milestone from Issue
on:
pull_request:
types: [opened, edited, reopened]
jobs:
sync-milestone:
runs-on: ubuntu-latest
permissions:
issues: read
pull-requests: write
steps:
- name: Sync Milestone
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ github.event.pull_request.number }}
REPO: ${{ github.repository }}
OWNER: ${{ github.repository_owner }}
run: |
# 1. Get milestones from ALL linked closing issues and the PR's current milestone via GraphQL.
# closingIssuesReferences catches issues linked via "Fixes #123" and similar in the body or sidebar.
API_RESPONSE=$(gh api graphql -f query='
query($owner: String!, $name: String!, $pr: Int!) {
repository(owner: $owner, name: $name) {
pullRequest(number: $pr) {
milestone {
number
title
}
closingIssuesReferences(first: 20) {
nodes {
milestone {
number
title
}
}
}
}
}
}' -F owner="$OWNER" -F name="${REPO#*/}" -F pr=$PR_NUMBER)
# 2. Collect all X.Y.Z milestone titles from linked issues, dedupe, version-sort, pick highest.
MILESTONE_TITLE=$(echo "$API_RESPONSE" \
| jq -r '.data.repository.pullRequest.closingIssuesReferences.nodes[].milestone.title // empty' \
| grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' \
| sort -uV \
| tail -n1)
CURRENT_PR_MILESTONE_TITLE=$(echo "$API_RESPONSE" | jq -r '.data.repository.pullRequest.milestone.title // "null"')
# 3. No qualifying milestone found across all linked issues.
if [ -z "$MILESTONE_TITLE" ]; then
echo "No X.Y.Z milestone found on any linked issue. Nothing to sync."
exit 0
fi
# 4. PR already has that exact milestone title — nothing to do.
if [ "$MILESTONE_TITLE" = "$CURRENT_PR_MILESTONE_TITLE" ]; then
echo "PR already has milestone '$MILESTONE_TITLE' set. No update needed."
exit 0
fi
echo "Candidate milestone: '$MILESTONE_TITLE' (current PR milestone: '$CURRENT_PR_MILESTONE_TITLE')"
# 5. Confirm the milestone title exists as an OPEN milestone on this repo.
# gh pr edit --milestone only accepts open milestones; closed ones return "not found".
REPO_MILESTONE_MATCH=$(gh api "repos/$REPO/milestones?per_page=100&state=open" --paginate \
| jq -r '.[].title' \
| grep -Fx "$MILESTONE_TITLE" || true)
if [ -z "$REPO_MILESTONE_MATCH" ]; then
echo "::warning::Milestone '$MILESTONE_TITLE' not found as an open milestone on $REPO (it may be closed or deleted). Skipping PR milestone update."
exit 0
fi
# 6. Assign milestone to PR — treat any failure as a warning, never fail the job.
echo "Assigning milestone '$MILESTONE_TITLE' to PR #$PR_NUMBER..."
if ! gh pr edit "$PR_NUMBER" --milestone "$MILESTONE_TITLE" --repo "$REPO"; then
echo "::warning::Failed to assign milestone '$MILESTONE_TITLE' to PR #$PR_NUMBER. Manual update may be needed."
fi
exit 0