Skip to content

Commit 5c154c6

Browse files
authored
Merge pull request #9 from onestop-techbook/stats
章追加状況のワークフローを作成
2 parents e225583 + 2eef120 commit 5c154c6

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

.github/scripts/stats.ts

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { parse, stringify } from "jsr:@std/yaml";
2+
3+
// YAMLファイルの`.re`ファイルをカウントする関数
4+
function countChapters(data: unknown): number {
5+
if (Array.isArray(data)) {
6+
return data.reduce((count, item) => count + countChapters(item), 0);
7+
} else if (typeof data === "object" && data !== null) {
8+
return Object.values(data).reduce((count, value) => count + countChapters(value), 0);
9+
} else if (typeof data === "string" && data.endsWith(".re")) {
10+
return 1;
11+
}
12+
return 0;
13+
}
14+
15+
// Slack通知を送信する関数
16+
async function sendSlackNotification(webhookUrl: string, message: string) {
17+
const response = await fetch(webhookUrl, {
18+
method: "POST",
19+
headers: { "Content-Type": "application/json" },
20+
body: JSON.stringify({ text: message }),
21+
});
22+
23+
if (!response.ok) {
24+
console.error("Failed to send Slack notification:", await response.text());
25+
Deno.exit(1);
26+
}
27+
}
28+
29+
async function main(statsFile: string, yamlFile: string) {
30+
const webhookUrl = Deno.env.get("SLACK_WEBHOOK_URL");
31+
if (!webhookUrl) {
32+
console.error("SLACK_WEBHOOK_URL is not set.");
33+
Deno.exit(1);
34+
}
35+
36+
// 前回のデータを読み込む
37+
let previousStats: { chapterCount: number } = { chapterCount: 0 };
38+
try {
39+
const statsText = await Deno.readTextFile(statsFile);
40+
previousStats = JSON.parse(statsText);
41+
} catch {
42+
console.warn("No previous stats found, using defaults.");
43+
}
44+
45+
// YAMLファイルから現在の`.re`ファイル数を取得
46+
const yamlText = await Deno.readTextFile(yamlFile);
47+
const yamlData = parse(yamlText);
48+
const currentChapterCount = countChapters(yamlData);
49+
50+
console.log(`Previous .re file count: ${previousStats.chapterCount}`);
51+
console.log(`Current .re file count: ${currentChapterCount}`);
52+
53+
// 比較と通知
54+
if (currentChapterCount > previousStats.chapterCount) {
55+
const diff = currentChapterCount - previousStats.chapterCount;
56+
const message = `:tada: 『ワンストップ学び』現在${currentChapterCount}章(昨日より${diff}章増えました)
57+
https://github.com/onestop-techbook/learning
58+
`
59+
console.log("Change detected. Sending notification...");
60+
await sendSlackNotification(webhookUrl, message);
61+
} else {
62+
console.log("No changes in .re file count.");
63+
}
64+
65+
// 新しいデータを保存
66+
const newStats = { ...previousStats, chapterCount: currentChapterCount };
67+
await Deno.writeTextFile(statsFile, JSON.stringify(newStats, null, 2));
68+
console.log("Updated stats saved.");
69+
}
70+
71+
// スクリプトの引数からファイル名を取得
72+
if (Deno.args.length !== 2) {
73+
console.error("Usage: deno run --allow-read --allow-env --allow-net stats.ts <statsFile> <yamlFile>");
74+
Deno.exit(1);
75+
}
76+
77+
const [statsFile, yamlFile] = Deno.args;
78+
await main(statsFile, yamlFile);
File renamed without changes.

.github/workflows/stats.yml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Count .re Files and Notify
2+
3+
on:
4+
schedule:
5+
- cron: '0 0 * * *'
6+
workflow_dispatch:
7+
pull_request:
8+
9+
jobs:
10+
check-and-notify:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v3
16+
17+
- name: 'Setup Deno'
18+
uses: denoland/setup-deno@v2
19+
with:
20+
deno-version: v2.x
21+
22+
# 前回のデータをキャッシュから復元
23+
- name: Restore cache
24+
uses: actions/cache@v3
25+
id: cache
26+
with:
27+
path: stats_cache.json
28+
key: daily_stats
29+
30+
- name: Initialize stats_cache.json if not restored
31+
run: |
32+
if [ ! -f stats_cache.json ]; then
33+
echo '{"chapterCount": 0}' > stats_cache.json
34+
fi
35+
36+
- name: Compare and Notify with Deno
37+
env:
38+
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
39+
run: |
40+
deno run --allow-read --allow-write --allow-env --allow-net .github/scripts/stats.ts \
41+
stats_cache.json src/catalog.yml
42+
43+
- name: Save cache
44+
uses: actions/cache@v3
45+
with:
46+
path: stats_cache.json
47+
key: daily_stats

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
stats_cache.json

0 commit comments

Comments
 (0)