generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
191 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { setFailed } from "@actions/core"; | ||
import { CustomError } from "ts-custom-error"; | ||
|
||
import { extractArticle } from "../extractors/extractArticle"; | ||
import { FeedEntry } from "../extractors/types/feed-extractor"; | ||
import logger from "../logger"; | ||
import { UnknownError } from "../utils/errors"; | ||
import { storeFile } from "../utils/fs"; | ||
import { buildFilename, getOutputDir } from "../utils/io"; | ||
|
||
export async function processEntry(feedEntry: FeedEntry) { | ||
const outputDir = getOutputDir(); | ||
try { | ||
const article = await extractArticle(feedEntry); | ||
const filename = buildFilename(article.url); | ||
const destinationFile = `${outputDir}/${filename}.json`; | ||
const fileContents = JSON.stringify(article, null, 2); | ||
storeFile(destinationFile, fileContents); | ||
logger.info( | ||
`New article stored: "%s". Path: "%s"`, | ||
article.url, | ||
destinationFile, | ||
); | ||
} catch (err) { | ||
if (err instanceof CustomError) { | ||
logger.warn(err.message); | ||
return; | ||
} | ||
|
||
setFailed(new UnknownError(err).message); | ||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { setFailed } from "@actions/core"; | ||
|
||
import { FeedWithoutEntriesError } from "../extractors/errors"; | ||
import { extractFeed, FeedWithEntries } from "../extractors/extractFeed"; | ||
import { FeedEntry } from "../extractors/types/feed-extractor"; | ||
import logger from "../logger"; | ||
import { UnknownError } from "../utils/errors"; | ||
import { processEntry } from "./processEntry"; | ||
import { processFeed } from "./processFeed"; | ||
|
||
jest.mock("@actions/core"); | ||
jest.mock("../extractors/extractFeed"); | ||
jest.mock("../logger"); | ||
jest.mock("./processEntry"); | ||
|
||
describe("processor", () => { | ||
describe("processFeed", () => { | ||
logger.warn = jest.fn(); | ||
const setFailedMock = setFailed as jest.MockedFunction<typeof setFailed>; | ||
const extractFeedMock = extractFeed as jest.MockedFunction< | ||
typeof extractFeed | ||
>; | ||
const processEntryMock = processEntry as jest.MockedFunction< | ||
typeof processEntry | ||
>; | ||
|
||
it("returns error if something unexpected happens", async () => { | ||
const url = new URL("https://www.google.com"); | ||
const thrownError = new Error("Unpredictable error"); | ||
const expectedError = new UnknownError(thrownError); | ||
|
||
extractFeedMock.mockRejectedValue(thrownError); | ||
|
||
await processFeed(url); | ||
expect(logger.warn).not.toHaveBeenCalled(); | ||
expect(setFailedMock).toHaveBeenCalledWith(expectedError.message); | ||
}); | ||
|
||
it("warns the error if something happens", async () => { | ||
const url = new URL("https://www.google.com"); | ||
const expectedError = new FeedWithoutEntriesError(url, {}); | ||
extractFeedMock.mockRejectedValue(expectedError); | ||
|
||
await processFeed(url); | ||
expect(logger.warn).toHaveBeenCalledWith(expectedError.message); | ||
expect(setFailedMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("process the entries", async () => { | ||
const entry = { id: "test-entry" } as FeedEntry; | ||
const feedData = { entries: [entry] } as FeedWithEntries; | ||
extractFeedMock.mockResolvedValue(feedData); | ||
|
||
await processFeed(new URL("https://www.google.com")); | ||
|
||
expect(processEntryMock).toHaveBeenCalledWith(entry); | ||
expect(logger.warn).not.toHaveBeenCalledWith(); | ||
expect(setFailedMock).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { setFailed } from "@actions/core"; | ||
import { CustomError } from "ts-custom-error"; | ||
|
||
import { extractFeed } from "../extractors/extractFeed"; | ||
import logger from "../logger"; | ||
import { UnknownError } from "../utils/errors"; | ||
import { processEntry } from "./processEntry"; | ||
|
||
export async function processFeed(feedUrl: URL) { | ||
try { | ||
const feedData = await extractFeed(feedUrl); | ||
void Promise.all(feedData.entries.map((entry) => processEntry(entry))); | ||
} catch (err) { | ||
if (err instanceof CustomError) { | ||
logger.warn(err.message); | ||
return; | ||
} | ||
|
||
setFailed(new UnknownError(err).message); | ||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters