Skip to content

Commit

Permalink
fix: only extract linked issues for User PRs
Browse files Browse the repository at this point in the history
  • Loading branch information
setchy committed Jun 10, 2024
1 parent 5e7daeb commit 8b0f0b5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
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
10 changes: 5 additions & 5 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,8 +268,7 @@ async function getGitifySubjectForPullRequest(
}

const reviews = await getLatestReviewForReviewers(notification);
const linkedIssues =
pr.user.type === 'User' ? parseLinkedIssuesFromPrBody(pr.body) : [];
const linkedIssues = parseLinkedIssuesFromPr(pr);

return {
state: prState,
Expand Down Expand Up @@ -337,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

0 comments on commit 8b0f0b5

Please sign in to comment.