-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaction.ts
More file actions
261 lines (244 loc) · 7.23 KB
/
action.ts
File metadata and controls
261 lines (244 loc) · 7.23 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
import * as core from "@actions/core";
import {
type ExperimentalCoderSDKCreateTaskRequest,
TaskNameSchema,
TaskNotFoundError,
CoderAPIError,
type CoderClient,
type TaskId,
} from "./coder-client";
import type { ActionInputs, ActionOutputs } from "./schemas";
import type { getOctokit } from "@actions/github";
export type Octokit = ReturnType<typeof getOctokit>;
export class CoderTaskAction {
constructor(
private readonly coder: CoderClient,
private readonly octokit: Octokit,
private readonly inputs: ActionInputs,
) {}
/**
* Parse owner and repo from issue URL
*/
parseGithubIssueURL(): {
githubOrg: string;
githubRepo: string;
githubIssueNumber: number;
} {
if (!this.inputs.githubIssueURL) {
throw new Error(`Missing issue URL`);
}
// Parse: https://github.com/owner/repo/issues/123
const match = this.inputs.githubIssueURL.match(
/([^/]+)\/([^/]+)\/issues\/(\d+)/,
);
if (!match) {
throw new Error(`Invalid issue URL: ${this.inputs.githubIssueURL}`);
}
return {
githubOrg: match[1],
githubRepo: match[2],
githubIssueNumber: parseInt(match[3], 10),
};
}
/**
* Generate task URL
*/
generateTaskUrl(coderUsername: string, taskID: TaskId): string {
// Strip query params and anchors from the base URL
const baseURL = this.inputs.coderURL.split(/[?#]/)[0].replace(/\/$/, "");
return `${baseURL}/tasks/${coderUsername}/${taskID}`;
}
/**
* Comment on GitHub issue with task link
*/
async commentOnIssue(
taskUrl: string,
owner: string,
repo: string,
issueNumber: number,
): Promise<void> {
const body = `Task created: ${taskUrl}`;
try {
// Try to find existing comment from bot
const { data: comments } = await this.octokit.rest.issues.listComments({
owner,
repo,
issue_number: issueNumber,
});
// Find the last comment that starts with "Task created:"
const existingComment = comments
.reverse()
.find((comment: { body?: string }) =>
comment.body?.startsWith("Task created:"),
);
if (existingComment) {
// Update existing comment
await this.octokit.rest.issues.updateComment({
owner,
repo,
comment_id: existingComment.id,
body,
});
} else {
// Create new comment
await this.octokit.rest.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body,
});
}
} catch (error) {
core.error(`Failed to comment on issue: ${error}`);
}
}
/**
* Main action execution
*/
async run(): Promise<ActionOutputs> {
let coderUsername: string;
if (this.inputs.coderUsername) {
core.info(`Using provided Coder username: ${this.inputs.coderUsername}`);
coderUsername = this.inputs.coderUsername;
} else {
core.info(
`Looking up Coder user by GitHub user ID: ${this.inputs.githubUserID}`,
);
const coderUser = await this.coder.getCoderUserByGitHubId(
this.inputs.githubUserID,
);
coderUsername = coderUser.username;
}
const { githubOrg, githubRepo, githubIssueNumber } =
this.parseGithubIssueURL();
core.info(`GitHub owner: ${githubOrg}`);
core.info(`GitHub repo: ${githubRepo}`);
core.info(`GitHub issue number: ${githubIssueNumber}`);
core.info(`Coder username: ${coderUsername}`);
if (!this.inputs.coderTaskNamePrefix || !this.inputs.githubIssueURL) {
throw new Error(
"either taskName or both taskNamePrefix and issueURL must be provided",
);
}
core.info(`Coder organization: ${this.inputs.coderOrganization}`);
const taskNameString = `${this.inputs.coderTaskNamePrefix}-${githubIssueNumber}`;
const taskName = TaskNameSchema.parse(taskNameString);
core.info(`Coder Task name: ${taskName}`);
const template = await this.coder.getTemplateByOrganizationAndName(
this.inputs.coderOrganization,
this.inputs.coderTemplateName,
);
core.info(
`Coder Template: ${template.name} (id:${template.id}, active_version_id:${template.active_version_id})`,
);
const templateVersionPresets = await this.coder.getTemplateVersionPresets(
template.active_version_id,
);
let presetID: string | undefined;
// If no preset specified, use default preset
if (!this.inputs.coderTemplatePreset) {
core.info(`Coder Template: Using default preset`);
for (const preset of templateVersionPresets) {
if (preset.Default) {
presetID = preset.ID;
break;
}
}
} else {
for (const preset of templateVersionPresets) {
if (preset.Name === this.inputs.coderTemplatePreset) {
presetID = preset.ID;
break;
}
}
}
// Ensure that we found a valid preset if the user specifically requested one
if (!!this.inputs.coderTemplatePreset && !presetID) {
throw new Error(`Preset ${this.inputs.coderTemplatePreset} not found`);
}
core.info(`Coder Template: Preset ID: ${presetID}`);
const existingTask = await this.coder.getTask(coderUsername, taskName);
if (existingTask) {
core.info(
`Coder Task: already exists: ${existingTask.name} (id: ${existingTask.id} status: ${existingTask.status})`,
);
try {
// Wait for task to become active and idle before sending
// input. The agent may be in "working" state even when
// the task status is "active", and sending input in that
// state causes 409/502 errors.
core.info(
`Coder Task: waiting for task ${existingTask.name} to become active and idle...`,
);
await this.coder.waitForTaskActive(
coderUsername,
existingTask.id,
core.debug,
1_200_000,
);
core.info("Coder Task: Sending prompt to existing task...");
// Send prompt to existing task using the task ID (UUID)
await this.coder.sendTaskInput(
coderUsername,
existingTask.id,
this.inputs.coderTaskPrompt,
);
core.info("Coder Task: Prompt sent successfully");
return {
coderUsername,
taskName: existingTask.name,
taskUrl: this.generateTaskUrl(coderUsername, existingTask.id),
taskCreated: false,
};
} catch (error) {
if (
error instanceof TaskNotFoundError ||
(error instanceof CoderAPIError && error.statusCode === 404)
) {
core.warning(
`Lost contact with task '${existingTask.name}' (404 during polling). Creating a new task.`,
);
// Fall through to task creation below.
} else {
throw error;
}
}
}
core.info("Creating Coder task...");
const req: ExperimentalCoderSDKCreateTaskRequest = {
name: taskName,
template_version_id: template.active_version_id,
template_version_preset_id: presetID,
input: this.inputs.coderTaskPrompt,
};
// Create new task
const createdTask = await this.coder.createTask(coderUsername, req);
core.info(
`Coder Task: created successfully (status: ${createdTask.status})`,
);
// 5. Generate task URL
const taskUrl = this.generateTaskUrl(coderUsername, createdTask.id);
core.info(`Coder Task: URL: ${taskUrl}`);
// 6. Comment on issue if requested
if (this.inputs.commentOnIssue) {
core.info(
`Commenting on issue ${githubOrg}/${githubRepo}#${githubIssueNumber}`,
);
await this.commentOnIssue(
taskUrl,
githubOrg,
githubRepo,
githubIssueNumber,
);
core.info(`Comment posted successfully`);
} else {
core.info(`Skipping comment on issue (commentOnIssue is false)`);
}
return {
coderUsername,
taskName,
taskUrl,
taskCreated: true,
};
}
}