-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
112 lines (101 loc) · 2.82 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
const core = require("@actions/core")
const github = require("@actions/github")
const axios = require("axios")
const moment = require("moment")
const createCapybaraRun = async (
capybaraUrl,
clientId,
location,
owner,
repoName,
workflowId,
ref,
durationInMinutes,
maximumDelayInSeconds
) => {
try {
const capybaraInstance = axios.create({
baseURL: capybaraUrl,
timeout: 60000,
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
})
const response = await capybaraInstance.post("/v1/github/workflows/runs", {
clientId: clientId,
repository: {
owner: owner,
name: repoName,
workflowId: workflowId,
ref: ref,
},
schedule: {
location: location,
approximateWorkflowRunDurationInMinutes: durationInMinutes,
maximumDelayInSeconds: maximumDelayInSeconds,
},
})
core.setOutput(
"bestTimeToStart",
moment(response.data.scheduledTime).format("DD/MM/YYYY HH:mm:ss")
)
return response.data
} catch (error) {
console.log(error)
}
}
const cancelGitHubRun = async (owner, repoName) => {
try {
const githubInstance = axios.create({
baseURL: "https://api.github.com/",
timeout: 5000,
headers: {
Accept: "application/vnd.github+json",
Authorization: `Bearer ${core.getInput("github-token")}`,
},
})
const response = await githubInstance.post(
`/repos/${owner}/${repoName}/actions/runs/${github.context.runId}/cancel`
)
return response.data
} catch (error) {
console.log(error)
}
}
try {
const isCapybaraDispatch =
github.context.payload.inputs &&
github.context.payload.inputs.isCapybaraDispatch &&
github.context.payload.inputs.isCapybaraDispatch !== "false"
? github.context.payload.inputs.isCapybaraDispatch
: false
if (!isCapybaraDispatch) {
// The workflow is not triggered by Capybara backend - call the backend and cancel the build. Otherwise, ignore.
const capybaraUrl = core.getInput("capybaraUrl")
const clientId = core.getInput("clientId")
const workflowId = core.getInput("workflowId")
const repoName = core.getInput("repoName")
const ref = core.getInput("ref")
const owner = core.getInput("owner")
const durationInMinutes = core.getInput("durationInMinutes")
const maximumDelayInSeconds = core.getInput("maximumDelayInSeconds")
const location = core.getInput("location")
createCapybaraRun(
capybaraUrl,
clientId,
location,
owner,
repoName,
workflowId,
ref,
durationInMinutes,
maximumDelayInSeconds
)
cancelGitHubRun(owner, repoName)
} else {
core.setOutput("bestTimeToStart", "CANCELLED")
}
} catch (error) {
core.setFailed(error.message)
}