-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReportHelper.js
91 lines (79 loc) · 2.34 KB
/
ReportHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const Git = require('nodegit');
const Path = require('path');
const lhrc_options = require('../lighthouserc.js');
const ApiClient = require('@lhci/utils/src/api-client.js');
/**
* @param {LHCI.UploadCommand.Options} options
* @return {Promise<void>}
*/
module.exports = async function uploadToLhciServer(lhr, options = false) {
if ( !options ) {
options = lhrc_options.ci.upload;
}
if (!options.token) { throw new Error('Must provide token for LHCI target'); }
const api = new ApiClient({...options, rootURL: options.serverBaseUrl});
api.setBuildToken(options.token);
const project = await api.findProjectByToken(options.token);
if (!project) {
throw new Error('Could not find active project with provided token');
}
const repo = await Git.Repository.open(Path.resolve(__dirname, '../'));
const head_commit = await repo.getHeadCommit();
const branch = 'master';
const hash = await head_commit.sha();
const authoSignature = await head_commit.author();
const commitMessage = await head_commit.message();
const commitDate = await head_commit.date();
const author = await authoSignature.name();
const ancestorHash = false;
const runDate = new Date();
const build = await api.createBuild({
projectId: project.id,
lifecycle: 'unsealed',
hash,
branch,
ancestorHash,
commitMessage: commitMessage,
author: author,
avatarUrl: '',
externalBuildUrl: '',
runAt: runDate.toISOString(),
committedAt: commitDate.toISOString(),
ancestorCommittedAt: ancestorHash ? undefined : undefined,
});
const targetUrlMap = new Map();
const buildViewUrl = new URL(
`/app/projects/${project.slug}/compare/${build.id}`,
options.serverBaseUrl
);
const parsedLHR = lhr;
const url = parsedLHR.finalUrl;
const run = await api.createRun({
projectId: project.id,
buildId: build.id,
representative: false,
url,
lhr: JSON.stringify(lhr),
});
buildViewUrl.searchParams.set('compareUrl', url);
targetUrlMap.set(parsedLHR.finalUrl, buildViewUrl.href);
buildViewUrl.searchParams.delete('compareUrl');
await api.sealBuild(build.projectId, build.id);
const info = {
options,
project,
build,
run: {
id: run.id,
date: runDate
},
commit: {
branch,
hash,
commitMessage,
commitDate,
author
},
};
return info;
}