-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
184 lines (162 loc) · 5.71 KB
/
index.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const core = require('@actions/core');
const exec = require('@actions/exec');
const github = require('@actions/github');
(async () => {
try {
await setup();
} catch (error) {
core.setFailed(error.message);
}
})();
async function setup() {
// Required inputs
const stepName = core.getInput("step-name")
const validation = core.getInput("validation")
const build = core.getInput("build")
// Get the Ontrack information
let project = core.getInput("project")
let branch = core.getInput("branch")
if (!project) {
project = github.context.repo.repo
}
if (!branch) {
// TODO Supports for pull requests
const branchPrefix = 'refs/heads/';
if (github.context.ref.startsWith('refs/heads/')) {
branch = github.context.ref.substring(branchPrefix.length);
} else {
throw `Ref not supported: ${github.context.ref}`
}
}
// Data type
const dataType = core.getInput("type")
const dataFlags = core.getInput("flags")
if (dataFlags && !dataType) {
throw `Flags are provided without a data type.`
}
// Logging
const logging = core.getInput("logging") === 'true' || core.getInput("logging") === true
console.log(`Step name: ${stepName}`)
console.log(`Project: ${project}`)
console.log(`Branch: ${branch}`)
console.log(`Validation: ${validation}`)
console.log(`Build: ${build}`)
console.log(`Data type: ${dataType}`)
console.log(`Data flags: ${dataFlags}`)
// Getting information about the step to measure
const info = await computeStepRunInfo(logging, stepName)
console.log(`${stepName} step Ontrack status: ${info.status}`)
console.log(`${stepName} step duration: ${info.duration} seconds`)
console.log(`${stepName} step URL: ${info.url}`)
console.log(`${stepName} step event: ${info.event}`)
// CLI command to prepare
const executable = core.getInput("executable")
let args = ["validate", "--project", project, "--branch", branch, "--build", build, "--validation", validation]
// Run info
args.push("--run-time", info.duration, "--source-type", "github", "--source-uri", info.url, "--trigger-type", info.event)
// Data
if (dataType) {
args.push(dataType)
if (dataFlags) {
const flags = dataFlags.split(" ")
args = args.concat(flags)
}
}
// If no data, status must be passed
else {
args.push("--status", info.status)
}
// Logging
// console.log(`CLI ${executable} `, args)
// Running the command
await exec.exec(executable, args)
}
async function computeStepRunInfo(logging, stepName) {
const token = core.getInput("token")
const octokit = github.getOctokit(token)
// A bit of logging to follow things through
console.log(`Run ID: ${github.context.runId}`)
console.log(`Job name: ${github.context.job}`)
// Gets the step after it's been completed
const response = await getCompletedStep(logging, octokit, stepName)
const job = response.job
const step = response.step
// Step information
const status = step.status
const conclusion = step.conclusion
const startedAt = new Date(step.started_at)
const completedAt = new Date(step.completed_at)
console.log(`Step status: ${status}`)
console.log(`Step conclusion: ${conclusion}`)
console.log(`Step started at: ${startedAt}`)
console.log(`Step completed at: ${completedAt}`)
// Gets the duration
const duration = (completedAt - startedAt) / 1000
// Job link
const url = job.html_url
// Converts the conclusion to an Ontrack status
let ontrackStatus;
if (conclusion === 'success') {
ontrackStatus = 'PASSED';
} else {
ontrackStatus = 'FAILED';
}
// Final information
return {
duration,
url,
event: github.context.eventName,
status: ontrackStatus
}
}
function getCompletedStep(logging, octokit, stepName) {
const start = new Date()
const timeoutMs = 30000
return new Promise((resolve, reject) => {
const wait = setInterval(function () {
if (logging) {
console.log(`Fetching step status: ${stepName}`)
}
getStep(octokit, stepName)
.then(response => {
if (logging) {
console.log("Step: ", response.step)
console.log(`Step status: ${response.step.status}`)
}
if (response.step.status === 'completed') {
if (logging) {
console.log(`Step is completed: ${stepName}`)
}
clearInterval(wait);
resolve(response);
} else if (new Date() - start > timeoutMs) {
clearInterval(wait);
reject(`Timeout waiting for step ${stepName} to be completed`)
}
})
.catch(reject)
}, 2000);
});
}
async function getStep(octokit, stepName) {
const run = await octokit.rest.actions.listJobsForWorkflowRun({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
run_id: github.context.runId
});
// Looks for the job
const job = run.data.jobs.find((item) => item.name === github.context.job);
if (!job) {
throw `Job not found in current workflow: ${github.context.job}`
}
// Looks for the step
const step = job.steps.find((item) => item.name === stepName);
if (!step) {
throw `Step not found in current job: ${stepName}`
}
// OK
return {
job,
step
}
}