Skip to content

Commit

Permalink
fix: update fetch failed logic to correctly warn if badge fails to fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
prototypicalpro committed Aug 21, 2020
1 parent cc7fa93 commit 3900d7c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
15 changes: 11 additions & 4 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

17 changes: 12 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ const enum Inputs {
async function fetchAndWriteBadge(
url: string,
filepath: string
): Promise<string> {
): Promise<string | null> {
const res = await fetch(url)
if (!res.ok || res.headers.get('content-type'))
if (!res.ok || !res.headers.get('content-type')) {
core.warning(
`Fetching badge ${url} failed with status code ${res.status}: ${res.statusText}`
)
return null
}
const filePathExtension = `${filepath}.${mime.extension(
res.headers.get('content-type') as string
)}`
Expand Down Expand Up @@ -120,6 +122,7 @@ export default async function run(): Promise<void> {
}
return true
})
// print debugging info
if (!validBadges.length) {
core.warning("Didn't find any badges to replace!")
return
Expand All @@ -132,12 +135,16 @@ export default async function run(): Promise<void> {
fetchAndWriteBadge(b, path.join(outputSvgDir, `badge-${i}`))
)
)
// zip the arrays and filter out null paths
const inputPathsAndUrls = deDupedBadges
.map((d, i) => {
return {url: d, path: paths[i]}
})
.filter(o => o.path !== null)
// replace all instances of each badge url with the new path
const output = replaceUrls(
input,
deDupedBadges.map((d, i) => {
return {url: d, path: paths[i]}
})
inputPathsAndUrls as {path: string; url: string}[]
)
// write the output to file
await fs.promises.writeFile(outputFile, output)
Expand Down

0 comments on commit 3900d7c

Please sign in to comment.