forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcla.test.ts
213 lines (189 loc) · 8.37 KB
/
cla.test.ts
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
/* eslint-disable @typescript-eslint/naming-convention */
import type {Step} from '@kie/act-js';
import {MockGithub} from '@kie/mock-github';
import type {CreateRepositoryFile} from '@kie/mock-github';
import path from 'path';
import {assertCLAJobExecuted} from './assertions/claAssertions';
import {CLA__CLA__CHECK_MATCH__STEP_MOCKS, CLA__CLA__NO_MATCHES__STEP_MOCKS, CLA__CLA__RECHECK_MATCH__STEP_MOCKS} from './mocks/claMocks';
import ExtendedAct from './utils/ExtendedAct';
import * as utils from './utils/utils';
jest.setTimeout(90 * 1000);
let mockGithub: MockGithub;
const FILES_TO_COPY_INTO_TEST_REPO: CreateRepositoryFile[] = [
...utils.deepCopy(utils.FILES_TO_COPY_INTO_TEST_REPO),
{
src: path.resolve(__dirname, '..', '.github', 'workflows', 'cla.yml'),
dest: '.github/workflows/cla.yml',
},
];
describe('test workflow cla', () => {
const secrets: Record<string, string> = {
CLA_BOTIFY_TOKEN: 'dummy_cla_botify_token',
};
const githubToken = 'dummy_github_token';
const actor = 'Dummy Author';
// eslint-disable-next-line @typescript-eslint/require-await
beforeAll(async () => {
// in case of the tests being interrupted without cleanup the mock repo directory may be left behind
// which breaks the next test run, this removes any possible leftovers
utils.removeMockRepoDir();
});
beforeEach(async () => {
// create a local repository and copy required files
mockGithub = new MockGithub({
repo: {
testClaWorkflowRepo: {
files: FILES_TO_COPY_INTO_TEST_REPO,
},
},
});
await mockGithub.setup();
});
afterEach(async () => {
await mockGithub?.teardown();
});
describe('event is issue_comment', () => {
const event = 'issue_comment';
describe('no regex matches', () => {
const commentBody = 'Comment body';
const eventData = {
action: 'created',
issue: {
pull_request: {
number: 1234,
},
},
comment: {
body: commentBody,
},
};
it('workflow executes, CLA assistant step not run', async () => {
const repoPath = mockGithub?.repo.getPath('testClaWorkflowRepo') ?? '';
const workflowPath = path.join(repoPath, '.github', 'workflows', 'cla.yml');
let act = new ExtendedAct(repoPath, workflowPath);
act = utils.setUpActParams(act, event, eventData, secrets, githubToken);
const testMockSteps = {
CLA: CLA__CLA__NO_MATCHES__STEP_MOCKS,
};
const result = await act.runEvent(event, {
workflowFile: path.join(repoPath, '.github', 'workflows', 'cla.yml'),
mockSteps: testMockSteps,
actor,
logFile: utils.getLogFilePath('cla', expect.getState().currentTestName),
});
assertCLAJobExecuted(result, commentBody, `${repoPath}/remote/origin`, true, false);
});
});
describe('check regex matches', () => {
const commentBody = 'I have read the CLA Document and I hereby sign the CLA';
const eventData = {
action: 'created',
issue: {
pull_request: {
number: 1234,
},
},
comment: {
body: commentBody,
},
};
it('workflow executes, CLA assistant step run', async () => {
const repoPath = mockGithub?.repo.getPath('testClaWorkflowRepo') ?? '';
const workflowPath = path.join(repoPath, '.github', 'workflows', 'cla.yml');
let act = new ExtendedAct(repoPath, workflowPath);
act = utils.setUpActParams(act, event, eventData, secrets, githubToken);
const testMockSteps = {
CLA: CLA__CLA__CHECK_MATCH__STEP_MOCKS,
};
const result = await act.runEvent(event, {
workflowFile: path.join(repoPath, '.github', 'workflows', 'cla.yml'),
mockSteps: testMockSteps,
actor,
logFile: utils.getLogFilePath('cla', expect.getState().currentTestName),
});
assertCLAJobExecuted(result, commentBody, `${repoPath}/remote/origin`, true, true);
});
});
describe('re-check regex matches', () => {
const commentBody = 'recheck';
const eventData = {
action: 'created',
issue: {
pull_request: {
number: 1234,
},
},
comment: {
body: commentBody,
},
};
it('workflow executes, CLA assistant step run', async () => {
const repoPath = mockGithub?.repo.getPath('testClaWorkflowRepo') ?? '';
const workflowPath = path.join(repoPath, '.github', 'workflows', 'cla.yml');
let act = new ExtendedAct(repoPath, workflowPath);
act = utils.setUpActParams(act, event, eventData, secrets, githubToken);
const testMockSteps = {
CLA: CLA__CLA__RECHECK_MATCH__STEP_MOCKS,
};
const result = await act.runEvent(event, {
workflowFile: path.join(repoPath, '.github', 'workflows', 'cla.yml'),
mockSteps: testMockSteps,
actor,
logFile: utils.getLogFilePath('cla', expect.getState().currentTestName),
});
assertCLAJobExecuted(result, commentBody, `${repoPath}/remote/origin`, true, true);
});
});
});
describe('event is pull_request_target', () => {
const event = 'pull_request_target';
describe("no regex matches - there's no comment", () => {
const eventData = {
action: 'opened',
issue: {
pull_request: {
number: 1234,
},
},
};
it('workflow executes, CLA assistant step still run', async () => {
const repoPath = mockGithub?.repo.getPath('testClaWorkflowRepo') ?? '';
const workflowPath = path.join(repoPath, '.github', 'workflows', 'cla.yml');
let act = new ExtendedAct(repoPath, workflowPath);
act = utils.setUpActParams(act, event, eventData, secrets, githubToken);
const testMockSteps = {
CLA: CLA__CLA__NO_MATCHES__STEP_MOCKS,
};
const result = await act.runEvent(event, {
workflowFile: path.join(repoPath, '.github', 'workflows', 'cla.yml'),
mockSteps: testMockSteps,
actor,
logFile: utils.getLogFilePath('cla', expect.getState().currentTestName),
});
assertCLAJobExecuted(result, '', `${repoPath}/remote/origin`, true, true);
});
});
});
describe('different event', () => {
const event = 'push';
it('workflow does not execute', async () => {
const eventData = {
ref: 'main',
};
const repoPath = mockGithub?.repo.getPath('testClaWorkflowRepo') ?? '';
const workflowPath = path.join(repoPath, '.github', 'workflows', 'cla.yml');
let act = new ExtendedAct(repoPath, workflowPath);
act = utils.setUpActParams(act, event, eventData, secrets, githubToken);
const testMockSteps = {
CLA: CLA__CLA__NO_MATCHES__STEP_MOCKS,
};
const result: Step[] = await act.runEvent(event, {
workflowFile: path.join(repoPath, '.github', 'workflows', 'cla.yml'),
mockSteps: testMockSteps,
actor,
logFile: utils.getLogFilePath('cla', expect.getState().currentTestName),
});
assertCLAJobExecuted(result, '', `${repoPath}/remote/origin`, false);
});
});
});