88 lines
2.7 KiB
JavaScript
88 lines
2.7 KiB
JavaScript
import https from "node:https";
|
|
import process from "node:process";
|
|
|
|
const token = process.env.GITEA_TOKEN;
|
|
const owner = "Henry_Du";
|
|
const repo = "BYWCloudreve";
|
|
const apiBase = "https://gitea.fse-media.group/api/v1";
|
|
|
|
const agent = new https.Agent({ rejectUnauthorized: false });
|
|
|
|
function request(method, path, body) {
|
|
return new Promise((resolve, reject) => {
|
|
const url = new URL(apiBase + path);
|
|
const data = body ? JSON.stringify(body) : null;
|
|
const req = https.request(
|
|
{
|
|
method,
|
|
hostname: url.hostname,
|
|
port: url.port || 443,
|
|
path: url.pathname + url.search,
|
|
headers: {
|
|
Authorization: `token ${token}`,
|
|
Accept: "application/json",
|
|
"Content-Type": "application/json",
|
|
...(data ? { "Content-Length": Buffer.byteLength(data) } : {}),
|
|
},
|
|
agent,
|
|
},
|
|
(res) => {
|
|
let chunks = "";
|
|
res.setEncoding("utf8");
|
|
res.on("data", (c) => (chunks += c));
|
|
res.on("end", () => {
|
|
const ok = res.statusCode >= 200 && res.statusCode < 300;
|
|
let parsed = chunks;
|
|
try {
|
|
parsed = JSON.parse(chunks);
|
|
} catch {
|
|
/* noop */
|
|
}
|
|
if (ok) resolve(parsed);
|
|
else reject(new Error(`HTTP ${res.statusCode}: ${typeof parsed === "string" ? parsed : JSON.stringify(parsed)}`));
|
|
});
|
|
}
|
|
);
|
|
req.on("error", reject);
|
|
if (data) req.write(data);
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
async function main() {
|
|
if (!token) {
|
|
console.error("GITEA_TOKEN is missing");
|
|
process.exit(2);
|
|
}
|
|
const action = process.argv[2] || "status";
|
|
if (action === "status") {
|
|
const runs = await request("GET", `/repos/${owner}/${repo}/actions/runs?limit=5`);
|
|
console.log(JSON.stringify(runs, null, 2));
|
|
return;
|
|
}
|
|
if (action === "dispatch") {
|
|
const workflows = await request("GET", `/repos/${owner}/${repo}/actions/workflows`);
|
|
const wf = (workflows.workflows || []).find((w) => /build-deploy/.test(w.path) || /build/.test(w.name.toLowerCase()));
|
|
if (!wf) {
|
|
console.error("No workflow found, workflows available:", workflows.workflows?.map((w) => w.path));
|
|
process.exit(3);
|
|
}
|
|
console.log("Dispatching workflow:", wf.path, "id:", wf.id);
|
|
await request("POST", `/repos/${owner}/${repo}/actions/workflows/${wf.id}/dispatches`, {
|
|
ref: "main",
|
|
inputs: { logLevel: "info" },
|
|
});
|
|
console.log("Dispatched OK");
|
|
const runs = await request("GET", `/repos/${owner}/${repo}/actions/runs?limit=3`);
|
|
console.log(JSON.stringify(runs, null, 2));
|
|
return;
|
|
}
|
|
console.error("Unknown action:", action);
|
|
process.exit(1);
|
|
}
|
|
|
|
main().catch((e) => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
});
|