You've already forked dynamic-badges-action
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c5522a81a6 | |||
| 6440ffc708 | |||
| 001178768a | |||
| 4ba87a7f3c | |||
| 22fd46189b | |||
| 8b04e20b05 | |||
| 1952930e4d | |||
| 2329d5dd80 | |||
| 46dee54362 | |||
| 5fce6bdccf | |||
| 33748372a2 | |||
| b348384cf9 | |||
|
|
7142847813 | ||
|
|
51bbdc7e8a |
35
.github/workflows/badges.yml
vendored
35
.github/workflows/badges.yml
vendored
@@ -13,32 +13,16 @@ jobs:
|
|||||||
- name: Get the Numbers
|
- name: Get the Numbers
|
||||||
run: echo "ANSWER=42" >> $GITHUB_ENV
|
run: echo "ANSWER=42" >> $GITHUB_ENV
|
||||||
- name: Create the Badge
|
- name: Create the Badge
|
||||||
uses: schneegans/dynamic-badges-action@master
|
uses: https://hub.cybercinch.nz/cybercinch/dynamic-badges-action@master
|
||||||
with:
|
with:
|
||||||
auth: ${{ secrets.GIST_SECRET }}
|
auth: ${{ secrets.BADGE_API_TOKEN }}
|
||||||
gistID: 2ab8f1d386f13aaebccbd87dac94068d
|
host: https://badges.cybercinch.nz/cybercinch/dynamic-badges-action/answer
|
||||||
filename: answer.json
|
|
||||||
label: The Answer
|
label: The Answer
|
||||||
message: is ${{ env.ANSWER }}
|
message: is ${{ env.ANSWER }}
|
||||||
valColorRange: ${{ env.ANSWER }}
|
valColorRange: ${{ env.ANSWER }}
|
||||||
maxColorRange: 100
|
maxColorRange: 100
|
||||||
minColorRange: 0
|
minColorRange: 0
|
||||||
|
|
||||||
svg-badge:
|
|
||||||
name: Create SVG Badge
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Create the Badge
|
|
||||||
uses: schneegans/dynamic-badges-action@master
|
|
||||||
with:
|
|
||||||
auth: ${{ secrets.GIST_SECRET }}
|
|
||||||
gistID: 2ab8f1d386f13aaebccbd87dac94068d
|
|
||||||
filename: badge.svg
|
|
||||||
label: This is
|
|
||||||
message: an SVG badge
|
|
||||||
color: red
|
|
||||||
labelColor: blue
|
|
||||||
|
|
||||||
color-range-badges:
|
color-range-badges:
|
||||||
name: "Create Color Range Badges"
|
name: "Create Color Range Badges"
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@@ -57,18 +41,11 @@ jobs:
|
|||||||
- 90
|
- 90
|
||||||
- 100
|
- 100
|
||||||
steps:
|
steps:
|
||||||
- name: "Stagger"
|
|
||||||
run: |
|
|
||||||
# Gist returns 500 if we try to update many files in the same gist all at once.
|
|
||||||
# Thanks to Ned Batchelder for this idea!
|
|
||||||
# https://nedbatchelder.com/blog/202209/making_a_coverage_badge.html
|
|
||||||
sleep $( expr ${{ matrix.total }} / 10 )
|
|
||||||
- name: "Make badge"
|
- name: "Make badge"
|
||||||
uses: schneegans/dynamic-badges-action@master
|
uses: https://hub.cybercinch.nz/cybercinch/dynamic-badges-action@master
|
||||||
with:
|
with:
|
||||||
auth: ${{ secrets.GIST_SECRET }}
|
auth: ${{ secrets.BADGE_API_TOKEN }}
|
||||||
gistID: 2ab8f1d386f13aaebccbd87dac94068d
|
host: https://badges.cybercinch.nz/cybercinch/dynamic-badges-action/color_badge_${{ matrix.total }}
|
||||||
filename: color_badge_${{ matrix.total }}.json
|
|
||||||
label: Coverage
|
label: Coverage
|
||||||
message: ${{ matrix.total }}%
|
message: ${{ matrix.total }}%
|
||||||
minColorRange: 50
|
minColorRange: 50
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ Embed the badge with:
|
|||||||
|
|
||||||
| Parameter | Description | Supported in SVG Mode |
|
| Parameter | Description | Supported in SVG Mode |
|
||||||
| ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------- |
|
| ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------- |
|
||||||
| `host` | Default is `https://api.github.com/gists/`. You can change this if you're using GitHub enterprise. The URL will be something like `github-enterprise-hostname/api/v3/gists`. | ✅ |
|
| `host` | Default is `https://api.github.com/gists/`. You can change this if you're using GitHub enterprise. The URL will be something like `https://github-enterprise-hostname/api/v3/gists/`. | ✅ |
|
||||||
| `forceUpdate` | Default is `false`. If set to `true`, the gist will be updated even if the content did not change. | ✅ |
|
| `forceUpdate` | Default is `false`. If set to `true`, the gist will be updated even if the content did not change. | ✅ |
|
||||||
|
|
||||||
### Shields.io Parameters (optional)
|
### Shields.io Parameters (optional)
|
||||||
@@ -175,6 +175,10 @@ For all values in between, the color will be interpolated.
|
|||||||
minColorRange: 0
|
minColorRange: 0
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Self-hosted runner compatibility
|
||||||
|
|
||||||
|
The current action is compatible with `node20` only. In order to use it with a self-hosted runner, you must use runner with `node20` installed. Your runner version must be equal or higher than [v2.308.0](https://github.com/actions/runner/releases/tag/v2.308.0).
|
||||||
|
|
||||||
## Contributing to Dynamic Badges Action
|
## Contributing to Dynamic Badges Action
|
||||||
|
|
||||||
Whenever you encounter a :beetle: **bug** or have :tada: **feature request**,
|
Whenever you encounter a :beetle: **bug** or have :tada: **feature request**,
|
||||||
|
|||||||
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
|
||||||
|
|||||||
120
index.js
120
index.js
@@ -8,36 +8,56 @@
|
|||||||
|
|
||||||
import core from "@actions/core";
|
import core from "@actions/core";
|
||||||
import { makeBadge } from "badge-maker";
|
import { makeBadge } from "badge-maker";
|
||||||
|
import { createRequire } from "module";
|
||||||
|
const require = createRequire(import.meta.url);
|
||||||
|
|
||||||
const gistUrl = new URL(core.getInput("gistID"), core.getInput("host"));
|
const util = require('util');
|
||||||
|
|
||||||
|
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, {
|
console.log("Making post request to: %s", hostUrl);
|
||||||
|
|
||||||
|
const response = await fetch(hostUrl, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers,
|
headers,
|
||||||
body,
|
body,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
core.setFailed(
|
console.log("Returned: %j", response.body);
|
||||||
`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
|
||||||
|
console.log("Running patch on %s", hostUrl);
|
||||||
|
const response2 = await fetch(hostUrl, {
|
||||||
|
method: 'PATCH',
|
||||||
|
headers,
|
||||||
|
body,
|
||||||
|
});
|
||||||
|
if (!response2.ok) {
|
||||||
|
core.setFailed(
|
||||||
|
`Failed to create gist, response status code: ${response.status} ${response.statusText}`
|
||||||
|
);
|
||||||
|
|
||||||
return;
|
|
||||||
|
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 {
|
||||||
@@ -45,16 +65,11 @@ try {
|
|||||||
// and message attributes are always required. All others are optional and added to the
|
// and message attributes are always required. All others are optional and added to the
|
||||||
// content object only if they are given to the action.
|
// content object only if they are given to the action.
|
||||||
let data = {
|
let data = {
|
||||||
|
schemaVersion: 1,
|
||||||
label: core.getInput("label"),
|
label: core.getInput("label"),
|
||||||
message: core.getInput("message"),
|
message: core.getInput("message"),
|
||||||
};
|
};
|
||||||
|
|
||||||
const filename = core.getInput("filename");
|
|
||||||
const isSvgFile = filename.endsWith(".svg");
|
|
||||||
|
|
||||||
if (!isSvgFile) {
|
|
||||||
data.schemaVersion = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compute the message color based on the given inputs.
|
// Compute the message color based on the given inputs.
|
||||||
const color = core.getInput("color");
|
const color = core.getInput("color");
|
||||||
@@ -110,27 +125,27 @@ try {
|
|||||||
data.labelColor = labelColor;
|
data.labelColor = labelColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isSvgFile && isError != "") {
|
if (isError != "") {
|
||||||
data.isError = isError;
|
data.isError = isError;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isSvgFile && namedLogo != "") {
|
if (namedLogo != "") {
|
||||||
data.namedLogo = namedLogo;
|
data.namedLogo = namedLogo;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isSvgFile && logoSvg != "") {
|
if (logoSvg != "") {
|
||||||
data.logoSvg = logoSvg;
|
data.logoSvg = logoSvg;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isSvgFile && logoColor != "") {
|
if (logoColor != "") {
|
||||||
data.logoColor = logoColor;
|
data.logoColor = logoColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isSvgFile && logoWidth != "") {
|
if (logoWidth != "") {
|
||||||
data.logoWidth = parseInt(logoWidth);
|
data.logoWidth = parseInt(logoWidth);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isSvgFile && logoPosition != "") {
|
if (logoPosition != "") {
|
||||||
data.logoPosition = logoPosition;
|
data.logoPosition = logoPosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,73 +153,18 @@ try {
|
|||||||
data.style = style;
|
data.style = style;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isSvgFile && cacheSeconds != "") {
|
if (cacheSeconds != "") {
|
||||||
data.cacheSeconds = parseInt(cacheSeconds);
|
data.cacheSeconds = parseInt(cacheSeconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
let content = "";
|
let content = "";
|
||||||
|
|
||||||
if (isSvgFile) {
|
content = JSON.stringify({ payload: data });
|
||||||
content = makeBadge(data);
|
|
||||||
} else {
|
|
||||||
content = JSON.stringify(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
// For the POST request, the above content is set as file contents for the
|
console.log("Body of request: %s", content);
|
||||||
// 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