Skip to content

Commit

Permalink
Draft
Browse files Browse the repository at this point in the history
  • Loading branch information
katydecorah committed Jan 24, 2024
1 parent e8f5508 commit 267b58e
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 7 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/bookmarks-advanced.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Add bookmark with additional properties
run-name: Add bookmark (${{ inputs.url }})

on:
workflow_dispatch:
inputs:
url:
description: The URL to bookmark.
required: true
type: string
notes:
description: Notes about the bookmark.
type: string
date:
description: Date (YYYY-MM-DD). The default date is today.
type: string
tags:
description: Add tags to categorize the bookmark. Separate each tag with a comma. Optional.
type: string
rating:
description: Rate the bookmark from 1 to 5. Optional.
type: string
quote:
description: Add a quote from the bookmark. Optional.
type: string

jobs:
add-bookmark:
runs-on: macOS-latest
name: Add bookmark
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Bookmark action
uses: ./
with:
filename: _data/recipes.json
additional-properties: rating,quote
- name: Download the thumbnail image
run: curl "${{ env.BookmarkImage }}" -o "img/${{ env.BookmarkImageOutput }}"
if: env.BookmarkImage != ''
- name: Commit files
run: |
git pull
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git add -A && git commit -m "Bookmark ${{ env.BookmarkTitle }}"
git push
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ inputs:
export-image:
description: "Export the URL's `image` to download later and set `image` property."
default: "true"
additional-properties:
description: "Additional properties to add to the bookmark from the workflow payload formatted as a comma delimited string."
default: ""
21 changes: 17 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68188,7 +68188,7 @@ var get_metadata_awaiter = (undefined && undefined.__awaiter) || function (thisA



function getMetadata({ url, notes, date, tags, }) {
function getMetadata({ url, notes, date, tags, additionalProperties, }) {
var _a, _b;
return get_metadata_awaiter(this, void 0, void 0, function* () {
try {
Expand All @@ -68201,9 +68201,9 @@ function getMetadata({ url, notes, date, tags, }) {
if (!waybackUrl) {
(0,core.warning)(`No wayback url found for ${url}`);
}
return Object.assign(Object.assign(Object.assign({ title: result.ogTitle || "", site: result.ogSiteName || "", author: result.author || "", date, description: result.ogDescription || "", url: result.ogUrl || result.requestUrl || url, image, type: result.ogType || "" }, (notes && { notes })), (tags && { tags: toArray(tags) })), (waybackUrl && {
return Object.assign(Object.assign(Object.assign(Object.assign({ title: result.ogTitle || "", site: result.ogSiteName || "", author: result.author || "", date, description: result.ogDescription || "", url: result.ogUrl || result.requestUrl || url, image, type: result.ogType || "" }, (notes && { notes })), (tags && { tags: toArray(tags) })), (waybackUrl && {
waybackUrl,
}));
})), additionalProperties);
}
catch (error) {
throw new Error(`Error getting metadata for ${url}: ${error.result.error}`);
Expand Down Expand Up @@ -68250,7 +68250,20 @@ function action() {
const date = payload.date || new Date().toISOString().slice(0, 10);
(0,core.exportVariable)("DateBookmarked", date);
const filename = (0,core.getInput)("filename");
const page = (yield getMetadata({ url, notes, date, tags }));
const additionalPropertiesList = (0,core.getInput)("additional-properties")
? toArray((0,core.getInput)("additional-properties"))
: undefined;
const additionalProperties = additionalPropertiesList === null || additionalPropertiesList === void 0 ? void 0 : additionalPropertiesList.reduce((acc, property) => {
acc[property] = payload[property];
return acc;
}, {});
const page = (yield getMetadata({
url,
notes,
date,
tags,
additionalProperties,
}));
const bookmarks = yield addBookmark(filename, page);
if (!bookmarks) {
(0,core.setFailed)(`Unable to add bookmark`);
Expand Down
63 changes: 63 additions & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,69 @@ describe("bookmark", () => {
`);
});

test("works, additional properties", async () => {
jest.useFakeTimers().setSystemTime(new Date("2022-09-11"));

// eslint-disable-next-line no-import-assign
Object.defineProperty(github, "context", {
value: {
payload: {
inputs: {
url: "https://katydecorah.com",
notes: "note",
prop1: "my property",
anotherProp: "another property",
},
},
},
});

ogs.mockResolvedValueOnce({ result: pen15 });
jest.spyOn(core, "getInput").mockImplementation((v) => {
switch (v) {
case "filename":
return "_data/recipes.json";
case "additional-properties":
return "prop1, anotherProp";
case "export-image":
return "true";
default:
return "";
}
});
jest.spyOn(promises, "readFile").mockResolvedValueOnce(JSON.stringify([]));
const writeFileSpy = jest.spyOn(promises, "writeFile").mockImplementation();

await action();
expect(exportVariable).toHaveBeenNthCalledWith(
1,
"DateBookmarked",
new Date().toISOString().slice(0, 10)
);
expect(setFailed).not.toHaveBeenCalled();
expect(writeFileSpy.mock.calls[0]).toMatchInlineSnapshot(`
[
"_data/recipes.json",
"[
{
"title": "PEN15",
"site": "Hulu",
"author": "",
"date": "2022-09-11",
"description": "PEN15 is middle school as it really happened. Maya Erskine and Anna Konkle star in this adult comedy, playing versions of themselves as thirteen-year-old outcasts in the year 2000, surrounded by actual thirteen-year-olds, where the best day of your life can turn into your worst with the stroke of a gel pen.",
"url": "https://www.hulu.com/series/pen15-8c87035d-2b10-4b10-a233-ca5b3597145d",
"image": "bookmark-pen15.jpg",
"type": "tv_show",
"notes": "note",
"prop1": "my property",
"anotherProp": "another property"
}
]",
"utf-8",
]
`);
});

test("cannot get bookmarks", async () => {
// eslint-disable-next-line no-import-assign
Object.defineProperty(github, "context", {
Expand Down
5 changes: 4 additions & 1 deletion src/get-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ export async function getMetadata({
notes,
date,
tags,
additionalProperties,
}: {
url: string;
notes?: string;
date: string;
tags?: string;
additionalProperties?: Record<string, string>;
}): Promise<Bookmark | undefined> {
try {
const { result } = await ogs({ url });
Expand All @@ -40,12 +42,13 @@ export async function getMetadata({
...(waybackUrl && {
waybackUrl,
}),
...additionalProperties,
};
} catch (error) {
throw new Error(`Error getting metadata for ${url}: ${error.result.error}`);
}
}

function toArray(tags: string): string[] {
export function toArray(tags: string): string[] {
return tags.split(",").map((f) => f.trim());
}
22 changes: 20 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as github from "@actions/github";
import { isUrl, isDate } from "./utils.js";
import { saveBookmarks } from "./save-bookmarks";
import { addBookmark, Bookmark } from "./add-bookmark";
import { getMetadata } from "./get-metadata";
import { getMetadata, toArray } from "./get-metadata";

type Payload = {
url: string;
Expand Down Expand Up @@ -37,7 +37,25 @@ export async function action() {

const filename = getInput("filename");

const page = (await getMetadata({ url, notes, date, tags })) as Bookmark;
const additionalPropertiesList = getInput("additional-properties")
? toArray(getInput("additional-properties"))
: undefined;

const additionalProperties = additionalPropertiesList?.reduce(
(acc, property) => {
acc[property] = payload[property];
return acc;
},
{}
);

const page = (await getMetadata({
url,
notes,
date,
tags,
additionalProperties,
})) as Bookmark;
const bookmarks = await addBookmark(filename, page);
if (!bookmarks) {
setFailed(`Unable to add bookmark`);
Expand Down

0 comments on commit 267b58e

Please sign in to comment.