Files
dynamic-badges-action/index.js

223 lines
6.7 KiB
JavaScript
Raw Normal View History

2022-10-09 06:27:21 +02:00
//////////////////////////////////////////////////////////////////////////////////////////
// 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. //
// Copyright: (c) 2020 Simon Schneegans //
//////////////////////////////////////////////////////////////////////////////////////////
2020-08-15 12:02:21 +02:00
const core = require('@actions/core');
2020-08-15 13:30:05 +02:00
const http = require('https');
2020-08-15 12:02:21 +02:00
2022-10-09 06:27:21 +02:00
// Performs an HTTP request and returns a Promise accordingly. See docs of
// http.request() for the available options.
function doRequest(options, data) {
return new Promise((resolve, reject) => {
const req = http.request(options, res => {
res.setEncoding('utf8');
let responseBody = '';
2020-08-15 21:00:33 +02:00
2022-10-09 06:27:21 +02:00
res.on('data', (chunk) => {
responseBody += chunk;
});
2022-10-08 21:23:46 +02:00
2022-10-09 06:27:21 +02:00
res.on('end', () => {
const {statusCode, statusMessage} = res;
resolve({statusCode, statusMessage, body: JSON.parse(responseBody)});
});
2022-10-08 21:29:53 +02:00
});
2022-10-08 21:23:46 +02:00
2022-10-09 06:27:21 +02:00
req.on('error', (err) => {
reject(err);
});
2022-10-08 21:23:46 +02:00
2022-10-09 06:27:21 +02:00
req.write(data)
req.end();
});
}
2022-10-08 21:23:46 +02:00
2022-10-09 06:27:21 +02:00
// 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
function updateGist(data) {
const updateGistOptions = {
host: 'api.github.com',
path: '/gists/' + core.getInput('gistID'),
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length,
'User-Agent': 'Schneegans',
'Authorization': 'token ' + core.getInput('auth'),
}
};
2022-10-08 21:23:46 +02:00
2022-10-09 06:27:21 +02:00
doRequest(updateGistOptions, data).then(res => {
if (res.statusCode < 200 || res.statusCode >= 400) {
core.setFailed(
'Failed to create gist, response status code: ' + res.statusCode +
', status message: ' + res.statusMessage);
} else {
console.log('Success!');
}
});
}
2022-10-08 21:23:46 +02:00
2022-10-09 06:27:21 +02:00
// We wrap the entire action in a try / catch block so we can set it to "failed" if
// something goes wrong.
try {
2022-10-09 06:27:21 +02:00
// 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.
2020-08-15 21:00:33 +02:00
let content = {
2020-08-15 13:25:03 +02:00
schemaVersion: 1,
label: core.getInput('label'),
message: core.getInput('message')
};
2020-08-15 12:33:06 +02:00
// Compute the message color based on the given inputs.
2022-04-18 13:09:18 +02:00
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');
2022-04-18 13:09:18 +02:00
const colorRangeLightness = core.getInput('colorRangeLightness');
2020-08-15 12:33:06 +02:00
if (minColorRange != '' && maxColorRange != '' && valColorRange != '') {
const max = parseFloat(maxColorRange);
const min = parseFloat(minColorRange);
2022-10-08 21:23:46 +02:00
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;
2022-04-18 13:09:18 +02:00
if (colorRangeSaturation != '') {
sat = parseFloat(colorRangeSaturation);
2022-04-18 13:09:18 +02:00
}
let lig = 40;
2022-04-18 13:09:18 +02:00
if (colorRangeLightness != '') {
lig = parseFloat(colorRangeLightness);
}
2022-04-18 13:09:18 +02:00
content.color = 'hsl(' + hue + ', ' + sat + '%, ' + lig + '%)';
} else if (color != '') {
2020-08-15 21:00:33 +02:00
content.color = color;
2020-08-15 12:33:06 +02:00
}
// 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');
const filename = core.getInput('filename');
if (labelColor != '') {
content.labelColor = labelColor;
}
2020-08-15 13:40:04 +02:00
if (isError != '') {
2020-08-15 21:00:33 +02:00
content.isError = isError;
2020-08-15 13:25:03 +02:00
}
2020-08-15 13:40:04 +02:00
if (namedLogo != '') {
2020-08-15 21:00:33 +02:00
content.namedLogo = namedLogo;
2020-08-15 13:25:03 +02:00
}
2020-08-15 13:40:04 +02:00
if (logoSvg != '') {
2020-08-15 21:00:33 +02:00
content.logoSvg = logoSvg;
2020-08-15 13:25:03 +02:00
}
2020-08-15 13:40:04 +02:00
if (logoColor != '') {
2020-08-15 21:00:33 +02:00
content.logoColor = logoColor;
2020-08-15 13:25:03 +02:00
}
2020-08-15 13:40:04 +02:00
if (logoWidth != '') {
2020-08-15 21:00:33 +02:00
content.logoWidth = parseInt(logoWidth);
2020-08-15 13:25:03 +02:00
}
2020-08-15 13:40:04 +02:00
if (logoPosition != '') {
2020-08-15 21:00:33 +02:00
content.logoPosition = logoPosition;
2020-08-15 13:25:03 +02:00
}
2020-08-15 13:40:04 +02:00
if (style != '') {
2020-08-15 21:00:33 +02:00
content.style = style;
2020-08-15 13:25:03 +02:00
}
2020-08-15 13:40:04 +02:00
if (cacheSeconds != '') {
2020-08-15 21:00:33 +02:00
content.cacheSeconds = parseInt(cacheSeconds);
2020-08-15 13:25:03 +02:00
}
2020-08-15 21:00:33 +02:00
// For the POST request, the above content is set as file contents for the
// given filename.
2022-10-08 21:23:46 +02:00
const request =
JSON.stringify({files: {[filename]: {content: JSON.stringify(content)}}});
2022-10-09 06:27:21 +02:00
// 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.
2022-10-09 06:01:08 +02:00
if (core.getBooleanInput('forceUpdate')) {
updateGist(request);
2022-10-09 06:27:21 +02:00
} else {
2022-10-09 06:27:21 +02:00
// Get the old gist.
const getGistOptions = {
host: 'api.github.com',
path: '/gists/' + core.getInput('gistID'),
method: 'GET',
headers: {
'Content-Type': 'application/json',
'User-Agent': 'Schneegans',
'Authorization': 'token ' + core.getInput('auth'),
}
};
2022-10-08 21:23:46 +02:00
doRequest(getGistOptions, JSON.stringify({})).then(oldGist => {
if (oldGist.statusCode < 200 || oldGist.statusCode >= 400) {
2022-10-09 06:27:21 +02:00
// print the error, but don't fail the action.
console.log(
2022-10-08 21:23:46 +02:00
'Failed to get gist, response status code: ' + oldGist.statusCode +
', status message: ' + oldGist.statusMessage);
}
2022-10-09 06:01:08 +02:00
let shouldUpdate = true;
2022-10-09 06:27:21 +02:00
if (oldGist && oldGist.body && oldGist.body.files && oldGist.body.files[filename]) {
const oldContent = oldGist.body.files[filename].content;
if (oldContent === JSON.stringify(content)) {
2022-10-09 06:27:21 +02:00
console.log(`Content did not change, not updating gist at ${filename}.`);
shouldUpdate = false;
2020-08-15 13:25:03 +02:00
}
}
if (shouldUpdate) {
if (oldGist.body.files[filename]) {
2022-10-09 06:27:21 +02:00
console.log(`Content changed, updating gist at ${filename}.`);
2022-03-26 10:50:40 +01:00
} else {
2022-10-09 06:27:21 +02:00
console.log(`Content didn't exist, creating gist at ${filename}.`);
}
2020-08-15 12:33:06 +02:00
2022-10-08 21:29:53 +02:00
updateGist(request);
}
});
}
2022-10-08 21:29:53 +02:00
2020-08-15 12:02:21 +02:00
} catch (error) {
2020-08-15 13:25:03 +02:00
core.setFailed(error);
}