-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcount-reward.ts
More file actions
57 lines (39 loc) · 1.61 KB
/
count-reward.ts
File metadata and controls
57 lines (39 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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()) throw new ReferenceError('No reward data is found for the last month.');
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 --global user.name "github-actions[bot]"`;
await $`git config --global 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`;
await $`gh release create ${tagName} --notes ${summaryText}`;