-
Notifications
You must be signed in to change notification settings - Fork 6
/
action.js
63 lines (55 loc) · 1.37 KB
/
action.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
const core = require('@actions/core')
const axios = require('axios')
async function webrequest (url, method, payload, headers, username, password) {
const auth = username && password ? { username, password } : null
const config = {
url,
method,
auth,
data: payload,
headers
}
try {
const response = await axios(config)
return response
} catch (error) {
console.error(error)
}
}
async function main () {
try {
// inputs from action
const url = core.getInput('url') || ('https://github.com/' + process.env.GITHUB_REPOSITORY)
const method = 'post'
const payload = { url: url }
// current time
const time = new Date().toTimeString()
// http request to external API
const response = await webrequest(
url,
method,
payload
)
const statusCode = response.status
const data = response.data
const outputObject = {
url,
method,
payload,
time,
statusCode,
data
}
const consoleOutputJSON = JSON.stringify(outputObject, undefined, 2)
console.log(consoleOutputJSON)
if (statusCode >= 400) {
core.setFailed(`HTTP request failed with status code: ${statusCode}`)
} else {
const outputJSON = JSON.stringify(outputObject)
core.setOutput('output', outputJSON)
}
} catch (error) {
core.setFailed(error.message)
}
}
main()