-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
98 lines (98 loc) · 4.48 KB
/
index.js
File metadata and controls
98 lines (98 loc) · 4.48 KB
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
92
93
94
95
96
97
98
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core = require('@actions/core');
const github = require('@actions/github');
const request = require('request-promise-native');
const dateFormat = require('dateformat');
const token = require('@highwaythree/jira-github-actions-common');
async function submitBuildInfo(accessToken) {
const cloudInstanceBaseUrl = core.getInput('cloud-instance-base-url');
const cloudURL = new URL('/_edge/tenant_info', cloudInstanceBaseUrl);
let cloudId = await request(cloudURL.href);
cloudId = JSON.parse(cloudId);
cloudId = cloudId.cloudId;
const pipelineId = core.getInput('pipeline-id');
const buildNumber = core.getInput('build-number');
const buildDisplayName = core.getInput('build-display-name');
const buildState = core.getInput('build-state');
const buildUrl = core.getInput('build-url');
const updateSequenceNumber = core.getInput('update-sequence-number');
let lastUpdated = core.getInput('last-updated');
const issueKeys = core.getInput('issue-keys');
const commitId = core.getInput('commit-id');
const buildRefUrl = core.getInput('build-ref-url');
const testInfoTotalNum = core.getInput('test-info-total-num');
const testInfoNumPassed = core.getInput('test-info-num-passed');
const testInfoNumFailed = core.getInput('test-info-num-failed');
const testInfoNumSkipped = core.getInput('test-info-num-skipped');
lastUpdated = dateFormat(lastUpdated, "yyyy-mm-dd'T'HH:MM:ss'Z'");
const buildRef = {
commit: {
id: commitId || github.sha || github.context.sha,
repositoryUri: buildRefUrl || `${github.context.payload.repository.url}/actions/runs/${process.env['GITHUB_RUN_ID']}`,
},
ref: {
name: "buildRef",
uri: buildRefUrl || `${github.context.payload.repository.url}/actions/runs/${process.env['GITHUB_RUN_ID']}`,
},
};
let build = {
schemaVersion: "1.0",
pipelineId: pipelineId || `${github.context.payload.repository.full_name} ${github.context.workflow}`,
buildNumber: buildNumber || process.env['GITHUB_RUN_NUMBER'] || '',
updateSequenceNumber: updateSequenceNumber || process.env['GITHUB_RUN_ID'],
displayName: buildDisplayName || `Workflow: ${github.context.workflow} (#${process.env['GITHUB_RUN_NUMBER']})`,
url: buildUrl || `${github.context.payload.repository.url}/actions/runs/${process.env['GITHUB_RUN_ID']}`,
state: buildState || process.env['BUILD_STATE'],
lastUpdated: lastUpdated || dateFormat(github.context.payload.head_commit.timestamp, "yyyy-mm-dd'T'HH:MM:ss'Z'"),
issueKeys: issueKeys.split(',') || [],
references: [buildRef] || [],
};
if (testInfoTotalNum) {
// console.log("assign test info");
build.testInfo = {
totalNumber: testInfoTotalNum || undefined,
numberPassed: testInfoNumPassed || undefined,
numberFailed: testInfoNumFailed || undefined,
numberSkipped: testInfoNumSkipped || undefined,
};
}
let bodyData = {
builds: [build]
};
bodyData = JSON.stringify(bodyData);
const options = {
method: 'POST',
url: "https://api.atlassian.com/jira/builds/0.1/cloud/" + cloudId + "/bulk",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
Authorization: "Bearer " + accessToken,
},
body: bodyData,
};
let responseJson = await request(options);
let response = JSON.parse(responseJson);
if (response.rejectedBuilds && response.rejectedBuilds.length > 0) {
const rejectedBuild = response.rejectedBuilds[0];
// console.log("errors: ", rejectedBuild.errors);
let errors = rejectedBuild.errors.map((error) => error.message).join(',');
errors.substr(0, errors.length - 1);
// console.log("joined errors: ", errors);
core.setFailed(errors);
}
core.setOutput("response", responseJson);
}
exports.submitBuildInfo = submitBuildInfo;
(async function () {
try {
const clientId = core.getInput('client-id');
const clientSecret = core.getInput('client-secret');
const accessTokenResponse = await token.getAccessToken(clientId, clientSecret);
await submitBuildInfo(accessTokenResponse.access_token);
// console.log("finished submiting build info");
}
catch (error) {
core.setFailed(error.message);
}
})();