forked from DevilTea/simple-travis-ci-trigger
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
143 lines (122 loc) · 3.67 KB
/
app.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
/*
* Based on simple-travis-ci-trigger project
* Copyright (c) 2020 DevilTea
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const session = require('koa-generic-session');
const convert = require('koa-convert');
const CSRF = require('koa-csrf');
const Router = require('koa-router');
const fs = require('fs');
const { Octokit } = require('@octokit/core');
const config = require('./config.json');
const repo = config.github_actions.repository;
function octokit() {
return new Octokit({
auth: config.github_actions.token,
userAgent: 'API explorer',
timeZone: 'Asia/Taipei'
});
}
async function start() {
async function triggerBuild(){
await octokit().request(`POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches`, {
owner: repo.owner,
repo: repo.name,
workflow_id: repo.workflow_id,
ref: repo.branch
});
}
async function getBuildStatus() {
try {
const { data } = await octokit().request('GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs', {
owner: repo.owner,
repo: repo.name,
workflow_id: repo.workflow_id
});
if (data.total_count === 0) {
return "unknown";
}
const run = data.workflow_runs[0];
switch (run.status) {
/*
* status: completed
* conclusion: “success”, “failure”, “neutral”, “cancelled”, “skipped”, “timed_out”, or “action_required”.
* only indicate the workflow run is finished, need to log conclusion message
*/
case "completed":
return run.conclusion;
case "in_progress":
return "running";
case "queued":
return "queued";
default:
return "unknown";
}
} catch (err) {
console.error(err);
return "unknown";
}
}
const indexTemplate = (() => {
return fs.readFileSync('./template/index.template.html')
.toString()
.replace(/%{REPO}%/g, repo.owner + '/' + repo.name);
})()
let currentStatus = await getBuildStatus()
let isTriggerBuffering = false
function newInterval () {
return setInterval(async () => {
currentStatus = isTriggerBuffering ? currentStatus : await getBuildStatus()
}, 5000)
}
let interval = newInterval()
const router = new Router()
router.get('/', async (ctx) => {
ctx.status = 200
ctx.body = indexTemplate
.replace(/%{CSRF}%/g, ctx.csrf)
})
router.get('/api/status', async (ctx) => {
ctx.status = 200
ctx.body = {
status: currentStatus
}
})
router.post('/api/builds', async (ctx) => {
if (!["running", "queued"].includes(currentStatus) && !isTriggerBuffering) {
isTriggerBuffering = true
clearInterval(interval)
interval = null
await triggerBuild()
currentStatus = "queued"
setTimeout(() => {
interval = newInterval()
isTriggerBuffering = false
}, 5000)
}
ctx.status = 201
})
const app = new Koa();
// set the session keys
app.keys = config.sessionKeys;
// add session support
app.use(convert(session()));
// add body parsing
app.use(bodyParser());
// add the CSRF middleware
app.use(new CSRF({
invalidTokenMessage: 'Invalid CSRF token',
invalidTokenStatusCode: 403,
excludedMethods: ['GET', 'HEAD', 'OPTIONS'],
disableQuery: false
}));
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(config.port, () => console.log(`Service is online: http://localhost:${config.port}`));
}
start().catch((error) => console.log(error))