Skip to content

Commit 41bfbb4

Browse files
committed
Move fetchCommit back to serve.js
1 parent c4c872a commit 41bfbb4

File tree

4 files changed

+78
-80
lines changed

4 files changed

+78
-80
lines changed

lib.js

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,6 @@ function isSHA(id) {
4646
}
4747

4848

49-
/**
50-
* Fetch a full-length SHA commit corresponding to a given ID.
51-
* @param {string} id - A commit SHA of any length, or branch name.
52-
* @param {boolean|null} [isBranch] - If true, id treated as a branch name. Inferred from id by default.
53-
* @param {string} [module] - (Sub)module name. REPO_NAME by default.
54-
* @return {Promise} - Resolved to full commit SHA.
55-
*/
56-
function fetchCommit(id, isBranch = null, module) {
57-
isBranch = isBranch === null ? !isSHA(id) : isBranch;
58-
const data = {
59-
owner: process.env['REPO_OWNER'],
60-
repo: module || process.env.REPO_NAME,
61-
id: id
62-
};
63-
let endpoint = `GET /repos/:owner/:repo/${isBranch ? 'branches' : 'commits'}/:id`;
64-
return request(endpoint, data).then(response => {
65-
return isBranch ? response.data.commit.sha : response.data.sha;
66-
});
67-
}
68-
69-
7049
/**
7150
* Returns a full filepath. Plays nicely with ~.
7251
* @param {String} p - Path to resolve.
@@ -359,13 +338,13 @@ function startJobTimer(job, kill_children = false) {
359338

360339
/**
361340
* Set dynamic env variables for node-coveralls.
341+
* NB: This does not support submodules.
362342
* @param {Object} job - The Job with an associated process in the data field.
363343
*/
364344
async function initCoveralls(job) {
365345
log.extend('pipeline')('Setting COVERALLS env variables');
366346
process.env.COVERALLS_SERVICE_NAME = job.data.context;
367-
// todo check if submodule
368-
process.env.COVERALLS_GIT_COMMIT = await fetchCommit(job.data.sha);
347+
process.env.COVERALLS_GIT_COMMIT = job.data.sha;
369348
process.env.COVERALLS_SERVICE_JOB_ID = job.id;
370349
}
371350

@@ -756,5 +735,5 @@ module.exports = {
756735
ensureArray, loadTestRecords, compareCoverage, computeCoverage, getBadgeData, log, shortID,
757736
openTunnel, APIError, queue, partial, startJobTimer, updateJobFromRecord, shortCircuit, isSHA,
758737
fullpath, strToBool, saveTestRecords, listSubmodules, getRepoPath, addParam, context2routine,
759-
buildRoutine, fetchCommit
738+
buildRoutine
760739
};

