-
Notifications
You must be signed in to change notification settings - Fork 287
Expand file tree
/
Copy pathbot-assignment-fresh-issue.test.js
More file actions
195 lines (164 loc) · 5.78 KB
/
Copy pathbot-assignment-fresh-issue.test.js
File metadata and controls
195 lines (164 loc) · 5.78 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
// Regression coverage for stale issue_comment payloads: the bots must refetch issue state before assigning.
const { describe, it } = require('node:test');
const assert = require('node:assert/strict');
const runGfiAssignBot = require('./bot-gfi-assign-on-comment.js');
const runBeginnerAssignBot = require('./bot-beginner-assign-on-comment.js');
function createContext({ labelName, payloadAssignees = [], freshLabels, freshAssignees = [], freshState = 'open' }) {
const labels = [{ name: labelName }];
return {
repo: {
owner: 'hiero-ledger',
repo: 'hiero-sdk-python',
},
payload: {
repository: {
owner: { login: 'hiero-ledger' },
name: 'hiero-sdk-python',
},
issue: {
number: 123,
labels,
assignees: payloadAssignees,
},
comment: {
body: '/assign',
user: {
login: 'new-contributor',
type: 'User',
},
},
},
freshIssue: {
number: 123,
title: 'Example issue',
state: freshState,
labels: freshLabels ?? labels,
assignees: freshAssignees,
},
};
}
function createGithubMock(context, { freshIssueError } = {}) {
const calls = {
comments: [],
assignees: [],
};
const github = {
rest: {
issues: {
get: async () => {
if (freshIssueError) {
throw freshIssueError;
}
return { data: context.freshIssue };
},
createComment: async (params) => {
calls.comments.push(params);
return { data: {} };
},
addAssignees: async (params) => {
calls.assignees.push(params);
return { data: {} };
},
},
},
paginate: async () => [],
graphql: async () => ({ search: { issueCount: 1 } }),
};
return { github, calls };
}
describe('assignment bots use fresh issue state before assigning', () => {
it('GFI bot does not assign when webhook payload is stale but issue is now assigned', async () => {
const context = createContext({
labelName: 'Good First Issue',
payloadAssignees: [],
freshAssignees: [{ login: 'current-assignee' }],
});
const { github, calls } = createGithubMock(context);
await runGfiAssignBot({ github, context });
assert.equal(calls.assignees.length, 0);
assert.equal(calls.comments.length, 1);
assert.match(calls.comments[0].body, /already assigned/i);
assert.match(calls.comments[0].body, /@current-assignee/);
});
it('GFI bot exits safely when fresh issue fetch fails before assignment', async () => {
const context = createContext({
labelName: 'Good First Issue',
payloadAssignees: [],
});
const { github, calls } = createGithubMock(context, {
freshIssueError: Object.assign(new Error('GitHub API unavailable'), { status: 503 }),
});
await runGfiAssignBot({ github, context });
assert.equal(calls.assignees.length, 0);
assert.equal(calls.comments.length, 0);
});
it('GFI bot exits when the fresh issue is no longer a Good First Issue', async () => {
const context = createContext({
labelName: 'Good First Issue',
payloadAssignees: [],
freshLabels: [{ name: 'help wanted' }],
});
const { github, calls } = createGithubMock(context);
await runGfiAssignBot({ github, context });
assert.equal(calls.assignees.length, 0);
assert.equal(calls.comments.length, 0);
});
it('GFI bot exits when the fresh issue is closed before assignment', async () => {
const context = createContext({
labelName: 'Good First Issue',
payloadAssignees: [],
freshState: 'closed',
});
const { github, calls } = createGithubMock(context);
await runGfiAssignBot({ github, context });
assert.equal(calls.assignees.length, 0);
assert.equal(calls.comments.length, 0);
});
it('beginner bot does not assign when webhook payload is stale but issue is now assigned', async () => {
const context = createContext({
labelName: 'skill: beginner',
payloadAssignees: [],
freshAssignees: [{ login: 'current-assignee' }],
});
const { github, calls } = createGithubMock(context);
await runBeginnerAssignBot({ github, context });
assert.equal(calls.assignees.length, 0);
assert.equal(calls.comments.length, 1);
assert.match(calls.comments[0].body, /already assigned/i);
assert.match(calls.comments[0].body, /@current-assignee/);
});
it('beginner bot exits safely when fresh issue fetch fails before assignment', async () => {
const context = createContext({
labelName: 'skill: beginner',
payloadAssignees: [],
});
const { github, calls } = createGithubMock(context, {
freshIssueError: Object.assign(new Error('GitHub API unavailable'), { status: 503 }),
});
await runBeginnerAssignBot({ github, context });
assert.equal(calls.assignees.length, 0);
assert.equal(calls.comments.length, 0);
});
it('beginner bot exits when the fresh issue no longer has the beginner label', async () => {
const context = createContext({
labelName: 'skill: beginner',
payloadAssignees: [],
freshLabels: [{ name: 'help wanted' }],
});
const { github, calls } = createGithubMock(context);
await runBeginnerAssignBot({ github, context });
assert.equal(calls.assignees.length, 0);
assert.equal(calls.comments.length, 0);
});
it('beginner bot exits when the fresh issue is closed before assignment', async () => {
const context = createContext({
labelName: 'skill: beginner',
payloadAssignees: [],
freshState: 'closed',
});
const { github, calls } = createGithubMock(context);
await runBeginnerAssignBot({ github, context });
assert.equal(calls.assignees.length, 0);
assert.equal(calls.comments.length, 0);
});
});