Skip to content

Commit

Permalink
Add dry-run functionality (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
joschi committed Dec 25, 2022
1 parent 5f8e2bd commit 31772f4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 28 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 22 additions & 22 deletions dist/index.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/index.js.map

Large diffs are not rendered by default.

25 changes: 23 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <string>new SHA256Hash().hash(<string>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: ${(<Error>e).message}`);
}
}

return;
}

// authenticate with mastodon
let masto: MastoClient;
try {
Expand Down Expand Up @@ -105,6 +124,8 @@ export async function main(): Promise<void> {
core.debug(`cacheLimit: ${cacheLimit}`);
const statusVisibility: 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);
Expand All @@ -116,7 +137,7 @@ export async function main(): Promise<void> {
rss = await filterCachedItems(<FeedEntry[]>rss, cache);

// post the new items
await postItems(apiEndpoint, apiToken, <FeedEntry[]>rss, statusVisibility, cache);
await postItems(apiEndpoint, apiToken, <FeedEntry[]>rss, statusVisibility, dryRun, cache);

// write the cache
await writeCache(cacheFile, cacheLimit, cache);
Expand Down

0 comments on commit 31772f4

Please sign in to comment.