Skip to content

Commit a4ef060

Browse files
Simplify workflow to use pull_request trigger (#626)
1 parent 00b5533 commit a4ef060

File tree

1 file changed

+5
-78
lines changed

1 file changed

+5
-78
lines changed
Lines changed: 5 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
name: Ensure lists are sorted
22

33
on:
4-
pull_request_target:
5-
branches: [ "master" ]
6-
workflow_dispatch: {}
7-
issue_comment:
8-
types: [ created ]
9-
merge_group: {}
4+
pull_request:
105

116
concurrency:
12-
# For pull_request_target workflows we want to use head_ref -- the branch triggering the workflow. Otherwise,
13-
# use ref, which is the branch for a push event or workflow trigger. And for an issue comment just give up grouping.
14-
group: ${{ github.event_name == 'pull_request_target' && github.head_ref || (github.event_name == 'issue_comment' && github.run_id || github.ref) }}
15-
cancel-in-progress: ${{ github.event_name == 'pull_request_target' }}
7+
group: ${{ github.head_ref }}
8+
cancel-in-progress: true
169

1710
jobs:
1811
tests:
@@ -23,88 +16,24 @@ jobs:
2316
issues: read
2417
pull-requests: read
2518
checks: write
26-
if: github.event_name != 'issue_comment'
2719

2820
steps:
29-
- name: Get PR branch
30-
if: github.event_name == 'issue_comment'
31-
uses: alessbell/[email protected] # Fork of xt0rted/pull-request-comment-branch, see https://github.com/xt0rted/pull-request-comment-branch/issues/322
32-
id: comment_branch
33-
34-
- name: Get Refs
35-
id: get_head_ref
36-
run: |
37-
# Accurate for push events, merge queues, and workflow dispatch.
38-
head_ref="${{ github.ref }}"
39-
repo="${{ github.repository }}"
40-
41-
if [[ "${{ github.event_name }}" == 'pull_request_target' ]]; then
42-
head_ref="${{ github.head_ref }}"
43-
repo="${{ github.event.pull_request.head.repo.full_name }}"
44-
elif [[ "${{ github.event_name }}" == 'issue_comment' ]]; then
45-
# Rely on comment_branch to figure out the head and base
46-
head_ref="${{ steps.comment_branch.outputs.head_ref }}"
47-
repo="${{ steps.comment_branch.outputs.head_owner }}/${{ steps.comment_branch.outputs.head_repo }}"
48-
fi
49-
50-
echo "##[set-output name=head_ref;]$head_ref"
51-
echo "##[set-output name=repo;]$repo"
52-
5321
- name: Checkout
5422
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
5523
with:
56-
repository: ${{ steps.get_head_ref.outputs.repo }}
57-
ref: ${{ steps.get_head_ref.outputs.head_ref }}
5824
fetch-depth: 0
5925

60-
- name: Validate Branch vs. Trigerring SHA
61-
run: |
62-
# If this is from a pull request validate that what we checked out is the same as the PR head.
63-
# If not we'll just fail -- the workflow will be cancelled momentarily.
64-
if [[ "${{ github.event_name }}" == 'pull_request_target' ]]; then
65-
if [[ "${{ github.event.pull_request.head.sha }}" != "$(git rev-parse HEAD)" ]]; then
66-
echo "Workflow is out of date with branch, cancelling"
67-
exit 1
68-
fi
69-
fi
70-
71-
- name: Get Refs
72-
id: get_base_ref
73-
run: |
74-
run_all=""
75-
base_ref=""
76-
77-
if [[ "${{ github.event_name }}" == 'pull_request_target' ]]; then
78-
# Detect changes based on whatever we're merging into.
79-
base_ref="${{ github.base_ref }}"
80-
elif [[ "${{ github.event_name }}" == 'push' || "${{ github.event_name }}" == 'merge_group' ]]; then
81-
# Detect changes based on the previous commit
82-
base_ref="$(git rev-parse HEAD^)"
83-
elif [[ "${{ github.event_name }}" == 'workflow_dispatch' ]]; then
84-
# Run on a target
85-
run_all="true"
86-
elif [[ "${{ github.event_name }}" == 'issue_comment' ]]; then
87-
# Rely on comment_branch to figure out base
88-
base_ref="${{ steps.comment_branch.outputs.base_ref }}"
89-
fi
90-
91-
echo "##[set-output name=run_all;]$run_all"
92-
echo "##[set-output name=base_ref;]$base_ref"
93-
9426
- uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065
9527
with:
9628
python-version: '3.10'
9729

9830
- name: Sort file content order
99-
if: github.event_name != 'issue_comment'
10031
run: |
10132
python ./scripts/sorter.py
10233
10334
- name: Commit & Push Results, if needed
104-
if: github.event_name != 'issue_comment'
105-
id: final_basic_validation
35+
id: push_results
10636
run: |
107-
10837
if [ -z "$(git status --porcelain)" ]; then
10938
echo "No files changed, nothing to do"
11039
exit 0
@@ -114,6 +43,4 @@ jobs:
11443
git config user.email '[email protected]'
11544
git add '*.txt'
11645
git commit -m "Auto sorting static files"
117-
# This will only work when running for a pull_request_target, but rather than filter we'll let this expose
118-
# any issues.
119-
git push origin ${{ steps.get_head_ref.outputs.head_ref }}
46+
git push origin HEAD:${{ github.head_ref }}

0 commit comments

Comments
 (0)