Skip to content

Commit b215aa4

Browse files
committed
feat: include commit sha in output
1 parent 2914d69 commit b215aa4

File tree

5 files changed

+95
-9
lines changed

5 files changed

+95
-9
lines changed

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ exports.detect = async ({ path, repository, packageName }) => {
1212

1313
result.name = packageInfo.name;
1414
result.version = packageInfo.version;
15+
result.commit = await packageInfo.getCommit();
1516

1617
const travis = await Travis.detect(packageInfo);
1718

lib/loader.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
const Fs = require('fs');
44
const GitUrlParse = require('git-url-parse');
55
const Path = require('path');
6-
const SimpleGit = require('simple-git/promise');
76
const Wreck = require('@hapi/wreck');
87

8+
const Utils = require('./utils');
99

1010
const internals = {};
1111

@@ -15,6 +15,15 @@ internals.createRepositoryLoader = (repository) => {
1515
const parsedRepository = GitUrlParse(repository);
1616

1717
return {
18+
getCommit: async () => {
19+
20+
const simpleGit = Utils.simpleGit();
21+
const httpRepository = GitUrlParse.stringify(parsedRepository, 'http');
22+
const result = await simpleGit.listRemote([httpRepository, 'HEAD']);
23+
const [head] = result.split(/\s+/);
24+
25+
return head;
26+
},
1827
loadFile: async (filename) => {
1928

2029
if (parsedRepository.source !== 'github.com') {
@@ -33,13 +42,18 @@ internals.createRepositoryLoader = (repository) => {
3342

3443
internals.createPathLoader = async (path) => {
3544

36-
const isRepo = await SimpleGit(path).checkIsRepo();
45+
const simpleGit = Utils.simpleGit(path);
46+
const isRepo = await simpleGit.checkIsRepo();
3747

3848
if (!isRepo) {
3949
throw new Error(`${path} is not a git repository`);
4050
}
4151

4252
return {
53+
getCommit: () => {
54+
55+
return simpleGit.revparse(['HEAD']);
56+
},
4357
loadFile: (filename) => {
4458

4559
const fullPath = Path.join(path, filename);

lib/package.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@ const Loader = require('./loader');
55

66
exports.detect = async ({ path, repository, packageName }) => {
77

8-
const { loadFile } = await Loader.create({ path, repository, packageName });
8+
const { loadFile, getCommit } = await Loader.create({ path, repository, packageName });
99

1010
const packageJson = JSON.parse((await loadFile('package.json')).toString());
1111

1212
const { name, version, engines } = packageJson;
1313

14-
return { name, version, engines, loadFile };
14+
return {
15+
name,
16+
version,
17+
engines,
18+
19+
getCommit,
20+
loadFile
21+
};
1522
};

lib/utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
const SimpleGit = require('simple-git/promise');
4+
5+
/* $lab:coverage:off$ */
6+
// this is wrapped primarily to be able to stub it
7+
exports.simpleGit = (...args) => SimpleGit(...args);
8+
/* $lab:coverage:on$ */

test/index.js

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const Tmp = require('tmp');
99

1010
const NodeSupport = require('..');
1111

12+
const Utils = require('../lib/utils');
13+
1214

1315
const { describe, it, beforeEach, afterEach } = exports.lab = require('@hapi/lab').script();
1416
const { expect } = require('@hapi/code');
@@ -24,10 +26,6 @@ internals.prepareFixture = async ({ travisYml, packageJson, git = true } = {}) =
2426

2527
internals.tmpObjects.push(tmpObj);
2628

27-
if (git) {
28-
await SimpleGit(tmpObj.name).init();
29-
}
30-
3129
if (travisYml) {
3230
Fs.copyFileSync(Path.join(__dirname, 'fixtures', travisYml), Path.join(tmpObj.name, '.travis.yml'));
3331
}
@@ -37,15 +35,41 @@ internals.prepareFixture = async ({ travisYml, packageJson, git = true } = {}) =
3735
version: '0.0.0-development'
3836
}));
3937

38+
if (git) {
39+
const simpleGit = SimpleGit(tmpObj.name);
40+
await simpleGit.init();
41+
await simpleGit.add('./*');
42+
await simpleGit.commit('initial commit', ['--no-gpg-sign']);
43+
}
44+
4045
return tmpObj.name;
4146
};
4247

4348

49+
internals.assertCommit = (result) => {
50+
51+
expect(result.commit).to.match(/^[0-9a-f]{40}$/);
52+
delete result.commit;
53+
};
54+
4455
describe('node-support', () => {
4556

57+
let listRemoteStub;
58+
4659
beforeEach(() => {
4760

4861
Sinon.useFakeTimers(new Date('2020-02-02T20:00:02Z'));
62+
63+
listRemoteStub = Sinon.stub().throws();
64+
65+
Sinon.stub(Utils, 'simpleGit').callsFake((...args) => {
66+
67+
const simpleGit = SimpleGit(...args);
68+
69+
Sinon.stub(simpleGit, 'listRemote').callsFake(listRemoteStub);
70+
71+
return simpleGit;
72+
});
4973
});
5074

5175
afterEach(() => {
@@ -70,6 +94,8 @@ describe('node-support', () => {
7094

7195
const result = await NodeSupport.detect({ path });
7296

97+
internals.assertCommit(result);
98+
7399
expect(result).to.equal({
74100
name: 'node-support',
75101
version: '0.0.0-development',
@@ -87,6 +113,8 @@ describe('node-support', () => {
87113

88114
const result = await NodeSupport.detect({ path });
89115

116+
internals.assertCommit(result);
117+
90118
expect(result).to.equal({
91119
name: 'test-module',
92120
version: '0.0.0-development',
@@ -102,6 +130,8 @@ describe('node-support', () => {
102130

103131
const result = await NodeSupport.detect({ path });
104132

133+
internals.assertCommit(result);
134+
105135
expect(result).to.equal({
106136
name: 'test-module',
107137
version: '0.0.0-development',
@@ -120,6 +150,8 @@ describe('node-support', () => {
120150

121151
const result = await NodeSupport.detect({ path });
122152

153+
internals.assertCommit(result);
154+
123155
expect(result).to.equal({
124156
name: 'test-module',
125157
version: '0.0.0-development',
@@ -138,6 +170,8 @@ describe('node-support', () => {
138170

139171
const result = await NodeSupport.detect({ path });
140172

173+
internals.assertCommit(result);
174+
141175
expect(result).to.equal({
142176
name: 'test-module',
143177
version: '0.0.0-development',
@@ -156,6 +190,8 @@ describe('node-support', () => {
156190

157191
const result = await NodeSupport.detect({ path });
158192

193+
internals.assertCommit(result);
194+
159195
expect(result).to.equal({
160196
name: 'test-module',
161197
version: '0.0.0-development',
@@ -174,6 +210,8 @@ describe('node-support', () => {
174210

175211
const result = await NodeSupport.detect({ path });
176212

213+
internals.assertCommit(result);
214+
177215
expect(result).to.equal({
178216
name: 'test-module',
179217
version: '0.0.0-development',
@@ -192,6 +230,8 @@ describe('node-support', () => {
192230

193231
const result = await NodeSupport.detect({ path });
194232

233+
internals.assertCommit(result);
234+
195235
expect(result).to.equal({
196236
name: 'test-module',
197237
version: '0.0.0-development',
@@ -210,6 +250,8 @@ describe('node-support', () => {
210250

211251
const result = await NodeSupport.detect({ path });
212252

253+
internals.assertCommit(result);
254+
213255
expect(result).to.equal({
214256
name: 'test-module',
215257
version: '0.0.0-development',
@@ -228,6 +270,8 @@ describe('node-support', () => {
228270

229271
const result = await NodeSupport.detect({ path });
230272

273+
internals.assertCommit(result);
274+
231275
expect(result).to.equal({
232276
name: 'test-module',
233277
version: '0.0.0-development',
@@ -246,6 +290,8 @@ describe('node-support', () => {
246290

247291
const result = await NodeSupport.detect({ path });
248292

293+
internals.assertCommit(result);
294+
249295
expect(result).to.equal({
250296
name: 'test-module',
251297
version: '0.0.0-development',
@@ -264,6 +310,8 @@ describe('node-support', () => {
264310

265311
const result = await NodeSupport.detect({ path });
266312

313+
internals.assertCommit(result);
314+
267315
expect(result).to.equal({
268316
name: 'test-module',
269317
version: '0.0.0-development',
@@ -282,6 +330,8 @@ describe('node-support', () => {
282330

283331
const result = await NodeSupport.detect({ path });
284332

333+
internals.assertCommit(result);
334+
285335
expect(result).to.equal({
286336
name: 'test-module',
287337
version: '0.0.0-development',
@@ -301,7 +351,6 @@ describe('node-support', () => {
301351
});
302352
});
303353

304-
305354
describe('repository', () => {
306355

307356
beforeEach(() => {
@@ -319,6 +368,9 @@ describe('node-support', () => {
319368

320369
it('returns node versions from `.travis.yml` in the repository', async () => {
321370

371+
listRemoteStub
372+
.returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n');
373+
322374
Nock('https://raw.githubusercontent.com')
323375
.get('/pkgjs/node-support/HEAD/package.json')
324376
.reply(200, Fs.readFileSync(Path.join(__dirname, '..', 'package.json')))
@@ -327,9 +379,13 @@ describe('node-support', () => {
327379

328380
const result = await NodeSupport.detect({ repository: 'git+https://github.com/pkgjs/node-support.git' });
329381

382+
expect(listRemoteStub.callCount).to.equal(1);
383+
expect(listRemoteStub.args[0]).to.equal([['http://github.com/pkgjs/node-support.git', 'HEAD']]);
384+
330385
expect(result).to.equal({
331386
name: 'node-support',
332387
version: '0.0.0-development',
388+
commit: '9cef39d21ad229dea4b10295f55b0d9a83800b23',
333389
timestamp: 1580673602000,
334390
travis: {
335391
raw: ['10', '12', '13']

0 commit comments

Comments
 (0)