Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .github/workflows/commit-check.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Commit Check

on:
pull_request:
pull_request_target:
branches: 'main'
workflow_dispatch:

Expand All @@ -11,14 +11,17 @@ jobs:
permissions: # use permissions because of use pr-comments
contents: read
pull-requests: write
issues: write
steps:
- uses: actions/checkout@v5
with:
ref: ${{ github.event.pull_request.head.sha }} # checkout PR HEAD commit
ref: ${{ format('refs/pull/{0}/head', github.event.pull_request.number) }} # checkout PR HEAD commit
fetch-depth: 0 # fetch all history for all branches and tags
persist-credentials: false
- uses: ./ # self test
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # use GITHUB_TOKEN because of use pr-comments
GITHUB_PR_NUMBER: ${{ github.event.pull_request.number }} # pass PR number for pull_request_target events
with:
message: true
branch: true
Expand All @@ -28,4 +31,4 @@ jobs:
merge-base: true
imperative: true
job-summary: true
pr-comments: ${{ github.event_name == 'pull_request' }}
pr-comments: ${{ github.event_name == 'pull_request' || github.event_name == 'pull_request_target' }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
venv/
.venv/
__pycache__/
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ name: Commit Check

on:
push:
pull_request:
pull_request_target:
branches: 'main'

jobs:
Expand All @@ -36,11 +36,13 @@ jobs:
permissions: # use permissions because use of pr-comments
contents: read
pull-requests: write
issues: write
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }} # checkout PR HEAD commit
ref: ${{ format('refs/pull/{0}/head', github.event.pull_request.number) }} # checkout PR HEAD commit
fetch-depth: 0 # required for merge-base check
persist-credentials: false
- uses: commit-check/commit-check-action@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # use GITHUB_TOKEN because use of pr-comments
Expand All @@ -53,7 +55,7 @@ jobs:
merge-base: false
imperative: false
job-summary: true
pr-comments: ${{ github.event_name == 'pull_request' }}
pr-comments: ${{ github.event_name == 'pull_request' || github.event_name == 'pull_request_target' }}
```

## Used By
Expand Down Expand Up @@ -136,9 +138,7 @@ jobs:
- Default: `false`

> [!IMPORTANT]
> `pr-comments` is an experimental feature. By default, it's disabled. To use it, you need to set `GITHUB_TOKEN` in the GitHub Action.
>
> This feature currently doesn’t work with forked repositories. For more details, refer to issue [#77](https://github.com/commit-check/commit-check-action/issues/77).
> `pr-comments` is an experimental feature. By default, it's disabled. To use it, you need to set `GITHUB_TOKEN` in the GitHub Action and ensure your workflow has `issues: write` permission.

Note: the default rule of above inputs is following [this configuration](https://github.com/commit-check/commit-check/blob/main/.commit-check.yml). If you want to customize, just add your `.commit-check.yml` config file under your repository root directory.

Expand Down
20 changes: 14 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
GITHUB_REPOSITORY = os.getenv("GITHUB_REPOSITORY")
GITHUB_REF = os.getenv("GITHUB_REF")
GITHUB_PR_NUMBER = os.getenv("GITHUB_PR_NUMBER")


def log_env_vars():
Expand Down Expand Up @@ -116,12 +117,19 @@ def add_pr_comments() -> int:
try:
token = os.getenv("GITHUB_TOKEN")
repo_name = os.getenv("GITHUB_REPOSITORY")
pr_number = os.getenv("GITHUB_REF")
if pr_number is not None:
pr_number = pr_number.split("/")[-2]
else:
# Handle the case where GITHUB_REF is not set
raise ValueError("GITHUB_REF environment variable is not set")

# Get PR number from GITHUB_PR_NUMBER (for pull_request_target) or extract from GITHUB_REF (for pull_request)
pr_number = os.getenv("GITHUB_PR_NUMBER")
if pr_number is None:
# Fallback to extracting from GITHUB_REF for backward compatibility
github_ref = os.getenv("GITHUB_REF")
if github_ref is not None:
pr_number = github_ref.split("/")[-2]
else:
# Handle the case where neither environment variable is set
raise ValueError(
"Neither GITHUB_PR_NUMBER nor GITHUB_REF environment variable is set"
)

# Initialize GitHub client
g = Github(token)
Expand Down