From 6681122530eeb44fc0fd25baf6666a35a58bb9ed Mon Sep 17 00:00:00 2001 From: Jochen Schalanda Date: Sun, 25 Dec 2022 22:58:15 +0100 Subject: [PATCH] Add `dry-run` functionality Refs https://github.com/selfagency/mastofeedbot/issues/3 --- action.yml | 4 ++++ src/index.ts | 25 +++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index 7ed4c9d..815260f 100644 --- a/action.yml +++ b/action.yml @@ -12,6 +12,10 @@ inputs: description: 'Visibility of the posted status (public | unlisted | private | direct)' required: false default: 'public' + dry-run: + description: 'Only fetch RSS feed and update cache but skip posting to Mastodon.' + required: false + default: 'false' cache-file: description: 'Cache file' required: true diff --git a/src/index.ts b/src/index.ts index 3bc015b..2db5a70 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,7 +23,26 @@ async function writeCache(cacheFile: string, cacheLimit: number, cache: string[] } } -async function postItems(apiEndpoint: string, apiToken: string, rss: FeedEntry[], visibility: StatusVisibility, cache: string[]) { +async function postItems( + apiEndpoint: string, apiToken: string, rss: FeedEntry[], + visibility: StatusVisibility, dryRun: boolean, cache: string[]) { + if (dryRun) { + // Add new items to cache + for (const item of rss) { + try { + const hash = new SHA256Hash().hash(item.link); + core.debug(`Adding ${item.title} with hash ${hash} to cache`); + + // add the item to the cache + cache.push(hash); + } catch (e) { + core.setFailed(`Failed to ad item to cache: ${(e).message}`); + } + } + + return; + } + // authenticate with mastodon let masto: MastoClient; try { @@ -105,6 +124,8 @@ export async function main(): Promise { core.debug(`cacheLimit: ${cacheLimit}`); const statusVisibility: StatusVisibility = core.getInput('status-visibility', { trimWhitespace: true }); core.debug(`statusVisibility: ${statusVisibility}`); + const dryRun: boolean = core.getBooleanInput('dry-run'); + core.debug(`dryRun: ${dryRun}`); // get the rss feed let rss = await getRss(rssFeed); @@ -116,7 +137,7 @@ export async function main(): Promise { rss = await filterCachedItems(rss, cache); // post the new items - await postItems(apiEndpoint, apiToken, rss, statusVisibility, cache); + await postItems(apiEndpoint, apiToken, rss, statusVisibility, dryRun, cache); // write the cache await writeCache(cacheFile, cacheLimit, cache);