|
| 1 | +name: Sync Milestone from Issue |
| 2 | +on: |
| 3 | + pull_request: |
| 4 | + types: [opened, edited, reopened] |
| 5 | + |
| 6 | +jobs: |
| 7 | + sync-milestone: |
| 8 | + runs-on: ubuntu-latest |
| 9 | + permissions: |
| 10 | + issues: read |
| 11 | + pull-requests: write |
| 12 | + steps: |
| 13 | + - name: Sync Milestone |
| 14 | + env: |
| 15 | + GH_TOKEN: ${{ github.token }} |
| 16 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 17 | + REPO: ${{ github.repository }} |
| 18 | + OWNER: ${{ github.repository_owner }} |
| 19 | + run: | |
| 20 | + # 1. Get the milestone of the officially linked issues and the PR's current milestone via GraphQL |
| 21 | + # This catches issues linked via "Fixes #123" and similar in the body or the UI sidebar. |
| 22 | + API_RESPONSE=$(gh api graphql -f query=' |
| 23 | + query($owner: String!, $name: String!, $pr: Int!) { |
| 24 | + repository(owner: $owner, name: $name) { |
| 25 | + pullRequest(number: $pr) { |
| 26 | + milestone { |
| 27 | + number |
| 28 | + } |
| 29 | + closingIssuesReferences(first: 1) { |
| 30 | + nodes { |
| 31 | + milestone { |
| 32 | + number |
| 33 | + title |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + }' -F owner="$OWNER" -F name="${REPO#*/}" -F pr=$PR_NUMBER) |
| 40 | +
|
| 41 | + MILESTONE_NUMBER=$(echo "$API_RESPONSE" | jq -r '.data.repository.pullRequest.closingIssuesReferences.nodes[0].milestone.number // "null"') |
| 42 | + MILESTONE_TITLE=$(echo "$API_RESPONSE" | jq -r '.data.repository.pullRequest.closingIssuesReferences.nodes[0].milestone.title // "null"') |
| 43 | + CURRENT_PR_MILESTONE=$(echo "$API_RESPONSE" | jq -r '.data.repository.pullRequest.milestone.number // "null"') |
| 44 | +
|
| 45 | + # 2. Check if a milestone was found |
| 46 | + if [ "$MILESTONE_NUMBER" != "null" ] && [ -n "$MILESTONE_NUMBER" ]; then |
| 47 | + if [ "$MILESTONE_NUMBER" = "$CURRENT_PR_MILESTONE" ]; then |
| 48 | + echo "PR already has milestone '$MILESTONE_TITLE' set. No update needed." |
| 49 | + else |
| 50 | + echo "Found milestone '$MILESTONE_TITLE' from linked issue. Assigning to PR..." |
| 51 | + gh pr edit $PR_NUMBER --milestone "$MILESTONE_TITLE" --repo "$REPO" |
| 52 | + fi |
| 53 | + else |
| 54 | + echo "No milestone found on linked issues or no issues linked." |
| 55 | + fi |
0 commit comments