Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
blond committed May 31, 2016
1 parent 8cdc610 commit a123550
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 50 deletions.
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ const TEAMCITY_BUILD_QUEUE_PATHNAME = 'guestAuth/app/rest/buildQueue';
module.exports = function(teamcityUrl, options) {
assert(isUrl(teamcityUrl), 'You should specify url to TeamCity');

const opts = Object.assing({ teamcityUrl }, options);

return got(url.resolve(teamcityUrl, TEAMCITY_BUILD_QUEUE_PATHNAME))
.then(response => {
const builds = parse(response);

return Promise.all(builds.map(build => loadBuild(build, opts)));
return Promise.all(builds.map(build => {
const buildUrl = url.resolve(teamcityUrl, build.href);

return loadBuild(buildUrl);
}));
})
.then(builds => filter(builds, options))
.then(builds => ({
builds: builds,
count: builds.length
size: builds.length
}));
};
12 changes: 1 addition & 11 deletions lib/load-build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const url = require('url');

const got = require('got');

const loadCompatibleAgents = require('./load-compatible-agents');
Expand All @@ -9,15 +7,7 @@ function loadBuild(buildUrl) {
return got(buildUrl).then(parseBuildsResponse);
}

module.exports = (buildInfo, options) => {
const href = buildInfo.href;

if (!href) {
return Promise.resolve(buildInfo);
}

const buildUrl = href && url.resolve(options.teamcityUrl, href);

module.exports = (buildUrl) => {
return Promise.all([
loadBuild(buildUrl),
loadCompatibleAgents(buildUrl).catch(() => ([])) // If build had time to start,
Expand Down
2 changes: 0 additions & 2 deletions lib/parse-builds-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@ module.exports = (response) => {
if (root.name === 'builds') {
return root.children.map(normalize);
}

return {};
};
51 changes: 51 additions & 0 deletions test/api.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

const test = require('ava');
const proxyquire = require('proxyquire');
const sinon = require('sinon');

test.beforeEach(t => {
t.context.gotStub = sinon.stub().returns(Promise.resolve());
t.context.parseStub = sinon.stub().returns([]);
t.context.loadStub = sinon.stub().returns(Promise.resolve([]));
t.context.filterStub = sinon.stub().returns([]);

t.context.run = proxyquire('../index', {
got: t.context.gotStub,
'./lib/parse-builds-response': t.context.parseStub,
'./lib/load-build': t.context.loadStub,
'./lib/filter-builds': t.context.filterStub
});
});

test('should get info by url', t => {
return t.context.run('http://tc.url')
.then(() => t.true(t.context.gotStub.calledWith('http://tc.url/guestAuth/app/rest/buildQueue')));
});

test('should returns builds of queue', t => {
const builds = [{ id: '1' }];

t.context.filterStub.returns(builds);

return t.context.run('http://tc.url')
.then(queue => t.deepEqual(queue.builds, builds));
});

test('should returns queue size', t => {
const builds = [{ id: '1' }];

t.context.filterStub.returns(builds);

return t.context.run('http://tc.url')
.then(queue => t.is(queue.size, builds.length));
});

test('should load build info', t => {
const builds = [{ href: 'build-url-1' }];

t.context.parseStub.returns(builds);

return t.context.run('http://tc.url')
.then(() => t.true(t.context.loadStub.calledWith('http://tc.url/build-url-1')));
});
59 changes: 26 additions & 33 deletions test/load-build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,43 @@ const proxyquire = require('proxyquire');
const sinon = require('sinon');

test.beforeEach(t => {
t.context.teamcityUrl = 'http://teamcity.my-domain.com';
t.context.build = {
name: 'build',
attributes: {
id: '1',
href: '/guestAuth/app/rest/buildQueue/id:1'
}
};

t.context.gotStub = sinon.stub().returns(Promise.resolve());
t.context.parseStub = sinon.stub().returns({ root: { name: 'new-build' } });
t.context.parseStub = sinon.stub().returns({});
t.context.loadAgentsStub = sinon.stub().returns(Promise.resolve([]));

t.context.loadBuildInfo = proxyquire('../lib/load-build-info', {
t.context.load = proxyquire('../lib/load-build', {
got: t.context.gotStub,
'./parse-response': t.context.parseStub
'./parse-builds-response': t.context.parseStub,
'./load-compatible-agents': t.context.loadAgentsStub
});
});

test('should return source info if no href attribute', t => {
const build = {
root: {
name: 'build',
attributes: { id: '1' }
}
};
test('should return build info', t => {
t.context.parseStub.returns({ id: '1' });

return t.context.loadBuildInfo(build, t.context.teamcityUrl)
.then(info => t.deepEqual(info, build.root));
return t.context.load('build_url')
.then(info => t.is(info.id, '1'));
});

test('should load build info', t => {
return t.context.loadBuildInfo(t.context.build, t.context.teamcityUrl)
.then(() => {
const buildUrl = `${t.context.teamcityUrl}/guestAuth/app/rest/buildQueue/id:1`;
const agentsUrl = `${t.context.teamcityUrl}/guestAuth/app/rest/buildQueue/id:1/compatibleAgents`;
test('should add empty field if no compatible agents', t => {
t.context.loadAgentsStub.returns(Promise.resolve([]));

return t.context.load('build_url')
.then(info => t.deepEqual(info.compatibleAgents, []));
});

test('should add empty field with compatible agents', t => {
const agents = [{ id: '1' }];

t.context.loadAgentsStub.returns(Promise.resolve(agents));

t.true(t.context.gotStub.calledWith(buildUrl));
t.true(t.context.gotStub.calledWith(agentsUrl));
});
return t.context.load('build_url')
.then(info => t.deepEqual(info.compatibleAgents, agents));
});

test('should return build info if no info about compatible agents', t => {
t.context.gotStub.onCall(1).returns(Promise.reject());
test('should not throw if an error occurred while getting compatible agents', t => {
t.context.loadAgentsStub.returns(Promise.reject());

return t.context.loadBuildInfo(t.context.build, t.context.teamcityUrl)
.then(info => t.deepEqual(info, { name: 'new-build' }));
return t.context.load('build_url')
.then(info => t.deepEqual(info.compatibleAgents, []));
});

0 comments on commit a123550

Please sign in to comment.