skillhub/.github/scripts/count-reward.ts
South Drifter 66acc6e57b
chore: add GitHub-reward form, scripts & actions (#221)
* chore: add GitHub-reward form, scripts & actions

* fix: Deno system permissions in GitHub actions

* fix: 3 detail bugs
2026-04-07 19:02:59 +08:00

66 lines
1.7 KiB
TypeScript

import { $, YAML } from "npm:zx";
import { Reward } from "./type.ts";
$.verbose = true;
const rawTags =
await $`git tag --list "reward-*" --format="%(refname:short) %(creatordate:short)"`;
const lastMonth = new Date();
lastMonth.setMonth(lastMonth.getMonth() - 1);
const lastMonthStr = lastMonth.toJSON().slice(0, 7);
const rewardTags = rawTags.stdout
.split("\n")
.filter((line) => line.split(/\s+/)[1] >= lastMonthStr)
.map((line) => line.split(/\s+/)[0]);
let rawYAML = "";
for (const tag of rewardTags)
rawYAML += (await $`git tag -l --format="%(contents)" ${tag}`) + "\n";
if (!rawYAML.trim()) {
console.warn("No reward data is found for the last month.");
process.exit(0);
}
const rewards = YAML.parse(rawYAML) as Reward[];
const groupedRewards = Object.groupBy(rewards, ({ payee }) => payee);
const summaryList = Object.entries(groupedRewards).map(([payee, rewards]) => {
const reward = rewards!.reduce(
(acc, { currency, reward }) => {
acc[currency] ??= 0;
acc[currency] += reward;
return acc;
},
{} as Record<string, number>,
);
return {
payee,
reward,
accounts: rewards!.map(({ payee: _, ...account }) => account),
};
});
const summaryText = YAML.stringify(summaryList);
console.log(summaryText);
const tagName = `statistic-${new Date().toJSON().slice(0, 7)}`;
await $`git config user.name "github-actions[bot]"`;
await $`git config user.email "github-actions[bot]@users.noreply.github.com"`;
await $`git tag -a ${tagName} $(git rev-parse HEAD) -m ${summaryText}`;
await $`git push origin --tags --no-verify`;
await $`git config unset user.name`;
await $`git config unset user.email`;
await $`gh release create ${tagName} --notes ${summaryText}`;