-
Notifications
You must be signed in to change notification settings - Fork 762
Expand file tree
/
Copy pathgithubRepository.test.ts
More file actions
223 lines (196 loc) · 9.34 KB
/
Copy pathgithubRepository.test.ts
File metadata and controls
223 lines (196 loc) · 9.34 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { default as assert } from 'assert';
import { SinonSandbox, createSandbox } from 'sinon';
import { CredentialStore } from '../../github/credentials';
import { MockCommandRegistry } from '../mocks/mockCommandRegistry';
import { MockTelemetry } from '../mocks/mockTelemetry';
import { GitHubRemote, Remote } from '../../common/remote';
import { Protocol } from '../../common/protocol';
import { GitHubRepository } from '../../github/githubRepository';
import { Uri } from 'vscode';
import { MockExtensionContext } from '../mocks/mockExtensionContext';
import { GitHubManager } from '../../authentication/githubServer';
import { GitHubServerType } from '../../common/authentication';
import { CheckState, PullRequestCheckStatus } from '../../github/interface';
describe('GitHubRepository', function () {
let sinon: SinonSandbox;
let credentialStore: CredentialStore;
let telemetry: MockTelemetry;
let context: MockExtensionContext;
beforeEach(function () {
sinon = createSandbox();
MockCommandRegistry.install(sinon);
telemetry = new MockTelemetry();
context = new MockExtensionContext();
credentialStore = new CredentialStore(telemetry, context);
});
afterEach(function () {
sinon.restore();
});
describe('isGitHubDotCom', function () {
it('detects when the remote is pointing to github.com', function () {
const url = 'https://github.com/some/repo';
const remote = new GitHubRemote('origin', url, new Protocol(url), GitHubServerType.GitHubDotCom);
const rootUri = Uri.file('C:\\users\\test\\repo');
const dotcomRepository = new GitHubRepository(1, remote, rootUri, credentialStore, telemetry);
assert(GitHubManager.isGithubDotCom(Uri.parse(remote.url).authority));
});
it('detects when the remote is pointing somewhere other than github.com', function () {
const url = 'https://github.enterprise.horse/some/repo';
const remote = new GitHubRemote('origin', url, new Protocol(url), GitHubServerType.GitHubDotCom);
const rootUri = Uri.file('C:\\users\\test\\repo');
const dotcomRepository = new GitHubRepository(1, remote, rootUri, credentialStore, telemetry);
// assert(! dotcomRepository.isGitHubDotCom);
});
});
describe('deduplicateStatusChecks', function () {
function createStatus(overrides: Partial<PullRequestCheckStatus> & { id: string; context: string }): PullRequestCheckStatus {
return {
databaseId: undefined,
url: undefined,
avatarUrl: undefined,
state: CheckState.Success,
description: null,
targetUrl: null,
workflowName: undefined,
event: undefined,
isRequired: false,
isCheckRun: true,
...overrides,
};
}
function callDeduplicateStatusChecks(repo: GitHubRepository, statuses: PullRequestCheckStatus[]): PullRequestCheckStatus[] {
return (repo as any).deduplicateStatusChecks(statuses);
}
let repo: GitHubRepository;
beforeEach(function () {
const url = 'https://github.com/some/repo';
const remote = new GitHubRemote('origin', url, new Protocol(url), GitHubServerType.GitHubDotCom);
const rootUri = Uri.file('C:\\users\\test\\repo');
repo = new GitHubRepository(1, remote, rootUri, credentialStore, telemetry);
});
it('keeps checks with different events as separate entries', function () {
const statuses = [
createStatus({ id: '1', context: 'Build Linux / x86-64', event: 'push', workflowName: 'Build Linux' }),
createStatus({ id: '2', context: 'Build Linux / x86-64', event: 'pull_request', workflowName: 'Build Linux' }),
];
const result = callDeduplicateStatusChecks(repo, statuses);
assert.strictEqual(result.length, 2);
});
it('deduplicates checks with the same name, event, and workflow', function () {
const statuses = [
createStatus({ id: '1', context: 'Build Linux / x86-64', event: 'push', workflowName: 'Build Linux', state: CheckState.Success }),
createStatus({ id: '2', context: 'Build Linux / x86-64', event: 'push', workflowName: 'Build Linux', state: CheckState.Success }),
];
const result = callDeduplicateStatusChecks(repo, statuses);
assert.strictEqual(result.length, 1);
assert.strictEqual(result[0].id, '2'); // higher ID preferred
});
it('keeps checks from different workflows as separate entries', function () {
const statuses = [
createStatus({ id: '1', context: 'build', event: 'push', workflowName: 'CI' }),
createStatus({ id: '2', context: 'build', event: 'push', workflowName: 'Nightly' }),
];
const result = callDeduplicateStatusChecks(repo, statuses);
assert.strictEqual(result.length, 2);
});
it('prefers pending checks over completed ones during deduplication', function () {
const statuses = [
createStatus({ id: '1', context: 'test', event: 'push', workflowName: 'CI', state: CheckState.Success }),
createStatus({ id: '2', context: 'test', event: 'push', workflowName: 'CI', state: CheckState.Pending }),
];
const result = callDeduplicateStatusChecks(repo, statuses);
assert.strictEqual(result.length, 1);
assert.strictEqual(result[0].state, CheckState.Pending);
});
it('handles status contexts without event or workflowName', function () {
const statuses = [
createStatus({ id: '1', context: 'ci/jenkins', isCheckRun: false }),
createStatus({ id: '2', context: 'ci/travis', isCheckRun: false }),
];
const result = callDeduplicateStatusChecks(repo, statuses);
assert.strictEqual(result.length, 2);
});
});
describe('computeAwaitingApprovalStatuses', function () {
function callComputeAwaitingApprovalStatuses(
repo: GitHubRepository,
checkSuites: any[] | undefined,
existingStatuses: PullRequestCheckStatus[],
prUrl: string,
): PullRequestCheckStatus[] {
return (repo as any).computeAwaitingApprovalStatuses(checkSuites, existingStatuses, prUrl);
}
function createSuite(overrides: Partial<{ status: string; conclusion: string | null; workflowName: string; event: string }>) {
const { status = 'WAITING', conclusion = null, workflowName, event } = overrides;
return {
status,
conclusion,
workflowRun: workflowName ? { event: event ?? 'pull_request', workflow: { name: workflowName } } : null,
app: null,
};
}
let repo: GitHubRepository;
beforeEach(function () {
const url = 'https://github.com/some/repo';
const remote = new GitHubRemote('origin', url, new Protocol(url), GitHubServerType.GitHubDotCom);
const rootUri = Uri.file('C:\\users\\test\\repo');
repo = new GitHubRepository(1, remote, rootUri, credentialStore, telemetry);
});
it('returns nothing when there are no check suites', function () {
assert.strictEqual(callComputeAwaitingApprovalStatuses(repo, undefined, [], 'url').length, 0);
assert.strictEqual(callComputeAwaitingApprovalStatuses(repo, [], [], 'url').length, 0);
});
it('surfaces a pending status for a waiting workflow', function () {
const suites = [createSuite({ status: 'WAITING', workflowName: 'CI' })];
const result = callComputeAwaitingApprovalStatuses(repo, suites, [], 'https://github.com/some/repo/pull/1');
assert.strictEqual(result.length, 1);
assert.strictEqual(result[0].state, CheckState.Pending);
assert.strictEqual(result[0].context, 'CI');
assert.strictEqual(result[0].workflowName, 'CI');
assert.strictEqual(result[0].targetUrl, 'https://github.com/some/repo/pull/1');
});
it('surfaces a pending status for a requested workflow', function () {
const suites = [createSuite({ status: 'REQUESTED', workflowName: 'CI' })];
const result = callComputeAwaitingApprovalStatuses(repo, suites, [], 'url');
assert.strictEqual(result.length, 1);
assert.strictEqual(result[0].state, CheckState.Pending);
});
it('ignores suites that have already concluded', function () {
const suites = [createSuite({ status: 'COMPLETED', conclusion: 'SUCCESS', workflowName: 'CI' })];
assert.strictEqual(callComputeAwaitingApprovalStatuses(repo, suites, [], 'url').length, 0);
});
it('ignores suites that are in progress', function () {
const suites = [createSuite({ status: 'IN_PROGRESS', workflowName: 'CI' })];
assert.strictEqual(callComputeAwaitingApprovalStatuses(repo, suites, [], 'url').length, 0);
});
it('does not duplicate a workflow already represented by an existing status', function () {
const suites = [createSuite({ status: 'WAITING', workflowName: 'CI' })];
const existing = [{
id: '1',
databaseId: undefined,
url: undefined,
avatarUrl: undefined,
state: CheckState.Success,
description: null,
targetUrl: null,
context: 'CI / build',
workflowName: 'CI',
event: 'pull_request',
isRequired: false,
isCheckRun: true,
} as PullRequestCheckStatus];
assert.strictEqual(callComputeAwaitingApprovalStatuses(repo, suites, existing, 'url').length, 0);
});
it('falls back to a generic context when the workflow name is unknown', function () {
const suites = [createSuite({ status: 'WAITING' })];
const result = callComputeAwaitingApprovalStatuses(repo, suites, [], 'url');
assert.strictEqual(result.length, 1);
assert.strictEqual(result[0].workflowName, undefined);
assert.ok(result[0].context.length > 0);
});
});
});