Skip to content

Commit 2bea44d

Browse files
committed
Retry access to github API
1 parent 86de4f6 commit 2bea44d

File tree

1 file changed

+72
-48
lines changed

1 file changed

+72
-48
lines changed

plugins/github-enricher/gatsby-node.js

Lines changed: 72 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -115,62 +115,86 @@ const fetchScmInfo = async (scmUrl, artifactId) => {
115115
}
116116
}`
117117

118-
const res = await fetch("https://api.github.com/graphql", {
119-
method: "POST",
120-
body: JSON.stringify({ query }),
121-
headers: {
122-
Authorization: `Bearer ${accessToken}`,
123-
},
124-
})
125-
const body = await res.json()
126-
127-
const {
128-
data: {
129-
repository: {
130-
issues: { totalCount },
131-
defaultBranchRef,
132-
metaInfs,
133-
subfolderMetaInfs,
134-
shortenedSubfolderMetaInfs,
135-
openGraphImageUrl,
118+
let body = undefined
119+
let count = 0
120+
121+
// We sometimes get bad results back from the git API where json() is null, so do a bit of retrying
122+
while (!body?.data && count < 3) {
123+
count++
124+
const res = await fetch("https://api.github.com/graphql", {
125+
method: "POST",
126+
body: JSON.stringify({ query }),
127+
headers: {
128+
Authorization: `Bearer ${accessToken}`,
136129
},
137-
repositoryOwner: { avatarUrl },
138-
},
139-
} = body
140-
141-
const allMetaInfs = [
142-
...(metaInfs ? metaInfs.entries : []),
143-
...(subfolderMetaInfs ? subfolderMetaInfs.entries : []),
144-
...(shortenedSubfolderMetaInfs ? shortenedSubfolderMetaInfs.entries : []),
145-
]
146-
147-
const extensionYamls = allMetaInfs.filter(entry =>
148-
entry.path.endsWith("/quarkus-extension.yaml")
149-
)
130+
})
131+
body = await res.json()
132+
if (!body?.data) {
133+
console.warn(
134+
"Retrying GitHub fetch for",
135+
artifactId,
136+
"- response is",
137+
body
138+
)
139+
}
140+
}
150141

151-
scmInfo.issues = totalCount
142+
if (body) {
143+
const {
144+
data: {
145+
repository: {
146+
issues: { totalCount },
147+
defaultBranchRef,
148+
metaInfs,
149+
subfolderMetaInfs,
150+
shortenedSubfolderMetaInfs,
151+
openGraphImageUrl,
152+
},
153+
repositoryOwner: { avatarUrl },
154+
},
155+
} = body
152156

153-
scmInfo.owner = coords.owner
154-
scmInfo.ownerImageUrl = avatarUrl
157+
const allMetaInfs = [
158+
...(metaInfs ? metaInfs.entries : []),
159+
...(subfolderMetaInfs ? subfolderMetaInfs.entries : []),
160+
...(shortenedSubfolderMetaInfs
161+
? shortenedSubfolderMetaInfs.entries
162+
: []),
163+
]
155164

156-
// We should only have one extension yaml - if we have more, don't guess, and if we have less, don't set anything
157-
if (extensionYamls.length === 1) {
158-
scmInfo.extensionYamlUrl = `${scmUrl}/blob/${defaultBranchRef?.name}/${extensionYamls[0].path}`
159-
}
165+
const extensionYamls = allMetaInfs.filter(entry =>
166+
entry.path.endsWith("/quarkus-extension.yaml")
167+
)
160168

161-
// Only look at the social media preview if it's been set by the user; otherwise we know it will be the owner avatar with some text we don't want
162-
// This mechanism is a bit fragile, but should work for now
163-
// Default pattern https://opengraph.githubassets.com/3096043220541a8ea73deb5cb6baddf0f01d50244737d22402ba12d665e9aec2/quarkiverse/quarkus-openfga-client
164-
// Customised pattern https://repository-images.githubusercontent.com/437045322/39ad4dec-e606-4b21-bb24-4c09a4790b58
169+
scmInfo.issues = totalCount
165170

166-
const isCustomizedSocialMediaPreview =
167-
openGraphImageUrl?.includes("githubusercontent")
171+
scmInfo.owner = coords.owner
172+
scmInfo.ownerImageUrl = avatarUrl
168173

169-
if (isCustomizedSocialMediaPreview) {
170-
scmInfo.socialImage = openGraphImageUrl
171-
}
174+
// We should only have one extension yaml - if we have more, don't guess, and if we have less, don't set anything
175+
if (extensionYamls.length === 1) {
176+
scmInfo.extensionYamlUrl = `${scmUrl}/blob/${defaultBranchRef?.name}/${extensionYamls[0].path}`
177+
}
172178

173-
return scmInfo
179+
// Only look at the social media preview if it's been set by the user; otherwise we know it will be the owner avatar with some text we don't want
180+
// This mechanism is a bit fragile, but should work for now
181+
// Default pattern https://opengraph.githubassets.com/3096043220541a8ea73deb5cb6baddf0f01d50244737d22402ba12d665e9aec2/quarkiverse/quarkus-openfga-client
182+
// Customised pattern https://repository-images.githubusercontent.com/437045322/39ad4dec-e606-4b21-bb24-4c09a4790b58
183+
184+
const isCustomizedSocialMediaPreview =
185+
openGraphImageUrl?.includes("githubusercontent")
186+
187+
if (isCustomizedSocialMediaPreview) {
188+
scmInfo.socialImage = openGraphImageUrl
189+
}
190+
191+
return scmInfo
192+
} else {
193+
console.warn(
194+
"Cannot read GitHub information, because the API did not return any data."
195+
)
196+
return scmInfo
197+
}
174198
} else {
175199
console.warn(
176200
"Cannot read GitHub information, because the environment variable `GITHUB_TOKEN` has not been set."

0 commit comments

Comments
 (0)