Skip to content

Commit 45bf8a6

Browse files
committed
fix(cli): better branch attribution in travis
1 parent 09d7bdd commit 45bf8a6

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

packages/cli/src/upload/upload.js

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const childProcess = require('child_process');
1111
const ApiClient = require('@lhci/utils/src/api-client.js');
1212
const {getSavedLHRs} = require('@lhci/utils/src/saved-reports.js');
1313

14+
const envVars = process.env;
15+
1416
/**
1517
* @param {import('yargs').Argv} yargs
1618
*/
@@ -28,9 +30,12 @@ function buildCommand(yargs) {
2830
* @return {string}
2931
*/
3032
function getCurrentHash() {
31-
if (process.env.TRAVIS_COMMIT) return process.env.TRAVIS_COMMIT;
33+
if (envVars.TRAVIS_PULL_REQUEST_SHA) return envVars.TRAVIS_PULL_REQUEST_SHA;
34+
if (envVars.TRAVIS_COMMIT) return envVars.TRAVIS_COMMIT;
3235

33-
const result = childProcess.spawnSync('git', ['rev-parse', 'HEAD'], {encoding: 'utf8'});
36+
const result = childProcess.spawnSync('git', ['rev-list', '--no-merges', '-n', '1', 'HEAD'], {
37+
encoding: 'utf8',
38+
});
3439
if (result.status !== 0) {
3540
throw new Error('Unable to determine current hash with `git rev-parse HEAD`');
3641
}
@@ -42,7 +47,8 @@ function getCurrentHash() {
4247
* @return {string}
4348
*/
4449
function getCurrentBranch() {
45-
if (process.env.TRAVIS_BRANCH) return process.env.TRAVIS_BRANCH.slice(0, 40);
50+
if (envVars.TRAVIS_PULL_REQUEST_BRANCH) return envVars.TRAVIS_PULL_REQUEST_BRANCH.slice(0, 40);
51+
if (envVars.TRAVIS_BRANCH) return envVars.TRAVIS_BRANCH.slice(0, 40);
4652

4753
const result = childProcess.spawnSync('git', ['rev-parse', '--abbrev-ref', 'HEAD'], {
4854
encoding: 'utf8',
@@ -58,16 +64,15 @@ function getCurrentBranch() {
5864
* @return {string}
5965
*/
6066
function getExternalBuildUrl() {
61-
return process.env.TRAVIS_BUILD_WEB_URL || '';
67+
return envVars.TRAVIS_BUILD_WEB_URL || '';
6268
}
6369

6470
/**
71+
* @param {string} hash
6572
* @return {string}
6673
*/
67-
function getCommitMessage() {
68-
if (process.env.TRAVIS_COMMIT_MESSAGE) return process.env.TRAVIS_COMMIT_MESSAGE.slice(0, 80);
69-
70-
const result = childProcess.spawnSync('git', ['log', '--format=%s', '-n', '1'], {
74+
function getCommitMessage(hash = 'HEAD') {
75+
const result = childProcess.spawnSync('git', ['log', '--format=%s', '-n', '1', hash], {
7176
encoding: 'utf8',
7277
});
7378
if (result.status !== 0) {
@@ -78,10 +83,11 @@ function getCommitMessage() {
7883
}
7984

8085
/**
86+
* @param {string} hash
8187
* @return {string}
8288
*/
83-
function getAuthor() {
84-
const result = childProcess.spawnSync('git', ['log', '--format=%aN <%aE>', '-n', '1'], {
89+
function getAuthor(hash = 'HEAD') {
90+
const result = childProcess.spawnSync('git', ['log', '--format=%aN <%aE>', '-n', '1', hash], {
8591
encoding: 'utf8',
8692
});
8793
if (result.status !== 0) {
@@ -92,20 +98,21 @@ function getAuthor() {
9298
}
9399

94100
/**
101+
* @param {string} hash
95102
* @return {string}
96103
*/
97-
function getAvatarUrl() {
98-
const result = childProcess.spawnSync('git', ['log', '--format=%aE', '-n', '1'], {
104+
function getAvatarUrl(hash = 'HEAD') {
105+
const result = childProcess.spawnSync('git', ['log', '--format=%aE', '-n', '1', hash], {
99106
encoding: 'utf8',
100107
});
101108
if (result.status !== 0) {
102109
throw new Error('Unable to determine commit email with `git log --format=%aE -n 1`');
103110
}
104111

105112
// Use default gravatar image, see https://en.gravatar.com/site/implement/images/.
106-
const hash = crypto.createHash('md5');
107-
hash.update(result.stdout.trim().toLowerCase());
108-
return `https://www.gravatar.com/avatar/${hash.digest('hex')}.jpg?d=identicon`;
113+
const md5 = crypto.createHash('md5');
114+
md5.update(result.stdout.trim().toLowerCase());
115+
return `https://www.gravatar.com/avatar/${md5.digest('hex')}.jpg?d=identicon`;
109116
}
110117

111118
/**
@@ -147,16 +154,17 @@ async function runCommand(options) {
147154
throw new Error('Could not find active project with provided token');
148155
}
149156

157+
const hash = getCurrentHash();
150158
const branch = getCurrentBranch();
151159

152160
const build = await api.createBuild({
153161
projectId: project.id,
154162
lifecycle: 'unsealed',
155-
hash: getCurrentHash(),
163+
hash,
156164
branch,
157-
commitMessage: getCommitMessage(),
158-
author: getAuthor(),
159-
avatarUrl: getAvatarUrl(),
165+
commitMessage: getCommitMessage(hash),
166+
author: getAuthor(hash),
167+
avatarUrl: getAvatarUrl(hash),
160168
ancestorHash: branch === 'master' ? getAncestorHashForMaster() : getAncestorHashForBranch(),
161169
externalBuildUrl: getExternalBuildUrl(),
162170
runAt: new Date().toISOString(),

0 commit comments

Comments
 (0)