diff --git a/index.ts b/index.ts index b666d04..2729423 100644 --- a/index.ts +++ b/index.ts @@ -2,6 +2,7 @@ import { parseHTML } from "linkedom" import { ndjsonToJson, prettyStringify, requireBasicAuth, tripleBool, type Change, type Entry } from "./lib"; import * as fs from "fs/promises" import { existsSync } from "fs"; +import { $ } from "bun" interface ChangeWithMessage { changes: Change[], @@ -279,10 +280,32 @@ Bun.serve({ }) }); - delete changelist_ids[id]; await updateChangelistIds(); + try { + await $`git add artists.ndjson`; + await $`git commit -m "Auto-merge."`; + + const push = await $`git push origin main`.nothrow(); + const out = `${push.stdout}${push.stderr}`; + + if (out.includes("its remote counterpart")) { + await $`git pull --rebase`; + const retry = await $`git push origin main`.nothrow(); + + const retryOut = `${retry.stdout}${retry.stderr}`; + if (!retryOut.includes("main -> main")) { + throw new Error("Push failed after rebase"); + } + } else if (!out.includes("main -> main")) { + throw new Error("Push failed"); + } + } catch (err) { + console.error("Error:", err); + return new ClientResponse("Failed to automerge to git. Error: " + err, { status: 400 }); + } + return new ClientResponse("Merged successfully."); } },