Skip to content

Commit

Permalink
Merge pull request #1619 from intuit/first-bug
Browse files Browse the repository at this point in the history
better first-time-contributor thanking
  • Loading branch information
hipstersmoothie authored Oct 30, 2020
2 parents deb1a7f + 96a0681 commit d6eedeb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const setup = (
git: {
graphql,
options: { repo: "repo", owner: "test" },
getUserByUsername: jest.fn().mockReturnValue({}),
github: {
repos: {
listContributors: () => Promise.resolve({ data: contributors }),
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
54 changes: 45 additions & 9 deletions plugins/first-time-contributor/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
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";
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[];
};
};
};
}

/**
Expand Down Expand Up @@ -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<Record<string, IssueCount>>(`
const userCommits = await auto.git?.graphql<QueryResponse>(`
{
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[commits.length - 1].hash} 0", first: 1, author: { id: "${user.node_id}" }) {
edges {
node {
message
}
}
}
}
}
}
}
`)

if (prs && prs.search.issueCount <= 1) {
if (
userCommits &&
userCommits.repository.object.history.edges.length === 0
) {
newContributors.push(author);
}
}
Expand Down

0 comments on commit d6eedeb

Please sign in to comment.