Skip to content

Commit

Permalink
feat(gitlab): implement requiredStatusChecks
Browse files Browse the repository at this point in the history
  • Loading branch information
sarunint committed Oct 7, 2019
1 parent eaab9ce commit 6d4c552
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 35 deletions.
60 changes: 34 additions & 26 deletions lib/platform/gitlab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,6 @@ export async function getBranchStatus(
// null means disable status checks, so it always succeeds
return 'success';
}
if (Array.isArray(requiredStatusChecks) && requiredStatusChecks.length) {
// This is Unsupported
logger.warn({ requiredStatusChecks }, `Unsupported requiredStatusChecks`);
return 'failed';
}

if (!(await branchExists(branchName))) {
throw new Error('repository-changed');
Expand All @@ -336,27 +331,40 @@ export async function getBranchStatus(
const branchSha = await config.storage.getBranchCommit(branchName);
// Now, check the statuses for that commit
const url = `projects/${config.repository}/repository/commits/${branchSha}/statuses`;
const res = await api.get(url, { paginate: true });
logger.debug(`Got res with ${res.body.length} results`);
if (res.body.length === 0) {
// Return 'pending' if we have no status checks
return 'pending';
}
let status = 'success';
// Return 'success' if all are success
res.body.forEach((check: { status: string; allow_failure?: boolean }) => {
// If one is failed then don't overwrite that
if (status !== 'failure') {
if (!check.allow_failure) {
if (check.status === 'failed') {
status = 'failure';
} else if (check.status !== 'success') {
({ status } = check);
}
}
}
});
return status;
const res = await api.get<
{ status: string; name: string; allow_failure?: boolean }[]
>(url, { paginate: true });
const commitStatuses = res.body;
logger.debug(`Got res with ${commitStatuses.length} results`);
const filteredCommitStatuses = commitStatuses.filter(
status =>
requiredStatusChecks.length === 0 ||
requiredStatusChecks.includes(status.name)
);

const names = filteredCommitStatuses.map(s => s.name);
const statuses = filteredCommitStatuses
.filter(s => !s.allow_failure)
.map(s => s.status);
const allRequiredStatusExists = requiredStatusChecks.every(s =>
names.includes(s)
);

let combinedStatuses: string;
if (statuses.includes('failed') || statuses.includes('canceled')) {
combinedStatuses = 'failed';
} else if (
statuses.includes('pending') ||
statuses.includes('running') ||
!allRequiredStatusExists ||
(requiredStatusChecks.length === 0 && filteredCommitStatuses.length === 0)
) {
combinedStatuses = 'pending';
} else {
combinedStatuses = 'success';
}

return combinedStatuses;
}

export async function getBranchStatusCheck(
Expand Down
77 changes: 68 additions & 9 deletions test/platform/gitlab/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,6 @@ describe('platform/gitlab', () => {
const res = await gitlab.getBranchStatus('somebranch', null);
expect(res).toEqual('success');
});
it('return failed if unsupported requiredStatusChecks', async () => {
const res = await gitlab.getBranchStatus('somebranch', ['foo']);
expect(res).toEqual('failed');
});
it('returns pending if no results', async () => {
await initRepo();
api.get.mockReturnValueOnce({
Expand Down Expand Up @@ -385,7 +381,7 @@ describe('platform/gitlab', () => {
const res = await gitlab.getBranchStatus('somebranch', []);
expect(res).toEqual('success');
});
it('returns failure if any mandatory jobs fails', async () => {
it('returns failed if any mandatory jobs fails', async () => {
await initRepo();
api.get.mockReturnValueOnce({
body: [
Expand All @@ -395,15 +391,78 @@ describe('platform/gitlab', () => {
],
} as any);
const res = await gitlab.getBranchStatus('somebranch', []);
expect(res).toEqual('failure');
expect(res).toEqual('failed');
});
it('returns custom statuses', async () => {
it('returns pending if any mandatory jobs are on pending', async () => {
await initRepo();
api.get.mockReturnValueOnce({
body: [{ status: 'success' }, { status: 'foo' }],
body: [
{ status: 'pending' },
{ status: 'failed', allow_failure: true },
{ status: 'success' },
],
} as any);
const res = await gitlab.getBranchStatus('somebranch', []);
expect(res).toEqual('foo');
expect(res).toEqual('pending');
});
it('returns success if all specified are success', async () => {
await initRepo();
api.get.mockReturnValueOnce({
body: [
{ status: 'success', name: 'test1' },
{ status: 'success', name: 'test2' },
{ status: 'failed', name: 'test3' },
],
} as any);
const res = await gitlab.getBranchStatus('somebranch', [
'test1',
'test2',
]);
expect(res).toEqual('success');
});
it('returns failed if not all specified are success', async () => {
await initRepo();
api.get.mockReturnValueOnce({
body: [
{ status: 'success', name: 'test1' },
{ status: 'success', name: 'test2' },
{ status: 'failed', name: 'test3' },
],
} as any);
const res = await gitlab.getBranchStatus('somebranch', [
'test1',
'test3',
]);
expect(res).toEqual('failed');
});
it('returns pending if some specified are pending', async () => {
await initRepo();
api.get.mockReturnValueOnce({
body: [
{ status: 'success', name: 'test1' },
{ status: 'pending', name: 'test2' },
{ status: 'failed', name: 'test3' },
],
} as any);
const res = await gitlab.getBranchStatus('somebranch', [
'test1',
'test2',
]);
expect(res).toEqual('pending');
});
it('returns pending if some specified are not exists', async () => {
await initRepo();
api.get.mockReturnValueOnce({
body: [
{ status: 'success', name: 'test1' },
{ status: 'pending', name: 'test2' },
],
} as any);
const res = await gitlab.getBranchStatus('somebranch', [
'test1',
'test3',
]);
expect(res).toEqual('pending');
});
it('throws repository-changed', async () => {
expect.assertions(1);
Expand Down

0 comments on commit 6d4c552

Please sign in to comment.