Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dry-run functionality #11

Merged
merged 2 commits into from
Dec 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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