This repository was archived by the owner on Jul 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (67 loc) · 2.74 KB
/
index.js
File metadata and controls
70 lines (67 loc) · 2.74 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
import fetch from 'node-fetch';
const core = require('@actions/core');
const github = require('@actions/github');
const EventSource = require('eventsource');
// most @actions toolkit packages have async methods
async function run() {
try {
const image = core.getInput('image', { required: true });
if ( image == "" ) {
core.setFailed("image is a required input");
return;
}
const ghrepo = github.context.repo;
const reponame = core.getInput('reponame') || `github.com/${ghrepo.owner}/${ghrepo.repo}`;
const ref = `${reponame}%${image}#${github.context.sha}`;
const b5apisrv = core.getInput('barney-api-server', { required: true });
const bsycompatsrv = core.getInput('bsy-compat-server', { required: false });
if ( b5apisrv == "" ) {
core.setFailed("barney-api-server is a required input");
return;
}
core.info( `building ${ref} on ${b5apisrv}` )
fetch(`${b5apisrv}/job`, {
method: 'post',
body: `{ "snapshot-spec": "${ref}" }`,
headers: {'Content-Type': 'application/json; version=v0'}
}).then(res => {
if (!res.ok) {
core.setFailed(`job creation failed: ${res.status}`)
return;
}
const jobStatus = res.headers.get('location');
core.info(`Created job: ${jobStatus}`);
if (bsycompatsrv != "") {
core.notice(`Job Status: ${bsycompatsrv}${jobStatus}/status`);
}
var es = new EventSource(`${b5apisrv}${jobStatus}`, {
headers: {'Accept': 'text/event-stream; version=v0'}
});
function waitForJobCompletion(resolve, reject) {
es.addEventListener('replace', function (event) {
var jobrec = JSON.parse(event.data);
var jobrecPretty = JSON.stringify(jobrec, null, 2);
core.info(`Job ${jobrec.id} ${jobrec.status}\nJob record: ${jobrecPretty}`);
if (jobrec.status == 'passed') {
resolve(jobrec.status);
} else if (jobrec.status != 'running') {
reject(jobrec.status);
}
});
}
var waiter = new Promise(waitForJobCompletion);
waiter.then((status) => {
core.info(`Job ${status}`);
es.close();
}).catch(status => {
core.setFailed(`Job \u001b[38;2;255;0;0m${status}`);
es.close();
})
}).catch(error => {
core.setFailed(error);
})
} catch (error) {
core.setFailed(error.message);
}
}
run();