Skip to content

Commit bcb9373

Browse files
committed
chore: try catch
Signed-off-by: exploreriii <[email protected]>
1 parent 3ebe6dd commit bcb9373

File tree

1 file changed

+102
-93
lines changed

1 file changed

+102
-93
lines changed

.github/scripts/bot-gfi-assign-on-comment.js

Lines changed: 102 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -84,121 +84,130 @@ and you’ll be automatically assigned. Feel free to ask questions here if anyth
8484

8585
/// START OF SCRIPT ///
8686
module.exports = async ({ github, context }) => {
87+
try {
88+
const { issue, comment } = context.payload;
89+
const { owner, repo } = context.repo;
90+
91+
console.log('[gfi-assign] Payload snapshot:', {
92+
issueNumber: issue?.number,
93+
commenter: comment?.user?.login,
94+
commenterType: comment?.user?.type,
95+
commentBody: comment?.body,
96+
});
8797

88-
const { issue, comment } = context.payload;
89-
const { owner, repo } = context.repo;
90-
91-
console.log('[gfi-assign] Payload snapshot:', {
92-
issueNumber: issue?.number,
93-
commenter: comment?.user?.login,
94-
commenterType: comment?.user?.type,
95-
commentBody: comment?.body,
96-
});
97-
98-
// Reject if issue, comment or comment user is missing, reject bots, or if no /assign message
99-
if (!issue?.number) {
100-
console.log('[gfi-assign] Exit: missing issue number');
101-
return;
102-
}
98+
// Reject if issue, comment or comment user is missing, reject bots, or if no /assign message
99+
if (!issue?.number) {
100+
console.log('[gfi-assign] Exit: missing issue number');
101+
return;
102+
}
103103

104-
if (!comment?.body) {
105-
console.log('[gfi-assign] Exit: missing comment body');
106-
return;
107-
}
104+
if (!comment?.body) {
105+
console.log('[gfi-assign] Exit: missing comment body');
106+
return;
107+
}
108108

109-
if (!comment?.user?.login) {
110-
console.log('[gfi-assign] Exit: missing comment user login');
111-
return;
112-
}
109+
if (!comment?.user?.login) {
110+
console.log('[gfi-assign] Exit: missing comment user login');
111+
return;
112+
}
113113

114-
if (comment.user.type === 'Bot') {
115-
console.log('[gfi-assign] Exit: comment authored by bot');
116-
return;
117-
}
114+
if (comment.user.type === 'Bot') {
115+
console.log('[gfi-assign] Exit: comment authored by bot');
116+
return;
117+
}
118118

119-
if (!commentRequestsAssignment(comment.body)) {
120-
// Only remind if:
121-
// - GFI
122-
// - unassigned
123-
// - reminder not already posted
124-
if (
125-
issueIsGoodFirstIssue(issue) &&
126-
!issue.assignees?.length
127-
) {
128-
const comments = await github.paginate(
129-
github.rest.issues.listComments,
130-
{
131-
owner,
132-
repo,
133-
issue_number: issue.number,
134-
per_page: 100,
119+
if (!commentRequestsAssignment(comment.body)) {
120+
// Only remind if:
121+
// - GFI
122+
// - unassigned
123+
// - reminder not already posted
124+
if (
125+
issueIsGoodFirstIssue(issue) &&
126+
!issue.assignees?.length
127+
) {
128+
const comments = await github.paginate(
129+
github.rest.issues.listComments,
130+
{
131+
owner,
132+
repo,
133+
issue_number: issue.number,
134+
per_page: 100,
135+
}
136+
);
137+
138+
const reminderAlreadyPosted = comments.some(c =>
139+
c.body?.includes(ASSIGN_REMINDER_MARKER)
140+
);
141+
142+
if (!reminderAlreadyPosted) {
143+
await github.rest.issues.createComment({
144+
owner,
145+
repo,
146+
issue_number: issue.number,
147+
body: buildAssignReminder(comment.user.login),
148+
});
149+
150+
console.log('[gfi-assign] Posted /assign reminder');
135151
}
136-
);
152+
}
137153

138-
const reminderAlreadyPosted = comments.some(c =>
139-
c.body?.includes(ASSIGN_REMINDER_MARKER)
140-
);
154+
console.log('[gfi-assign] Exit: comment does not request assignment');
155+
return;
156+
}
141157

142-
if (!reminderAlreadyPosted) {
143-
await github.rest.issues.createComment({
144-
owner,
145-
repo,
146-
issue_number: issue.number,
147-
body: buildAssignReminder(comment.user.login),
148-
});
158+
console.log('[gfi-assign] Assignment command detected');
149159

150-
console.log('[gfi-assign] Posted /assign reminder');
151-
}
160+
// Reject if issue is not a Good First Issue
161+
if (!issueIsGoodFirstIssue(issue)) {
162+
console.log('[gfi-assign] Exit: issue is not a Good First Issue');
163+
return;
152164
}
153165

154-
console.log('[gfi-assign] Exit: comment does not request assignment');
155-
return;
156-
}
166+
console.log('[gfi-assign] Issue is labeled Good First Issue');
157167

158-
console.log('[gfi-assign] Assignment command detected');
168+
// Get requester username and issue number to enable comments and assignments
169+
const requesterUsername = comment.user.login;
170+
const issueNumber = issue.number;
159171

160-
// Reject if issue is not a Good First Issue
161-
if (!issueIsGoodFirstIssue(issue)) {
162-
console.log('[gfi-assign] Exit: issue is not a Good First Issue');
163-
return;
164-
}
172+
console.log('[gfi-assign] Requester:', requesterUsername);
173+
console.log('[gfi-assign] Current assignees:', issue.assignees?.map(a => a.login));
165174

166-
console.log('[gfi-assign] Issue is labeled Good First Issue');
175+
// Reject if issue is already assigned
176+
// Comment failure to the requester
177+
if (issue.assignees?.length > 0) {
178+
console.log('[gfi-assign] Exit: issue already assigned');
167179

168-
// Get requester username and issue number to enable comments and assignments
169-
const requesterUsername = comment.user.login;
170-
const issueNumber = issue.number;
180+
await github.rest.issues.createComment({
181+
owner,
182+
repo,
183+
issue_number: issueNumber,
184+
body: commentAlreadyAssigned(requesterUsername, issue),
185+
});
171186

172-
console.log('[gfi-assign] Requester:', requesterUsername);
173-
console.log('[gfi-assign] Current assignees:', issue.assignees?.map(a => a.login));
187+
console.log('[gfi-assign] Posted already-assigned comment');
188+
return;
189+
}
174190

175-
// Reject if issue is already assigned
176-
// Comment failure to the requester
177-
if (issue.assignees?.length > 0) {
178-
console.log('[gfi-assign] Exit: issue already assigned');
191+
console.log('[gfi-assign] Assigning issue to requester');
179192

180-
await github.rest.issues.createComment({
193+
// All validations passed and user has requested assignment on a GFI
194+
// Assign the issue to the requester
195+
// Do not comment on success
196+
await github.rest.issues.addAssignees({
181197
owner,
182198
repo,
183199
issue_number: issueNumber,
184-
body: commentAlreadyAssigned(requesterUsername, issue),
200+
assignees: [requesterUsername],
185201
});
186202

187-
console.log('[gfi-assign] Posted already-assigned comment');
188-
return;
203+
console.log('[gfi-assign] Assignment completed successfully');
204+
} catch (error) {
205+
console.error('[gfi-assign] Error:', {
206+
message: error.message,
207+
status: error.status,
208+
issueNumber: context.payload.issue?.number,
209+
commenter: context.payload.comment?.user?.login,
210+
});
211+
throw error;
189212
}
190-
191-
console.log('[gfi-assign] Assigning issue to requester');
192-
193-
// All validations passed and user has requested assignment on a GFI
194-
// Assign the issue to the requester
195-
// Do not comment on success
196-
await github.rest.issues.addAssignees({
197-
owner,
198-
repo,
199-
issue_number: issueNumber,
200-
assignees: [requesterUsername],
201-
});
202-
203-
console.log('[gfi-assign] Assignment completed successfully');
204213
};

0 commit comments

Comments
 (0)