From 459a6694556bf1a935e9e85c38e13353a707e252 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Tue, 19 Aug 2025 23:31:35 +0300 Subject: [PATCH 01/10] feat: output pr_comments to github action --- main.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/main.py b/main.py index ef9c920..9305e37 100755 --- a/main.py +++ b/main.py @@ -168,6 +168,9 @@ def add_pr_comments() -> int: print(f"Creating a new comment on PR #{pr_number}.") pull_request.create_comment(body=pr_comments) + # output pr_comments to GitHub Actions + print(f"::set-output name=pr_comments::{pr_comments}") + return 0 if result_text is None else 1 except Exception as e: print(f"Error posting PR comment: {e}", file=sys.stderr) From c388ea5c86f947e94d086b817c9c19e53d6d50a5 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Tue, 19 Aug 2025 23:34:51 +0300 Subject: [PATCH 02/10] feat: use GITHUB_OUTPUT instead --- main.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 9305e37..66b42d6 100755 --- a/main.py +++ b/main.py @@ -168,8 +168,9 @@ def add_pr_comments() -> int: print(f"Creating a new comment on PR #{pr_number}.") pull_request.create_comment(body=pr_comments) - # output pr_comments to GitHub Actions - print(f"::set-output name=pr_comments::{pr_comments}") + # output pr_comments to $GITHUB_OUTPUT + with open(os.environ["GITHUB_OUTPUT"], "a") as output_file: + output_file.write(f"pr_comments={pr_comments}\n") return 0 if result_text is None else 1 except Exception as e: From af229490a2f6e7f272e4f46beaa115944eef8dc6 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Tue, 19 Aug 2025 23:40:59 +0300 Subject: [PATCH 03/10] feat: save to pr-comments.txt --- .github/workflows/commit-check.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.github/workflows/commit-check.yml b/.github/workflows/commit-check.yml index b22e048..f92b233 100644 --- a/.github/workflows/commit-check.yml +++ b/.github/workflows/commit-check.yml @@ -29,3 +29,12 @@ jobs: imperative: true job-summary: true pr-comments: ${{ github.event_name == 'pull_request' }} + + - name: Save GITHUB_OUTPUT to pr-comments.txt + run: echo "$GITHUB_OUTPUT" > pr-comments.txt + + - name: Upload pr-comments.txt + uses: actions/upload-artifact@v4 + with: + name: commit-check-pr-comments + path: pr-comments.txt From 0a63946c14e1b01fbf7115c2dd7bf8f22d077d92 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Tue, 19 Aug 2025 23:56:37 +0300 Subject: [PATCH 04/10] fix: update main.py --- main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 66b42d6..c631563 100755 --- a/main.py +++ b/main.py @@ -169,8 +169,8 @@ def add_pr_comments() -> int: pull_request.create_comment(body=pr_comments) # output pr_comments to $GITHUB_OUTPUT - with open(os.environ["GITHUB_OUTPUT"], "a") as output_file: - output_file.write(f"pr_comments={pr_comments}\n") + with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as env_file: + env_file.write(f"pr_comments={pr_comments}\n") return 0 if result_text is None else 1 except Exception as e: From 4305833cee8178098f02dc9f15ba30a4e0d40335 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Tue, 19 Aug 2025 23:59:32 +0300 Subject: [PATCH 05/10] fix: update commit-check.yml --- .github/workflows/commit-check.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/commit-check.yml b/.github/workflows/commit-check.yml index f92b233..3cd26b2 100644 --- a/.github/workflows/commit-check.yml +++ b/.github/workflows/commit-check.yml @@ -30,8 +30,8 @@ jobs: job-summary: true pr-comments: ${{ github.event_name == 'pull_request' }} - - name: Save GITHUB_OUTPUT to pr-comments.txt - run: echo "$GITHUB_OUTPUT" > pr-comments.txt + - name: Save PR comments to pr-comments.txt + run: echo "${{ steps.commit-check.outputs.pr_comments }}" > pr-comments.txt - name: Upload pr-comments.txt uses: actions/upload-artifact@v4 From 6b1640759be256247a8d88f9681bc4beb2391304 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Wed, 20 Aug 2025 00:01:50 +0300 Subject: [PATCH 06/10] fix: update commit-check.yml --- .github/workflows/commit-check.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/commit-check.yml b/.github/workflows/commit-check.yml index 3cd26b2..aab4ee6 100644 --- a/.github/workflows/commit-check.yml +++ b/.github/workflows/commit-check.yml @@ -17,6 +17,7 @@ jobs: ref: ${{ github.event.pull_request.head.sha }} # checkout PR HEAD commit fetch-depth: 0 # fetch all history for all branches and tags - uses: ./ # self test + id: commit-check env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # use GITHUB_TOKEN because of use pr-comments with: From 1c123d797c5ac4365b035d247f48545836990ff0 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Wed, 20 Aug 2025 00:14:33 +0300 Subject: [PATCH 07/10] fix: add output to action.yml --- .github/workflows/commit-check.yml | 2 +- action.yml | 8 +++++++- main.py | 4 ++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/.github/workflows/commit-check.yml b/.github/workflows/commit-check.yml index aab4ee6..4b42cd7 100644 --- a/.github/workflows/commit-check.yml +++ b/.github/workflows/commit-check.yml @@ -2,7 +2,7 @@ name: Commit Check on: pull_request: - branches: 'main' + branches: [ 'main' ] workflow_dispatch: jobs: diff --git a/action.yml b/action.yml index eb12512..f74136a 100644 --- a/action.yml +++ b/action.yml @@ -48,7 +48,8 @@ inputs: runs: using: "composite" steps: - - name: Install dependencies and run commit-check + - id: run + name: Install dependencies and run commit-check shell: bash run: | if [[ "$RUNNER_OS" == "Linux" ]]; then @@ -84,3 +85,8 @@ runs: DRY_RUN: ${{ inputs.dry-run }} JOB_SUMMARY: ${{ inputs.job-summary }} PR_COMMENTS: ${{ inputs.pr-comments }} + +outputs: + pr_comments: + description: The formatted PR comment body containing commit-check results + value: ${{ steps.run.outputs.pr_comments }} diff --git a/main.py b/main.py index c631563..af5a0d2 100755 --- a/main.py +++ b/main.py @@ -169,8 +169,8 @@ def add_pr_comments() -> int: pull_request.create_comment(body=pr_comments) # output pr_comments to $GITHUB_OUTPUT - with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as env_file: - env_file.write(f"pr_comments={pr_comments}\n") + with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output_file: + output_file.write(f"pr_comments={pr_comments}\n") return 0 if result_text is None else 1 except Exception as e: From eabbe7019d300218a72532aba7692edae7bffe3a Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Wed, 20 Aug 2025 00:17:36 +0300 Subject: [PATCH 08/10] fix: add output to action.yml --- action.yml | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/action.yml b/action.yml index f74136a..4c31942 100644 --- a/action.yml +++ b/action.yml @@ -45,10 +45,16 @@ inputs: description: post results to the pull request comments required: false default: false + +outputs: + pr_comments: + description: The formatted PR comment body containing commit-check results + value: ${{ steps.commit-check.outputs.pr_comments }} + runs: using: "composite" steps: - - id: run + - id: commit-check name: Install dependencies and run commit-check shell: bash run: | @@ -85,8 +91,3 @@ runs: DRY_RUN: ${{ inputs.dry-run }} JOB_SUMMARY: ${{ inputs.job-summary }} PR_COMMENTS: ${{ inputs.pr-comments }} - -outputs: - pr_comments: - description: The formatted PR comment body containing commit-check results - value: ${{ steps.run.outputs.pr_comments }} From af84ea112f5458a7a86c924712734df17baf6e70 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Wed, 20 Aug 2025 00:30:55 +0300 Subject: [PATCH 09/10] fix: pr_comments output always being set regardless of PR_COMMENTS setting * Initial plan * Fix pr_comments output always being set regardless of PR_COMMENTS setting Co-authored-by: shenxianpeng <3353385+shenxianpeng@users.noreply.github.com> * Update .gitignore to exclude Python cache files Co-authored-by: shenxianpeng <3353385+shenxianpeng@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: shenxianpeng <3353385+shenxianpeng@users.noreply.github.com> --- .gitignore | 3 +++ main.py | 21 +++++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 43f4b6f..7ef7583 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ venv/ .venv/ +__pycache__/ +*.pyc +*.pyo diff --git a/main.py b/main.py index af5a0d2..9e6ee3b 100755 --- a/main.py +++ b/main.py @@ -108,6 +108,20 @@ def add_job_summary() -> int: return 0 if result_text is None else 1 +def set_pr_comments_output() -> None: + """Sets the pr_comments output regardless of PR_COMMENTS setting.""" + result_text = read_result_file() + pr_comments = ( + SUCCESS_TITLE + if result_text is None + else f"{FAILURE_TITLE}\n```\n{result_text}\n```" + ) + + # output pr_comments to $GITHUB_OUTPUT + with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output_file: + output_file.write(f"pr_comments={pr_comments}\n") + + def add_pr_comments() -> int: """Posts the commit check result as a comment on the pull request.""" if PR_COMMENTS == "false": @@ -168,10 +182,6 @@ def add_pr_comments() -> int: print(f"Creating a new comment on PR #{pr_number}.") pull_request.create_comment(body=pr_comments) - # output pr_comments to $GITHUB_OUTPUT - with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output_file: - output_file.write(f"pr_comments={pr_comments}\n") - return 0 if result_text is None else 1 except Exception as e: print(f"Error posting PR comment: {e}", file=sys.stderr) @@ -203,6 +213,9 @@ def main(): ret_code = run_commit_check() ret_code += add_job_summary() ret_code += add_pr_comments() + + # Always set pr_comments output regardless of PR_COMMENTS setting + set_pr_comments_output() if DRY_RUN == "true": ret_code = 0 From e263dec17371a62019bc9a8ec50467a793b02cee Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Wed, 20 Aug 2025 00:36:55 +0300 Subject: [PATCH 10/10] fix: add output to action.yml --- .github/workflows/commit-check.yml | 15 +++++++++++---- main.py | 4 ++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/.github/workflows/commit-check.yml b/.github/workflows/commit-check.yml index 4b42cd7..747f226 100644 --- a/.github/workflows/commit-check.yml +++ b/.github/workflows/commit-check.yml @@ -34,8 +34,15 @@ jobs: - name: Save PR comments to pr-comments.txt run: echo "${{ steps.commit-check.outputs.pr_comments }}" > pr-comments.txt - - name: Upload pr-comments.txt - uses: actions/upload-artifact@v4 + - name: Post PR comments + uses: actions/github-script@v6 with: - name: commit-check-pr-comments - path: pr-comments.txt + script: | + const fs = require('fs'); + const content = fs.readFileSync('pr-comments.txt', 'utf8'); + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: content + }); diff --git a/main.py b/main.py index 9e6ee3b..f8c7f18 100755 --- a/main.py +++ b/main.py @@ -116,7 +116,7 @@ def set_pr_comments_output() -> None: if result_text is None else f"{FAILURE_TITLE}\n```\n{result_text}\n```" ) - + # output pr_comments to $GITHUB_OUTPUT with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output_file: output_file.write(f"pr_comments={pr_comments}\n") @@ -213,7 +213,7 @@ def main(): ret_code = run_commit_check() ret_code += add_job_summary() ret_code += add_pr_comments() - + # Always set pr_comments output regardless of PR_COMMENTS setting set_pr_comments_output()