2017-06-25 05:11:24 +00:00
|
|
|
const logger = require('../_fixtures/logger');
|
2017-06-22 19:35:32 +00:00
|
|
|
|
|
|
|
describe('api/gitlab', () => {
|
|
|
|
let gitlab;
|
2017-10-17 05:15:01 +00:00
|
|
|
let get;
|
2017-06-22 19:35:32 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
// clean up env
|
|
|
|
delete process.env.GITLAB_TOKEN;
|
|
|
|
delete process.env.GITLAB_ENDPOINT;
|
|
|
|
|
|
|
|
// reset module
|
|
|
|
jest.resetModules();
|
2017-10-17 19:46:49 +00:00
|
|
|
jest.mock('../../lib/api/gl-got-wrapper');
|
2017-06-22 19:35:32 +00:00
|
|
|
gitlab = require('../../lib/api/gitlab');
|
2017-10-17 19:46:49 +00:00
|
|
|
get = require('../../lib/api/gl-got-wrapper');
|
2017-06-22 19:35:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('getRepos', () => {
|
|
|
|
async function getRepos(...args) {
|
|
|
|
// repo info
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce(() => ({
|
2017-06-22 19:35:32 +00:00
|
|
|
body: [
|
|
|
|
{
|
|
|
|
path_with_namespace: 'a/b',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path_with_namespace: 'c/d',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}));
|
|
|
|
return gitlab.getRepos(...args);
|
|
|
|
}
|
|
|
|
it('should throw an error if no token is provided', async () => {
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
await gitlab.getRepos();
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
expect(err.message).toBe('No token found for getRepos');
|
|
|
|
});
|
|
|
|
it('should throw an error if it receives an error', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementation(() => {
|
2017-06-22 19:35:32 +00:00
|
|
|
throw new Error('getRepos error');
|
|
|
|
});
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
await gitlab.getRepos('sometoken');
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
expect(err.message).toBe('getRepos error');
|
|
|
|
});
|
|
|
|
it('should return an array of repos', async () => {
|
|
|
|
const repos = await getRepos('sometoken');
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.mock.calls).toMatchSnapshot();
|
2017-06-22 19:35:32 +00:00
|
|
|
expect(repos).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
it('should support a custom endpoint', async () => {
|
|
|
|
const repos = await getRepos('sometoken', 'someendpoint');
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.mock.calls).toMatchSnapshot();
|
2017-06-22 19:35:32 +00:00
|
|
|
expect(repos).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
async function initRepo(...args) {
|
|
|
|
// projects/owned
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce();
|
2017-06-22 19:35:32 +00:00
|
|
|
// projects/${config.repoName
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce(() => ({
|
2017-06-22 19:35:32 +00:00
|
|
|
body: {
|
|
|
|
default_branch: 'master',
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
// user
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce(() => ({
|
2017-06-22 19:35:32 +00:00
|
|
|
body: {
|
|
|
|
email: 'a@b.com',
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
return gitlab.initRepo(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('initRepo', () => {
|
|
|
|
[
|
|
|
|
[undefined, ['mytoken'], 'mytoken', undefined],
|
|
|
|
[
|
|
|
|
undefined,
|
|
|
|
['mytoken', 'https://my.custom.endpoint/'],
|
|
|
|
'mytoken',
|
|
|
|
'https://my.custom.endpoint/',
|
|
|
|
],
|
|
|
|
['myenvtoken', [], 'myenvtoken', undefined],
|
|
|
|
].forEach(([envToken, args, token, endpoint], i) => {
|
|
|
|
it(`should initialise the config for the repo - ${i}`, async () => {
|
|
|
|
if (envToken !== undefined) {
|
|
|
|
process.env.GITLAB_TOKEN = envToken;
|
|
|
|
}
|
|
|
|
const config = await initRepo('some/repo', ...args);
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.mock.calls).toMatchSnapshot();
|
2017-06-22 19:35:32 +00:00
|
|
|
expect(config).toMatchSnapshot();
|
|
|
|
expect(process.env.GITLAB_TOKEN).toBe(token);
|
|
|
|
expect(process.env.GITLAB_ENDPOINT).toBe(endpoint);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('uses provided logger', async () => {
|
|
|
|
const config = await initRepo(
|
|
|
|
'some/repo',
|
|
|
|
'some_token',
|
|
|
|
'an_endpoint',
|
|
|
|
logger
|
|
|
|
);
|
|
|
|
expect(config).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
it('should throw an error if no token is provided', async () => {
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
await gitlab.initRepo('some/repo');
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
expect(err.message).toBe(
|
|
|
|
'No token found for GitLab repository some/repo'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it('should throw an error if receiving an error', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementation(() => {
|
2017-06-22 19:35:32 +00:00
|
|
|
throw new Error('always error');
|
|
|
|
});
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
await gitlab.initRepo('some/repo', 'sometoken');
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
expect(err.message).toBe('always error');
|
|
|
|
});
|
|
|
|
it('should use api v4', async () => {
|
|
|
|
// projects/owned
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce(() => {
|
2017-06-22 19:35:32 +00:00
|
|
|
throw new Error('any error');
|
|
|
|
});
|
|
|
|
// projects/${config.repoName
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce(() => ({
|
2017-06-22 19:35:32 +00:00
|
|
|
body: {
|
|
|
|
default_branch: 'master',
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
// user
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce(() => ({
|
2017-06-22 19:35:32 +00:00
|
|
|
body: {
|
|
|
|
email: 'a@b.com',
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
const config = await initRepo('some/repo', 'some_token');
|
|
|
|
expect(config).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
2017-07-06 08:26:18 +00:00
|
|
|
describe('setBaseBranch(branchName)', () => {
|
|
|
|
it('sets the base branch', async () => {
|
|
|
|
await initRepo('some/repo', 'token');
|
|
|
|
// getBranchCommit
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce(() => ({
|
2017-07-06 08:26:18 +00:00
|
|
|
body: {
|
|
|
|
object: {
|
|
|
|
sha: '1238',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
await gitlab.setBaseBranch('some-branch');
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.mock.calls).toMatchSnapshot();
|
2017-07-06 08:26:18 +00:00
|
|
|
});
|
|
|
|
});
|
2017-06-22 19:35:32 +00:00
|
|
|
describe('findFilePaths(fileName)', () => {
|
2017-10-18 06:25:42 +00:00
|
|
|
it('returns empty array if error', async () => {
|
|
|
|
await initRepo('some/repo', 'token');
|
|
|
|
get.mockImplementationOnce(() => {
|
|
|
|
throw new Error('some error');
|
|
|
|
});
|
|
|
|
const files = await gitlab.findFilePaths('someething');
|
|
|
|
expect(files).toEqual([]);
|
|
|
|
});
|
2017-10-16 09:59:59 +00:00
|
|
|
it('warns if truncated result', async () => {
|
2017-06-22 19:35:32 +00:00
|
|
|
await initRepo('some/repo', 'token');
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce(() => ({
|
2017-10-16 09:59:59 +00:00
|
|
|
body: [],
|
|
|
|
}));
|
2017-06-22 19:35:32 +00:00
|
|
|
const files = await gitlab.findFilePaths('package.json');
|
2017-06-28 17:37:08 +00:00
|
|
|
expect(files.length).toBe(0);
|
2017-06-22 19:35:32 +00:00
|
|
|
});
|
2017-10-16 09:59:59 +00:00
|
|
|
it('caches the result', async () => {
|
|
|
|
await initRepo('some/repo', 'token');
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce(() => ({
|
2017-10-16 09:59:59 +00:00
|
|
|
body: [],
|
|
|
|
}));
|
|
|
|
let files = await gitlab.findFilePaths('package.json');
|
|
|
|
expect(files.length).toBe(0);
|
|
|
|
files = await gitlab.findFilePaths('package.js');
|
|
|
|
expect(files.length).toBe(0);
|
|
|
|
});
|
|
|
|
it('should return the files matching the fileName', async () => {
|
|
|
|
await initRepo('some/repo', 'token');
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce(() => ({
|
2017-10-16 09:59:59 +00:00
|
|
|
body: [
|
|
|
|
{ type: 'blob', path: 'package.json' },
|
|
|
|
{
|
|
|
|
type: 'blob',
|
|
|
|
path: 'some-dir/package.json.some-thing-else',
|
|
|
|
},
|
|
|
|
{ type: 'blob', path: 'src/app/package.json' },
|
|
|
|
{ type: 'blob', path: 'src/otherapp/package.json' },
|
|
|
|
],
|
|
|
|
}));
|
|
|
|
const files = await gitlab.findFilePaths('package.json');
|
|
|
|
expect(files).toMatchSnapshot();
|
|
|
|
});
|
2017-06-22 19:35:32 +00:00
|
|
|
});
|
|
|
|
describe('branchExists(branchName)', () => {
|
|
|
|
it('should return true if 200 OK', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce(() => ({ statusCode: 200 }));
|
2017-06-22 19:35:32 +00:00
|
|
|
const branchExists = await gitlab.branchExists('foo');
|
|
|
|
expect(branchExists).toBe(true);
|
|
|
|
});
|
|
|
|
it('should return false if not 200 OK', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce(() => ({ statusCode: 500 }));
|
2017-06-22 19:35:32 +00:00
|
|
|
const branchExists = await gitlab.branchExists('foo');
|
|
|
|
expect(branchExists).toBe(false);
|
|
|
|
});
|
|
|
|
it('should return false if 404 error received', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce(() =>
|
2017-06-22 19:35:32 +00:00
|
|
|
Promise.reject({
|
|
|
|
statusCode: 404,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
const branchExists = await gitlab.branchExists('foo');
|
|
|
|
expect(branchExists).toBe(false);
|
|
|
|
});
|
|
|
|
it('should return error if non-404 error thrown', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce(() =>
|
2017-06-22 19:35:32 +00:00
|
|
|
Promise.reject({
|
|
|
|
statusCode: 500,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
let e;
|
|
|
|
try {
|
|
|
|
await gitlab.branchExists('foo');
|
|
|
|
} catch (err) {
|
|
|
|
e = err;
|
|
|
|
}
|
|
|
|
expect(e.statusCode).toBe(500);
|
|
|
|
});
|
|
|
|
});
|
2017-06-25 05:11:24 +00:00
|
|
|
describe('getBranch', () => {
|
|
|
|
it('returns a branch', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({ body: 'foo' });
|
2017-06-25 05:11:24 +00:00
|
|
|
const branch = await gitlab.getBranch('branch-name');
|
|
|
|
expect(branch).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
it('nulls on error', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce(() => {
|
2017-06-25 05:11:24 +00:00
|
|
|
throw new Error('not found');
|
|
|
|
});
|
|
|
|
const branch = await gitlab.getBranch('branch-name');
|
|
|
|
expect(branch).toBe(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('getBranchPr(branchName)', () => {
|
|
|
|
it('should return null if no PR exists', async () => {
|
|
|
|
await initRepo('some/repo', 'token');
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce(() => ({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: [],
|
|
|
|
}));
|
|
|
|
const pr = await gitlab.getBranchPr('somebranch');
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.mock.calls).toMatchSnapshot();
|
2017-06-25 05:11:24 +00:00
|
|
|
expect(pr).toBe(null);
|
|
|
|
});
|
|
|
|
it('should return the PR object', async () => {
|
|
|
|
await initRepo('some/repo', 'token');
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce(() => ({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: [{ number: 91, source_branch: 'somebranch' }],
|
|
|
|
}));
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce(() => ({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: {
|
2017-08-31 19:06:19 +00:00
|
|
|
iid: 91,
|
2017-06-25 05:11:24 +00:00
|
|
|
additions: 1,
|
|
|
|
deletions: 1,
|
|
|
|
commits: 1,
|
2017-08-31 19:06:19 +00:00
|
|
|
source_branch: 'some-branch',
|
2017-06-25 05:11:24 +00:00
|
|
|
base: {
|
|
|
|
sha: '1234',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
const pr = await gitlab.getBranchPr('somebranch');
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.mock.calls).toMatchSnapshot();
|
2017-06-25 05:11:24 +00:00
|
|
|
expect(pr).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
2017-07-05 05:02:25 +00:00
|
|
|
describe('getBranchStatus(branchName, requiredStatusChecks)', () => {
|
2017-06-25 05:11:24 +00:00
|
|
|
beforeEach(() => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: {
|
|
|
|
commit: {
|
|
|
|
id: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2017-07-05 05:02:25 +00:00
|
|
|
it('returns success if requiredStatusChecks null', async () => {
|
|
|
|
await initRepo('some/repo', 'token');
|
|
|
|
const res = await gitlab.getBranchStatus('somebranch', null);
|
|
|
|
expect(res).toEqual('success');
|
|
|
|
});
|
|
|
|
it('return failed if unsupported requiredStatusChecks', async () => {
|
|
|
|
await initRepo('some/repo', 'token');
|
|
|
|
const res = await gitlab.getBranchStatus('somebranch', ['foo']);
|
|
|
|
expect(res).toEqual('failed');
|
|
|
|
});
|
2017-06-25 05:11:24 +00:00
|
|
|
it('returns pending if no results', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: [],
|
|
|
|
});
|
2017-07-05 05:02:25 +00:00
|
|
|
const res = await gitlab.getBranchStatus('somebranch', []);
|
2017-06-25 05:11:24 +00:00
|
|
|
expect(res).toEqual('pending');
|
|
|
|
});
|
|
|
|
it('returns success if all are success', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: [{ status: 'success' }, { status: 'success' }],
|
|
|
|
});
|
2017-07-05 05:02:25 +00:00
|
|
|
const res = await gitlab.getBranchStatus('somebranch', []);
|
2017-06-25 05:11:24 +00:00
|
|
|
expect(res).toEqual('success');
|
|
|
|
});
|
|
|
|
it('returns failure if any are failed', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: [{ status: 'success' }, { status: 'failed' }],
|
|
|
|
});
|
2017-07-05 05:02:25 +00:00
|
|
|
const res = await gitlab.getBranchStatus('somebranch', []);
|
2017-06-25 05:11:24 +00:00
|
|
|
expect(res).toEqual('failure');
|
|
|
|
});
|
|
|
|
it('returns custom statuses', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: [{ status: 'success' }, { status: 'foo' }],
|
|
|
|
});
|
2017-07-05 05:02:25 +00:00
|
|
|
const res = await gitlab.getBranchStatus('somebranch', []);
|
2017-06-25 05:11:24 +00:00
|
|
|
expect(res).toEqual('foo');
|
|
|
|
});
|
|
|
|
});
|
2017-08-08 21:03:52 +00:00
|
|
|
describe('getBranchStatusCheck', () => {
|
|
|
|
beforeEach(() => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-08-08 21:03:52 +00:00
|
|
|
body: {
|
|
|
|
commit: {
|
|
|
|
id: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('returns null if no results', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-08-08 21:03:52 +00:00
|
|
|
body: [],
|
|
|
|
});
|
|
|
|
const res = await gitlab.getBranchStatusCheck(
|
|
|
|
'somebranch',
|
|
|
|
'some-context'
|
|
|
|
);
|
|
|
|
expect(res).toEqual(null);
|
|
|
|
});
|
|
|
|
it('returns null if no matching results', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-08-08 21:03:52 +00:00
|
|
|
body: [{ name: 'context-1', status: 'pending' }],
|
|
|
|
});
|
|
|
|
const res = await gitlab.getBranchStatusCheck(
|
|
|
|
'somebranch',
|
|
|
|
'some-context'
|
|
|
|
);
|
|
|
|
expect(res).toEqual(null);
|
|
|
|
});
|
|
|
|
it('returns status if name found', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-08-08 21:03:52 +00:00
|
|
|
body: [
|
|
|
|
{ name: 'context-1', state: 'pending' },
|
|
|
|
{ name: 'some-context', state: 'success' },
|
|
|
|
{ name: 'context-3', state: 'failed' },
|
|
|
|
],
|
|
|
|
});
|
|
|
|
const res = await gitlab.getBranchStatusCheck(
|
|
|
|
'somebranch',
|
|
|
|
'some-context'
|
|
|
|
);
|
|
|
|
expect(res).toEqual('success');
|
|
|
|
});
|
|
|
|
});
|
2017-08-06 13:38:10 +00:00
|
|
|
describe('setBranchStatus', () => {
|
|
|
|
it('sets branch status', async () => {
|
|
|
|
await initRepo('some/repo', 'token');
|
|
|
|
// getBranchCommit
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-08-06 13:38:10 +00:00
|
|
|
body: {
|
|
|
|
commit: {
|
|
|
|
id: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await gitlab.setBranchStatus(
|
|
|
|
'some-branch',
|
|
|
|
'some-context',
|
|
|
|
'some-description',
|
|
|
|
'some-state',
|
|
|
|
'some-url'
|
|
|
|
);
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.post.mock.calls).toHaveLength(1);
|
2017-08-06 13:38:10 +00:00
|
|
|
});
|
|
|
|
});
|
2017-06-25 05:11:24 +00:00
|
|
|
describe('deleteBranch(branchName)', () => {
|
|
|
|
it('should send delete', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.delete = jest.fn();
|
2017-06-25 05:11:24 +00:00
|
|
|
await gitlab.deleteBranch('some-branch');
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.delete.mock.calls.length).toBe(1);
|
2017-06-25 05:11:24 +00:00
|
|
|
});
|
|
|
|
});
|
2017-08-28 09:37:09 +00:00
|
|
|
describe('getBranchLastCommitTime', () => {
|
|
|
|
it('should return a Date', async () => {
|
|
|
|
await initRepo('some/repo', 'token');
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-08-28 09:37:09 +00:00
|
|
|
body: [
|
|
|
|
{
|
|
|
|
id: 'ed899a2f4b50b4370feeea94676502b42383c746',
|
|
|
|
short_id: 'ed899a2f4b5',
|
|
|
|
title: 'Replace sanitize with escape once',
|
|
|
|
author_name: 'Dmitriy Zaporozhets',
|
|
|
|
author_email: 'dzaporozhets@sphereconsultinginc.com',
|
|
|
|
authored_date: '2012-09-20T11:50:22+03:00',
|
|
|
|
committer_name: 'Administrator',
|
|
|
|
committer_email: 'admin@example.com',
|
|
|
|
committed_date: '2012-09-20T11:50:22+03:00',
|
|
|
|
created_at: '2012-09-20T11:50:22+03:00',
|
|
|
|
message: 'Replace sanitize with escape once',
|
|
|
|
parent_ids: ['6104942438c14ec7bd21c6cd5bd995272b3faff6'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '6104942438c14ec7bd21c6cd5bd995272b3faff6',
|
|
|
|
short_id: '6104942438c',
|
|
|
|
title: 'Sanitize for network graph',
|
|
|
|
author_name: 'randx',
|
|
|
|
author_email: 'dmitriy.zaporozhets@gmail.com',
|
|
|
|
committer_name: 'Dmitriy',
|
|
|
|
committer_email: 'dmitriy.zaporozhets@gmail.com',
|
|
|
|
created_at: '2012-09-20T09:06:12+03:00',
|
|
|
|
message: 'Sanitize for network graph',
|
|
|
|
parent_ids: ['ae1d9fb46aa2b07ee9836d49862ec4e2c46fbbba'],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
const res = await gitlab.getBranchLastCommitTime('some-branch');
|
|
|
|
expect(res).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
it('handles error', async () => {
|
|
|
|
await initRepo('some/repo', 'token');
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-08-28 09:37:09 +00:00
|
|
|
body: [],
|
|
|
|
});
|
|
|
|
const res = await gitlab.getBranchLastCommitTime('some-branch');
|
|
|
|
expect(res).toBeDefined();
|
|
|
|
});
|
|
|
|
});
|
2017-06-25 05:11:24 +00:00
|
|
|
describe('addAssignees(issueNo, assignees)', () => {
|
|
|
|
it('should add the given assignees to the issue', async () => {
|
|
|
|
await initRepo('some/repo', 'token');
|
|
|
|
await gitlab.addAssignees(42, ['someuser']);
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.put.mock.calls).toMatchSnapshot();
|
2017-06-25 05:11:24 +00:00
|
|
|
});
|
|
|
|
it('should log error if more than one assignee', async () => {
|
|
|
|
await initRepo('some/repo', 'token');
|
|
|
|
await gitlab.addAssignees(42, ['someuser', 'someotheruser']);
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.put.mock.calls).toMatchSnapshot();
|
2017-06-25 05:11:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('addReviewers(issueNo, reviewers)', () => {
|
|
|
|
it('should add the given reviewers to the PR', async () => {
|
|
|
|
await initRepo('some/repo', 'token');
|
|
|
|
await gitlab.addReviewers(42, ['someuser', 'someotheruser']);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('addLabels(issueNo, labels)', () => {
|
|
|
|
it('should add the given labels to the issue', async () => {
|
|
|
|
await initRepo('some/repo', 'token');
|
|
|
|
await gitlab.addLabels(42, ['foo', 'bar']);
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.put.mock.calls).toMatchSnapshot();
|
2017-06-25 05:11:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('findPr(branchName, prTitle, state)', () => {
|
|
|
|
it('returns null if no results', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: [],
|
|
|
|
});
|
|
|
|
const pr = await gitlab.findPr('some-branch');
|
|
|
|
expect(pr).toBe(null);
|
|
|
|
});
|
|
|
|
it('returns null if no matching titles', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: [
|
|
|
|
{
|
|
|
|
source_branch: 'some-branch',
|
2017-08-31 16:21:40 +00:00
|
|
|
iid: 1,
|
2017-06-25 05:11:24 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
source_branch: 'some-branch',
|
2017-08-31 16:21:40 +00:00
|
|
|
iid: 2,
|
2017-06-25 05:11:24 +00:00
|
|
|
title: 'foo',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
const pr = await gitlab.findPr('some-branch', 'some-title');
|
|
|
|
expect(pr).toBe(null);
|
|
|
|
});
|
|
|
|
it('returns last result if multiple match', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: [
|
|
|
|
{
|
|
|
|
source_branch: 'some-branch',
|
2017-08-31 16:21:40 +00:00
|
|
|
iid: 1,
|
2017-06-25 05:11:24 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
source_branch: 'some-branch',
|
2017-08-31 16:21:40 +00:00
|
|
|
iid: 2,
|
2017-06-25 05:11:24 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
const pr = await gitlab.findPr('some-branch');
|
|
|
|
expect(pr.number).toBe(2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('createPr(branchName, title, body)', () => {
|
|
|
|
it('returns the PR', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.post.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: {
|
|
|
|
id: 1,
|
|
|
|
iid: 12345,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const pr = await gitlab.createPr('some-branch', 'some-title', 'the-body');
|
|
|
|
expect(pr).toMatchSnapshot();
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.post.mock.calls).toMatchSnapshot();
|
2017-07-06 12:12:52 +00:00
|
|
|
});
|
|
|
|
it('uses default branch', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.post.mockReturnValueOnce({
|
2017-07-06 12:12:52 +00:00
|
|
|
body: {
|
|
|
|
id: 1,
|
|
|
|
iid: 12345,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const pr = await gitlab.createPr(
|
|
|
|
'some-branch',
|
|
|
|
'some-title',
|
|
|
|
'the-body',
|
|
|
|
true
|
|
|
|
);
|
|
|
|
expect(pr).toMatchSnapshot();
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.post.mock.calls).toMatchSnapshot();
|
2017-06-25 05:11:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('getPr(prNo)', () => {
|
|
|
|
it('returns the PR', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: {
|
|
|
|
id: 1,
|
|
|
|
iid: 12345,
|
|
|
|
description: 'a merge request',
|
|
|
|
state: 'merged',
|
|
|
|
merge_status: 'cannot_be_merged',
|
2017-08-31 19:06:19 +00:00
|
|
|
source_branch: 'some-branch',
|
2017-06-25 05:11:24 +00:00
|
|
|
},
|
|
|
|
});
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: {
|
|
|
|
commit: {},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const pr = await gitlab.getPr(12345);
|
|
|
|
expect(pr).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('updatePr(prNo, title, body)', () => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
it('updates the PR', async () => {
|
2017-06-28 08:10:40 +00:00
|
|
|
await gitlab.updatePr(1, 'title', 'body');
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.put.mock.calls.length).toEqual(1);
|
2017-06-25 05:11:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('mergePr(pr)', () => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
it('merges the PR', async () => {
|
|
|
|
await gitlab.mergePr({ number: 1 });
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.put.mock.calls.length).toEqual(1);
|
2017-06-25 05:11:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('getFile(filePath, branchName)', () => {
|
|
|
|
it('gets the file with v4 by default', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: {
|
|
|
|
content: 'foo',
|
|
|
|
},
|
|
|
|
});
|
2017-09-29 07:05:36 +00:00
|
|
|
const res = await gitlab.getFile('some/path');
|
2017-06-25 05:11:24 +00:00
|
|
|
expect(res).toMatchSnapshot();
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.mock.calls[0][0].indexOf('some%2Fpath')).not.toBe(-1);
|
2017-06-25 05:11:24 +00:00
|
|
|
});
|
|
|
|
it('gets the file with v3', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: {},
|
|
|
|
});
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: {},
|
|
|
|
});
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: {},
|
|
|
|
});
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: {
|
|
|
|
content: 'foo',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const config = await gitlab.initRepo('some-repo', 'some-token');
|
|
|
|
expect(config).toMatchSnapshot();
|
2017-08-07 08:51:17 +00:00
|
|
|
const res = await gitlab.getFile('some-path');
|
2017-06-25 05:11:24 +00:00
|
|
|
expect(res).toMatchSnapshot();
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.mock.calls[3][0].indexOf('file_path')).not.toBe(-1);
|
2017-06-25 05:11:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('getFileContent(filePath, branchName)', () => {
|
|
|
|
it('gets the file', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: {
|
|
|
|
content: 'foo',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const res = await gitlab.getFileContent('some-path', 'some-branch');
|
|
|
|
expect(res).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
it('returns null for 404', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce(() => Promise.reject({ statusCode: 404 }));
|
2017-06-25 05:11:24 +00:00
|
|
|
const res = await gitlab.getFileContent('some-path', 'some-branch');
|
|
|
|
expect(res).toBe(null);
|
|
|
|
});
|
|
|
|
it('throws error for non-404', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce(() => Promise.reject({ statusCode: 403 }));
|
2017-06-25 05:11:24 +00:00
|
|
|
let e;
|
|
|
|
try {
|
|
|
|
await gitlab.getFileContent('some-path', 'some-branch');
|
|
|
|
} catch (err) {
|
|
|
|
e = err;
|
|
|
|
}
|
|
|
|
expect(e).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('getFileJson(filePath, branchName)', () => {
|
|
|
|
it('returns null for 404', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce(() => Promise.reject({ statusCode: 404 }));
|
2017-06-25 05:11:24 +00:00
|
|
|
const res = await gitlab.getFileJson('some-path', 'some-branch');
|
|
|
|
expect(res).toBe(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('createFile(branchName, filePath, fileContents, message)', () => {
|
|
|
|
it('creates file with v4', async () => {
|
|
|
|
await gitlab.createFile(
|
|
|
|
'some-branch',
|
|
|
|
'some-path',
|
|
|
|
'some-contents',
|
|
|
|
'some-message'
|
|
|
|
);
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.post.mock.calls).toMatchSnapshot();
|
|
|
|
expect(get.post.mock.calls[0][1].body.file_path).not.toBeDefined();
|
2017-06-25 05:11:24 +00:00
|
|
|
});
|
|
|
|
it('creates file with v3', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: {},
|
|
|
|
});
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: {},
|
|
|
|
});
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: {},
|
|
|
|
});
|
|
|
|
await gitlab.initRepo('some-repo', 'some-token');
|
|
|
|
await gitlab.createFile(
|
|
|
|
'some-branch',
|
|
|
|
'some-path',
|
|
|
|
'some-contents',
|
|
|
|
'some-message'
|
|
|
|
);
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.post.mock.calls).toMatchSnapshot();
|
|
|
|
expect(get.post.mock.calls[0][1].body.file_path).toBeDefined();
|
2017-06-25 05:11:24 +00:00
|
|
|
});
|
|
|
|
describe('updateFile(branchName, filePath, fileContents, message)', () => {
|
|
|
|
it('creates file with v4', async () => {
|
|
|
|
await gitlab.updateFile(
|
|
|
|
'some-branch',
|
|
|
|
'some-path',
|
|
|
|
'some-contents',
|
|
|
|
'some-message'
|
|
|
|
);
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.put.mock.calls).toMatchSnapshot();
|
|
|
|
expect(get.put.mock.calls[0][1].body.file_path).not.toBeDefined();
|
2017-06-25 05:11:24 +00:00
|
|
|
});
|
|
|
|
it('creates file with v3', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: {},
|
|
|
|
});
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: {},
|
|
|
|
});
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: {},
|
|
|
|
});
|
|
|
|
await gitlab.initRepo('some-repo', 'some-token');
|
|
|
|
await gitlab.updateFile(
|
|
|
|
'some-branch',
|
|
|
|
'some-path',
|
|
|
|
'some-contents',
|
|
|
|
'some-message'
|
|
|
|
);
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.put.mock.calls).toMatchSnapshot();
|
|
|
|
expect(get.put.mock.calls[0][1].body.file_path).toBeDefined();
|
2017-06-25 05:11:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('createBranch(branchName)', () => {
|
|
|
|
it('creates file with v4', async () => {
|
|
|
|
await gitlab.createBranch('some-branch');
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.post.mock.calls).toMatchSnapshot();
|
|
|
|
expect(get.post.mock.calls[0][1].body.branch_name).not.toBeDefined();
|
2017-06-25 05:11:24 +00:00
|
|
|
});
|
|
|
|
it('creates file with v3', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: {},
|
|
|
|
});
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: {},
|
|
|
|
});
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: {},
|
|
|
|
});
|
|
|
|
await gitlab.initRepo('some-repo', 'some-token');
|
|
|
|
await gitlab.createBranch('some-branch');
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.post.mock.calls).toMatchSnapshot();
|
|
|
|
expect(get.post.mock.calls[0][1].body.branch_name).toBeDefined();
|
2017-06-25 05:11:24 +00:00
|
|
|
});
|
|
|
|
});
|
2017-07-27 07:36:36 +00:00
|
|
|
describe('getSubDirectories(path)', () => {
|
|
|
|
it('should return subdirectories', async () => {
|
|
|
|
await initRepo('some/repo', 'token');
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce(() => ({
|
2017-07-27 07:36:36 +00:00
|
|
|
body: [{ type: 'tree', name: 'a' }, { type: 'file', name: 'b' }],
|
|
|
|
}));
|
|
|
|
const dirList = await gitlab.getSubDirectories('some-path');
|
2017-10-17 05:15:01 +00:00
|
|
|
expect(get.mock.calls).toMatchSnapshot();
|
2017-07-27 07:36:36 +00:00
|
|
|
expect(dirList).toHaveLength(1);
|
|
|
|
expect(dirList).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
2017-06-25 05:11:24 +00:00
|
|
|
describe('commitFilesToBranch(branchName, files, message, parentBranch)', () => {
|
|
|
|
it('creates branch', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({ statusCode: 404 });
|
2017-06-25 06:14:59 +00:00
|
|
|
await gitlab.commitFilesToBranch('some-branch', [], 'some-message');
|
2017-06-25 05:11:24 +00:00
|
|
|
});
|
|
|
|
it('does not create branch and updates file', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({ statusCode: 200 });
|
|
|
|
get.mockReturnValueOnce({
|
2017-06-25 05:11:24 +00:00
|
|
|
body: {
|
|
|
|
content: 'hello',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const file = {
|
|
|
|
name: 'foo',
|
|
|
|
contents: 'bar',
|
|
|
|
};
|
|
|
|
await gitlab.commitFilesToBranch(
|
|
|
|
'some-branch',
|
|
|
|
[file],
|
|
|
|
'some-message',
|
|
|
|
'parent-branch'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it('does not create branch and creates file', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({ statusCode: 200 });
|
|
|
|
get.mockReturnValueOnce(Promise.reject({ statusCode: 404 }));
|
2017-06-25 05:11:24 +00:00
|
|
|
const file = {
|
|
|
|
name: 'foo',
|
|
|
|
contents: 'bar',
|
|
|
|
};
|
|
|
|
await gitlab.commitFilesToBranch(
|
|
|
|
'some-branch',
|
|
|
|
[file],
|
|
|
|
'some-message',
|
|
|
|
'parent-branch'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-07-07 05:54:09 +00:00
|
|
|
describe('getCommitMessages()', () => {
|
|
|
|
it('returns commits messages', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockReturnValueOnce({
|
2017-07-07 05:54:09 +00:00
|
|
|
body: [
|
|
|
|
{
|
|
|
|
title: 'foo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'bar',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
const res = await gitlab.getCommitMessages();
|
|
|
|
expect(res).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
it('swallows errors', async () => {
|
2017-10-17 05:15:01 +00:00
|
|
|
get.mockImplementationOnce(() => {
|
2017-07-07 05:54:09 +00:00
|
|
|
throw new Error('some-error');
|
|
|
|
});
|
|
|
|
const res = await gitlab.getCommitMessages();
|
|
|
|
expect(res).toHaveLength(0);
|
|
|
|
});
|
|
|
|
});
|
2017-06-22 19:35:32 +00:00
|
|
|
});
|