2021-08-04 03:45:24 +00:00
|
|
|
import { Gitlab, Types } from '@gitbeaker/node';
|
2020-08-15 04:40:56 +00:00
|
|
|
import { expect } from 'chai';
|
2020-08-29 00:06:31 +00:00
|
|
|
import { NullLogger } from 'noicejs';
|
2021-07-30 04:42:54 +00:00
|
|
|
import sinon from 'sinon';
|
2020-08-28 22:34:31 +00:00
|
|
|
|
2020-08-28 23:51:46 +00:00
|
|
|
import { RemoteOptions } from '../../src';
|
2021-08-04 03:45:24 +00:00
|
|
|
import { GitlabRemote, INJECT_GITLAB } from '../../src/remote/gitlab';
|
2020-08-29 00:06:31 +00:00
|
|
|
import { createRemoteContainer } from './helpers';
|
2020-08-28 23:51:46 +00:00
|
|
|
|
2021-08-04 03:45:24 +00:00
|
|
|
const { stub } = sinon;
|
2021-07-30 04:42:54 +00:00
|
|
|
|
2020-08-28 23:51:46 +00:00
|
|
|
const REMOTE_OPTIONS: Omit<RemoteOptions, 'container'> = {
|
|
|
|
data: {
|
|
|
|
token: 'test',
|
|
|
|
type: 'token',
|
|
|
|
},
|
|
|
|
dryrun: false,
|
|
|
|
logger: NullLogger.global,
|
|
|
|
type: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
const DRYRUN_OPTIONS = {
|
|
|
|
...REMOTE_OPTIONS,
|
|
|
|
dryrun: true,
|
|
|
|
};
|
|
|
|
|
2021-08-04 03:45:24 +00:00
|
|
|
const STUB_PROJECT: Types.ProjectExtendedSchema = {
|
|
|
|
id: 0,
|
2020-08-28 23:51:46 +00:00
|
|
|
namespace: {
|
2021-08-04 03:45:24 +00:00
|
|
|
id: 0,
|
|
|
|
name: '',
|
|
|
|
path: '',
|
|
|
|
kind: '',
|
|
|
|
/* eslint-disable-next-line camelcase */
|
|
|
|
full_path: '',
|
|
|
|
/* eslint-disable-next-line camelcase */
|
|
|
|
avatar_url: '',
|
|
|
|
/* eslint-disable-next-line camelcase */
|
|
|
|
web_url: '',
|
2020-08-28 23:51:46 +00:00
|
|
|
},
|
2021-08-04 03:45:24 +00:00
|
|
|
} as Types.ProjectExtendedSchema;
|
2020-08-15 04:40:56 +00:00
|
|
|
|
|
|
|
describe('gitlab remote', () => {
|
2020-08-28 23:51:46 +00:00
|
|
|
describe('create comment endpoint', () => {
|
|
|
|
it('should create comments when dryrun=false', async () => {
|
2020-08-29 00:06:31 +00:00
|
|
|
const { container, module } = await createRemoteContainer();
|
2020-08-28 23:51:46 +00:00
|
|
|
|
2021-08-04 03:45:24 +00:00
|
|
|
const client = new Gitlab({});
|
2020-08-28 23:51:46 +00:00
|
|
|
stub(client.Projects, 'show').returns(Promise.resolve(STUB_PROJECT));
|
|
|
|
const createStub = stub(client.IssueNotes, 'create');
|
2021-08-04 03:45:24 +00:00
|
|
|
module.bind(INJECT_GITLAB).toInstance(client);
|
2020-08-28 23:51:46 +00:00
|
|
|
|
|
|
|
const remote = await container.create(GitlabRemote, REMOTE_OPTIONS);
|
|
|
|
const status = await remote.connect();
|
|
|
|
expect(status).to.equal(true);
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
changes: [],
|
|
|
|
errors: [],
|
|
|
|
issue: '1',
|
|
|
|
project: '',
|
|
|
|
};
|
|
|
|
const result = await remote.createComment(data);
|
|
|
|
|
|
|
|
expect(result).to.include(data);
|
2021-08-04 03:45:24 +00:00
|
|
|
expect(createStub).to.have.callCount(1).and.been.calledWithMatch(0, 1);
|
2020-08-28 23:51:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not create comments when dryrun=true', async () => {
|
2020-08-29 00:06:31 +00:00
|
|
|
const { container, module } = await createRemoteContainer();
|
2020-08-28 23:51:46 +00:00
|
|
|
|
2021-08-04 03:45:24 +00:00
|
|
|
const client = new Gitlab({});
|
2020-08-28 23:51:46 +00:00
|
|
|
stub(client.Projects, 'show').returns(Promise.resolve(STUB_PROJECT));
|
|
|
|
const createStub = stub(client.IssueNotes, 'create');
|
2021-08-04 03:45:24 +00:00
|
|
|
module.bind(INJECT_GITLAB).toInstance(client);
|
2020-08-28 23:51:46 +00:00
|
|
|
|
|
|
|
const remote = await container.create(GitlabRemote, DRYRUN_OPTIONS);
|
|
|
|
const status = await remote.connect();
|
|
|
|
expect(status).to.equal(true);
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
changes: [],
|
|
|
|
errors: [],
|
|
|
|
issue: '1',
|
|
|
|
project: '',
|
|
|
|
};
|
|
|
|
const result = await remote.createComment(data);
|
|
|
|
|
|
|
|
expect(result).to.include(data);
|
|
|
|
expect(createStub).to.have.callCount(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-08-28 22:34:31 +00:00
|
|
|
describe('create label endpoint', () => {
|
|
|
|
it('should create labels when dryrun=false', async () => {
|
2020-08-29 00:06:31 +00:00
|
|
|
const { container, module } = await createRemoteContainer();
|
2020-08-28 22:34:31 +00:00
|
|
|
|
2021-08-04 03:45:24 +00:00
|
|
|
const client = new Gitlab({});
|
2020-08-28 23:51:46 +00:00
|
|
|
stub(client.Projects, 'show').returns(Promise.resolve(STUB_PROJECT));
|
2020-08-28 22:34:31 +00:00
|
|
|
const createStub = stub(client.Labels, 'create');
|
2021-08-04 03:45:24 +00:00
|
|
|
module.bind(INJECT_GITLAB).toInstance(client);
|
2020-08-28 22:34:31 +00:00
|
|
|
|
2020-08-28 23:51:46 +00:00
|
|
|
const remote = await container.create(GitlabRemote, REMOTE_OPTIONS);
|
2020-08-28 22:34:31 +00:00
|
|
|
const status = await remote.connect();
|
|
|
|
expect(status).to.equal(true);
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
color: '',
|
|
|
|
desc: '',
|
|
|
|
name: 'foo',
|
|
|
|
project: '',
|
|
|
|
};
|
|
|
|
const result = await remote.createLabel(data);
|
|
|
|
|
|
|
|
expect(result).to.include(data);
|
2021-08-04 03:45:24 +00:00
|
|
|
expect(createStub).to.have.callCount(1).and.been.calledWithMatch(0, 'foo');
|
2020-08-28 22:34:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not create labels when dryrun=true', async () => {
|
2020-08-29 00:06:31 +00:00
|
|
|
const { container, module } = await createRemoteContainer();
|
2020-08-28 22:34:31 +00:00
|
|
|
|
2021-08-04 03:45:24 +00:00
|
|
|
const client = new Gitlab({});
|
2020-08-28 23:51:46 +00:00
|
|
|
stub(client.Projects, 'show').returns(Promise.resolve(STUB_PROJECT));
|
2020-08-28 22:34:31 +00:00
|
|
|
const createStub = stub(client.Labels, 'create');
|
2021-08-04 03:45:24 +00:00
|
|
|
module.bind(INJECT_GITLAB).toInstance(client);
|
2020-08-28 22:34:31 +00:00
|
|
|
|
2020-08-28 23:51:46 +00:00
|
|
|
const remote = await container.create(GitlabRemote, DRYRUN_OPTIONS);
|
2020-08-28 22:34:31 +00:00
|
|
|
const status = await remote.connect();
|
|
|
|
expect(status).to.equal(true);
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
color: '',
|
|
|
|
desc: '',
|
|
|
|
name: 'foo',
|
|
|
|
project: '',
|
|
|
|
};
|
|
|
|
const result = await remote.createLabel(data);
|
|
|
|
|
|
|
|
expect(result).to.include(data);
|
|
|
|
expect(createStub).to.have.callCount(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('delete label endpoint', () => {
|
|
|
|
it('should delete labels when dryrun=false', async () => {
|
2020-08-29 00:06:31 +00:00
|
|
|
const { container, module } = await createRemoteContainer();
|
2020-08-28 22:34:31 +00:00
|
|
|
|
2021-08-04 03:45:24 +00:00
|
|
|
const client = new Gitlab({});
|
2020-08-28 23:51:46 +00:00
|
|
|
stub(client.Projects, 'show').returns(Promise.resolve(STUB_PROJECT));
|
2020-08-28 22:34:31 +00:00
|
|
|
const removeStub = stub(client.Labels, 'remove');
|
2021-08-04 03:45:24 +00:00
|
|
|
module.bind(INJECT_GITLAB).toInstance(client);
|
2020-08-28 22:34:31 +00:00
|
|
|
|
2020-08-28 23:51:46 +00:00
|
|
|
const remote = await container.create(GitlabRemote, REMOTE_OPTIONS);
|
2020-08-28 22:34:31 +00:00
|
|
|
|
|
|
|
const status = await remote.connect();
|
|
|
|
expect(status).to.equal(true);
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
color: '',
|
|
|
|
desc: '',
|
|
|
|
name: 'foo',
|
|
|
|
project: '',
|
|
|
|
};
|
|
|
|
const result = await remote.deleteLabel(data);
|
|
|
|
|
|
|
|
expect(result).to.include(data);
|
2021-08-04 03:45:24 +00:00
|
|
|
expect(removeStub).to.have.callCount(1).and.been.calledWithMatch(0, 'foo');
|
2020-08-28 22:34:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not delete labels when dryrun=true', async () => {
|
2020-08-29 00:06:31 +00:00
|
|
|
const { container, module } = await createRemoteContainer();
|
2020-08-28 22:34:31 +00:00
|
|
|
|
2021-08-04 03:45:24 +00:00
|
|
|
const client = new Gitlab({});
|
2020-08-28 23:51:46 +00:00
|
|
|
stub(client.Projects, 'show').returns(Promise.resolve(STUB_PROJECT));
|
2020-08-28 22:34:31 +00:00
|
|
|
const removeStub = stub(client.Labels, 'remove');
|
2021-08-04 03:45:24 +00:00
|
|
|
module.bind(INJECT_GITLAB).toInstance(client);
|
2020-08-28 22:34:31 +00:00
|
|
|
|
2020-08-28 23:51:46 +00:00
|
|
|
const remote = await container.create(GitlabRemote, DRYRUN_OPTIONS);
|
2020-08-28 22:34:31 +00:00
|
|
|
|
|
|
|
const status = await remote.connect();
|
|
|
|
expect(status).to.equal(true);
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
color: '',
|
|
|
|
desc: '',
|
|
|
|
name: 'foo',
|
|
|
|
project: '',
|
|
|
|
};
|
|
|
|
const result = await remote.deleteLabel(data);
|
|
|
|
|
|
|
|
expect(result).to.include(data);
|
|
|
|
expect(removeStub).to.have.callCount(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('list issues endpoint', () => {
|
|
|
|
it('should list issues when dryrun=*', async () => {
|
2020-08-29 00:06:31 +00:00
|
|
|
const { container, module } = await createRemoteContainer();
|
2020-08-28 22:34:31 +00:00
|
|
|
|
2021-08-04 03:45:24 +00:00
|
|
|
const client = new Gitlab({});
|
2020-08-28 23:51:46 +00:00
|
|
|
stub(client.Projects, 'show').returns(Promise.resolve(STUB_PROJECT));
|
2020-08-28 22:34:31 +00:00
|
|
|
const listStub = stub(client.Issues, 'all').returns(Promise.resolve([]));
|
2021-08-04 03:45:24 +00:00
|
|
|
module.bind(INJECT_GITLAB).toInstance(client);
|
2020-08-28 22:34:31 +00:00
|
|
|
|
|
|
|
for (const dryrun of [true, false]) {
|
|
|
|
const remote = await container.create(GitlabRemote, {
|
2020-08-28 23:51:46 +00:00
|
|
|
...REMOTE_OPTIONS,
|
2020-08-28 22:34:31 +00:00
|
|
|
dryrun,
|
|
|
|
});
|
|
|
|
|
|
|
|
const status = await remote.connect();
|
|
|
|
expect(status).to.equal(true);
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
project: '',
|
|
|
|
};
|
|
|
|
const result = await remote.listIssues(data);
|
|
|
|
|
|
|
|
expect(result).to.deep.equal([]);
|
|
|
|
expect(listStub).to.have.callCount(1);
|
|
|
|
listStub.resetHistory();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('list labels endpoint', () => {
|
|
|
|
it('should list labels when dryrun=*', async () => {
|
2020-08-29 00:06:31 +00:00
|
|
|
const { container, module } = await createRemoteContainer();
|
2020-08-28 22:34:31 +00:00
|
|
|
|
2021-08-04 03:45:24 +00:00
|
|
|
const client = new Gitlab({});
|
2020-08-28 23:51:46 +00:00
|
|
|
stub(client.Projects, 'show').returns(Promise.resolve(STUB_PROJECT));
|
2020-08-28 22:34:31 +00:00
|
|
|
const listStub = stub(client.Labels, 'all').returns(Promise.resolve([]));
|
2021-08-04 03:45:24 +00:00
|
|
|
module.bind(INJECT_GITLAB).toInstance(client);
|
2020-08-28 22:34:31 +00:00
|
|
|
|
|
|
|
for (const dryrun of [true, false]) {
|
|
|
|
const remote = await container.create(GitlabRemote, {
|
2020-08-28 23:51:46 +00:00
|
|
|
...REMOTE_OPTIONS,
|
2020-08-28 22:34:31 +00:00
|
|
|
dryrun,
|
|
|
|
});
|
|
|
|
|
|
|
|
const status = await remote.connect();
|
|
|
|
expect(status).to.equal(true);
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
project: '',
|
|
|
|
};
|
|
|
|
const result = await remote.listLabels(data);
|
|
|
|
|
|
|
|
expect(result).to.deep.equal([]);
|
|
|
|
expect(listStub).to.have.callCount(1);
|
|
|
|
listStub.resetHistory();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2020-08-28 23:51:46 +00:00
|
|
|
|
|
|
|
describe('update issue endpoint', () => {
|
|
|
|
it('should update issues when dryrun=false', async () => {
|
2020-08-29 00:06:31 +00:00
|
|
|
const { container, module } = await createRemoteContainer();
|
2020-08-28 23:51:46 +00:00
|
|
|
|
2021-08-04 03:45:24 +00:00
|
|
|
const client = new Gitlab({});
|
2020-08-28 23:51:46 +00:00
|
|
|
stub(client.Projects, 'show').returns(Promise.resolve(STUB_PROJECT));
|
|
|
|
const updateStub = stub(client.Issues, 'edit');
|
2021-08-04 03:45:24 +00:00
|
|
|
module.bind(INJECT_GITLAB).toInstance(client);
|
2020-08-28 23:51:46 +00:00
|
|
|
|
|
|
|
const remote = await container.create(GitlabRemote, REMOTE_OPTIONS);
|
|
|
|
const status = await remote.connect();
|
|
|
|
expect(status).to.equal(true);
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
issue: '1',
|
|
|
|
labels: [],
|
|
|
|
name: 'bar',
|
|
|
|
project: 'foo',
|
|
|
|
};
|
|
|
|
const result = await remote.updateIssue(data);
|
|
|
|
|
|
|
|
expect(result).to.include(data);
|
2021-08-04 03:45:24 +00:00
|
|
|
expect(updateStub).to.have.callCount(1).and.been.calledWithMatch(0, 1);
|
2020-08-28 23:51:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not update issues when dryrun=true', async () => {
|
2020-08-29 00:06:31 +00:00
|
|
|
const { container, module } = await createRemoteContainer();
|
2020-08-28 23:51:46 +00:00
|
|
|
|
2021-08-04 03:45:24 +00:00
|
|
|
const client = new Gitlab({});
|
2020-08-28 23:51:46 +00:00
|
|
|
stub(client.Projects, 'show').returns(Promise.resolve(STUB_PROJECT));
|
|
|
|
const updateStub = stub(client.Issues, 'edit');
|
2021-08-04 03:45:24 +00:00
|
|
|
module.bind(INJECT_GITLAB).toInstance(client);
|
2020-08-28 23:51:46 +00:00
|
|
|
|
|
|
|
const remote = await container.create(GitlabRemote, DRYRUN_OPTIONS);
|
|
|
|
const status = await remote.connect();
|
|
|
|
expect(status).to.equal(true);
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
issue: 'foo',
|
|
|
|
labels: [],
|
|
|
|
name: '',
|
|
|
|
project: '',
|
|
|
|
};
|
|
|
|
const result = await remote.updateIssue(data);
|
|
|
|
|
|
|
|
expect(result).to.include(data);
|
|
|
|
expect(updateStub).to.have.callCount(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('update label endpoint', () => {
|
|
|
|
it('should update labels when dryrun=false', async () => {
|
2020-08-29 00:06:31 +00:00
|
|
|
const { container, module } = await createRemoteContainer();
|
2020-08-28 23:51:46 +00:00
|
|
|
|
2021-08-04 03:45:24 +00:00
|
|
|
const client = new Gitlab({});
|
2020-08-28 23:51:46 +00:00
|
|
|
stub(client.Projects, 'show').returns(Promise.resolve(STUB_PROJECT));
|
|
|
|
const updateStub = stub(client.Labels, 'edit').returns(Promise.resolve({
|
|
|
|
color: 'red',
|
|
|
|
default: false,
|
|
|
|
description: 'bar',
|
|
|
|
id: 0,
|
|
|
|
name: 'foo',
|
|
|
|
url: '',
|
2021-08-04 03:45:24 +00:00
|
|
|
/* eslint-disable camelcase */
|
|
|
|
text_color: '',
|
|
|
|
description_html: '',
|
|
|
|
open_issues_count: 0,
|
|
|
|
closed_issues_count: 0,
|
|
|
|
open_merge_requests_count: 0,
|
|
|
|
subscribed: false,
|
|
|
|
priority: 0,
|
|
|
|
is_project_label: false,
|
2020-08-28 23:51:46 +00:00
|
|
|
}));
|
2021-08-04 03:45:24 +00:00
|
|
|
module.bind(INJECT_GITLAB).toInstance(client);
|
2020-08-28 23:51:46 +00:00
|
|
|
|
|
|
|
const remote = await container.create(GitlabRemote, REMOTE_OPTIONS);
|
|
|
|
const status = await remote.connect();
|
|
|
|
expect(status).to.equal(true);
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
color: 'red',
|
|
|
|
desc: 'bar',
|
|
|
|
name: 'foo',
|
|
|
|
project: '',
|
|
|
|
};
|
|
|
|
const result = await remote.updateLabel(data);
|
|
|
|
|
|
|
|
expect(result).to.include(data);
|
2021-08-04 03:45:24 +00:00
|
|
|
expect(updateStub).to.have.callCount(1).and.been.calledWithMatch(0, 'foo');
|
2020-08-28 23:51:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not update labels when dryrun=true', async () => {
|
2020-08-29 00:06:31 +00:00
|
|
|
const { container, module } = await createRemoteContainer();
|
2020-08-28 23:51:46 +00:00
|
|
|
|
2021-08-04 03:45:24 +00:00
|
|
|
const client = new Gitlab({});
|
2020-08-28 23:51:46 +00:00
|
|
|
stub(client.Projects, 'show').returns(Promise.resolve(STUB_PROJECT));
|
|
|
|
const updateStub = stub(client.Labels, 'edit');
|
2021-08-04 03:45:24 +00:00
|
|
|
module.bind(INJECT_GITLAB).toInstance(client);
|
2020-08-28 23:51:46 +00:00
|
|
|
|
|
|
|
const remote = await container.create(GitlabRemote, DRYRUN_OPTIONS);
|
|
|
|
const status = await remote.connect();
|
|
|
|
expect(status).to.equal(true);
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
color: '',
|
|
|
|
desc: '',
|
|
|
|
name: 'foo',
|
|
|
|
project: '',
|
|
|
|
};
|
|
|
|
const result = await remote.updateLabel(data);
|
|
|
|
|
|
|
|
expect(result).to.include(data);
|
|
|
|
expect(updateStub).to.have.callCount(0);
|
|
|
|
});
|
|
|
|
});
|
2020-08-15 04:40:56 +00:00
|
|
|
});
|