From 267b58e573e99b738094c47fcf5bc17348d4351a Mon Sep 17 00:00:00 2001 From: Katy DeCorah Date: Wed, 24 Jan 2024 08:58:11 -0500 Subject: [PATCH] Draft --- .github/workflows/bookmarks-advanced.yml | 48 ++++++++++++++++++ action.yml | 3 ++ dist/index.js | 21 ++++++-- src/__tests__/index.test.ts | 63 ++++++++++++++++++++++++ src/get-metadata.ts | 5 +- src/index.ts | 22 ++++++++- 6 files changed, 155 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/bookmarks-advanced.yml diff --git a/.github/workflows/bookmarks-advanced.yml b/.github/workflows/bookmarks-advanced.yml new file mode 100644 index 00000000..dc3e0d8a --- /dev/null +++ b/.github/workflows/bookmarks-advanced.yml @@ -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 "action@github.com" + git config --local user.name "GitHub Action" + git add -A && git commit -m "Bookmark ${{ env.BookmarkTitle }}" + git push diff --git a/action.yml b/action.yml index 489008e2..b14301d5 100644 --- a/action.yml +++ b/action.yml @@ -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: "" diff --git a/dist/index.js b/dist/index.js index 6c75c8ee..0976a33e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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 { @@ -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}`); @@ -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`); diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index e3d1974b..4ee02d89 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -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", { diff --git a/src/get-metadata.ts b/src/get-metadata.ts index 82da39e0..b715f4e7 100644 --- a/src/get-metadata.ts +++ b/src/get-metadata.ts @@ -9,11 +9,13 @@ export async function getMetadata({ notes, date, tags, + additionalProperties, }: { url: string; notes?: string; date: string; tags?: string; + additionalProperties?: Record; }): Promise { try { const { result } = await ogs({ url }); @@ -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()); } diff --git a/src/index.ts b/src/index.ts index d69441b5..d986d782 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; @@ -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`);