-
Notifications
You must be signed in to change notification settings - Fork 6
/
self-hosted-arm64-runners.js
77 lines (67 loc) · 2.8 KB
/
self-hosted-arm64-runners.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
module.exports = async (context, req) => {
const action = req.body.action
const owner = req.body.repository.owner.login
const repo = req.body.repository.name
const sender = req.body.sender.login
const getToken = (() => {
let token
const get = async () => {
const getInstallationIdForRepo = require('./get-installation-id-for-repo')
const installationId = await getInstallationIdForRepo(context, owner, repo)
const getInstallationAccessToken = require('./get-installation-access-token')
return await getInstallationAccessToken(context, installationId)
}
return async () => token || (token = await get())
})()
const isAllowed = async (login) => {
if (login === 'gitforwindowshelper[bot]') return true
const getCollaboratorPermissions = require('./get-collaborator-permissions')
const token = await getToken()
const permission = await getCollaboratorPermissions(context, token, owner, repo, login)
return ['ADMIN', 'MAINTAIN', 'WRITE'].includes(permission.toString())
}
if (!await isAllowed(sender)) {
if (action !== 'completed') {
// Cancel workflow run
const { cancelWorkflowRun } = require('./check-runs')
const token = await getToken()
const workflowRunId = req.body.workflow_job.run_id
await cancelWorkflowRun(context, token, owner, repo, workflowRunId)
}
throw new Error(`${sender} is not allowed to do that`)
}
if (action === 'queued') {
// Spin up a new runner
const triggerWorkflowDispatch = require('./trigger-workflow-dispatch')
const token = await getToken()
const answer = await triggerWorkflowDispatch(
context,
token,
'git-for-windows',
'git-for-windows-automation',
'create-azure-self-hosted-runners.yml',
'main', {
runner_scope: 'repo-level'
}
)
return `The workflow run to create the self-hosted runner VM was started at ${answer.html_url}`
}
if (action === 'completed') {
// Delete the runner
const triggerWorkflowDispatch = require('./trigger-workflow-dispatch')
const token = await getToken()
const vmName = req.body.workflow_job.runner_name
const answer = await triggerWorkflowDispatch(
context,
token,
'git-for-windows',
'git-for-windows-automation',
'delete-self-hosted-runner.yml',
'main', {
runner_name: vmName
}
)
return `The workflow run to delete the self-hosted runner VM '${vmName}' was started at ${answer.html_url}`
}
return `Unhandled action: ${action}`
}