|
| 1 | +name: Auto label PR based on linked issue |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + pull-requests: write |
| 11 | + issues: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + apply-label: |
| 15 | + name: Auto label PR |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - name: Apply label to PR |
| 19 | + shell: bash |
| 20 | + env: |
| 21 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 22 | + run: | |
| 23 | + set -Eeuo pipefail |
| 24 | + |
| 25 | + PR_NUMBER="$(jq -r '.pull_request.number' "$GITHUB_EVENT_PATH")" |
| 26 | + OWNER="${GITHUB_REPOSITORY%%/*}" |
| 27 | + REPO="${GITHUB_REPOSITORY#*/}" |
| 28 | +
|
| 29 | + PR_TITLE="$(jq -r '.pull_request.title // empty' "$GITHUB_EVENT_PATH")" |
| 30 | + if [[ -n "$PR_TITLE" && "$PR_TITLE" == *"[GenAI]"* ]]; then |
| 31 | + gh pr edit "$PR_NUMBER" -R "${OWNER}/${REPO}" --add-label "GenAI" || { |
| 32 | + echo "::notice title=Could not add label::gh pr edit failed for 'GenAI' (label may not exist or token lacks write permissions)"; |
| 33 | + } |
| 34 | + fi |
| 35 | +
|
| 36 | + # Query to get the single closing issue and its labels |
| 37 | + QUERY="$(cat <<'GRAPHQL' |
| 38 | + query($owner:String!, $repo:String!, $pr:Int!) { |
| 39 | + repository(owner:$owner, name:$repo) { |
| 40 | + pullRequest(number:$pr) { |
| 41 | + closingIssuesReferences(first: 1) { |
| 42 | + nodes { |
| 43 | + number |
| 44 | + labels(first: 100) { |
| 45 | + nodes { name } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + GRAPHQL |
| 53 | + )" |
| 54 | +
|
| 55 | + BODY="$(jq -n \ |
| 56 | + --arg q "$QUERY" \ |
| 57 | + --arg owner "$OWNER" \ |
| 58 | + --arg repo "$REPO" \ |
| 59 | + --argjson pr "$PR_NUMBER" \ |
| 60 | + '{query:$q, variables:{owner:$owner, repo:$repo, pr:$pr}}')" |
| 61 | +
|
| 62 | + RESP="$(curl -sS \ |
| 63 | + -H "Authorization: Bearer $GITHUB_TOKEN" \ |
| 64 | + -H "Content-Type: application/json" \ |
| 65 | + -d "$BODY" \ |
| 66 | + https://api.github.com/graphql)" |
| 67 | + if jq -e '.errors' >/dev/null 2>&1 <<<"$RESP"; then |
| 68 | + echo "::error title=GraphQL API errors::$(jq -c '.errors' <<<"$RESP")" |
| 69 | + fi |
| 70 | +
|
| 71 | + ISSUE_NUMBER="$(jq -r '.data.repository.pullRequest.closingIssuesReferences.nodes[0].number // empty' <<<"$RESP")" |
| 72 | + if [[ -z "${ISSUE_NUMBER}" ]]; then |
| 73 | + echo "::notice title=No linked issue::PR #${PR_NUMBER} has no linked closing issue." |
| 74 | + exit 0 |
| 75 | + fi |
| 76 | +
|
| 77 | + # Extract label names from the linked issue |
| 78 | + LABELS="$(jq -r '.data.repository.pullRequest.closingIssuesReferences.nodes[0].labels.nodes[].name' <<<"$RESP" || true)" |
| 79 | +
|
| 80 | + # Pick the first matching 'fails-*' label and map to a single PR label |
| 81 | + selected="" |
| 82 | + for l in $LABELS; do |
| 83 | + case "$l" in |
| 84 | + fails-javac-compile) selected="fixes-javac-fail"; break;; |
| 85 | + fails-java-run) selected="fixes-java-run-fail"; break;; |
| 86 | + fails-native-image-run) selected="fixes-native-image-run-fail"; break;; |
| 87 | + fails-native-image-build) selected="fixes-native-image-build-fail"; break;; |
| 88 | + esac |
| 89 | + done |
| 90 | +
|
| 91 | + if [[ -z "$selected" ]]; then |
| 92 | + echo "::notice title=No mapping found::No 'fails-*' labels found on linked issue #${ISSUE_NUMBER}." |
| 93 | + exit 0 |
| 94 | + fi |
| 95 | + |
| 96 | + labels_json="$(jq -nc --arg l "$selected" '[$l]')" |
| 97 | +
|
| 98 | + gh pr edit "$PR_NUMBER" -R "${OWNER}/${REPO}" --add-label "$selected" || { |
| 99 | + echo "::notice title=Could not add label::gh pr edit failed (label may not exist or token lacks write permissions)"; |
| 100 | + } |
0 commit comments