Skip to content

Commit

Permalink
feat: retrieve issue co-authors (#445)
Browse files Browse the repository at this point in the history
## PR Checklist

- [x] Addresses an existing open issue: fixes #252
- [x] That issue was marked as [`status: accepting
prs`](https://github.com/JoshuaKGoldberg/all-contributors-for-repository/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22)
- [x] Steps in
[CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/all-contributors-for-repository/blob/main/.github/CONTRIBUTING.md)
were taken

## Overview

Adds in the same co-author detection using `description-to-co-authors`
for issue bodies as commit bodies.

💖
  • Loading branch information
JoshuaKGoldberg authored Aug 10, 2024
1 parent 2f6a3b9 commit e761182
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
},
"dependencies": {
"co-author-to-username": "^0.1.1",
"commit-to-co-authors": "^0.1.0",
"conventional-commits-parser": "^6.0.0",
"description-to-co-authors": "^0.3.0",
"octokit": "^4.0.0"
},
"devDependencies": {
Expand Down
19 changes: 9 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 55 additions & 7 deletions src/collect/adding/addAcceptedIssues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,32 @@ const options = {
};

const login = "abc123";
const loginCoAuthor = "other-login";

const createStubIssue = (label: string) =>
const mockDescriptionToCoAuthors = vi
.fn()
.mockReturnValue([{ username: loginCoAuthor }]);

vi.mock("description-to-co-authors", () => ({
get descriptionToCoAuthors() {
return mockDescriptionToCoAuthors;
},
}));

const createStubIssue = ({ body, labels }: Partial<AcceptedIssue>) =>
({
labels: [label],
body,
labels,
number: 0,
user: { login },
}) as AcceptedIssue;

describe("addAcceptedIssues", () => {
it("adds an issue when it has a bug label", () => {
const add = vi.fn();
const issue = createStubIssue(options.labelTypeBug);
const issue = createStubIssue({
labels: [options.labelTypeBug],
});

addAcceptedIssues([issue], { add }, options);

Expand All @@ -31,7 +45,9 @@ describe("addAcceptedIssues", () => {

it("adds an issue when it has a docs label", () => {
const add = vi.fn();
const issue = createStubIssue(options.labelTypeDocs);
const issue = createStubIssue({
labels: [options.labelTypeDocs],
});

addAcceptedIssues([issue], { add }, options);

Expand All @@ -40,7 +56,9 @@ describe("addAcceptedIssues", () => {

it("adds an issue when it has a feature label", () => {
const add = vi.fn();
const issue = createStubIssue(options.labelTypeIdeas);
const issue = createStubIssue({
labels: [options.labelTypeIdeas],
});

addAcceptedIssues([issue], { add }, options);

Expand All @@ -49,16 +67,46 @@ describe("addAcceptedIssues", () => {

it("adds an issue when it has a tool label", () => {
const add = vi.fn();
const issue = createStubIssue(options.labelTypeTool);
const issue = createStubIssue({
labels: [options.labelTypeTool],
});

addAcceptedIssues([issue], { add }, options);

expect(add).toHaveBeenCalledWith(login, issue.number, "tool");
});

it("adds an issue's co-author when the body includes them", () => {
const add = vi.fn();
const issue = createStubIssue({
body: `(mocked)`,
labels: [options.labelTypeTool],
});

addAcceptedIssues([issue], { add }, options);

expect(add).toHaveBeenCalledWith(login, issue.number, "tool");
expect(add).toHaveBeenCalledWith(loginCoAuthor, issue.number, "tool");
expect(mockDescriptionToCoAuthors).toHaveBeenCalledWith(issue.body);
});

it("doesn't call for co-authors when there is no issue body", () => {
const add = vi.fn();
const issue = createStubIssue({
labels: [options.labelTypeTool],
});

addAcceptedIssues([issue], { add }, options);

expect(add).toHaveBeenCalledWith(login, issue.number, "tool");
expect(add).toHaveBeenCalledTimes(1);
});

it("doesn't add an issue when it has an unrelated label", () => {
const add = vi.fn();
const issue = createStubIssue("other");
const issue = createStubIssue({
labels: ["other"],
});

addAcceptedIssues([issue], { add }, options);

Expand Down
25 changes: 19 additions & 6 deletions src/collect/adding/addAcceptedIssues.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { descriptionToCoAuthors } from "description-to-co-authors";
import { ContributorsCollection } from "../../ContributorsCollection.js";
import { AllContributorsForRepositoryOptions } from "../../options.js";
import { AcceptedIssue } from "../collecting/collectAcceptedIssues.js";
Expand Down Expand Up @@ -25,12 +26,24 @@ export function addAcceptedIssues(
// - 🔧 `tool`: authors of merged PRs that address issues labeled as tooling
[options.labelTypeTool, "tool"],
] as const) {
if (labels.some((label) => label === labelType)) {
contributors.add(
acceptedIssue.user?.login,
acceptedIssue.number,
contribution,
);
if (!labels.some((label) => label === labelType)) {
continue;
}

const logins = [];

if (acceptedIssue.user) {
logins.push(acceptedIssue.user.login);
}

if (acceptedIssue.body) {
for (const coAuthor of descriptionToCoAuthors(acceptedIssue.body)) {
logins.push(coAuthor.username);
}
}

for (const login of logins) {
contributors.add(login, acceptedIssue.number, contribution);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/collect/parsing/parseMergedPullAuthors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CachingCoAuthorToUsername } from "co-author-to-username";
import { commitToCoAuthors } from "commit-to-co-authors";
import { descriptionToCoAuthors } from "description-to-co-authors";

interface MergedPullForAuthors {
body?: string;
Expand All @@ -15,7 +15,7 @@ export async function parseMergedPullAuthors(
authors.push(mergedPull.user?.login);

if (mergedPull.body) {
const coAuthors = commitToCoAuthors(mergedPull.body);
const coAuthors = descriptionToCoAuthors(mergedPull.body);

for (const coAuthor of coAuthors) {
authors.push(await cachingCoAuthorToUsername(coAuthor));
Expand Down

0 comments on commit e761182

Please sign in to comment.