Files
dynamic-badges-action/index.js

114 lines
3.2 KiB
JavaScript
Raw Normal View History

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
try {
2020-08-15 21:00:33 +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.
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
2020-08-15 21:00:33 +02:00
// Get all optional attributes and add them to the content object if given.
2020-08-15 20:51:58 +02:00
const labelColor = core.getInput('labelColor');
const color = core.getInput('color');
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');
2020-08-15 20:46:27 +02:00
const logoPosition = core.getInput('logoPosition');
2020-08-15 20:51:58 +02:00
const style = core.getInput('style');
2020-08-15 20:46:27 +02:00
const cacheSeconds = core.getInput('cacheSeconds');
const minColorRange = core.getInput('minColorRange');
const maxColorRange = core.getInput('maxColorRange');
2020-08-15 12:33:06 +02:00
2020-08-15 13:40:04 +02:00
if (labelColor != '') {
2020-08-15 21:00:33 +02:00
content.labelColor = labelColor;
2020-08-15 12:33:06 +02:00
}
if (minColorRange != '' && maxColorRange != '') {
var max = parseFloat(maxColorRange);
var min = parseFloat(minColorRange);
var val = parseFloat(content.message);
if (val < min) val = min;
if (val > max) val = max;
let hue = Math.floor((val - min) / (max - min) * 100);
content.color = "hsl(" + hue + ", 100%, 50%)";
} else if (color != '') {
2020-08-15 21:00:33 +02:00
content.color = color;
2020-08-15 12:33:06 +02:00
}
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.
const request = JSON.stringify({
files: {[core.getInput('filename')]: {content: JSON.stringify(content)}}
2020-08-15 20:53:49 +02:00
});
2020-08-15 12:48:07 +02:00
2020-08-15 21:00:33 +02:00
// Perform the actual request. The user agent is required as defined in
// https://developer.github.com/v3/#user-agent-required
2020-08-15 13:25:03 +02:00
const req = http.request(
{
host: 'api.github.com',
2020-08-15 20:46:27 +02:00
path: '/gists/' + core.getInput('gistID'),
2020-08-15 13:30:05 +02:00
method: 'POST',
2020-08-15 13:25:03 +02:00
headers: {
'Content-Type': 'application/json',
2020-08-15 21:00:33 +02:00
'Content-Length': request.length,
2020-08-15 13:35:31 +02:00
'User-Agent': 'Schneegans',
2020-08-15 13:25:03 +02:00
'Authorization': 'token ' + core.getInput('auth'),
}
},
res => {
if (res.statusCode < 200 || res.statusCode >= 400) {
2022-03-26 10:50:40 +01:00
core.setFailed(
'Failed to create gist, response status code: ' + res.statusCode +
', status message: ' + res.statusMessage);
} else {
console.log('Success!');
}
2020-08-15 13:25:03 +02:00
});
2020-08-15 12:48:07 +02:00
2020-08-15 21:00:33 +02:00
req.write(request);
2020-08-15 12:48:07 +02:00
req.end();
2020-08-15 12:33:06 +02:00
2020-08-15 12:02:21 +02:00
} catch (error) {
2020-08-15 13:25:03 +02:00
core.setFailed(error);
}