diff --git a/.gitea/workflows/pull.yaml b/.gitea/workflows/pull.yaml index 262e0cd..37aa118 100644 --- a/.gitea/workflows/pull.yaml +++ b/.gitea/workflows/pull.yaml @@ -2,7 +2,7 @@ name: Update RSS on: workflow_dispatch: schedule: - - cron: '0 3 * * *' # daily at 3am + - cron: '57 10 * * *' # daily at 10:57 jobs: update-rss: @@ -14,4 +14,4 @@ jobs: node-version: lts/* cache: 'npm' - run: npm ci - - run: npx nodejsscript cli.js pull + - run: npx nodejsscript cli.mjs pull --git diff --git a/cli.mjs b/cli.mjs index 1b3282d..f0b4f83 100755 --- a/cli.mjs +++ b/cli.mjs @@ -2,6 +2,10 @@ /* jshint esversion: 11,-W097, -W040, module: true, node: true, expr: true, undef: true *//* global echo, $, pipe, s, fetch, cyclicLoop */ const url_drops= "https://pagenotfound.cz/drop/"; const { version, description }= s.cat("package.json").xargs(JSON.parse); +const paths= { + sitemap: "sitemap.json", + rss: "rss.xml", +}; /** * @typedef {Object} Article * @property {string} title @@ -25,16 +29,31 @@ $.api() .version(version) .describe(description) .command("pull", "Update article list") -.action(async function pull(){ +.option("--git", "Update git repository") +.action(async function pull({ git: is_git= false }){ const json= await sitemap(); toRSS(json); + if(is_git) + gitCommit(Object.values(paths)); $.exit(0); }) .parse(); +function gitCommit(files, des= "not specified"){ + if(!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`; +} /** @param {Sitemap} json */ async function toRSS(json){ - const path= "rss.xml"; + const path= paths.rss; const host= "https://pagenotfound.cz"; const articles= json.articles.map(function({ title, perex, author, loc, drop }){ return [ @@ -64,7 +83,7 @@ async function toRSS(json){ } import { JSDOM } from "jsdom"; async function sitemap(){ - const path= "sitemap.json"; + const path= paths.sitemap; /** @type {Sitemap} */ const json= s.test("-f", path) ? s.cat(path).xargs(JSON.parse) : { drops: [], articles: [] };