diff --git a/.github/workflows/commit-check.yml b/.github/workflows/commit-check.yml index b22e048..5c9a243 100644 --- a/.github/workflows/commit-check.yml +++ b/.github/workflows/commit-check.yml @@ -1,7 +1,7 @@ name: Commit Check on: - pull_request: + pull_request_target: branches: 'main' workflow_dispatch: @@ -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 @@ -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' }} diff --git a/.gitignore b/.gitignore index 43f4b6f..2233054 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ venv/ .venv/ +__pycache__/ diff --git a/README.md b/README.md index 478a8d4..2c3f9fd 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ name: Commit Check on: push: - pull_request: + pull_request_target: branches: 'main' jobs: @@ -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 @@ -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 @@ -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. diff --git a/main.py b/main.py index ef9c920..3bf39f5 100755 --- a/main.py +++ b/main.py @@ -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(): @@ -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)