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


##########
.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'

Review Comment:
   Done in 45672af32d with your filter: the sweep leaves out a merged pull 
request labelled `run ci`, so its runs finish. The header comment, the 
AGENTS.md bullet and CONTRIBUTING.md now say why: on a release branch below the 
default the push after the merge builds without the tests, so the tests the 
label asked for are the only ones the change gets there. A dry run of the sweep 
script (with `gh run cancel` replaced by `echo`) for the push of #16770's merge 
into 7.0.x, labelled `run ci`, no longer lists its head among the unneeded 
ones, while the push of #16777's merge, unlabelled, still does.
   
   A head commit starting with `[run tests]` also asks for the tests on a pull 
request, but the label is the documented way to test a backport, and none of 
the last 100 merged pull requests used the marker, so I left it out rather than 
read the head commit of every merged pull request.
   



##########
CONTRIBUTING.md:
##########
@@ -278,10 +278,12 @@ Every push and pull request runs the GitHub Actions 
workflows, and runner time i
 project, so two controls limit what runs:
 
 * A draft pull request runs no workflow unless it carries the `run ci` label; 
marking it ready for review starts CI.
-  A pull request labelled `no ci` runs no workflow at all, draft or not. (The 
Release Drafter still labels every
-  pull request into a release branch; it runs no build.) Labels are read when 
a push arrives, so add `run ci` (or
-  remove `no ci`) and then push, or mark the draft ready, to get a run. 
Pushing to a draft still cancels the run in
-  flight for it, as every push to a pull request or a branch does.
+  A pull request labelled `no ci` runs no workflow at all, draft or not. 
Labels are read when a push arrives, so add
+  `run ci` (or remove `no ci`) and then push, or mark the draft ready, to get 
a run. Pushing to a draft still cancels
+  the run in flight for it, as every push to a pull request or a branch does.
+* Adding `no ci` to a pull request cancels the runs it has in flight, and 
merging a pull request cancels the runs

Review Comment:
   Changed to "merging a pull request into a release branch" in 45672af32d.
   



-- 
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