You've already forked dynamic-badges-action
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
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:
18
action.yml
18
action.yml
@@ -8,13 +8,7 @@ branding:
|
|||||||
color: "green"
|
color: "green"
|
||||||
inputs:
|
inputs:
|
||||||
auth:
|
auth:
|
||||||
description: "Your secret with the gist scope"
|
description: "Your secret token"
|
||||||
required: true
|
|
||||||
gistID:
|
|
||||||
description: "The ID of the gist to use"
|
|
||||||
required: true
|
|
||||||
filename:
|
|
||||||
description: "The *.json or *.svg filename of the badge data"
|
|
||||||
required: true
|
required: true
|
||||||
label:
|
label:
|
||||||
description: "The left text of the badge"
|
description: "The left text of the badge"
|
||||||
@@ -23,13 +17,9 @@ inputs:
|
|||||||
description: "The right text of the badge"
|
description: "The right text of the badge"
|
||||||
required: true
|
required: true
|
||||||
host:
|
host:
|
||||||
description: "The base URL of the gist API"
|
description: "The base URL of the badgestor API"
|
||||||
default: "https://api.github.com/gists/"
|
default: "https://badges.mydomain.com/"
|
||||||
required: false
|
required: true
|
||||||
forceUpdate:
|
|
||||||
description: "If set to true, the gist will be updated even if the content did not change"
|
|
||||||
default: "false"
|
|
||||||
required: false
|
|
||||||
labelColor:
|
labelColor:
|
||||||
description: "The left color of the badge"
|
description: "The left color of the badge"
|
||||||
required: false
|
required: false
|
||||||
|
|||||||
91
index.js
91
index.js
@@ -9,35 +9,48 @@
|
|||||||
import core from "@actions/core";
|
import core from "@actions/core";
|
||||||
import { makeBadge } from "badge-maker";
|
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
|
// 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
|
// required as defined in https://developer.github.com/v3/#user-agent-required
|
||||||
async function updateGist(body) {
|
async function updateBadge(body) {
|
||||||
const headers = new Headers([
|
const headers = new Headers([
|
||||||
["Content-Type", "application/json"],
|
["Content-Type", "application/json"],
|
||||||
["Content-Length", new TextEncoder().encode(body).length],
|
["Content-Length", new TextEncoder().encode(body).length],
|
||||||
["User-Agent", "Schneegans"],
|
["User-Agent", "gitea-dynamic-badges"],
|
||||||
["Authorization", `token ${core.getInput("auth")}`],
|
["x-api-key", `${core.getInput("auth")}`],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const response = await fetch(gistUrl, {
|
let response = await fetch(hostUrl, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers,
|
headers,
|
||||||
body,
|
body,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
core.setFailed(
|
console.log(`Fetching badge failed: ${response.status} ${response.statusText}`);
|
||||||
`Failed to create gist, response status code: ${response.status} ${response.statusText}`
|
if (response.status === 409) {
|
||||||
);
|
// This means likely the badge already exists. Try to patch
|
||||||
|
response = await fetch(hostUrl, {
|
||||||
return;
|
method: 'PATCH',
|
||||||
|
headers,
|
||||||
|
body,
|
||||||
|
});
|
||||||
|
if (!response.ok) {
|
||||||
|
core.setFailed(
|
||||||
|
`Failed to create gist, response status code: ${response.status} ${response.statusText}`
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("Success!");
|
console.log("Success!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// We wrap the entire action in a try / catch block so we can set it to "failed" if
|
// We wrap the entire action in a try / catch block so we can set it to "failed" if
|
||||||
// something goes wrong.
|
// something goes wrong.
|
||||||
try {
|
try {
|
||||||
@@ -147,64 +160,12 @@ try {
|
|||||||
if (isSvgFile) {
|
if (isSvgFile) {
|
||||||
content = makeBadge(data);
|
content = makeBadge(data);
|
||||||
} else {
|
} 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
|
updateBadge(content);
|
||||||
// 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) {
|
} catch (error) {
|
||||||
core.setFailed(error);
|
core.setFailed(error);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user