serve.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,34 @@ srv.post('/github', async (req, res, next) => {
120120

121121
///////////////////// STATUS DETAILS /////////////////////
122122

123+
/**
124+
* Serve the test records for requested commit id. Returns JSON data for the commit.
125+
* @param {string} id - A commit SHA of any length, or branch name.
126+
* @param {boolean|null} [isBranch] - If true, id treated as a branch name. Inferred from id by default.
127+
* @param {string} [module] - (Sub)module name. REPO_NAME by default.
128+
* @return {Promise} - Resolved to full commit SHA.
129+
*/
130+
function fetchCommit(id, isBranch = null, module) {
131+
isBranch = isBranch === null ? !lib.isSHA(id) : isBranch;
132+
const data = {
133+
owner: process.env['REPO_OWNER'],
134+
repo: module || process.env.REPO_NAME,
135+
id: id
136+
};
137+
let endpoint = `GET /repos/:owner/:repo/${isBranch ? 'branches' : 'commits'}/:id`;
138+
return request(endpoint, data).then(response => {
139+
return isBranch ? response.data.commit.sha : response.data.sha;
140+
});
141+
}
142+
123143
/**
124144
* Parse the short SHA or branch name and redirect to static reports directory.
125145
*/
126146
srv.get(`/coverage/:id`, (req, res) => {
127147
let id = lib.shortID(req.params.id);
128148
let isSHA = (req.query.branch || !lib.isSHA(req.params.id)) === false;
129149
console.log('Request for test coverage for ' + (isSHA ? `commit ${id}` : `branch ${req.params.id}`));
130-
lib.fetchCommit(req.params.id, !isSHA, req.query.module)
150+
fetchCommit(req.params.id, !isSHA, req.query.module)
131151
.then(id => {
132152
log('Commit ID found: %s', id);
133153
res.redirect(301, `/${ENDPOINT}/coverage/${id}`);
@@ -158,7 +178,7 @@ srv.get(`/${ENDPOINT}/records/:id`, function (req, res) {
158178
let id = lib.shortID(req.params.id);
159179
let isSHA = (req.query.branch || !lib.isSHA(req.params.id)) === false;
160180
console.log('Request for test records for ' + (isSHA ? `commit ${id}` : `branch ${req.params.id}`));
161-
lib.fetchCommit(req.params.id, !isSHA, req.query.module)
181+
fetchCommit(req.params.id, !isSHA, req.query.module)
162182
.then(id => {
163183
log('Commit ID found: %s', id);
164184
let record = lib.loadTestRecords(id);
@@ -203,7 +223,7 @@ srv.get(`/${ENDPOINT}/:id`, function (req, res) {
203223
`Request for test ${log_only ? 'log' : 'stdout'} for ` +
204224
(isSHA ? `commit ${id}` : `branch ${req.params.id}`)
205225
);
206-
lib.fetchCommit(req.params.id, !isSHA, req.query.module)
226+
fetchCommit(req.params.id, !isSHA, req.query.module)
207227
.then(id => res.redirect(301, '/log/' + id))
208228
.catch(err => {
209229
log('%s', err.message);
@@ -331,7 +351,7 @@ srv.get('/:badge/:repo/:id', async (req, res) => {
331351
}
332352
let isSHA = lib.isSHA(req.params.id);
333353
// Find head commit of branch
334-
return lib.fetchCommit(req.params.id, !isSHA, req.params.repo)
354+
return fetchCommit(req.params.id, !isSHA, req.params.repo)
335355
.then(id => {
336356
data['context'] = context;
337357
data['sha'] = id;
@@ -603,4 +623,4 @@ queue.on('finish', (err, job) => { // On job end post result to API
603623
});
604624
});
605625

606-
module.exports = {updateStatus, srv, handler, setAccessToken, eventCallback};
626+
module.exports = {updateStatus, srv, handler, setAccessToken, eventCallback, fetchCommit};

test/lib.test.js

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const expect = require('chai').expect;
1111
const lib = require('../lib');
1212
const queue = require('../lib').queue;
1313
const {stdErr} = require('./fixtures/static');
14-
const nock = require('nock'); // for mocking outbound requests
1514

1615
ids = [
1716
'cabe27e5c8b8cb7cdc4e152f1cf013a89adc7a71',
@@ -55,55 +54,6 @@ describe('strToBool function', () => {
5554
});
5655

5756

58-
/**
59-
* This tests the fetchCommit function. When provided an incomplete SHA or branch name, it should
60-
* return the full commit hash.
61-
*/
62-
describe('fetchCommit', () => {
63-
var scope; // Our server mock
64-
65-
before(function () {
66-
scope = nock('https://api.github.com');
67-
});
68-
69-
after(function () {
70-
nock.cleanAll();
71-
});
72-
73-
it('expect full SHA from short id', (done) => {
74-
const id = ids[0].slice(0, 7);
75-
scope.get(`/repos/${process.env.REPO_OWNER}/${process.env.REPO_NAME}/commits/${id}`)
76-
.reply(200, {sha: ids[0]});
77-
// Check full ID returned
78-
lib.fetchCommit(id)
79-
.then(id => {
80-
expect(id).eq(ids[0]);
81-
scope.done();
82-
done();
83-
});
84-
});
85-
86-
it('expect full SHA from branch and module', (done) => {
87-
const branch = 'develop';
88-
const repo = 'foobar';
89-
scope.get(`/repos/${process.env.REPO_OWNER}/${repo}/branches/${branch}`)
90-
.reply(200, {
91-
commit: {
92-
sha: ids[0]
93-
}
94-
});
95-
// Check full ID returned
96-
lib.fetchCommit(branch, true, repo)
97-
.then(id => {
98-
expect(id).eq(ids[0]);
99-
scope.done();
100-
done();
101-
});
102-
});
103-
104-
});
105-
106-
10757
/**
10858
* A test for the function partial. Should curry function input.
10959
*/

test/serve.test.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const assert = require('chai').assert;
88
const appAuth = require('@octokit/auth-app');
99

1010
const APIError = require('../lib').APIError;
11-
const {updateStatus, setAccessToken, eventCallback, srv} = require('../serve');
11+
const {updateStatus, setAccessToken, eventCallback, srv, fetchCommit} = require('../serve');
1212
const queue = require('../lib').queue;
1313
const config = require('../config/config').settings;
1414
const {token} = require('./fixtures/static');
@@ -793,6 +793,55 @@ describe('logs endpoint', () => {
793793
});
794794

795795

796+
/**
797+
* This tests the fetchCommit function. When provided an incomplete SHA or branch name, it should
798+
* return the full commit hash.
799+
*/
800+
describe('fetchCommit', () => {
801+
var scope; // Our server mock
802+
803+
before(function () {
804+
scope = nock('https://api.github.com');
805+
});
806+
807+
after(function () {
808+
nock.cleanAll();
809+
});
810+
811+
it('expect full SHA from short id', (done) => {
812+
const id = SHA.slice(0, 7);
813+
scope.get(`/repos/${process.env.REPO_OWNER}/${process.env.REPO_NAME}/commits/${id}`)
814+
.reply(200, {sha: SHA});
815+
// Check full ID returned
816+
fetchCommit(id)
817+
.then(id => {
818+
expect(id).eq(SHA);
819+
scope.done();
820+
done();
821+
});
822+
});
823+
824+
it('expect full SHA from branch and module', (done) => {
825+
const branch = 'develop';
826+
const repo = 'foobar';
827+
scope.get(`/repos/${process.env.REPO_OWNER}/${repo}/branches/${branch}`)
828+
.reply(200, {
829+
commit: {
830+
sha: SHA
831+
}
832+
});
833+
// Check full ID returned
834+
fetchCommit(branch, true, repo)
835+
.then(id => {
836+
expect(id).eq(SHA);
837+
scope.done();
838+
done();
839+
});
840+
});
841+
842+
});
843+
844+
796845
/**
797846
* This tests the logs/records endpoint. When provided a SHA it should return the corresponding
798847
* JSON record.

0 commit comments

Comments
 (0)