From 0f8f5c4050853b745ee7eec6cd3361f2bcd6cd2b Mon Sep 17 00:00:00 2001 From: Andrew Lisowski Date: Tue, 14 Apr 2020 15:57:21 -0700 Subject: [PATCH] cache results of graphql queries --- plugins/first-time-contributor/src/index.ts | 31 ++++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/plugins/first-time-contributor/src/index.ts b/plugins/first-time-contributor/src/index.ts index 4bbafd172..37016909b 100644 --- a/plugins/first-time-contributor/src/index.ts +++ b/plugins/first-time-contributor/src/index.ts @@ -14,6 +14,8 @@ export default class FirstTimeContributorPlugin implements IPlugin { /** Tap into auto plugin points. */ apply(auto: Auto) { + const cache: Record> = {}; + auto.hooks.onCreateChangelog.tap(this.name, (changelog) => { const base = new URL(changelog.options.baseUrl).origin; @@ -23,6 +25,27 @@ export default class FirstTimeContributorPlugin implements IPlugin { return `${name}${username ? (name ? ` (${link})` : link) : ""}`; }; + /** Get the PRs made by a user */ + const getContributions = async (username: string) => { + if (cache[username]) { + return cache[username]; + } + + const response = await auto.git?.graphql(` + { + search(first: 2, type: ISSUE, query: "repo:${auto.git?.options.owner}/${auto.git?.options.repo} is:pr is:merged author:${username}") { + issueCount + } + } + `); + + if (response) { + cache[username] = response; + } + + return response; + }; + changelog.hooks.addToBody.tapPromise( this.name, async (notes, commits) => { @@ -34,13 +57,7 @@ export default class FirstTimeContributorPlugin implements IPlugin { } // prettier-ignore - const prs = await auto.git?.graphql(` - { - search(first: 2, type: ISSUE, query: "repo:${auto.git?.options.owner}/${auto.git?.options.repo} is:pr is:merged author:${author.username}") { - issueCount - } - } - `); + const prs = await getContributions(author.username) if (prs && prs.search.issueCount <= 1) { return author;