Skip to content

Commit 7517dcc

Browse files
committed
chore: harden pull request check workflow
1 parent d37232a commit 7517dcc

1 file changed

Lines changed: 66 additions & 14 deletions

File tree

.github/workflows/pull-request-check.yml

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ on:
44
pull_request_target:
55
types: [ edited, opened ]
66

7-
env:
8-
GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
7+
permissions:
8+
issues: read
9+
pull-requests: read
910

1011
jobs:
1112
pr-check:
@@ -14,22 +15,73 @@ jobs:
1415
runs-on: ubuntu-latest
1516
steps:
1617
- name: check branch name
17-
uses: apecloud-inc/check-branch-name@v0.1.0
1818
if: github.event.pull_request.head.repo.full_name == github.repository
19-
with:
20-
branch_pattern: 'feature/|bugfix/|release/|hotfix/|support/|releasing/|dependabot/'
21-
comment_for_invalid_branch_name: 'This branch name is not following the standards: feature/|bugfix/|release/|hotfix/|support/|releasing/|dependabot/'
22-
fail_if_invalid_branch_name: 'true'
23-
ignore_branch_pattern: 'main|master'
19+
env:
20+
HEAD_REF: ${{ github.head_ref }}
21+
run: |
22+
branch_pattern='^(feature/|bugfix/|release/|hotfix/|support/|releasing/|dependabot/)'
23+
ignore_branch_pattern='^(main|master)$'
24+
25+
if [[ "$HEAD_REF" =~ $ignore_branch_pattern ]]; then
26+
echo "This branch should be ignored"
27+
exit 0
28+
fi
29+
30+
if [[ "$HEAD_REF" =~ $branch_pattern ]]; then
31+
echo "This branch has a valid name"
32+
exit 0
33+
fi
34+
35+
echo "::error::This branch name is not following the standards: feature/|bugfix/|release/|hotfix/|support/|releasing/|dependabot/"
36+
exit 1
2437
2538
- name: check PR title
2639
uses: amannn/action-semantic-pull-request@v5
40+
env:
41+
GITHUB_TOKEN: ${{ github.token }}
2742

28-
- uses: actions/checkout@v4
2943
- name: check issue link
44+
env:
45+
GH_TOKEN: ${{ github.token }}
46+
REPO: ${{ github.repository }}
47+
REPO_OWNER: ${{ github.repository_owner }}
48+
PR_NUMBER: ${{ github.event.pull_request.number }}
49+
PR_TITLE: ${{ github.event.pull_request.title }}
3050
run: |
31-
bash ${{ github.workspace }}/.github/utils/issue_link.sh \
32-
${{ github.repository }} \
33-
${{ github.repository_owner }} \
34-
${{ github.event.pull_request.number }} \
35-
"${{ github.event.pull_request.title }}"
51+
if [[ "$PR_TITLE" == chore* || "$PR_TITLE" == docs* ]]; then
52+
echo "PR skip the issue check"
53+
exit 0
54+
fi
55+
56+
repo_name="${REPO/${REPO_OWNER}\//}"
57+
closing_issues_references="$(
58+
gh api graphql \
59+
-f owner="$REPO_OWNER" \
60+
-f name="$repo_name" \
61+
-F number="$PR_NUMBER" \
62+
-f query='
63+
query($owner: String!, $name: String!, $number: Int!) {
64+
repository(owner: $owner, name: $name) {
65+
pullRequest(number: $number) {
66+
closingIssuesReferences(first: 10) {
67+
edges {
68+
node {
69+
title
70+
number
71+
}
72+
}
73+
}
74+
}
75+
}
76+
}' \
77+
--jq '.data.repository.pullRequest.closingIssuesReferences.edges'
78+
)"
79+
80+
echo "Closing Issues References: $closing_issues_references"
81+
82+
if [[ "$closing_issues_references" == "[]" ]]; then
83+
echo "PR has no Issues References"
84+
exit 1
85+
fi
86+
87+
echo "PR has Issues References"

0 commit comments

Comments
 (0)