From b42b453f76f5381d39de3420f7e18066b8930640 Mon Sep 17 00:00:00 2001 From: Rachel Powers <508861+Ryex@users.noreply.github.com> Date: Fri, 14 Mar 2025 12:58:42 -0700 Subject: [PATCH] ci(blocking-lables): cleanup lables with a pr closes Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com> --- .github/actions/add-labels/action.yml | 2 +- .github/actions/create-comment/action.yml | 4 +- .github/actions/delete-labels/action.yml | 2 + .../{blocked_prs.yml => blocked-prs.yml} | 68 ++++++------------- .github/workflows/manual-add-label.yml | 2 +- .github/workflows/manual-delete-labels.yml | 2 +- ..._blocking_pr.yml => merge-blocking_pr.yml} | 66 ++++++++++++------ 7 files changed, 74 insertions(+), 72 deletions(-) rename .github/workflows/{blocked_prs.yml => blocked-prs.yml} (80%) rename .github/workflows/{merge_blocking_pr.yml => merge-blocking_pr.yml} (52%) diff --git a/.github/actions/add-labels/action.yml b/.github/actions/add-labels/action.yml index 9adb87945..82b237497 100644 --- a/.github/actions/add-labels/action.yml +++ b/.github/actions/add-labels/action.yml @@ -19,7 +19,7 @@ inputs: the list order is the same as `labels`. Missing or blank values (e.g. `FFFFFF,,FFFFFF`) use the `default-color` default-color: description: default color to create labels with - default: "#D4C5F9" + default: "#ffffff" runs: using: "composite" diff --git a/.github/actions/create-comment/action.yml b/.github/actions/create-comment/action.yml index 5e5925504..484f1e5af 100644 --- a/.github/actions/create-comment/action.yml +++ b/.github/actions/create-comment/action.yml @@ -1,5 +1,5 @@ name: Create Issue Comment -description: Create or updste an issue comment +description: Create or update an issue comment inputs: comment: description: Comment Text @@ -19,7 +19,7 @@ inputs: required: true repository: description: the OWNER/REPOSITORY to operate on - required: true + default: ${{ github.repository }} runs: using: "composite" diff --git a/.github/actions/delete-labels/action.yml b/.github/actions/delete-labels/action.yml index 25a8d5fad..65d3ba3d4 100644 --- a/.github/actions/delete-labels/action.yml +++ b/.github/actions/delete-labels/action.yml @@ -70,10 +70,12 @@ runs: ' ) echo "label_ids=$data" >> "$GITHUB_OUTPUT" + echo "num_labels=$(jq -r 'length' <<< "$data")" >> "$GITHUB_OUTPUT" - name: Delete Labels id: delete_labels shell: bash + if: fromJSON( steps.collect_labels.outputs.num_labels ) > 0 env: GH_TOKEN: ${{ inputs.gh_token }} LABELS: ${{ steps.collect_labels.outputs.label_ids }} diff --git a/.github/workflows/blocked_prs.yml b/.github/workflows/blocked-prs.yml similarity index 80% rename from .github/workflows/blocked_prs.yml rename to .github/workflows/blocked-prs.yml index b83c01c0c..3201e5f7a 100644 --- a/.github/workflows/blocked_prs.yml +++ b/.github/workflows/blocked-prs.yml @@ -54,7 +54,6 @@ jobs: } ' <<< "$PR_JSON")" } >> "$GITHUB_ENV" - cat $GITHUB_ENV - name: Setup From Dispatch Event if: github.event_name == 'workflow_dispatch' @@ -133,7 +132,7 @@ jobs: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} BLOCKING_PRS: ${{ steps.pr_ids.outputs.prs }} run: | - BLOCKED_PR_DATA=$( + blocked_pr_data=$( while read -r pr_data ; do gh api \ -H "Accept: application/vnd.github+json" \ @@ -156,56 +155,33 @@ jobs: ' done < <(jq -c '.blocking[]' <<< "$BLOCKING_PRS") | jq -c -s ) - echo "data=$BLOCKED_PR_DATA" >> "$GITHUB_OUTPUT" - echo "all_merged=$(jq -r 'all(.[].merged; .)' <<< "$BLOCKED_PR_DATA")" >> "$GITHUB_OUTPUT" + blocked_by_labels=$(jq -c 'map( select( .merged | not ) | "blocked-by:" + (.number | tostring))' <<< "$blocked_pr_data" ) + echo "data=$blocked_pr_data" >> "$GITHUB_OUTPUT" + echo "all_merged=$(jq -r 'all(.[].merged; .)' <<< "$blocked_pr_data")" >> "$GITHUB_OUTPUT" + echo "blocked_by_labels=$blocked_by_labels" >> "$GITHUB_OUTPUT" + echo "current_blocking=$(jq -c 'map( select( .merged | not ) | .number )' <<< "$blocked_pr_data" )" >> "$GITHUB_OUTPUT" - - name: Apply Blocked Label if Missing + - name: Apply Blocked by Labels id: label_blocked - if: fromJSON(steps.pr_ids.outputs.prs).numBlocking > 0 && !contains(fromJSON(env.JOB_DATA).prLabels, 'blocked') && !fromJSON(steps.blocking_data.outputs.all_merged) + if: fromJSON(steps.pr_ids.outputs.prs).numBlocking > 0 && steps.blocking_data.outputs.blocked_by_labels != '' continue-on-error: true - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - gh api \ - --method POST \ - -H "Accept: application/vnd.github+json" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - "/repos/$OWNER/$REPO/issues/$PR_NUMBER/labels" \ - -f "labels[]=blocked" + uses: ./.github/actions/add-labels + with: + repository: ${{ github.repository }} + gh_token: ${{ secrets.GITHUB_TOKEN }} + issues: ${{ env.PR_NUMBER }} + labels: ${{ join( fromJSON(steps.blocking_data.outputs.blocked_by_labels), ',' ) }} - - name: Remove 'blocked' Label if All Dependencies Are Merged - id: unlabel_blocked - if: fromJSON(steps.pr_ids.outputs.prs).numBlocking > 0 && fromJSON(steps.blocking_data.outputs.all_merged) - continue-on-error: true - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - gh api \ - --method DELETE \ - -H "Accept: application/vnd.github+json" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - "/repos/$OWNER/$REPO/issues/$PR_NUMBER/labels/blocked" - - - name: Apply 'blocking' Label to Dependencies if Missing + - name: Apply 'blocking:' Label to Unmerged Dependencies id: label_blocking if: fromJSON(steps.pr_ids.outputs.prs).numBlocking > 0 continue-on-error: true - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - BLOCKING_DATA: ${{ steps.blocking_data.outputs.data }} - run: | - # label pr dependencies with 'blocking' if not already - while read -r pr_data ; do - if jq -e 'all(.labels[]; . != "blocking")' <<< "$pr_data" > /dev/null ; then - pr=$(jq -r '.number' <<< "$pr_data") - gh api \ - --method POST \ - -H "Accept: application/vnd.github+json" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - "/repos/$OWNER/$REPO/issues/$pr/labels" \ - -f "labels[]=blocking" - fi - done < <(jq -c '.[]' <<< "$BLOCKING_DATA") + uses: ./.github/actions/add-labels + with: + repository: ${{ github.repository }} + gh_token: ${{ secrets.GITHUB_TOKEN }} + issues: ${{ join( fromJSON(steps.blocking_data.outputs.current_blocking) , ',' ) }} + labels: ${{ format( 'blocking:{0}', env.PR_NUMBER ) }} - name: Apply Blocking PR Status Check id: blocked_check @@ -229,7 +205,7 @@ jobs: -f "state=$(jq -r 'if .merged then "success" else "failure" end' <<< "$pr_data")" \ -f "target_url=$(jq -r '.basePrUrl' <<< "$pr_data" )" \ -f "description=$DESC" \ - -f "context=continuous-integration/blocked-pr-check:$(jq '.number' <<< "$pr_data")" + -f "context=ci/blocking-pr-check:$(jq '.number' <<< "$pr_data")" done < <(jq -c '.[]' <<< "$BLOCKING_DATA") - name: Context Comment diff --git a/.github/workflows/manual-add-label.yml b/.github/workflows/manual-add-label.yml index 8a06ae213..fdb69e389 100644 --- a/.github/workflows/manual-add-label.yml +++ b/.github/workflows/manual-add-label.yml @@ -1,4 +1,4 @@ -name: Apply labels in bulk +name: "Manual: Apply Labels in Bulk" on: workflow_dispatch: diff --git a/.github/workflows/manual-delete-labels.yml b/.github/workflows/manual-delete-labels.yml index e8c4b985d..300c0b52a 100644 --- a/.github/workflows/manual-delete-labels.yml +++ b/.github/workflows/manual-delete-labels.yml @@ -1,5 +1,5 @@ -name: Delete labels in bulk +name: "Manual: Delete labels in bulk" on: workflow_dispatch: diff --git a/.github/workflows/merge_blocking_pr.yml b/.github/workflows/merge-blocking_pr.yml similarity index 52% rename from .github/workflows/merge_blocking_pr.yml rename to .github/workflows/merge-blocking_pr.yml index 1127d2369..a3f40a1a8 100644 --- a/.github/workflows/merge_blocking_pr.yml +++ b/.github/workflows/merge-blocking_pr.yml @@ -6,11 +6,13 @@ on: - closed jobs: - update_blocked_status: + update-blocked-status: name: Update Blocked Status runs-on: ubuntu-latest - if: github.event.pull_request.merged == true + # a pr that was a `blocking:` label was merged. + # find the open pr's it was blocked by and trigger a refresh of their state + if: github.event.pull_request.merged == true && contains( join( github.event.pull_request.labels.*.name, ',' ), "blocking:" ) permissions: issues: write @@ -22,30 +24,32 @@ jobs: id: gather_deps env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR_NUMBER: ${{ github.event.pull_request.number }} run: | owner=$(echo "${{ github.repository }}" | cut -d '/' -f 1) repo=$(echo "${{ github.repository }}" | cut -d '/' -f 2) + query=" + query(\$repo: String!, \$owner: String!, \$endCursor: String) { + repository(name: \$repo, owner: \$owner) { + pullRequests(first: 100, after: \$endCursor, states: [OPEN], labels: [\"blocked-by:${PR_NUMBER}\"]) { + nodes { + number + bodyText + merged + } + pageInfo { + hasNextPage + endCursor + } + } + } + } + " blocked_prs=$( gh api graphql \ -f repo="$repo" \ -f owner="$owner" \ - -f query=' - query($repo: String!, $owner: String!, $endCursor: String) { - repository(name: $repo, owner: $owner) { - pullRequests(first: 100, after: $endCursor, states: [OPEN], labels: ["blocked"]) { - nodes { - number - bodyText - merged - } - pageInfo { - hasNextPage - endCursor - } - } - } - } - ' \ + -f query="$query" \ --paginate \ --slurp \ | jq -c --argjson pr "${{ github.event.pull_request.number }}" ' @@ -60,7 +64,7 @@ jobs: echo "deps=$blocked_prs" >> "$GITHUB_OUTPUT" echo "numdeps=$(jq -r '. | length' <<< "$blocked_prs")" >> "$GITHUB_OUTPUT" - - name: Trigger Blocked PP Workflows for Dependants + - name: Trigger Blocked PR Workflows for Dependants if: fromJSON(steps.gather_deps.outputs.numdeps) > 0 env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -71,8 +75,28 @@ jobs: --method POST \ -H "Accept: application/vnd.github+json" \ -H "X-GitHub-Api-Version: 2022-11-28" \ - "/repos/${{ github.repository }}/actions/workflows/blocked_prs.yml/dispatches" \ + "/repos/${{ github.repository }}/actions/workflows/blocked-prs.yml/dispatches" \ -f "ref=${{ github.ref_name }}" \ -f "inputs[pr_id]=$pr" done < <(jq -c '.[].number' <<< "$DEPS") + label-cleanup: + # this pr is closed, no need for these anymore + name: "Cleanup Related blocking: or blocked-by: labels" + runs-on: ubuntu-latest + + permissions: + issues: write + pull-requests: write + + steps: + - name: Checkout Default Branch + uses: actions/checkout@v4 + with: + ref: ${{ github.event.repository.default_branch }} + - name: Delete Related Labels + uses: ./.github/actions/delete-labels + with: + repository: ${{ github.repository }} + gh_token: ${{ secrets.GITHUB_TOKEN }} + labels: ${{ format('blocking:{0},blocked-by:{0}', github.event.pull_request.number ) }}