-
Notifications
You must be signed in to change notification settings - Fork 394
Expand file tree
/
Copy pathcreate_issue.cjs
More file actions
118 lines (100 loc) · 3.77 KB
/
create_issue.cjs
File metadata and controls
118 lines (100 loc) · 3.77 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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 context (triggered by an issue event)
const parentIssueNumber = context.payload?.issue?.number;
// Parse labels from environment variable (comma-separated string)
const labelsEnv = process.env.GITHUB_AW_ISSUE_LABELS;
const labels = labelsEnv ? labelsEnv.split(',').map(/** @param {string} label */ label => label.trim()).filter(/** @param {string} label */ label => label) : [];
// Parse the output to extract title and body
const lines = outputContent.split('\n');
let title = '';
let bodyLines = [];
let foundTitle = false;
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
// Skip empty lines until we find the title
if (!foundTitle && line === '') {
continue;
}
// First non-empty line becomes the title
if (!foundTitle && line !== '') {
// Remove markdown heading syntax if present
title = line.replace(/^#+\s*/, '').trim();
foundTitle = true;
continue;
}
// Everything else goes into the body
if (foundTitle) {
bodyLines.push(lines[i]); // Keep original formatting
}
}
// If no title was found, use a default
if (!title) {
title = 'Agent Output';
}
// Apply title prefix if provided via environment variable
const titlePrefix = process.env.GITHUB_AW_ISSUE_TITLE_PREFIX;
if (titlePrefix && !title.startsWith(titlePrefix)) {
title = titlePrefix + title;
}
if (parentIssueNumber) {
console.log('Detected issue context, parent issue #' + parentIssueNumber);
// Add reference to parent issue in the child issue body
bodyLines.push(`Related to #${parentIssueNumber}`);
}
// Add AI disclaimer with run id, run htmlurl
// Add AI disclaimer with workflow run information
const runId = context.runId;
const runUrl = context.payload.repository
? `${context.payload.repository.html_url}/actions/runs/${runId}`
: `https://github.com/actions/runs/${runId}`;
bodyLines.push(``, ``, `> Generated by Agentic Workflow Run [${runId}](${runUrl})`, '');
// Prepare the body content
const body = bodyLines.join('\n').trim();
console.log('Creating issue with title:', title);
console.log('Labels:', labels);
console.log('Body length:', body.length);
// Create the issue using GitHub API
const { data: issue } = await github.rest.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: title,
body: body,
labels: labels
});
console.log('Created issue #' + issue.number + ': ' + issue.html_url);
// If we have a parent issue, add a comment to it referencing the new child issue
if (parentIssueNumber) {
try {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: parentIssueNumber,
body: `Created related issue: #${issue.number}`
});
console.log('Added comment to parent issue #' + parentIssueNumber);
} catch (error) {
console.log('Warning: Could not add comment to parent issue:', error instanceof Error ? error.message : String(error));
}
}
// Set output for other jobs to use
core.setOutput('issue_number', issue.number);
core.setOutput('issue_url', issue.html_url);
// write issue to summary
await core.summary.addRaw(`
## GitHub Issue
- Issue ID: ${issue.number}
- Issue URL: ${issue.html_url}
`).write();
}
await main();