Files
dynamic-badges-action/index.js

171 lines
4.8 KiB
JavaScript
Raw Permalink 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. //
//////////////////////////////////////////////////////////////////////////////////////////
// SPDX-FileCopyrightText: Simon Schneegans <code@simonschneegans.de>
// SPDX-License-Identifier: MIT
2023-10-07 19:31:13 +02:00
import core from "@actions/core";
import { makeBadge } from "badge-maker";
2024-07-31 15:20:25 +12:00
import { createRequire } from "module";
const require = createRequire(import.meta.url);
2022-10-08 21:23:46 +02:00
2024-07-31 15:18:36 +12:00
const util = require('util');
2024-07-31 15:16:44 +12:00
const hostUrl = new URL(core.getInput("host"));
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
async function updateBadge(body) {
const headers = new Headers([
2023-10-07 19:31:13 +02:00
["Content-Type", "application/json"],
2023-10-09 08:24:17 +02:00
["Content-Length", new TextEncoder().encode(body).length],
["User-Agent", "gitea-dynamic-badges"],
["x-api-key", `${core.getInput("auth")}`],
]);
2024-07-31 16:03:13 +12:00
console.log("Making post request to: %s", hostUrl);
const response = await fetch(hostUrl, {
2023-10-07 19:31:13 +02:00
method: "POST",
headers,
body,
2022-10-09 06:27:21 +02:00
});
if (!response.ok) {
2024-07-31 15:24:33 +12:00
console.log("Returned: %j", response.body);
if (response.status === 409) {
// This means likely the badge already exists. Try to patch
2024-07-31 16:03:13 +12:00
console.log("Running patch on %s", hostUrl);
2024-07-31 16:04:36 +12:00
const response2 = await fetch(hostUrl, {
method: 'PATCH',
headers,
body,
});
2024-07-31 16:03:13 +12:00
if (!response2.ok) {
core.setFailed(
`Failed to create gist, response status code: ${response.status} ${response.statusText}`
);
return;
}
}
}
2023-10-07 19:31:13 +02:00
console.log("Success!");
2022-10-09 06:27:21 +02:00
}
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 {
// 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 = {
schemaVersion: 1,
2023-10-07 19:31:13 +02:00
label: core.getInput("label"),
message: core.getInput("message"),
2020-08-15 13:25:03 +02:00
};
2020-08-15 12:33:06 +02:00
// Compute the message color based on the given inputs.
2023-10-07 19:31:13 +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");
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;
2023-10-07 19:31:13 +02:00
if (invertColorRange == "") {
hue = Math.floor(((val - min) / (max - min)) * 120);
} else {
hue = Math.floor(((max - val) / (max - min)) * 120);
}
let sat = 100;
2023-10-07 19:31:13 +02:00
if (colorRangeSaturation != "") {
sat = parseFloat(colorRangeSaturation);
2022-04-18 13:09:18 +02:00
}
let lig = 40;
2023-10-07 19:31:13 +02:00
if (colorRangeLightness != "") {
lig = parseFloat(colorRangeLightness);
}
2023-10-07 19:31:13 +02:00
data.color = "hsl(" + hue + ", " + sat + "%, " + lig + "%)";
} else if (color != "") {
data.color = color;
2020-08-15 12:33:06 +02:00
}
// Get all optional attributes and add them to the content object if given.
2023-10-07 19:31:13 +02:00
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 (isError != "") {
data.isError = isError;
2020-08-15 13:25:03 +02:00
}
if (namedLogo != "") {
data.namedLogo = namedLogo;
2020-08-15 13:25:03 +02:00
}
if (logoSvg != "") {
data.logoSvg = logoSvg;
2020-08-15 13:25:03 +02:00
}
if (logoColor != "") {
data.logoColor = logoColor;
2020-08-15 13:25:03 +02:00
}
if (logoWidth != "") {
data.logoWidth = parseInt(logoWidth);
2020-08-15 13:25:03 +02:00
}
if (logoPosition != "") {
data.logoPosition = logoPosition;
2020-08-15 13:25:03 +02:00
}
2023-10-07 19:31:13 +02:00
if (style != "") {
data.style = style;
2020-08-15 13:25:03 +02:00
}
if (cacheSeconds != "") {
data.cacheSeconds = parseInt(cacheSeconds);
}
2023-10-07 19:31:13 +02:00
let content = "";
2024-07-31 15:30:06 +12:00
content = JSON.stringify({ payload: data });
2022-10-08 21:23:46 +02:00
2024-07-31 15:28:04 +12:00
console.log("Body of request: %s", content);
updateBadge(content);
2020-08-15 12:02:21 +02:00
} catch (error) {
2020-08-15 13:25:03 +02:00
core.setFailed(error);
}