-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
203 lines (176 loc) · 5.19 KB
/
index.ts
File metadata and controls
203 lines (176 loc) · 5.19 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
import * as core from "@actions/core";
import { readFileSync } from "node:fs";
import type { Octokit } from "@octokit/rest";
/**
* Get input from environment variables (GitHub Actions pattern)
*/
export function getInput(name: string, required: true): string;
export function getInput(name: string, required?: false): string | undefined;
export function getInput(name: string, required = false): string | undefined {
const envName = `INPUT_${name.toUpperCase().replace(/ /g, "_")}`;
const value = process.env[envName];
if (required && !value) {
throw new Error(`Input required and not supplied: ${name}`);
}
return value?.trim() || undefined;
}
/**
* Read and parse GitHub event payload
* Returns undefined if the event payload cannot be read
*/
function getEventPayload(): Record<string, unknown> | undefined {
const eventPath = process.env.GITHUB_EVENT_PATH;
if (!eventPath) {
core.warning("GITHUB_EVENT_PATH not found");
return undefined;
}
try {
return JSON.parse(readFileSync(eventPath, "utf8"));
} catch (error) {
core.warning(`Failed to read event payload: ${error}`);
return undefined;
}
}
/**
* Extract comment ID from GitHub event payload
* Returns undefined if the event doesn't have a comment
*/
export function getCommentIdFromEvent(): number | undefined {
const eventData = getEventPayload();
if (!eventData) {
return undefined;
}
// Check if event has a comment object
const comment = eventData.comment as { id?: number } | undefined;
if (comment?.id) {
return comment.id;
}
return undefined;
}
/**
* Extract comment body from GitHub event payload
* Returns undefined if the event doesn't have a comment
*/
export function getCommentBodyFromEvent(): string | undefined {
const eventData = getEventPayload();
if (!eventData) {
return undefined;
}
// Check if event has a comment object with body
const comment = eventData.comment as { body?: string } | undefined;
if (comment?.body) {
return comment.body;
}
return undefined;
}
/**
* Parse repository owner and name from GITHUB_REPOSITORY
*/
export function parseRepository(): { owner: string; repo: string } {
const repository = process.env.GITHUB_REPOSITORY || "";
const [owner, repo] = repository.split("/");
if (!(owner && repo)) {
throw new Error(
`Invalid GITHUB_REPOSITORY format: ${repository}. Expected format: owner/repo`
);
}
return { owner, repo };
}
type ReactToCommentParams = {
octokit: Octokit;
owner: string;
repo: string;
eventName: string;
};
// Event types that support comment reactions
const COMMENT_EVENT_TYPES = [
"issue_comment",
"pull_request_review_comment",
] as const;
type CommentEventType = (typeof COMMENT_EVENT_TYPES)[number];
function isCommentEvent(eventName: string): eventName is CommentEventType {
return COMMENT_EVENT_TYPES.includes(eventName as CommentEventType);
}
/**
* React to a comment with an emoji
* Extracts comment ID from event payload and reacts if present
* Gracefully skips for non-comment events (e.g., workflow_run, pull_request)
*/
export async function reactToComment({
octokit,
owner,
repo,
eventName,
}: ReactToCommentParams): Promise<void> {
// Skip reaction for non-comment events
if (!isCommentEvent(eventName)) {
core.info(
`ℹ️ Event type '${eventName}' does not support comment reactions, skipping`,
);
return;
}
// Extract comment_id from GitHub event payload
const commentId = getCommentIdFromEvent();
// Only react if we have a comment ID
if (!commentId) {
core.info(
`ℹ️ No comment found in event payload, skipping comment reaction (event: ${eventName})`,
);
return;
}
core.info("👀 Reacting to comment");
// React based on event type
if (eventName === "pull_request_review_comment") {
await octokit.rest.reactions.createForPullRequestReviewComment({
owner,
repo,
comment_id: commentId,
content: "eyes",
});
} else if (eventName === "issue_comment") {
await octokit.rest.reactions.createForIssueComment({
owner,
repo,
comment_id: commentId,
content: "eyes",
});
}
core.info(`✅ Successfully added :eyes: reaction to comment ${commentId}`);
}
type AuggieParams = {
eventName: string;
prompt: string;
augmentApiKey: string;
augmentApiUrl: string;
workspaceRoot: string | undefined;
commentBody: string | undefined;
commentId: number | undefined;
};
/**
* Get the GitHub event name from environment variable
*/
export function getEventName(): string {
const eventName = process.env.GITHUB_EVENT_NAME;
if (!eventName) {
throw new Error("GITHUB_EVENT_NAME environment variable not found");
}
return eventName;
}
export function getAuggieParams(): AuggieParams {
const eventName = getEventName();
const prompt = getInput("prompt", true);
const augmentApiKey = getInput("augment_api_key", true);
const augmentApiUrl = getInput("augment_api_url", true);
const workspaceRoot = getInput("workspace_root");
const commentBody = getCommentBodyFromEvent();
const commentId = getCommentIdFromEvent();
return {
eventName,
prompt,
augmentApiKey,
augmentApiUrl,
workspaceRoot,
commentBody,
commentId,
}
}