feat: Customised action to allow use of dynamic json storage endpoint.
Some checks failed
Checks / Check REUSE (push) Successful in 27s
Build Badges / Create Answer Badge (push) Failing after 4s
Build Badges / Create Color Range Badges (0) (push) Has been cancelled
Build Badges / Create Color Range Badges (10) (push) Has been cancelled
Build Badges / Create Color Range Badges (100) (push) Has been cancelled
Build Badges / Create Color Range Badges (20) (push) Has been cancelled
Build Badges / Create Color Range Badges (30) (push) Has been cancelled
Build Badges / Create Color Range Badges (40) (push) Has been cancelled
Build Badges / Create Color Range Badges (50) (push) Has been cancelled
Build Badges / Create Color Range Badges (60) (push) Has been cancelled
Build Badges / Create Color Range Badges (70) (push) Has been cancelled
Build Badges / Create Color Range Badges (80) (push) Has been cancelled
Build Badges / Create Color Range Badges (90) (push) Has been cancelled
Build Badges / Create SVG Badge (push) Has been cancelled

This commit is contained in:
2024-07-31 11:46:19 +12:00
parent 7142847813
commit b348384cf9
2 changed files with 30 additions and 79 deletions

View File

@@ -8,13 +8,7 @@ branding:
color: "green"
inputs:
auth:
description: "Your secret with the gist scope"
required: true
gistID:
description: "The ID of the gist to use"
required: true
filename:
description: "The *.json or *.svg filename of the badge data"
description: "Your secret token"
required: true
label:
description: "The left text of the badge"
@@ -23,13 +17,9 @@ inputs:
description: "The right text of the badge"
required: true
host:
description: "The base URL of the gist API"
default: "https://api.github.com/gists/"
required: false
forceUpdate:
description: "If set to true, the gist will be updated even if the content did not change"
default: "false"
required: false
description: "The base URL of the badgestor API"
default: "https://badges.mydomain.com/"
required: true
labelColor:
description: "The left color of the badge"
required: false

View File

@@ -9,35 +9,48 @@
import core from "@actions/core";
import { makeBadge } from "badge-maker";
const gistUrl = new URL(core.getInput("gistID"), core.getInput("host"));
const hostUrl = new URL(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) {
async function updateBadge(body) {
const headers = new Headers([
["Content-Type", "application/json"],
["Content-Length", new TextEncoder().encode(body).length],
["User-Agent", "Schneegans"],
["Authorization", `token ${core.getInput("auth")}`],
["User-Agent", "gitea-dynamic-badges"],
["x-api-key", `${core.getInput("auth")}`],
]);
const response = await fetch(gistUrl, {
let response = await fetch(hostUrl, {
method: "POST",
headers,
body,
});
if (!response.ok) {
core.setFailed(
`Failed to create gist, response status code: ${response.status} ${response.statusText}`
);
return;
console.log(`Fetching badge failed: ${response.status} ${response.statusText}`);
if (response.status === 409) {
// This means likely the badge already exists. Try to patch
response = await fetch(hostUrl, {
method: 'PATCH',
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 {
@@ -147,64 +160,12 @@ try {
if (isSvgFile) {
content = makeBadge(data);
} else {
content = JSON.stringify(data);
content = JSON.stringify({ payload: { 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);
});
}
updateBadge(content);
} catch (error) {
core.setFailed(error);
}