diff --git a/.github/workflows/commit-check.yml b/.github/workflows/commit-check.yml index b22e048..747f226 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: @@ -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: @@ -29,3 +30,19 @@ jobs: imperative: true job-summary: true pr-comments: ${{ github.event_name == 'pull_request' }} + + - name: Save PR comments to pr-comments.txt + run: echo "${{ steps.commit-check.outputs.pr_comments }}" > pr-comments.txt + + - name: Post PR comments + uses: actions/github-script@v6 + with: + 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/.gitignore b/.gitignore index 43f4b6f..7ef7583 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ venv/ .venv/ +__pycache__/ +*.pyc +*.pyo diff --git a/action.yml b/action.yml index eb12512..4c31942 100644 --- a/action.yml +++ b/action.yml @@ -45,10 +45,17 @@ 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: - - name: Install dependencies and run commit-check + - id: commit-check + name: Install dependencies and run commit-check shell: bash run: | if [[ "$RUNNER_OS" == "Linux" ]]; then diff --git a/main.py b/main.py index ef9c920..f8c7f18 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": @@ -200,6 +214,9 @@ def main(): 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