Skip to content

Commit c168993

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 c168993

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,46 @@ 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
1325

1426
steps:
1527
- uses: srvaroa/labeler@master
1628
env:
1729
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
30+
31+
validate-labeler-worflow:
32+
runs-on: ubuntu-latest
33+
if: github.repository_owner == 'PHPCSStandards' && github.event_name == 'pull_request' && github.event.pull_request.merged == false
34+
35+
name: Validate changes to Labeler logic
36+
37+
steps:
38+
# Checkout is needed to use the `use_local_config` option.
39+
- name: Checkout code
40+
uses: actions/checkout@v3
41+
42+
- uses: srvaroa/labeler@master
43+
with:
44+
use_local_config: true
45+
fail_on_error: true
46+
env:
47+
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"

0 commit comments

Comments
 (0)