Skip to content

Commit

Permalink
Cache github information about the same repository (except issue coun…
Browse files Browse the repository at this point in the history
…ts, in the case where we have labels, and subfolder queries to find extension-yamls)
  • Loading branch information
holly-cummins committed Aug 2, 2023
1 parent 6c6b2e5 commit 1aab649
Show file tree
Hide file tree
Showing 2 changed files with 482 additions and 72 deletions.
72 changes: 55 additions & 17 deletions plugins/github-enricher/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const defaultOptions = {
nodeType: "Extension",
}

// To avoid hitting the git rate limiter retrieving information we already know, cache what we can
let repoCache = {}

let getLabels

// TODO ideally we would cache between builds, too
Expand Down Expand Up @@ -76,6 +79,11 @@ exports.onPreBootstrap = async ({}) => {
return yaml
}

// Mostly useful for testing
exports.clearCache = () => {
repoCache = {}
}

exports.onCreateNode = async (
{ node, actions, createNodeId, createContentDigest },
pluginOptions
Expand Down Expand Up @@ -145,6 +153,8 @@ async function fetchScmLabel(scmUrl, artifactId) {
}

const fetchScmInfo = async (scmUrl, artifactId, labels) => {
const cachedScmInfo = repoCache[scmUrl]

// TODO we can just treat label as an array, almost
const labelFilterString = labels
? `, filterBy: { labels: [${labels.map(label => `"${label}"`).join()}] }`
Expand All @@ -165,56 +175,81 @@ const fetchScmInfo = async (scmUrl, artifactId, labels) => {
)
: scmUrl + "/issues"

const scmInfo = { url: scmUrl, project, issuesUrl, labels }
// Take what we have cached as a base, if we have it
const scmInfo = cachedScmInfo ? cachedScmInfo : { url: scmUrl, project }

// Always set the issuesUrl and labels since the cached one might be invalid
scmInfo.issuesUrl = issuesUrl
scmInfo.labels = labels

const accessToken = process.env.GITHUB_TOKEN

// This query is long, because I can't find a way to do "or" or
// Batching this may not help that much because rate limits are done on query complexity and cost,
// not the number of actual http calls; see https://docs.github.com/en/graphql/overview/resource-limitations
if (accessToken) {
const query = `
query {
repository(owner:"${coords.owner}", name:"${coords.name}") {
issues(states:OPEN, ${labelFilterString}) {
const issuesQuery = `issues(states:OPEN, ${labelFilterString}) {
totalCount
}
defaultBranchRef {
name
}
metaInfs: object(expression: "HEAD:runtime/src/main/resources/META-INF/") {
}`

const subfoldersQuery = `subfolderMetaInfs: object(expression: "HEAD:${artifactId}/runtime/src/main/resources/META-INF/") {
... on Tree {
entries {
path
}
}
}
subfolderMetaInfs: object(expression: "HEAD:${artifactId}/runtime/src/main/resources/META-INF/") {
shortenedSubfolderMetaInfs: object(expression: "HEAD:${shortArtifactId}/runtime/src/main/resources/META-INF/") {
... on Tree {
entries {
path
}
}
}`

let query
if (cachedScmInfo) {
if (labels) {
query = `query {
repository(owner:"${coords.owner}", name:"${coords.name}") {
${issuesQuery}
${subfoldersQuery}
}`
} else {
query = `query {
repository(owner:"${coords.owner}", name:"${coords.name}") {
${subfoldersQuery}
}`
}
shortenedSubfolderMetaInfs: object(expression: "HEAD:${shortArtifactId}/runtime/src/main/resources/META-INF/") {
} else {
query = `query {
repository(owner:"${coords.owner}", name:"${coords.name}") {
${issuesQuery}
defaultBranchRef {
name
}
metaInfs: object(expression: "HEAD:runtime/src/main/resources/META-INF/") {
... on Tree {
entries {
path
}
}
}
${subfoldersQuery}
openGraphImageUrl
}
repositoryOwner(login: "${coords.owner}") {
avatarUrl
}
}`
}

// We sometimes get bad results back from the git API where json() is null, so do a bit of retrying
const body = await promiseRetry(
Expand Down Expand Up @@ -300,10 +335,13 @@ const fetchScmInfo = async (scmUrl, artifactId, labels) => {
scmInfo.socialImage = openGraphImageUrl
}

// Save this information for the next time
repoCache[scmUrl] = scmInfo

return scmInfo
} else {
console.warn(
`Cannot read GitHub information ofr ${artifactId}, because the API did not return any data.`
`Cannot read GitHub information for ${artifactId}, because the API did not return any data.`
)
return scmInfo
}
Expand Down
Loading

0 comments on commit 1aab649

Please sign in to comment.