Skip to content

Commit

Permalink
cache results of graphql queries
Browse files Browse the repository at this point in the history
  • Loading branch information
hipstersmoothie committed Apr 14, 2020
1 parent a1a8b0f commit 0f8f5c4
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions plugins/first-time-contributor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export default class FirstTimeContributorPlugin implements IPlugin {

/** Tap into auto plugin points. */
apply(auto: Auto) {
const cache: Record<string, Record<string, any>> = {};

auto.hooks.onCreateChangelog.tap(this.name, (changelog) => {
const base = new URL(changelog.options.baseUrl).origin;

Expand All @@ -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) => {
Expand All @@ -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;
Expand Down

0 comments on commit 0f8f5c4

Please sign in to comment.