Skip to content

Commit c369ef0

Browse files
committed
Do not consider urls that redirect to /pulls as valid when working out issues url
1 parent 86fa533 commit c369ef0

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

package-lock.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"date-fns": "^3.6.0",
2020
"encodeurl": "^2.0.0",
2121
"eslint-plugin-gatsby": "^1.0.2",
22+
"follow-redirect-url": "^2.0.1",
2223
"gatsby": "^4.25.8",
2324
"gatsby-plugin-feed": "^4.25.0",
2425
"gatsby-plugin-force-file-loader": "^4.0.2",

plugins/github-enricher/gatsby-node.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const followRedirect = require("follow-redirect-url")
2+
13
const gh = require("parse-github-url")
24
const path = require("path")
35
const encodeUrl = require("encodeurl")
@@ -275,11 +277,14 @@ const fetchGitHubInfo = async (scmUrl, groupId, artifactId, labels) => {
275277
const coords = gh(scmUrl)
276278
const project = coords.name
277279

278-
const { issuesUrl, issues } = await getIssueInformation(coords, labels, scmUrl)
280+
const scmInfo = { labels }
279281

280-
const scmInfo = { issuesUrl, issues }
282+
const { issuesUrl, issues } = await getIssueInformation(coords, labels, scmUrl)
281283

282-
scmInfo.labels = labels
284+
if (issuesUrl) {
285+
scmInfo.issuesUrl = issuesUrl
286+
scmInfo.issues = issues
287+
}
283288

284289
const imageInfo = await getImageInformation(coords, scmUrl)
285290

@@ -696,22 +701,36 @@ const getIssueInformationNoCache = async (coords, labels, scmUrl) => {
696701
// The parent objects may be undefined and destructuring nested undefineds is not good
697702
const issues = body?.data?.repository?.issues?.totalCount
698703

699-
// If we got an issue count we can be pretty confident our url will be ok, but otherwise, it might not be,
700-
// so check it. We don't check for every url because otherwise we start getting 429s and dropping good URLs
701-
if (!issues) {
704+
issuesUrl = await maybeIssuesUrl(issues, issuesUrl)
705+
706+
return { issues, issuesUrl }
707+
}
708+
709+
const maybeIssuesUrl = async (issues, issuesUrl) => {
710+
if (issues) {
711+
return issuesUrl
712+
} else {
713+
// If we got an issue count we can be pretty confident our url will be ok, but otherwise, it might not be,
714+
// so check it. We don't check for every url because otherwise we start getting 429s and dropping good URLs
702715
// We have to access the url exist as a dynamic import (because CJS), await it because dynamic imports give a promise, and then destructure it to get the default
703716
// A simple property read won't work
704717
const {
705718
default: urlExist,
706719
} = await import("url-exist")
707720

708721
const isValidUrl = await urlExist(issuesUrl)
709-
if (!isValidUrl) {
710-
issuesUrl = undefined
711-
}
722+
723+
let isOriginalUrl = isValidUrl && await isNotRedirectToPulls(issuesUrl)
724+
725+
return isOriginalUrl ? issuesUrl : undefined
712726
}
727+
}
713728

714-
return { issues, issuesUrl }
729+
const isNotRedirectToPulls = async (issuesUrl) => {
730+
// Being a valid url may not be enough, we also want to check for redirects to /pulls
731+
const urls = await followRedirect.startFollowing(issuesUrl)
732+
const finalUrl = urls[urls.length - 1]
733+
return !(finalUrl.url.includes("/pulls"))
715734
}
716735

717736
// This combines the sponsor opt-in information (which we only fully have after processing all nodes) with the companies and sponsor information for individual nodes,

0 commit comments

Comments
 (0)