-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.js
210 lines (173 loc) · 6.03 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//////////////////////////////////////////////////////////////////////////////////////////
// This file is part of the Dynamic Badges Action //
// It may be used under the terms of the MIT license. See the LICENSE file for details. //
//////////////////////////////////////////////////////////////////////////////////////////
// SPDX-FileCopyrightText: Simon Schneegans <[email protected]>
// SPDX-License-Identifier: MIT
import core from "@actions/core";
import { makeBadge } from "badge-maker";
const gistUrl = new URL(core.getInput("gistID"), core.getInput("host"));
// This uses the method above to update a gist with the given data. The user agent is
// required as defined in https://developer.github.com/v3/#user-agent-required
async function updateGist(body) {
const headers = new Headers([
["Content-Type", "application/json"],
["Content-Length", new TextEncoder().encode(body).length],
["User-Agent", "Schneegans"],
["Authorization", `token ${core.getInput("auth")}`],
]);
const response = await fetch(gistUrl, {
method: "POST",
headers,
body,
});
if (!response.ok) {
core.setFailed(
`Failed to create gist, response status code: ${response.status} ${response.statusText}`
);
return;
}
console.log("Success!");
}
// We wrap the entire action in a try / catch block so we can set it to "failed" if
// something goes wrong.
try {
// This object will be stringified and uploaded to the gist. The schemaVersion, label
// and message attributes are always required. All others are optional and added to the
// content object only if they are given to the action.
let data = {
label: core.getInput("label"),
message: core.getInput("message"),
};
const filename = core.getInput("filename");
const isSvgFile = filename.endsWith(".svg");
if (!isSvgFile) {
data.schemaVersion = 1;
}
// Compute the message color based on the given inputs.
const color = core.getInput("color");
const valColorRange = core.getInput("valColorRange");
const minColorRange = core.getInput("minColorRange");
const maxColorRange = core.getInput("maxColorRange");
const invertColorRange = core.getInput("invertColorRange");
const colorRangeSaturation = core.getInput("colorRangeSaturation");
const colorRangeLightness = core.getInput("colorRangeLightness");
if (minColorRange != "" && maxColorRange != "" && valColorRange != "") {
const max = parseFloat(maxColorRange);
const min = parseFloat(minColorRange);
let val = parseFloat(valColorRange);
if (val < min) val = min;
if (val > max) val = max;
let hue = 0;
if (invertColorRange == "") {
hue = Math.floor(((val - min) / (max - min)) * 120);
} else {
hue = Math.floor(((max - val) / (max - min)) * 120);
}
let sat = 100;
if (colorRangeSaturation != "") {
sat = parseFloat(colorRangeSaturation);
}
let lig = 40;
if (colorRangeLightness != "") {
lig = parseFloat(colorRangeLightness);
}
data.color = "hsl(" + hue + ", " + sat + "%, " + lig + "%)";
} else if (color != "") {
data.color = color;
}
// Get all optional attributes and add them to the content object if given.
const labelColor = core.getInput("labelColor");
const isError = core.getInput("isError");
const namedLogo = core.getInput("namedLogo");
const logoSvg = core.getInput("logoSvg");
const logoColor = core.getInput("logoColor");
const logoWidth = core.getInput("logoWidth");
const logoPosition = core.getInput("logoPosition");
const style = core.getInput("style");
const cacheSeconds = core.getInput("cacheSeconds");
if (labelColor != "") {
data.labelColor = labelColor;
}
if (!isSvgFile && isError != "") {
data.isError = isError;
}
if (!isSvgFile && namedLogo != "") {
data.namedLogo = namedLogo;
}
if (!isSvgFile && logoSvg != "") {
data.logoSvg = logoSvg;
}
if (!isSvgFile && logoColor != "") {
data.logoColor = logoColor;
}
if (!isSvgFile && logoWidth != "") {
data.logoWidth = parseInt(logoWidth);
}
if (!isSvgFile && logoPosition != "") {
data.logoPosition = logoPosition;
}
if (style != "") {
data.style = style;
}
if (!isSvgFile && cacheSeconds != "") {
data.cacheSeconds = parseInt(cacheSeconds);
}
let content = "";
if (isSvgFile) {
content = makeBadge(data);
} else {
content = JSON.stringify(data);
}
// For the POST request, the above content is set as file contents for the
// given filename.
const body = JSON.stringify({ files: { [filename]: { content } } });
// If "forceUpdate" is set to true, we can simply update the gist. If not, we have to
// get the gist data and compare it to the new value before.
if (core.getBooleanInput("forceUpdate")) {
updateGist(body);
} else {
// Get the old gist.
fetch(gistUrl, {
method: "GET",
headers: new Headers([
["Content-Type", "application/json"],
["User-Agent", "Schneegans"],
["Authorization", `token ${core.getInput("auth")}`],
]),
})
.then((response) => {
if (!response.ok) {
return Promise.reject(
`Failed to get gist: ${response.status} ${response.statusText}`
);
}
return response.json();
})
.then((oldGist) => {
let shouldUpdate = true;
if (oldGist?.files?.[filename]) {
const oldContent = oldGist.files[filename].content;
if (oldContent === content) {
console.log(
`Content did not change, not updating gist at ${filename}.`
);
shouldUpdate = false;
}
}
if (shouldUpdate) {
if (oldGist?.files?.[filename]) {
console.log(`Content changed, updating gist at ${filename}.`);
} else {
console.log(`Content didn't exist, creating gist at ${filename}.`);
}
updateGist(body);
}
})
.catch((error) => {
core.setFailed(error);
});
}
} catch (error) {
core.setFailed(error);
}