-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
134 lines (95 loc) · 2.66 KB
/
index.js
File metadata and controls
134 lines (95 loc) · 2.66 KB
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
const core = require("@actions/core");
const github = require("@actions/github");
const token = core.getInput("token");
const octokit = github.getOctokit(token);
const name = input("name", "");
const value = core.getInput("value");
const visibility = input("visibility", "all");
const push_to_org = (input("org", "") !== "");
const owner = input("owner", github.context.payload.repository.owner.login);
const repository = input("repository", github.context.payload.repository.name);
function path_() {
if (push_to_org) return "/orgs/" + owner;
if (repository.includes("/")) return "/repos/" + repository;
return "/repos/" + owner + "/" + repository;
}
function input(name, def) {
let inp = core.getInput(name).trim();
if (inp === "" || inp.toLowerCase() === "false") return def;
return inp;
}
const createVariable = (data) => {
let url = "POST " + path_();
url += "/actions/variables";
if (push_to_org) {
return octokit.request(url, {
name: name,
visibility: visibility,
value: data
});
}
return octokit.request(url, {
name: name,
value: data
});
};
const setVariable = (data) => {
let url = "PATCH " + path_();
url += "/actions/variables/" + name;
return octokit.request(url, {
name: name,
value: data
});
};
const getVariable = (varname) => {
let url = "GET " + path_();
url += "/actions/variables/" + varname;
return octokit.request(url);
};
const bootstrap = async () => {
let exists = false;
try {
const response = await getVariable(name);
exists = response.status === 200;
} catch (e) {
// Variable does not exist
}
try {
if (name === "") {
throw new Error("No name was specified!");
}
if (exists) {
const response = await setVariable(value);
if (response.status === 204) {
return "Successfully updated variable " + name + " to '" + value + "'.";
}
throw new Error("ERROR: Wrong status was returned: " + response.status);
} else {
const response = await createVariable(value);
if (response.status === 201) {
return "Successfully created variable " + name + " with value '" + value + "'.";
}
throw new Error("ERROR: Wrong status was returned: " + response.status);
}
} catch (e) {
core.setFailed(path_() + ": " + e.message);
console.error(e);
}
};
bootstrap()
.then(
(result) => {
// eslint-disable-next-line no-console
if (result != null) {
console.log(result);
}
},
(err) => {
// eslint-disable-next-line no-console
core.setFailed(err.message);
console.error(err);
},
)
.then(() => {
process.exit();
});