Skip to content

Commit c7f23e2

Browse files
committed
GH Actions: safeguard the PR label workflow some more
The `srvaroa/labeler` action runner will use the `labeler.yml` from the repo's default branch by default, which is good from a security perspective, but that means that PRs changing the `labeler.yml` file will not be tested until they have been merged and a _next_ PR is opened. As the `srvaroa/labeler` action runner will silently fail, this also means that the labeling will stop working without any indication (other than labels no longer being added). I'd like to prevent getting into that situation (again). The `yamllint` workflow I introduced earlier is already a big step in the right direction. This commit introduces a second safeguard: * It adds a second job to the workflow which will only run when the files relevant for the workflow are being changed in a PR. * In that case, this second job will: - run with the PR-local version of the `labeler.yml` file; - run on **_all_** pull request events (except merge), not only when the PR is opened. - fail the workflow run if any errors are encountered. This should make sure that this workflow is safeguarded properly and will continuing functioning as intended, even when changes are made to the logic. Refs: * srvaroa/labeler#105
1 parent 70ab081 commit c7f23e2

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

.github/workflows/label-new-prs.yml

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,48 @@ name: Label new PRs
22

33
on:
44
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target
5+
# The `pull_request_target` event is used for "normal" PRs to label them when they are opened.
6+
# This will use the `labeler.yml` file in the default (master) branch of the repo.
57
pull_request_target:
68
types:
79
- opened
810
- ready_for_review
911

12+
# The `pull_request` event is used for PRs which change the files which handle the labeling to prevent a silently failing action.
13+
# This will use the `labeler.yml` file in the PR branch.
14+
pull_request:
15+
paths:
16+
- '.github/workflows/label-new-prs.yml'
17+
- '.github/labeler.yml'
18+
1019
jobs:
1120
label-new-prs:
1221
runs-on: ubuntu-latest
22+
if: github.repository_owner == 'PHPCSStandards' && github.event_name == 'pull_request_target'
23+
24+
name: Add labels to new PRs
25+
26+
steps:
27+
- name: Label new PRs
28+
uses: srvaroa/labeler@master
29+
env:
30+
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
31+
32+
validate-labeler-worflow:
33+
runs-on: ubuntu-latest
34+
if: github.repository_owner == 'PHPCSStandards' && github.event_name == 'pull_request' && github.event.pull_request.merged == false
35+
36+
name: Validate changes to Labeler logic
1337

1438
steps:
15-
- uses: srvaroa/labeler@master
39+
# Checkout is needed to use the `use_local_config` option.
40+
- name: Checkout code
41+
uses: actions/checkout@v3
42+
43+
- name: Verify changes to the labeling logic
44+
uses: srvaroa/labeler@master
45+
with:
46+
use_local_config: true
47+
fail_on_error: true
1648
env:
1749
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"

0 commit comments

Comments
 (0)