From dcf1a456841964517c1edb1859c2a88240ed89b3 Mon Sep 17 00:00:00 2001 From: Jan Andrle Date: Fri, 28 Jun 2024 14:01:41 +0200 Subject: [PATCH] :zap: cron + git --- .gitea/workflows/pull.yaml | 6 +++--- mastodonCzStats.js | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/.gitea/workflows/pull.yaml b/.gitea/workflows/pull.yaml index b002d17..8432751 100644 --- a/.gitea/workflows/pull.yaml +++ b/.gitea/workflows/pull.yaml @@ -1,8 +1,8 @@ -name: Update RSS +name: Collect month data on: workflow_dispatch: schedule: - - cron: '39 19 * * *' # daily at 19:39 Prague + - cron: '0 12 1 * *' # At 12:00 Prague on the first day of the month jobs: update-rss: @@ -14,4 +14,4 @@ jobs: node-version: lts/* cache: 'npm' - run: npm ci - - run: npx nodejsscript cli.mjs pull --git + - run: npx nodejsscript mastodonCzStats.js --git diff --git a/mastodonCzStats.js b/mastodonCzStats.js index 88c8c42..dfa769f 100755 --- a/mastodonCzStats.js +++ b/mastodonCzStats.js @@ -9,7 +9,7 @@ $.api("", true) .version(version) .describe(description) .option("--git", "Commit changes to git") -.action(async function main(){ +.action(async function main({ git: is_git }){ let stats= []; for(const instance of instances_cz_pre){ echo.use("-R", "Shromažďuji instance: " + instance); @@ -28,11 +28,24 @@ $.api("", true) instances_cz: stats.sort((a, b)=> b.registrations-a.registrations), sum: stats.reduce((acc, stats) => sumStats([acc, stats]), { statuses: 0, logins: 0, registrations: 0, mau: 0, user_count: 0 }) }; - echo(JSON.stringify(out, null, " ")).to("./mastodonCzStats.json"); + echo(JSON.stringify(out, null, " ")).to(store); + if(is_git) gitCommit([ store ], "cron"); $.exit(0); }) .parse(); +function gitCommit(files, des= "not specified"){ + if(!files.length || !s.run`git diff --numstat`.trim()) + return echo("Nothig todo"); + + echo("Diff to save"); + s.run`git config user.name "Bot"`; + s.run`git config user.email "${"zc.murtnec@naj.elrdna".split("").reverse().join("")}"`; + s.run`git add ${files}`; + s.run`git commit -m "Updated by bot – ${des}"`; + s.run`git push`; + s.run`git config --remove-section user`; +} async function* collectStats(instances, weeks = 1) { for(const instance of instances){ const { stats, mau, user_count }= await fetchInstanceStats(instance).catch(e=> ({}));