Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: only extract linked issues for User PRs #1219

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions src/utils/subject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import type {
DiscussionAuthor,
DiscussionStateType,
Notification,
PullRequest,
Repository,
} from '../typesGitHub';
import {
getCheckSuiteAttributes,
getGitifySubjectDetails,
getLatestReviewForReviewers,
getWorkflowRunAttributes,
parseLinkedIssuesFromPrBody,
parseLinkedIssuesFromPr,
} from './subject';

const mockAuthor = partialMockUser('some-author');
Expand Down Expand Up @@ -982,13 +983,36 @@ describe('utils/subject.ts', () => {

describe('Pull Request With Linked Issues', () => {
it('returns empty if no pr body', () => {
const result = parseLinkedIssuesFromPrBody(null);
const mockPr = {
user: {
type: 'User',
},
body: null,
} as PullRequest;

const result = parseLinkedIssuesFromPr(mockPr);
expect(result).toEqual([]);
});

it('returns empty if pr from non-user', () => {
const mockPr = {
user: {
type: 'Bot',
},
body: 'This PR is linked to #1, #2, and #3',
} as PullRequest;
const result = parseLinkedIssuesFromPr(mockPr);
expect(result).toEqual([]);
});

it('returns linked issues', () => {
const mockPrBody = 'This PR is linked to #1, #2, and #3';
const result = parseLinkedIssuesFromPrBody(mockPrBody);
const mockPr = {
user: {
type: 'User',
},
body: 'This PR is linked to #1, #2, and #3',
} as PullRequest;
const result = parseLinkedIssuesFromPr(mockPr);
expect(result).toEqual(['#1', '#2', '#3']);
});
});
Expand Down
9 changes: 5 additions & 4 deletions src/utils/subject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
GitifyPullRequestReview,
GitifySubject,
Notification,
PullRequest,
PullRequestReview,
PullRequestStateType,
SubjectUser,
Expand Down Expand Up @@ -267,7 +268,7 @@ async function getGitifySubjectForPullRequest(
}

const reviews = await getLatestReviewForReviewers(notification);
const linkedIssues = parseLinkedIssuesFromPrBody(pr.body);
const linkedIssues = parseLinkedIssuesFromPr(pr);

return {
state: prState,
Expand Down Expand Up @@ -336,16 +337,16 @@ export async function getLatestReviewForReviewers(
});
}

export function parseLinkedIssuesFromPrBody(body: string): string[] {
export function parseLinkedIssuesFromPr(pr: PullRequest): string[] {
const linkedIssues: string[] = [];

if (!body) {
if (!pr.body || pr.user.type !== 'User') {
return linkedIssues;
}

const regexPattern = /\s*#(\d+)\s*/gi;

const matches = body.matchAll(regexPattern);
const matches = pr.body.matchAll(regexPattern);

for (const match of matches) {
if (match[0]) {
Expand Down