-
Notifications
You must be signed in to change notification settings - Fork 394
Expand file tree
/
Copy pathcreate_comment.cjs
More file actions
87 lines (71 loc) · 2.73 KB
/
create_comment.cjs
File metadata and controls
87 lines (71 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
async function main() {
// Read the agent output content from environment variable
const outputContent = process.env.GITHUB_AW_AGENT_OUTPUT;
if (!outputContent) {
console.log('No GITHUB_AW_AGENT_OUTPUT environment variable found');
return;
}
if (outputContent.trim() === '') {
console.log('Agent output content is empty');
return;
}
console.log('Agent output content length:', outputContent.length);
// Check if we're in an issue or pull request context
const isIssueContext = context.eventName === 'issues' || context.eventName === 'issue_comment';
const isPRContext = context.eventName === 'pull_request' || context.eventName === 'pull_request_review' || context.eventName === 'pull_request_review_comment';
if (!isIssueContext && !isPRContext) {
console.log('Not running in issue or pull request context, skipping comment creation');
return;
}
// Determine the issue/PR number and comment endpoint
let issueNumber;
let commentEndpoint;
if (isIssueContext) {
if (context.payload.issue) {
issueNumber = context.payload.issue.number;
commentEndpoint = 'issues';
} else {
console.log('Issue context detected but no issue found in payload');
return;
}
} else if (isPRContext) {
if (context.payload.pull_request) {
issueNumber = context.payload.pull_request.number;
commentEndpoint = 'issues'; // PR comments use the issues API endpoint
} else {
console.log('Pull request context detected but no pull request found in payload');
return;
}
}
if (!issueNumber) {
console.log('Could not determine issue or pull request number');
return;
}
let body = outputContent.trim();
// Add AI disclaimer with run id, run htmlurl
const runId = context.runId;
const runUrl = context.payload.repository
? `${context.payload.repository.html_url}/actions/runs/${runId}`
: `https://github.com/actions/runs/${runId}`;
body += `\n\n> Generated by Agentic Workflow Run [${runId}](${runUrl})\n`;
console.log(`Creating comment on ${commentEndpoint} #${issueNumber}`);
console.log('Comment content length:', body.length);
// Create the comment using GitHub API
const { data: comment } = await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
body: body
});
console.log('Created comment #' + comment.id + ': ' + comment.html_url);
// Set output for other jobs to use
core.setOutput('comment_id', comment.id);
core.setOutput('comment_url', comment.html_url);
// write comment id, url to the github_step_summary
await core.summary.addRaw(`
## GitHub Comment
- Comment ID: ${comment.id}
- Comment URL: ${comment.html_url}
`).write();
}
await main();