forked from pdrbrnd/create-status-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (44 loc) · 1.3 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
const core = require("@actions/core");
const github = require("@actions/github");
const acceptedStates = ["error", "failure", "pending", "success"];
async function run() {
try {
const token = core.getInput("token", { required: true });
const state = core.getInput("state", { required: true });
const url = core.getInput("url");
const description = core.getInput("description");
const context = core.getInput("context");
if (acceptedStates.indexOf(state) === -1) {
throw new Error(
`Invalid status provided. Must be one of ${acceptedStates.join(", ")}`
);
}
const {
context: { payload }
} = github;
const sha =
payload.pull_request && payload.pull_request.head
? payload.pull_request.head.sha
: github.context.sha;
const status = {
...github.context.repo,
sha,
state
};
const optional = [
{ value: url, key: "target_url" },
{ value: description, key: "description" },
{ value: context, key: "context" }
];
optional.forEach(value => {
if (value.value) {
status[value.key] = value.value;
}
});
const octokit = new github.GitHub(token);
await octokit.repos.createStatus(status);
} catch (error) {
core.setFailed(error.message);
}
}
run();