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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions .github/scripts/check-codes-comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
const fs = require("fs");

module.exports = async ({ github, context, core }) => {
const prNumberPath = process.env.PULL_REQUEST_NUMBER_PATH;
if (!prNumberPath || !fs.existsSync(prNumberPath) || !fs.statSync(prNumberPath).isFile()) {
core.info("Pull request number file not found.");
return;
}

const reportsPath = process.env.REPORTS_PATH;
if (!reportsPath || !fs.existsSync(reportsPath) || !fs.statSync(reportsPath).isFile()) {
core.info("Reports file not found.");
return;
}

const PULL_REQUEST_NUMBER = parseInt(fs.readFileSync(prNumberPath, "utf8").trim(), 10);
if (isNaN(PULL_REQUEST_NUMBER)) {
core.info("Failed to parse the pull request number.");
return;
}

const { owner, repo } = context.repo;

let pr;
try {
const response = await github.rest.pulls.get({
owner,
repo,
pull_number: PULL_REQUEST_NUMBER,
});
pr = response.data;
} catch (error) {
core.error(`Failed to fetch PR #${PULL_REQUEST_NUMBER}: ${error.message}`);
return;
Comment on lines +33 to +34
}

core.info(`Current PR state: ${pr.state}`);
if (pr.state !== "open") {
core.info("The pull request is not open. Skipping comment creation.");
return;
}

const run = context.payload.workflow_run;
if (!run) {
core.warning("context.payload.workflow_run is undefined. Ensure this script runs on the 'workflow_run' event.");
return;
}

if (pr.head.sha !== run.head_sha) {
core.info("PR head SHA does not match the workflow run head SHA. Skipping.");
return;
}

if (pr.head.ref !== run.head_branch) {
core.info("PR head branch does not match the workflow run head branch. Skipping.");
return;
}

if (pr.head.repo.full_name !== run.head_repository.full_name) {
core.info("PR head repository fullname does not match the workflow run head repository fullname. Skipping.");
return;
}

const comments = [];
const reports = fs.readFileSync(reportsPath, "utf8").trim().split("\n").filter(Boolean);
for (const report of reports) {
try {
const data = JSON.parse(report);

const filePath = data.location?.path;
const line = data.location?.range?.start?.line;
const message = data.message;
const severity = data.severity || "INFO";
const code = data.code?.value ? `[${data.code.value}] ` : "";

if (filePath && line) {
comments.push({
path: filePath,
line,
side: "RIGHT",
Comment thread
neveler marked this conversation as resolved.
Comment on lines +76 to +80
body: `**[${severity}]** ${code}\n\n${message}`
});
}
} catch (error) {
core.error(`Failed to parse report line: "${report}". Error: ${error.message}`);
}
}

if (comments.length === 0) {
core.info("No diagnostics found to report as comments.");
return;
}

core.info(`Successfully prepared ${comments.length} comment${comments.length > 1 ? "s" : ""} to post.`);

try {
await github.rest.pulls.createReview({
owner,
repo,
pull_number: PULL_REQUEST_NUMBER,
commit_id: run.head_sha,
body: "🤖 Static analysis found the following issues:",
event: "COMMENT",
comments: comments
});
core.info("Review comments successfully posted to the pull request.");
} catch (error) {
core.error(`Failed to post review comments to GitHub: ${error.message}`);
}
Comment on lines +107 to +109

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Fail the job when review creation fails

When GitHub rejects the review—for example because a diagnostic has an invalid position—or the API experiences a transient failure, this catch only emits an annotation and then returns normally. core.error does not mark an actions/github-script step as failed, so the dedicated comment workflow finishes successfully despite posting no diagnostics, concealing the malfunction from maintainers; rethrow the error or call core.setFailed.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

个人认为此错误完全可以忽略,没必要非得报错吧。

}
40 changes: 40 additions & 0 deletions .github/workflows/check-codes-comment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Check Codes Comment

on:
workflow_run:
workflows: ["Check Codes"]
types:
- completed

permissions:
actions: read
contents: read
pull-requests: write

jobs:
comment:
runs-on: ubuntu-latest
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'failure'
steps:
- name: Checkout Script
uses: actions/checkout@v7
with:
sparse-checkout: |
.github/scripts/check-codes-comment.js
sparse-checkout-cone-mode: false
- name: Download Artifact
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: checkstyle-artifact
path: ${{ runner.temp }}/checkstyle-artifact
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Generate Review Comment
uses: actions/github-script@v9
env:
REPORTS_PATH: ${{ runner.temp }}/checkstyle-artifact/ALL_REPORTS.jsonl
PULL_REQUEST_NUMBER_PATH: ${{ runner.temp }}/checkstyle-artifact/PULL_REQUEST_NUMBER
with:
script: |
const script = require("./.github/scripts/check-codes-comment.js");
await script({ github, context, core });
26 changes: 25 additions & 1 deletion .github/workflows/check-codes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- name: Set up JDK 17
- name: Setup JDK 17
uses: actions/setup-java@v5
with:
distribution: 'zulu'
Expand All @@ -27,3 +27,27 @@ jobs:
cache-cleanup: never
- name: Check Codes
run: ./gradlew checkstyle checkTranslations --no-daemon --parallel --stacktrace
- if: failure() && github.event_name == 'pull_request'
name: Setup reviewdog
uses: reviewdog/action-setup@d8edfce3dd5e1ec6978745e801f9c50b5ef80252 # v1.4.0
- if: failure() && github.event_name == 'pull_request'
name: Generate Artifact
run: |
echo "${{ github.event.number }}" > PULL_REQUEST_NUMBER
> ALL_REPORTS.jsonl
find . -path "*/build/reports/checkstyle/*.xml" | while read -r xml_file; do
if [ -s "$xml_file" ]; then
echo "Processing $xml_file..."
reviewdog -f=checkstyle -name="checkstyle" -reporter=rdjsonl < "$xml_file" >> ALL_REPORTS.jsonl
else
echo "Skipping empty or missing report: $xml_file"
fi
done
- if: failure() && github.event_name == 'pull_request'
name: Upload Artifact
uses: actions/upload-artifact@v7
with:
name: checkstyle-artifact
path: |
ALL_REPORTS.jsonl
PULL_REQUEST_NUMBER
4 changes: 2 additions & 2 deletions .github/workflows/pr-size-label.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ jobs:
uses: actions/checkout@v7
with:
sparse-checkout: |
.github/scripts
sparse-checkout-cone-mode: true
.github/scripts/pr-size-label.js
sparse-checkout-cone-mode: false
- name: Label PR by filtered changed lines
uses: actions/github-script@v9
with:
Expand Down