224 lines
8.9 KiB
YAML
224 lines
8.9 KiB
YAML
name: Repository Metrics
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- main
|
|
workflow_dispatch:
|
|
|
|
concurrency:
|
|
group: loc-history-main
|
|
cancel-in-progress: true
|
|
|
|
permissions:
|
|
contents: write
|
|
|
|
env:
|
|
GITEA_HOST: ${{ vars.GITEA_HOST || 'git.tool-ctrl.de' }}
|
|
HIVEGRID_GITEA_HOST_IP: ${{ vars.HIVEGRID_GITEA_HOST_IP }}
|
|
LOC_METRICS_OUTPUT_DIR: /tmp/hivegrid-loc-history
|
|
SOURCE_REF: ${{ gitea.ref }}
|
|
SOURCE_SHA: ${{ gitea.sha }}
|
|
|
|
jobs:
|
|
publish:
|
|
name: Generate and publish repository metrics
|
|
runs-on: toolctrl-ci
|
|
timeout-minutes: 10
|
|
steps:
|
|
- name: Resolve internal Gitea host
|
|
run: |
|
|
if [ -n "${HIVEGRID_GITEA_HOST_IP:-}" ]; then
|
|
if ! HIVEGRID_GITEA_HOST_IP="$HIVEGRID_GITEA_HOST_IP" node -e 'process.exit(require("node:net").isIP(process.env.HIVEGRID_GITEA_HOST_IP || "") ? 0 : 1)'; then
|
|
echo "HIVEGRID_GITEA_HOST_IP must be a single IPv4 or IPv6 token"
|
|
exit 1
|
|
fi
|
|
printf '%s git.tool-ctrl.de\n' "$HIVEGRID_GITEA_HOST_IP" >> /etc/hosts
|
|
fi
|
|
|
|
- name: Checkout full mainline history
|
|
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
|
|
with:
|
|
fetch-depth: 0
|
|
persist-credentials: false
|
|
|
|
- name: Set up Node.js
|
|
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
|
|
with:
|
|
node-version-file: .nvmrc
|
|
|
|
- name: Persist Node.js toolchain
|
|
run: bash scripts/ci/persist-node-toolchain.sh
|
|
|
|
- name: Verify source revision
|
|
run: |
|
|
set -eu
|
|
if [ "$SOURCE_REF" != "refs/heads/main" ]; then
|
|
echo "Repository metrics may only be published from refs/heads/main"
|
|
exit 1
|
|
fi
|
|
if ! printf '%s' "$SOURCE_SHA" | grep -Eq '^[0-9a-f]{40}$'; then
|
|
echo "Gitea SHA must be a full 40-character commit SHA"
|
|
exit 1
|
|
fi
|
|
if [ "$(git rev-parse HEAD)" != "$SOURCE_SHA" ]; then
|
|
echo "Checked-out HEAD does not match the Gitea event SHA"
|
|
exit 1
|
|
fi
|
|
|
|
- name: Generate repository metrics
|
|
run: |
|
|
mkdir -p "$LOC_METRICS_OUTPUT_DIR"
|
|
node scripts/metrics/loc-history.mjs \
|
|
--ref HEAD \
|
|
--label origin/main \
|
|
--output-dir "$LOC_METRICS_OUTPUT_DIR"
|
|
node scripts/metrics/commit-history.mjs \
|
|
--ref HEAD \
|
|
--label origin/main \
|
|
--days 60 \
|
|
--output-dir "$LOC_METRICS_OUTPUT_DIR"
|
|
test -s "$LOC_METRICS_OUTPUT_DIR/commit-history.csv"
|
|
test -s "$LOC_METRICS_OUTPUT_DIR/commit-history.svg"
|
|
test -s "$LOC_METRICS_OUTPUT_DIR/loc-history.csv"
|
|
test -s "$LOC_METRICS_OUTPUT_DIR/loc-history.svg"
|
|
|
|
- name: Verify repository-scoped Actions token
|
|
env:
|
|
LOC_METRICS_GITEA_TOKEN: ${{ gitea.token }}
|
|
run: |
|
|
node --input-type=module <<'NODE'
|
|
const host = process.env.GITEA_HOST ?? "";
|
|
const token = process.env.LOC_METRICS_GITEA_TOKEN ?? "";
|
|
|
|
if (!/^[A-Za-z0-9.-]+(?::\d+)?$/.test(host)) {
|
|
throw new Error("GITEA_HOST must be a hostname with an optional port");
|
|
}
|
|
if (!token) {
|
|
throw new Error("The built-in Gitea Actions token is required");
|
|
}
|
|
|
|
const response = await fetch(`https://${host}/api/v1/repos/toolctrl-labs/HiveGrid-Cloud`, {
|
|
headers: { Accept: "application/json", Authorization: `token ${token}` },
|
|
signal: AbortSignal.timeout(15_000),
|
|
});
|
|
if (!response.ok) {
|
|
throw new Error(`Actions token repository lookup failed with HTTP ${response.status}`);
|
|
}
|
|
|
|
const repository = await response.json();
|
|
if (
|
|
repository.full_name !== "toolctrl-labs/HiveGrid-Cloud" ||
|
|
repository.private !== true ||
|
|
repository.permissions?.pull !== true ||
|
|
repository.permissions?.push !== true ||
|
|
repository.permissions?.admin === true
|
|
) {
|
|
throw new Error("Actions token must have non-admin write access to exactly HiveGrid-Cloud");
|
|
}
|
|
const releases = await fetch(`https://${host}/api/v1/repos/toolctrl-labs/HiveGrid-Cloud/releases?limit=1`, {
|
|
headers: { Accept: "application/json", Authorization: `token ${token}` },
|
|
signal: AbortSignal.timeout(15_000),
|
|
});
|
|
if (releases.status === 403) {
|
|
throw new Error(
|
|
"The Actions token cannot read releases. Releases sit in Gitea's repository scope, which the workflow grants via `permissions: contents: write` — `code: write` only covers Git pushes.",
|
|
);
|
|
}
|
|
if (!releases.ok) {
|
|
throw new Error(`Actions token release lookup failed with HTTP ${releases.status}`);
|
|
}
|
|
|
|
console.log("Verified repository-scoped non-admin Actions write token with release access");
|
|
NODE
|
|
|
|
- name: Publish repository metrics as release assets
|
|
env:
|
|
LOC_METRICS_GITEA_TOKEN: ${{ gitea.token }}
|
|
run: |
|
|
node --input-type=module <<'NODE'
|
|
import { readFile } from "node:fs/promises";
|
|
|
|
// The LOC artefacts used to live on a long-lived `loc-metrics` branch, which
|
|
// showed up as abandoned work in every branch listing. They are release
|
|
// assets now: one reusable container, stable download URLs for the README,
|
|
// and nothing pushed into the repository's Git history.
|
|
const REPOSITORY = "toolctrl-labs/HiveGrid-Cloud";
|
|
const TAG = "loc-metrics";
|
|
const FILES = ["commit-history.csv", "commit-history.svg", "loc-history.csv", "loc-history.svg"];
|
|
|
|
const host = process.env.GITEA_HOST ?? "";
|
|
const token = process.env.LOC_METRICS_GITEA_TOKEN ?? "";
|
|
const outputDir = process.env.LOC_METRICS_OUTPUT_DIR ?? "";
|
|
const sourceSha = process.env.SOURCE_SHA ?? "";
|
|
|
|
if (!/^[A-Za-z0-9.-]+(?::\d+)?$/.test(host)) {
|
|
throw new Error("GITEA_HOST must be a hostname with an optional port");
|
|
}
|
|
if (!token) {
|
|
throw new Error("The built-in Gitea Actions token is required");
|
|
}
|
|
|
|
const api = `https://${host}/api/v1/repos/${REPOSITORY}`;
|
|
async function call(path, init = {}) {
|
|
return fetch(`${api}${path}`, {
|
|
...init,
|
|
headers: { Accept: "application/json", Authorization: `token ${token}`, ...(init.headers ?? {}) },
|
|
signal: AbortSignal.timeout(30_000),
|
|
});
|
|
}
|
|
|
|
let release;
|
|
const lookup = await call(`/releases/tags/${TAG}`);
|
|
if (lookup.ok) {
|
|
release = await lookup.json();
|
|
} else if (lookup.status === 404) {
|
|
const created = await call("/releases", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
tag_name: TAG,
|
|
target_commitish: sourceSha,
|
|
name: "Repository metrics",
|
|
body: "Automatically maintained LOC and commit-history artefacts. Not a product release.",
|
|
draft: false,
|
|
prerelease: false,
|
|
}),
|
|
});
|
|
if (!created.ok) {
|
|
throw new Error(`Creating the ${TAG} release failed with HTTP ${created.status}`);
|
|
}
|
|
release = await created.json();
|
|
} else {
|
|
throw new Error(`Looking up the ${TAG} release failed with HTTP ${lookup.status}`);
|
|
}
|
|
|
|
const listed = await call(`/releases/${release.id}/assets`);
|
|
if (!listed.ok) {
|
|
throw new Error(`Listing release assets failed with HTTP ${listed.status}`);
|
|
}
|
|
const assets = await listed.json();
|
|
|
|
for (const name of FILES) {
|
|
// Gitea cannot replace an asset in place, so the old one is removed
|
|
// first. The README link is unavailable for those few seconds.
|
|
for (const asset of assets.filter((candidate) => candidate.name === name)) {
|
|
const removed = await call(`/releases/${release.id}/assets/${asset.id}`, { method: "DELETE" });
|
|
if (!removed.ok && removed.status !== 404) {
|
|
throw new Error(`Deleting the previous ${name} failed with HTTP ${removed.status}`);
|
|
}
|
|
}
|
|
|
|
const attachment = new FormData();
|
|
attachment.set("attachment", new Blob([await readFile(`${outputDir}/${name}`)]), name);
|
|
const uploaded = await call(`/releases/${release.id}/assets?name=${encodeURIComponent(name)}`, {
|
|
method: "POST",
|
|
body: attachment,
|
|
});
|
|
if (!uploaded.ok) {
|
|
throw new Error(`Uploading ${name} failed with HTTP ${uploaded.status}`);
|
|
}
|
|
console.log(`Published ${name} to the ${TAG} release`);
|
|
}
|
|
NODE
|