Skip to content

Commit 1cf22d0

Browse files
Refactor analysis comment posting logic
1 parent 9239101 commit 1cf22d0

1 file changed

Lines changed: 61 additions & 68 deletions

File tree

.github/scripts/analyze-failure.js

Lines changed: 61 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -69,70 +69,34 @@ async function run() {
6969

7070
console.log(`Extracted log tail (${tailLogs.length} characters). Generating analysis...`);
7171

72-
// 5. Post Comment with Deduplication
73-
const runDetails = await octokit.rest.actions.getWorkflowRun({
74-
owner, repo, run_id: runId
75-
});
76-
const prs = runDetails.data.pull_requests;
77-
const headSha = runDetails.data.head_sha;
78-
79-
let existingComment = null;
80-
let isPr = false;
81-
let issueNumber = null;
82-
83-
if (prs && prs.length > 0) {
84-
isPr = true;
85-
issueNumber = prs[0].number;
86-
const comments = await octokit.rest.issues.listComments({ owner, repo, issue_number: issueNumber });
87-
existingComment = comments.data.find(c => c.user?.login?.includes('github-actions') && c.body.includes('🩺 Build Doctor Diagnosis'));
88-
} else {
89-
const comments = await octokit.rest.repos.listCommitComments({ owner, repo, commit_sha: headSha });
90-
existingComment = comments.data.find(c => c.user?.login?.includes('github-actions') && c.body.includes('🩺 Build Doctor Diagnosis'));
91-
}
92-
93-
if (existingComment) {
94-
console.log("Found existing Build Doctor comment. Checking for duplicate root cause...");
95-
const dedupPrompt = `
96-
You are a DevOps Expert and "Build Doctor".
97-
An existing diagnosis already exists for this code change:
98-
\`\`\`
99-
${existingComment.body}
100-
\`\`\`
101-
102-
A new workflow '${workflowName}' also failed with these logs:
103-
\`\`\`
104-
${tailLogs}
105-
\`\`\`
106-
107-
If the root cause of this new failure is fundamentally the SAME as the existing diagnosis (e.g., the same syntax error or missing dependency affects both workflows), respond with ONLY the word "DUPLICATE".
108-
If it is a fundamentally DIFFERENT root cause, provide a new diagnosis following the standard format:
109-
<br>
110-
### 🩺 Build Doctor Diagnosis (${workflowName})
111-
**1. Root Cause:** ...
112-
**2. Relevant Log Lines:** ...
113-
**3. Suggested Fix:** ...
114-
**Confidence:** ...
115-
`;
116-
117-
const result = await model.generateContent(dedupPrompt);
118-
const text = result.response.text().trim();
119-
120-
if (text.startsWith("DUPLICATE") || text.includes("DUPLICATE")) {
121-
console.log("Failure is a duplicate of existing diagnosis. Skipping comment.");
122-
return;
123-
} else {
124-
console.log("New root cause identified. Appending to existing comment...");
125-
const updatedBody = existingComment.body + "\n\n---\n\n" + text;
126-
if (isPr) {
127-
await octokit.rest.issues.updateComment({ owner, repo, comment_id: existingComment.id, body: updatedBody });
128-
} else {
129-
await octokit.rest.repos.updateCommitComment({ owner, repo, comment_id: existingComment.id, body: updatedBody });
130-
}
131-
return;
132-
}
133-
}
134-
135-
// 6. Generate Standard Analysis if no existing comment
72+
// 5. Generate Analysis with Gemini
73+
// const prompt = `
74+
// You are a DevOps Expert and "Build Doctor".
75+
// A GitHub Actions workflow '${workflowName}' failed.
76+
77+
// Analyze the following log snippet (last ${MAX_LOG_LINES} lines) to identify the root cause.
78+
79+
// Log Snippet:
80+
// \`\`\`
81+
// ${tailLogs}
82+
// \`\`\`
83+
84+
// Your response must be a concise Markdown comment suitable for a developer.
85+
// Structure:
86+
87+
// ## 🩺 Build Doctor Diagnosis
88+
89+
// **1. Root Cause:**
90+
// (Explain what went wrong in 1-2 senteces. Be specific: Syntax error, Dependencies, Test failure, Infra, etc.)
91+
92+
// **2. Relevant Log Lines:**
93+
// (Quote the specific error message from the logs)
94+
95+
// **3. Suggested Fix:**
96+
// (Actionable advice. If it's a code fix, show the snippet. If it's a config tweak, show the command or yaml change.)
97+
98+
// **Confidence:** (High/Medium/Low)
99+
// `;
136100
const prompt = `
137101
You are an expert DevOps engineer and "Build Doctor".
138102
A GitHub Actions workflow '${workflowName}' has failed.
@@ -187,11 +151,40 @@ async function run() {
187151

188152
console.log("Analysis generated. Posting comment...");
189153

190-
if (isPr) {
191-
await octokit.rest.issues.createComment({ owner, repo, issue_number: issueNumber, body: analysis });
192-
console.log(`Posted analysis to PR #${issueNumber}`);
154+
// 6. Post Comment
155+
// We need to find where to post.
156+
// If triggered by PR, we post to the PR.
157+
// If triggered by Push, we post to the Commit.
158+
159+
// Context is tricky in 'workflow_run'. We have to look at the 'workflow_run' event payload.
160+
// We can get the PRs associated with the run.
161+
const runDetails = await octokit.rest.actions.getWorkflowRun({
162+
owner,
163+
repo,
164+
run_id: runId
165+
});
166+
167+
const prs = runDetails.data.pull_requests;
168+
169+
if (prs && prs.length > 0) {
170+
// Post to the first associated PR
171+
const prNumber = prs[0].number;
172+
await octokit.rest.issues.createComment({
173+
owner,
174+
repo,
175+
issue_number: prNumber,
176+
body: analysis
177+
});
178+
console.log(`Posted analysis to PR #${prNumber}`);
193179
} else {
194-
await octokit.rest.repos.createCommitComment({ owner, repo, commit_sha: headSha, body: analysis });
180+
// Post to the Commit
181+
const headSha = runDetails.data.head_sha;
182+
await octokit.rest.repos.createCommitComment({
183+
owner,
184+
repo,
185+
commit_sha: headSha,
186+
body: analysis
187+
});
195188
console.log(`Posted analysis to Commit ${headSha}`);
196189
}
197190

0 commit comments

Comments
 (0)