Skip to content

Commit c5dec55

Browse files
committed
Use promise retry
1 parent 0fba343 commit c5dec55

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

plugins/github-enricher/gatsby-node.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const gh = require("parse-github-url")
22
const path = require("path")
33
const encodeUrl = require("encodeurl")
4+
const promiseRetry = require("promise-retry")
45

56
const { getCache } = require("gatsby/dist/utils/get-cache")
67
const { createRemoteFileNode } = require("gatsby-source-filesystem")
@@ -215,31 +216,32 @@ const fetchScmInfo = async (scmUrl, artifactId, labels) => {
215216
}
216217
}`
217218

218-
let body = undefined
219-
let count = 0
220-
221219
// We sometimes get bad results back from the git API where json() is null, so do a bit of retrying
222-
while (!body?.data && count < 3) {
223-
count++
224-
const res = await fetch("https://api.github.com/graphql", {
225-
method: "POST",
226-
body: JSON.stringify({ query }),
227-
headers: {
228-
Authorization: `Bearer ${accessToken}`,
229-
},
230-
})
231-
body = await res.json()
232-
if (!body?.data) {
233-
console.warn(
234-
"Retrying GitHub fetch for",
235-
artifactId,
236-
"- response is",
237-
body
238-
)
239-
}
240-
}
220+
const body = await promiseRetry(
221+
async () => {
222+
const res = await fetch("https://api.github.com/graphql", {
223+
method: "POST",
224+
body: JSON.stringify({ query }),
225+
headers: {
226+
Authorization: `Bearer ${accessToken}`,
227+
},
228+
})
229+
const ghBody = await res.json()
230+
if (!ghBody?.data) {
231+
throw Error(
232+
`Unsuccessful GitHub fetch for ${artifactId} - response is ${ghBody}`
233+
)
234+
}
235+
return ghBody
236+
},
237+
{ retries: 4, minTimeout: 4 * 1000 }
238+
).catch(e => {
239+
// Do not break the build for this, warn and carry on
240+
console.warn(e)
241+
return undefined
242+
})
241243

242-
if (!body?.data && !body?.data?.repository) {
244+
if (body?.data && !body?.data?.repository) {
243245
console.warn("Strange artifact for ", artifactId, "- response is", body)
244246
}
245247

0 commit comments

Comments
 (0)