From 4c591d5bec77e578071f2a58d074fa4e92012074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Tue, 17 Nov 2026 14:19:32 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(validate-news):=20skip=20lab?= =?UTF-8?q?el=20changes=20on=20closed=20PRs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adding a label to a merged pull request reran the news check against a head that is already on the base branch, so it found no fragment and failed, as on tox-dev/filelock#968. --- .../+news-skip-closed-pull-request.bugfix.md | 2 ++ docs/pr-checks.md | 3 ++ tests/conftest.py | 2 ++ tests/validate_news/test_news.py | 33 +++++++++++++++++++ validate-news/action.yml | 19 +++++++---- 5 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 changelog.d/+news-skip-closed-pull-request.bugfix.md diff --git a/changelog.d/+news-skip-closed-pull-request.bugfix.md b/changelog.d/+news-skip-closed-pull-request.bugfix.md new file mode 100644 index 0000000..78c88eb --- /dev/null +++ b/changelog.d/+news-skip-closed-pull-request.bugfix.md @@ -0,0 +1,2 @@ +`validate-news` no longer fails when a label changes on a merged or closed pull request, where the merged head is +already on the base branch and no fragment differs from the merge base. diff --git a/docs/pr-checks.md b/docs/pr-checks.md index a05b692..a5cdf4b 100644 --- a/docs/pr-checks.md +++ b/docs/pr-checks.md @@ -222,6 +222,9 @@ Requirements and pitfalls: the pull request has now. The job needs `pull-requests: read`. It adds no labels and writes nothing to GitHub. - Opening a pull request with labels, as `gh pr create --label` does, fires `labeled` a moment after `opened`. A label event within a minute of opening passes at once, since the run for `opened` reads the labels when it checks. +- A label event on a closed or merged pull request passes at once. After a merge the head is already on the base branch, + so the fragment check would find no fragment to count; a closed pull request is checked again by the `reopened` run, + so keep `reopened` in the trigger types. - Do not cancel runs in progress with a `concurrency` group: the cancelled run shows as a failed check while the other passes. diff --git a/tests/conftest.py b/tests/conftest.py index 9d666d3..9f2bbe7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -587,6 +587,8 @@ def _expression( return environment.get("PULL_NUMBER", "7") if expression == "github.event.action": return environment.get("EVENT_ACTION", "opened") + if expression == "github.event.pull_request.state": + return environment.get("PULL_STATE", "open") if expression == "github.event.pull_request.created_at": return environment.get("CREATED_AT", "2026-01-01T00:00:00Z") if expression == "github.event.pull_request.updated_at": diff --git a/tests/validate_news/test_news.py b/tests/validate_news/test_news.py index 8aaf2a6..956f657 100644 --- a/tests/validate_news/test_news.py +++ b/tests/validate_news/test_news.py @@ -477,3 +477,36 @@ def test_validate_news_skips_labels_set_while_opening( **_current_labels(github_api, "false"), ) assert (result.returncode, "add a changelog fragment" in result.stderr) == ((1, True) if checked else (0, False)) + + +@pytest.mark.parametrize( + ("event", "state", "checked"), + [ + pytest.param("labeled", "closed", False, id="labeled-after-merge"), + pytest.param("unlabeled", "closed", False, id="unlabeled-after-close"), + pytest.param("labeled", "open", True, id="labeled-while-open"), + ], +) +def test_validate_news_skips_labels_changed_on_a_closed_pull_request( + repository: Path, + tmp_path: Path, + action_scripts: Path, + event: str, + state: str, + checked: bool, + github_api: tuple[HTTPServer, dict[str, str]], +) -> None: + result = action( + repository, + tmp_path, + action_scripts, + "validate-news", + version="0.2.0", + BASE_REF="main", + BOT_AUTHOR="false", + EVENT_ACTION=event, + PULL_STATE=state, + UPDATED_AT="2026-01-02T00:00:00Z", + **_current_labels(github_api, "false"), + ) + assert (result.returncode, "add a changelog fragment" in result.stderr) == ((1, True) if checked else (0, False)) diff --git a/validate-news/action.yml b/validate-news/action.yml index 6f33dd1..3378331 100644 --- a/validate-news/action.yml +++ b/validate-news/action.yml @@ -28,15 +28,22 @@ inputs: runs: using: composite steps: - - id: opening - name: 🧭 Skip labels set while opening + - id: label-change + name: 🧭 Skip label changes the news check does not need if: github.event.action == 'labeled' || github.event.action == 'unlabeled' shell: bash env: + STATE: ${{ github.event.pull_request.state }} CREATED: ${{ github.event.pull_request.created_at }} UPDATED: ${{ github.event.pull_request.updated_at }} run: | set -euo pipefail + # A merged pull request's head is already on the base branch, so no fragment differs from the merge base. + if [[ "$STATE" != open ]]; then + echo "News check skipped: labels changed on a $STATE pull request" + echo 'skip=true' >>"$GITHUB_OUTPUT" + exit 0 + fi # `gh pr create --label` labels a pull request right after opening it, so `labeled` fires beside `opened`. The # run for `opened` reads the current labels when it checks, which leaves this run nothing to add. seconds=$(($(date -d "$UPDATED" +%s) - $(date -d "$CREATED" +%s))) @@ -46,7 +53,7 @@ runs: fi - id: news-change name: 🧭 Check whether news inputs changed - if: steps.opening.outputs.skip != 'true' + if: steps.label-change.outputs.skip != 'true' shell: bash env: BASE_REF: ${{ github.base_ref }} @@ -106,7 +113,7 @@ runs: echo "Changed fragments: $(python3 "$GITHUB_ACTION_PATH/../tools/plural.py" "$(<"$RUNNER_TEMP/news-fragment-count")" fragment) valid against origin/$BASE_REF" - id: labels name: 🏷️ Read the current labels - if: steps.opening.outputs.skip != 'true' + if: steps.label-change.outputs.skip != 'true' shell: bash env: GH_TOKEN: ${{ github.token }} @@ -119,7 +126,7 @@ runs: echo "Current labels of #$PULL: skip news $([[ "$skip" == true ]] && echo present || echo absent)" echo "skip=$skip" >>"$GITHUB_OUTPUT" - name: 🔍 Require news for human changes - if: steps.opening.outputs.skip != 'true' + if: steps.label-change.outputs.skip != 'true' shell: bash env: SKIP_NEWS: ${{ steps.labels.outputs.skip }} @@ -139,7 +146,7 @@ runs: fi - id: lock-change name: 🧭 Check whether release inputs changed - if: steps.opening.outputs.skip != 'true' + if: steps.label-change.outputs.skip != 'true' shell: bash env: BASE_REF: ${{ github.base_ref }}