|
| 1 | +const fs = require("fs"); |
| 2 | + |
| 3 | +module.exports = async ({ github, context, core }) => { |
| 4 | + const prNumberPath = process.env.PULL_REQUEST_NUMBER_PATH; |
| 5 | + if (!prNumberPath || !fs.existsSync(prNumberPath) || !fs.statSync(prNumberPath).isFile()) { |
| 6 | + core.setFailed("Pull request number file not found."); |
| 7 | + return; |
| 8 | + } |
| 9 | + |
| 10 | + const reportsPath = process.env.REPORTS_PATH; |
| 11 | + if (!reportsPath || !fs.existsSync(reportsPath) || !fs.statSync(reportsPath).isFile()) { |
| 12 | + core.setFailed("Reports file not found."); |
| 13 | + return; |
| 14 | + } |
| 15 | + |
| 16 | + const PULL_REQUEST_NUMBER = parseInt(fs.readFileSync(prNumberPath, "utf8").trim(), 10); |
| 17 | + if (isNaN(PULL_REQUEST_NUMBER)) { |
| 18 | + core.setFailed("Failed to parse the pull request number."); |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + const { owner, repo } = context.repo; |
| 23 | + |
| 24 | + let pr; |
| 25 | + try { |
| 26 | + const response = await github.rest.pulls.get({ |
| 27 | + owner, |
| 28 | + repo, |
| 29 | + pull_number: PULL_REQUEST_NUMBER, |
| 30 | + }); |
| 31 | + pr = response.data; |
| 32 | + } catch (error) { |
| 33 | + core.setFailed(`Failed to fetch PR #${PULL_REQUEST_NUMBER}: ${error.message}`); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + core.info(`Current PR state: ${pr.state}`); |
| 38 | + if (pr.state !== "open") { |
| 39 | + core.setFailed("The pull request is not open. Skipping comment creation."); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + const run = context.payload.workflow_run; |
| 44 | + if (!run) { |
| 45 | + core.setFailed("context.payload.workflow_run is undefined. Ensure this script runs on the 'workflow_run' event."); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + if (pr.head.sha !== run.head_sha) { |
| 50 | + core.setFailed("PR head SHA does not match the workflow run head SHA. Skipping."); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + if (pr.head.ref !== run.head_branch) { |
| 55 | + core.setFailed("PR head branch does not match the workflow run head branch. Skipping."); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (pr.head.repo.full_name !== run.head_repository.full_name) { |
| 60 | + core.setFailed("PR head repository fullname does not match the workflow run head repository fullname. Skipping."); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + const comments = []; |
| 65 | + const maxCommentCount = 10; |
| 66 | + const reports = fs.readFileSync(reportsPath, "utf8").trim().split("\n").filter(Boolean); |
| 67 | + for (const report of reports) { |
| 68 | + if (comments.length >= maxCommentCount) break; |
| 69 | + try { |
| 70 | + const data = JSON.parse(report); |
| 71 | + if (typeof data !== "object" || Array.isArray(data)) continue; |
| 72 | + |
| 73 | + const { location, message, severity, code } = data; |
| 74 | + if (typeof location !== "object" || Array.isArray(location)) continue; |
| 75 | + if (typeof message !== "string") continue; |
| 76 | + const severities = ["UNKNOWN_SEVERITY", "ERROR", "WARNING", "INFO", 0, 1, 2, 3]; |
| 77 | + if (!severities.includes(severity)) continue; |
| 78 | + const severityText = typeof severity === "string" ? severity : severities[severity]; |
| 79 | + if (typeof code !== "object" || Array.isArray(code)) continue; |
| 80 | + |
| 81 | + const path = location.path; |
| 82 | + const line = location.range?.start?.line; |
| 83 | + const codeText = code.value ? `[${code.value}] ` : ""; |
| 84 | + |
| 85 | + if (path && line) { |
| 86 | + comments.push({ |
| 87 | + path, |
| 88 | + line, |
| 89 | + side: "RIGHT", |
| 90 | + body: `**[${severityText}]** ${codeText}\n\n${message}` |
| 91 | + }); |
| 92 | + } |
| 93 | + } catch (error) { |
| 94 | + core.warning(`Failed to parse report: ${error.message}`); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (comments.length === 0) { |
| 99 | + core.info("No diagnostics found to report as comments."); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + core.info(`Successfully prepared ${comments.length} comment${comments.length > 1 ? "s" : ""} to post.`); |
| 104 | + |
| 105 | + try { |
| 106 | + await github.rest.pulls.createReview({ |
| 107 | + owner, |
| 108 | + repo, |
| 109 | + pull_number: PULL_REQUEST_NUMBER, |
| 110 | + commit_id: run.head_sha, |
| 111 | + body: "🤖 Static analysis found the following issues (max count 10):", |
| 112 | + event: "COMMENT", |
| 113 | + comments: comments |
| 114 | + }); |
| 115 | + core.info("Review comments successfully posted to the pull request."); |
| 116 | + } catch (error) { |
| 117 | + core.setFailed(`Failed to post review comments to GitHub: ${error.message}`); |
| 118 | + return; |
| 119 | + } |
| 120 | +} |
0 commit comments