-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinit.js
More file actions
406 lines (351 loc) · 14 KB
/
init.js
File metadata and controls
406 lines (351 loc) · 14 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/usr/bin/env node
const core = require('@actions/core');
const github = require('@actions/github');
const fs = require('fs');
const path = require('path');
/**
* Initialize the Application observability for AWS action by checking trigger conditions and creating initial tracking comment
*/
async function run() {
try {
// Get GitHub context
const context = github.context;
const payload = context.payload;
// Get inputs
const botName = process.env.BOT_NAME || '@awsapm';
const branchPrefix = process.env.BRANCH_PREFIX || 'awsapm/';
const targetBranch = process.env.TARGET_BRANCH || '';
const allowedNonWriteUsers = process.env.ALLOWED_NON_WRITE_USERS || '';
const customPrompt = process.env.CUSTOM_PROMPT || '';
const tracingMode = process.env.TRACING_MODE || 'false';
const testMode = process.env.TEST_MODE || 'false';
// Function to check for bot name trigger phrase
// Note: Phrases like "@awsapm-user" will be also considered valid.
// Keeping this logic simple by accepting such variations.
function containsTriggerPhrase(text) {
if (!text) return false;
return text.toLowerCase().includes(botName.toLowerCase());
}
// Check if trigger phrase is present in the event
let containsTrigger = false;
let triggerText = '';
let commentId = null;
let issueNumber = null;
let isPR = false;
let isEditEvent = false;
let triggerUsername = '';
if (context.eventName === 'issue_comment') {
const comment = payload.comment;
if (comment && comment.body && containsTriggerPhrase(comment.body)) {
containsTrigger = true;
triggerText = comment.body;
commentId = comment.id;
issueNumber = payload.issue.number;
isPR = !!payload.issue.pull_request;
isEditEvent = payload.action === 'edited';
triggerUsername = comment.user?.login || 'unknown';
}
} else if (context.eventName === 'pull_request_review_comment') {
const comment = payload.comment;
if (comment && comment.body && containsTriggerPhrase(comment.body)) {
containsTrigger = true;
triggerText = comment.body;
commentId = comment.id;
issueNumber = payload.pull_request.number;
isPR = true;
isEditEvent = payload.action === 'edited';
triggerUsername = comment.user?.login || 'unknown';
}
} else if (context.eventName === 'issues') {
const issue = payload.issue;
if (issue && ((issue.body && containsTriggerPhrase(issue.body)) ||
(issue.title && containsTriggerPhrase(issue.title)))) {
containsTrigger = true;
triggerText = issue.body || issue.title;
issueNumber = issue.number;
isPR = false;
isEditEvent = payload.action === 'edited';
triggerUsername = issue.user?.login || 'unknown';
// For 'issues' event, there's no comment - the trigger is in the issue body/title itself
// We still want to search for existing result comments when editing
commentId = null; // Explicitly set to null for clarity
}
} else if (testMode === 'true') {
// In test mode, always trigger and use custom prompt
containsTrigger = true;
triggerText = customPrompt;
triggerUsername = 'integration-test';
}
// Set output for action.yml to check
core.setOutput('contains_trigger', containsTrigger.toString());
if (!containsTrigger) {
return;
}
// Setup GitHub token with intelligent resolution (including GitHub App support)
let githubToken = process.env.OVERRIDE_GITHUB_TOKEN;
let tokenSource = 'custom';
if (!githubToken) {
githubToken = process.env.DEFAULT_WORKFLOW_TOKEN;
tokenSource = 'default';
}
if (!githubToken) {
throw new Error('GitHub token is required');
}
// Create Octokit instance
const octokit = github.getOctokit(githubToken);
// Check user permissions
const hasPermissions = await checkUserPermissions(octokit, context, issueNumber, allowedNonWriteUsers);
if (!hasPermissions) {
core.setOutput('contains_trigger', 'false');
return;
}
// Add immediate eye reaction to show the action is triggered
if (commentId) {
try {
await octokit.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: commentId,
content: 'eyes',
});
} catch (error) {
core.warning(`Failed to add reaction: ${error.message}`);
}
}
// Get repository default branch if not specified
let actualTargetBranch = targetBranch;
if (!actualTargetBranch) {
const repo = await octokit.rest.repos.get({
owner: context.repo.owner,
repo: context.repo.repo,
});
actualTargetBranch = repo.data.default_branch;
}
// Create branch name for this execution
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const awsapmBranch = `${branchPrefix}${context.runId}-${timestamp}`;
// Create or reuse tracking comment
let awsapmCommentId = null;
if (issueNumber) {
try {
// If this is an edit event, search for existing result comment to reuse
if (isEditEvent) {
const { data: comments } = await octokit.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
per_page: 100,
});
let existingComment = null;
if (commentId) {
// For issue_comment events: Find the result comment after the specific trigger comment
const triggerCommentIndex = comments.findIndex(c => c.id == commentId);
if (triggerCommentIndex !== -1) {
existingComment = comments
.slice(triggerCommentIndex + 1) // Start from next comment after trigger
.find(c =>
c.body && c.body.includes('Application observability for AWS Investigation')
);
} else {
core.warning(`Trigger comment ID ${commentId} not found in comments list. Creating new comment.`);
}
} else {
// For issues events: Find the first result comment
existingComment = comments.find(c =>
c.body && c.body.includes('Application observability for AWS Investigation')
);
}
if (existingComment) {
awsapmCommentId = existingComment.id;
// Update it to show re-investigating status
const reinvestigateBody = `🔄 **Re-investigating...**\n\n` +
`Request updated by @${triggerUsername}.\n\n` +
`Updated request:\n> ${triggerText.substring(0, 300)}${triggerText.length > 300 ? '...' : ''}\n\n` +
`⏳ Investigation in progress - [View workflow run](${context.payload.repository.html_url}/actions/runs/${context.runId})`;
await octokit.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: awsapmCommentId,
body: reinvestigateBody,
});
}
}
// Create new tracking comment if not reusing
if (!awsapmCommentId) {
const commentBody = `🔍 **Application observability for AWS Investigation Started**\n\n` +
`I'm analyzing this ${isPR ? 'PR' : 'issue'}...\n\n` +
`⏳ Investigation in progress - [View workflow run](${context.payload.repository.html_url}/actions/runs/${context.runId})`;
const comment = await octokit.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
body: commentBody,
});
awsapmCommentId = comment.data.id;
}
} catch (error) {
core.error(`Failed to create/update tracking comment: ${error.message}`);
}
}
// Create prompt file
const promptDir = path.join(process.env.RUNNER_TEMP || '/tmp', 'awsapm-prompts');
if (!fs.existsSync(promptDir)) {
fs.mkdirSync(promptDir, { recursive: true });
}
const promptFile = path.join(promptDir, 'awsapm-prompt.txt');
// Get repository info for prompt generation
let repoInfo;
try {
repoInfo = await getBasicRepoInfo(context, githubToken);
} catch (repoError) {
core.warning(`Failed to fetch repository info: ${repoError.message}`);
repoInfo = {
primaryLanguage: 'Unknown',
description: 'Repository information unavailable',
size: 0,
fileCount: 'Unknown',
topics: []
};
}
// Remove bot name from the user's request
const cleanedUserRequest = triggerText.replace(new RegExp(botName, 'gi'), '').trim();
if (testMode === 'true') {
// for integration test, use custom prompt directly
try {
fs.writeFileSync(promptFile, customPrompt);
} catch (error) {
core.error(`Failed to write custom prompt to file: ${error.message}`);
process.exit(1);
}
} else {
// Use the dynamic prompt generation with PR context
const { createGeneralPrompt } = require('./prompt-builder');
try {
const finalPrompt = await createGeneralPrompt(context, repoInfo, cleanedUserRequest, githubToken, awsapmBranch);
fs.writeFileSync(promptFile, finalPrompt);
} catch (promptError) {
core.error(`Failed to generate dynamic prompt: ${promptError.message}`);
// Fallback to basic prompt if dynamic generation fails
let fallbackPrompt = '';
if (customPrompt) {
fallbackPrompt = customPrompt + '\n\n';
}
fallbackPrompt += `Please analyze this ${isPR ? 'pull request' : 'issue'} using AI Agent for insights.\n\n`;
fallbackPrompt += `Original request: ${cleanedUserRequest}\n\n`;
fallbackPrompt += `Context: This is a ${context.eventName} event in ${context.repo.owner}/${context.repo.repo}`;
fs.writeFileSync(promptFile, fallbackPrompt);
}
}
// Set outputs
core.setOutput('GITHUB_TOKEN', githubToken);
core.setOutput('AWSAPM_BRANCH', awsapmBranch);
core.setOutput('TARGET_BRANCH', actualTargetBranch);
core.setOutput('awsapm_comment_id', awsapmCommentId);
core.setOutput('issue_number', issueNumber);
core.setOutput('is_pr', isPR);
core.setOutput('trigger_text', triggerText);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
core.error(`Init step failed: ${errorMessage}`);
core.setFailed(`Init step failed with error: ${errorMessage}`);
process.exit(1);
}
}
/**
* Check if user has write or admin permissions to the repository
*/
async function checkUserPermissions(octokit, context, issueNumber, allowedNonWriteUsers) {
const testMode = process.env.TEST_MODE || 'false';
if (testMode === 'true') {
return true;
}
const actor = context.actor;
core.debug(`Checking permissions for actor: ${actor}`);
try {
// Check permissions directly using the permission endpoint
const response = await octokit.rest.repos.getCollaboratorPermissionLevel({
owner: context.repo.owner,
repo: context.repo.repo,
username: actor,
});
const permissionLevel = response.data.permission;
if (permissionLevel === 'admin' || permissionLevel === 'write') {
return true;
}
// Check if user is in allowedNonWriteUsers list
if (allowedNonWriteUsers) {
const allowedUsers = allowedNonWriteUsers.split(',').map(u => u.trim()).filter(Boolean);
// Check for wildcard (allow all users)
if (allowedUsers.includes('*')) {
return true;
}
// Check if actor is in the allowed list
if (allowedUsers.includes(actor)) {
return true;
}
}
// User doesn't have sufficient permissions
core.warning(`Actor ${actor} has insufficient permissions: ${permissionLevel}`);
// Post explanatory comment
if (issueNumber) {
try {
const commentBody = `🚫 **Application observability for AWS Investigation - Access Denied**\n\n` +
`Sorry @${actor}, you don't have sufficient permissions to use this bot.\n\n` +
`**Required:** Write or Admin access to this repository\n` +
`**Your level:** ${permissionLevel}\n\n` +
`Please contact a repository maintainer if you believe this is an error.`;
await octokit.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
body: commentBody,
});
core.debug('Posted access denied comment');
} catch (commentError) {
core.error(`Failed to post access denied comment: ${commentError.message}`);
}
}
return false;
} catch (error) {
core.error(`Failed to check permissions: ${error}`);
throw new Error(`Failed to check permissions for ${actor}: ${error}`);
}
}
/**
* Get basic repository information for prompt generation
*/
async function getBasicRepoInfo(context, githubToken) {
try {
const octokit = github.getOctokit(githubToken);
const { data: repo } = await octokit.rest.repos.get({
owner: context.repo.owner,
repo: context.repo.repo,
});
// Get repository languages
const { data: languages } = await octokit.rest.repos.listLanguages({
owner: context.repo.owner,
repo: context.repo.repo,
});
const primaryLanguage = Object.keys(languages)[0] || 'Unknown';
return {
primaryLanguage,
description: repo.description,
size: repo.size,
fileCount: 'Unknown', // GitHub API doesn't provide file count directly
topics: repo.topics || []
};
} catch (error) {
core.warning(`Could not fetch repository info: ${error.message}`);
return {
primaryLanguage: 'Unknown',
description: 'Repository information unavailable',
size: 0,
fileCount: 'Unknown',
topics: []
};
}
}
if (require.main === module) {
run();
}
module.exports = { run };