From 0b88f08c29449040515afb03db2a5843cbb451fd Mon Sep 17 00:00:00 2001 From: Andrew Lisowski Date: Thu, 29 Oct 2020 14:20:06 -0700 Subject: [PATCH 1/2] better first-time-contributor thanking --- .../__tests__/first-time-contributor.test.ts | 17 +++--- plugins/first-time-contributor/src/index.ts | 54 +++++++++++++++---- 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/plugins/first-time-contributor/__tests__/first-time-contributor.test.ts b/plugins/first-time-contributor/__tests__/first-time-contributor.test.ts index 778976f8e..940f063c0 100644 --- a/plugins/first-time-contributor/__tests__/first-time-contributor.test.ts +++ b/plugins/first-time-contributor/__tests__/first-time-contributor.test.ts @@ -34,6 +34,7 @@ const setup = ( git: { graphql, options: { repo: "repo", owner: "test" }, + getUserByUsername: jest.fn().mockReturnValue({}), github: { repos: { listContributors: () => Promise.resolve({ data: contributors }), @@ -65,10 +66,10 @@ describe("First Time Contributor Plugin", () => { ]; graphql.mockReturnValueOnce({ - search: { issueCount: 0 }, + repository: { object: { history: { edges: [] } } }, }); graphql.mockReturnValueOnce({ - search: { issueCount: 0 }, + repository: { object: { history: { edges: [] } } }, }); expect(await hooks.addToBody.promise([], commits)).toMatchSnapshot(); @@ -82,7 +83,7 @@ describe("First Time Contributor Plugin", () => { ]; graphql.mockReturnValueOnce({ - search: { issueCount: 1 }, + repository: { object: { history: { edges: [] } } }, }); expect(await hooks.addToBody.promise([], commits)).toMatchSnapshot(); @@ -96,7 +97,7 @@ describe("First Time Contributor Plugin", () => { ]; graphql.mockReturnValueOnce({ - search: { issueCount: 1 }, + repository: { object: { history: { edges: [] } } }, }); expect(await hooks.addToBody.promise([], commits)).toMatchSnapshot(); @@ -110,7 +111,7 @@ describe("First Time Contributor Plugin", () => { ]; graphql.mockReturnValue({ - search: { issueCount: 2 }, + repository: { object: { history: { edges: [{}] } } }, }); expect(await hooks.addToBody.promise([], commits)).toMatchSnapshot(); @@ -124,7 +125,7 @@ describe("First Time Contributor Plugin", () => { ]; graphql.mockReturnValue({ - search: { issueCount: 1 }, + repository: { object: { history: { edges: [] } } }, }); expect(await hooks.addToBody.promise([], commits)).toMatchSnapshot(); @@ -137,7 +138,7 @@ describe("First Time Contributor Plugin", () => { ]; graphql.mockReturnValue({ - search: { issueCount: 1 }, + repository: { object: { history: { edges: [] } } }, }); expect(await hooks.addToBody.promise([], commits)).toMatchSnapshot(); @@ -154,7 +155,7 @@ describe("First Time Contributor Plugin", () => { ]; graphql.mockReturnValue({ - search: { issueCount: 1 }, + repository: { object: { history: { edges: [] } } }, }); expect(await hooks.addToBody.promise([], commits)).toMatchSnapshot(); diff --git a/plugins/first-time-contributor/src/index.ts b/plugins/first-time-contributor/src/index.ts index 3a26c4751..eaa74e1c3 100644 --- a/plugins/first-time-contributor/src/index.ts +++ b/plugins/first-time-contributor/src/index.ts @@ -1,4 +1,5 @@ -import { Auto, IPlugin } from "@auto-it/core"; +/* eslint-disable no-await-in-loop */ +import { Auto, getCurrentBranch, IPlugin } from "@auto-it/core"; import { ICommitAuthor } from "@auto-it/core/dist/log-parse"; import botList from "@auto-it/bot-list"; import flatMap from "array.prototype.flatmap"; @@ -6,9 +7,26 @@ import endent from "endent"; import urlJoin from "url-join"; import { URL } from "url"; -interface IssueCount { - /** Number of issues that match the query */ - issueCount: number; +interface QueryNode { + /** The query node */ + node: { + /** The commit message */ + message: "remove canary context"; + }; +} + +interface QueryResponse { + /** The repo queried */ + repository: { + /** The object queried */ + object: { + /** The commit history */ + history: { + /** The edges in the query */ + edges: QueryNode[]; + }; + }; + }; } /** @@ -44,17 +62,35 @@ export default class FirstTimeContributorPlugin implements IPlugin { continue; } + const user = await auto.git?.getUserByUsername(author.username); + + if (!user) { + continue; + } + // prettier-ignore - // eslint-disable-next-line no-await-in-loop - const prs = await auto.git?.graphql>(` + const userCommits = 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 + repository(name: "${auto.git?.options.repo}", owner: "${auto.git?.options.owner}") { + object(expression: "${getCurrentBranch()}") { + ... on Commit { + history(after: "${commits[0].hash} 0", first: 1, author: { id: "${user.id}" }) { + edges { + node { + message + } + } + } + } + } } } `) - if (prs && prs.search.issueCount <= 1) { + if ( + userCommits && + userCommits.repository.object.history.edges.length === 0 + ) { newContributors.push(author); } } From 96a06816b5009cac563678ad295f4deb80230014 Mon Sep 17 00:00:00 2001 From: Andrew Lisowski Date: Thu, 29 Oct 2020 16:13:42 -0700 Subject: [PATCH 2/2] use node_id and correct commit --- plugins/first-time-contributor/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/first-time-contributor/src/index.ts b/plugins/first-time-contributor/src/index.ts index eaa74e1c3..79b00ac9e 100644 --- a/plugins/first-time-contributor/src/index.ts +++ b/plugins/first-time-contributor/src/index.ts @@ -74,7 +74,7 @@ export default class FirstTimeContributorPlugin implements IPlugin { repository(name: "${auto.git?.options.repo}", owner: "${auto.git?.options.owner}") { object(expression: "${getCurrentBranch()}") { ... on Commit { - history(after: "${commits[0].hash} 0", first: 1, author: { id: "${user.id}" }) { + history(after: "${commits[commits.length - 1].hash} 0", first: 1, author: { id: "${user.node_id}" }) { edges { node { message