jdaugherty commented on code in PR #16779:
URL: https://github.com/apache/grails-core/pull/16779#discussion_r4393249038


##########
.github/workflows/cancel-unneeded-pr-runs.yml:
##########
@@ -0,0 +1,80 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Merging a pull request, or labelling it "no ci", does not stop the runs 
still queued or in progress for its head
+# commit, and their result can no longer change anything. This workflow 
cancels the pull_request runs of every
+# open pull request labelled "no ci", and on a push to a release branch also 
those of every pull request merged by
+# the pushed commit that were created before the merge. A run is kept when an 
open pull request not labelled
+# "no ci" has the same head commit, since it reports for that pull request 
too. A pull_request run from a fork gets
+# a read-only token, so the label only cancels at once on a pull request from 
this repository; the runs of a fork's
+# pull request are cancelled by the next push to a release branch. That is 
also why merges are found from the push
+# and not from the pull request being closed.
+name: "Cancel Unneeded PR Runs"
+on:
+  push:
+    branches:
+      - '[0-9]+.[0-9]+.x'
+  pull_request:
+    types: [ labeled ]
+permissions:
+  actions: write
+  contents: read
+  pull-requests: read
+jobs:
+  cancel:
+    name: "Cancel Unneeded PR Runs"
+    # Unlike every other workflow that reacts to a pull request, the "no ci" 
label runs this one rather than skipping it.
+    if: >-
+      github.event_name == 'push'
+      || (github.event.label.name == 'no ci' && 
github.event.pull_request.head.repo.full_name == github.repository)
+    runs-on: ubuntu-slim
+    timeout-minutes: 5
+    steps:
+      - name: "🛑 Cancel the runs of merged and \"no ci\" pull requests"
+        env:
+          EVENT: ${{ github.event_name }}
+          GH_TOKEN: ${{ github.token }}
+          REPO: ${{ github.repository }}
+          SHA: ${{ github.sha }}
+        run: |
+          # Every open pull request as its head repository, its head commit 
and whether it is labelled "no ci".
+          gh api --paginate "repos/$REPO/pulls?state=open&per_page=100" \
+            --jq '.[] | [.head.repo.full_name, .head.sha, (any(.labels[]; 
.name == "no ci") | tostring)] | @tsv' > open.tsv
+          # The head repository and head commit of each pull request whose 
runs are unneeded, with the time its runs
+          # must have been created before: the merge, or now for a pull 
request labelled "no ci".
+          {
+            if [[ "$EVENT" == push ]]; then
+              gh api "repos/$REPO/commits/$SHA/pulls" \
+                --jq '.[] | select(.merged_at != null) | 
[.head.repo.full_name, .head.sha, .merged_at] | @tsv'
+            fi
+            awk -F '\t' -v now="$(date -u +%Y-%m-%dT%H:%M:%SZ)" '$3 == "true" 
{ print $1 "\t" $2 "\t" now }' open.tsv
+          } > unneeded.tsv
+          # The runs of this workflow are never cancelled: a sweep started by 
another label would stop this one.
+          workflow=${GITHUB_WORKFLOW_REF#"$REPO/"}
+          workflow=${workflow%@*}
+          # Every pull_request run not finished yet, listed once rather than 
per pull request to spare the API rate
+          # limit; pending is a run waiting for its concurrency group.
+          for status in queued pending in_progress; do
+            gh api --paginate 
"repos/$REPO/actions/runs?event=pull_request&status=$status&per_page=100" \
+              --jq ".workflow_runs[] | select(.path != \"$workflow\") | [.id, 
.head_repository.full_name, .head_sha, .created_at] | @tsv"

Review Comment:
   Release Drafter doesn't label anything on 7.x. The pinned v7.3.1 
`release-drafter/release-drafter` action only builds the draft; v7 moved 
labelling into the separate `release-drafter/release-drafter/autolabeler` 
action, which we don't run. That's why 8.0.x removed the `pull_request` trigger 
(see the comment in its `release-notes.yml`). The draft is built from merged 
pull requests, so a run for an open pull request writes the same draft as the 
last push run. Merging pushes to the release branch, which runs the drafter on 
`push`, and the sweep only lists `pull_request` runs, so it never cancels that 
run. From 8.0.x up there are no pull-request drafter runs, so an exclusion 
would do nothing after the merge-up.
   
   The one gap is on 7.x: push and pull-request drafter runs share the 
`release-drafter-<base>` concurrency group, so a `no ci` label's drafter run 
replaces a queued push run, and the sweep then cancels that replacement. Fork 
pull requests already cause the same thing today: their run replaces the push 
run and then fails on the read-only token. I'd rather fix that by backporting 
8.0.x's removal of the trigger than by exempting the drafter here.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to