|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +set -ex |
| 4 | + |
| 5 | +[ -n "$GITHUB_REPOSITORY_OWNER" ] || { |
| 6 | + echo "Required environment variable GITHUB_REPOSITORY_OWNER not found" >&2 |
| 7 | + exit 1 |
| 8 | +} |
| 9 | +[ -n "$GITHUB_REPOSITORY" ] || { |
| 10 | + echo "Required environment variable GITHUB_REPOSITORY not found" >&2 |
| 11 | + exit 1 |
| 12 | +} |
| 13 | +[ -n "$GITHUB_REF_NAME" ] || { |
| 14 | + echo "Required environment variable GITHUB_REF_NAME not found" >&2 |
| 15 | + exit 1 |
| 16 | +} |
| 17 | +[ -n "$GITHUB_EVENT_PATH" ] || { |
| 18 | + echo "Required environment variable GITHUB_EVENT_PATH not found" >&2 |
| 19 | + exit 1 |
| 20 | +} |
| 21 | + |
| 22 | +owner=$GITHUB_REPOSITORY_OWNER |
| 23 | +repo=$(echo "$GITHUB_REPOSITORY" | cut -d/ -f2) |
| 24 | +label="lts-watch-$GITHUB_REF_NAME" |
| 25 | + |
| 26 | +# Lazy-get label node_id once |
| 27 | +label_id= |
| 28 | +fetch_label_id () { |
| 29 | + # shellcheck disable=SC2016 |
| 30 | + label_id="$(gh api graphql -f query=' |
| 31 | + query($owner:String!, $repo:String!, $name:String!) { |
| 32 | + repository(owner:$owner, name:$repo) { |
| 33 | + label(name:$name) { id } |
| 34 | + } |
| 35 | + }' -f owner="$owner" -f repo="$repo" -f name="$label" --jq '.data.repository.label.id')" |
| 36 | + |
| 37 | + if [ -z "$label_id" ] || [ "$label_id" = "null" ]; then |
| 38 | + echo "Could not resolve label id for '$label' in $owner/$repo" >&2 |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | +} |
| 42 | + |
| 43 | +jq -r '.commits[].message' < "$GITHUB_EVENT_PATH" \ |
| 44 | +| while IFS= read -r msg; do |
| 45 | + pr_url=$(echo "$msg" | git interpret-trailers --parse | awk -F': ' '$1 == "PR-URL" { print $2; exit }') |
| 46 | + pr_number=${pr_url##"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pull/"} |
| 47 | + |
| 48 | + # Filter out pr_number that are not numbers (e.g. if PR-URL points to a different repo) |
| 49 | + case "$pr_number" in |
| 50 | + ''|*[!0-9]*) continue ;; |
| 51 | + *) ;; |
| 52 | + esac |
| 53 | + |
| 54 | + # shellcheck disable=SC2016 |
| 55 | + pr_id="$(gh api graphql -f query=' |
| 56 | + query($owner:String!, $repo:String!, $number:Int!, $label:String!) { |
| 57 | + repository(owner:$owner, name:$repo) { |
| 58 | + pullRequest(number:$number) { |
| 59 | + id |
| 60 | + labels(first: 1, query: $label) { |
| 61 | + nodes { name } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + }' -f owner="$owner" -f repo="$repo" -F number="$pr_number" -f label="$label" \ |
| 66 | + --jq ' if (.data.repository.pullRequest.labels.nodes | length) then .data.repository.pullRequest.id end')" |
| 67 | + |
| 68 | + if [ -z "$pr_id" ] || [ "$pr_id" = "null" ]; then |
| 69 | + echo "PR #$pr_number not found or does not have the label" >&2 |
| 70 | + continue |
| 71 | + fi |
| 72 | + |
| 73 | + [ -n "$label_id" ] || fetch_label_id |
| 74 | + |
| 75 | + # shellcheck disable=SC2016 |
| 76 | + gh api graphql -f query=' |
| 77 | + mutation($labelableId:ID!, $labelIds:[ID!]!) { |
| 78 | + removeLabelsFromLabelable(input:{labelableId:$labelableId, labelIds:$labelIds}) { |
| 79 | + clientMutationId |
| 80 | + } |
| 81 | + }' -f labelableId="$pr_id" -f labelIds="[$label_id]" >/dev/null |
| 82 | + |
| 83 | + # gentle throttle |
| 84 | + sleep 0.2 |
| 85 | +done |
0 commit comments