Skip to content

refactor: subject exports and tests #900

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

Merged
merged 1 commit into from
Mar 19, 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
62 changes: 37 additions & 25 deletions src/utils/subject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import { mockAccounts } from '../__mocks__/mock-state';
import { mockedSingleNotification } from '../__mocks__/mockedData';
import {
getCheckSuiteAttributes,
getGitifySubjectForDiscussion,
getGitifySubjectForIssue,
getGitifySubjectForPullRequest,
getGitifySubjectDetails,
getWorkflowRunAttributes,
} from './subject';
import { SubjectType } from '../typesGithub';
describe('utils/state.ts', () => {
beforeEach(() => {
// axios will default to using the XHR adapter which can't be intercepted
Expand Down Expand Up @@ -142,6 +141,7 @@ describe('utils/state.ts', () => {
subject: {
...mockedSingleNotification.subject,
title: 'This is an answered discussion',
type: 'Discussion' as SubjectType,
},
};

Expand All @@ -165,7 +165,7 @@ describe('utils/state.ts', () => {
},
});

const result = await getGitifySubjectForDiscussion(
const result = await getGitifySubjectDetails(
mockNotification,
mockAccounts.token,
);
Expand All @@ -180,6 +180,7 @@ describe('utils/state.ts', () => {
subject: {
...mockedSingleNotification.subject,
title: 'This is a duplicate discussion',
type: 'Discussion' as SubjectType,
},
};

Expand All @@ -203,7 +204,7 @@ describe('utils/state.ts', () => {
},
});

const result = await getGitifySubjectForDiscussion(
const result = await getGitifySubjectDetails(
mockNotification,
mockAccounts.token,
);
Expand All @@ -218,6 +219,7 @@ describe('utils/state.ts', () => {
subject: {
...mockedSingleNotification.subject,
title: 'This is an open discussion',
type: 'Discussion' as SubjectType,
},
};

Expand All @@ -241,7 +243,7 @@ describe('utils/state.ts', () => {
},
});

const result = await getGitifySubjectForDiscussion(
const result = await getGitifySubjectDetails(
mockNotification,
mockAccounts.token,
);
Expand All @@ -256,6 +258,7 @@ describe('utils/state.ts', () => {
subject: {
...mockedSingleNotification.subject,
title: 'This is an outdated discussion',
type: 'Discussion' as SubjectType,
},
};

Expand All @@ -279,7 +282,7 @@ describe('utils/state.ts', () => {
},
});

const result = await getGitifySubjectForDiscussion(
const result = await getGitifySubjectDetails(
mockNotification,
mockAccounts.token,
);
Expand All @@ -294,6 +297,7 @@ describe('utils/state.ts', () => {
subject: {
...mockedSingleNotification.subject,
title: 'This is a reopened discussion',
type: 'Discussion' as SubjectType,
},
};

Expand All @@ -317,7 +321,7 @@ describe('utils/state.ts', () => {
},
});

const result = await getGitifySubjectForDiscussion(
const result = await getGitifySubjectDetails(
mockNotification,
mockAccounts.token,
);
Expand All @@ -332,6 +336,7 @@ describe('utils/state.ts', () => {
subject: {
...mockedSingleNotification.subject,
title: 'This is a resolved discussion',
type: 'Discussion' as SubjectType,
},
};

Expand All @@ -355,7 +360,7 @@ describe('utils/state.ts', () => {
},
});

const result = await getGitifySubjectForDiscussion(
const result = await getGitifySubjectDetails(
mockNotification,
mockAccounts.token,
);
Expand All @@ -370,6 +375,7 @@ describe('utils/state.ts', () => {
subject: {
...mockedSingleNotification.subject,
title: 'This is a discussion',
type: 'Discussion' as SubjectType,
},
};

Expand Down Expand Up @@ -402,16 +408,14 @@ describe('utils/state.ts', () => {
},
});

const result = await getGitifySubjectForDiscussion(
const result = await getGitifySubjectDetails(
mockNotification,
mockAccounts.token,
);

expect(result.state).toBe('OPEN');
expect(result.user).toBe(null);
});

it('handles unknown or missing results', async () => {});
});

describe('getGitifySubjectForIssue', () => {
Expand All @@ -424,7 +428,7 @@ describe('utils/state.ts', () => {
.get('/repos/manosim/notifications-test/issues/comments/302888448')
.reply(200, { user: { login: 'some-user' } });

const result = await getGitifySubjectForIssue(
const result = await getGitifySubjectDetails(
mockedSingleNotification,
mockAccounts.token,
);
Expand All @@ -442,7 +446,7 @@ describe('utils/state.ts', () => {
.get('/repos/manosim/notifications-test/issues/comments/302888448')
.reply(200, { user: { login: 'some-user' } });

const result = await getGitifySubjectForIssue(
const result = await getGitifySubjectDetails(
mockedSingleNotification,
mockAccounts.token,
);
Expand All @@ -460,7 +464,7 @@ describe('utils/state.ts', () => {
.get('/repos/manosim/notifications-test/issues/comments/302888448')
.reply(200, { user: { login: 'some-user' } });

const result = await getGitifySubjectForIssue(
const result = await getGitifySubjectDetails(
mockedSingleNotification,
mockAccounts.token,
);
Expand All @@ -478,7 +482,7 @@ describe('utils/state.ts', () => {
.get('/repos/manosim/notifications-test/issues/comments/302888448')
.reply(200, { user: { login: 'some-user' } });

const result = await getGitifySubjectForIssue(
const result = await getGitifySubjectDetails(
mockedSingleNotification,
mockAccounts.token,
);
Expand All @@ -496,7 +500,7 @@ describe('utils/state.ts', () => {
.get('/repos/manosim/notifications-test/issues/comments/302888448')
.reply(200, { user: { login: 'some-user' } });

const result = await getGitifySubjectForIssue(
const result = await getGitifySubjectDetails(
mockedSingleNotification,
mockAccounts.token,
);
Expand All @@ -507,6 +511,14 @@ describe('utils/state.ts', () => {
});

describe('getGitifySubjectForPullRequest', () => {
const mockNotification = {
...mockedSingleNotification,
subject: {
...mockedSingleNotification.subject,
type: 'PullRequest' as SubjectType,
},
};

it('closed pull request state', async () => {
nock('https://api.github.com')
.get('/repos/manosim/notifications-test/issues/1')
Expand All @@ -516,8 +528,8 @@ describe('utils/state.ts', () => {
.get('/repos/manosim/notifications-test/issues/comments/302888448')
.reply(200, { user: { login: 'some-user' } });

const result = await getGitifySubjectForPullRequest(
mockedSingleNotification,
const result = await getGitifySubjectDetails(
mockNotification,
mockAccounts.token,
);

Expand All @@ -534,8 +546,8 @@ describe('utils/state.ts', () => {
.get('/repos/manosim/notifications-test/issues/comments/302888448')
.reply(200, { user: { login: 'some-user' } });

const result = await getGitifySubjectForPullRequest(
mockedSingleNotification,
const result = await getGitifySubjectDetails(
mockNotification,
mockAccounts.token,
);

Expand All @@ -552,8 +564,8 @@ describe('utils/state.ts', () => {
.get('/repos/manosim/notifications-test/issues/comments/302888448')
.reply(200, { user: { login: 'some-user' } });

const result = await getGitifySubjectForPullRequest(
mockedSingleNotification,
const result = await getGitifySubjectDetails(
mockNotification,
mockAccounts.token,
);

Expand All @@ -570,8 +582,8 @@ describe('utils/state.ts', () => {
.get('/repos/manosim/notifications-test/issues/comments/302888448')
.reply(200, { user: { login: 'some-user' } });

const result = await getGitifySubjectForPullRequest(
mockedSingleNotification,
const result = await getGitifySubjectDetails(
mockNotification,
mockAccounts.token,
);

Expand Down
52 changes: 21 additions & 31 deletions src/utils/subject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Notification,
PullRequest,
PullRequestStateType,
User,
WorkflowRunAttributes,
} from '../typesGithub';
import { apiRequestAuth } from './api-requests';
Expand Down Expand Up @@ -77,7 +78,7 @@ function getCheckSuiteStatus(statusDisplayName: string): CheckSuiteStatus {
}
}

export function getGitifySubjectForCheckSuite(
function getGitifySubjectForCheckSuite(
notification: Notification,
): GitifySubject {
return {
Expand All @@ -86,7 +87,7 @@ export function getGitifySubjectForCheckSuite(
};
}

export async function getGitifySubjectForDiscussion(
async function getGitifySubjectForDiscussion(
notification: Notification,
token: string,
): Promise<GitifySubject> {
Expand Down Expand Up @@ -116,53 +117,31 @@ export async function getGitifySubjectForDiscussion(
};
}

export async function getGitifySubjectForIssue(
async function getGitifySubjectForIssue(
notification: Notification,
token: string,
): Promise<GitifySubject> {
const issue: Issue = (
await apiRequestAuth(notification.subject.url, 'GET', token)
).data;

let issueCommentUser = null;
if (notification.subject.latest_comment_url) {
const issueComment: IssueComments = (
await apiRequestAuth(
notification.subject.latest_comment_url,
'GET',
token,
)
).data;

issueCommentUser = issueComment.user.login;
}
const issueCommentUser = await getLatestCommentUser(notification, token);

return {
state: issue.state_reason ?? issue.state,
user: issueCommentUser,
user: issueCommentUser.login,
};
}

export async function getGitifySubjectForPullRequest(
async function getGitifySubjectForPullRequest(
notification: Notification,
token: string,
): Promise<GitifySubject> {
const pr: PullRequest = (
await apiRequestAuth(notification.subject.url, 'GET', token)
).data;

let prCommentUser = null;
if (notification.subject.latest_comment_url) {
const prComment: IssueComments = (
await apiRequestAuth(
notification.subject.latest_comment_url,
'GET',
token,
)
).data;

prCommentUser = prComment.user.login;
}
const prCommentUser = await getLatestCommentUser(notification, token);

let prState: PullRequestStateType = pr.state;
if (pr.merged) {
Expand All @@ -173,11 +152,11 @@ export async function getGitifySubjectForPullRequest(

return {
state: prState,
user: prCommentUser,
user: prCommentUser.login,
};
}

export function getGitifySubjectForWorkflowRun(
function getGitifySubjectForWorkflowRun(
notification: Notification,
): GitifySubject {
return {
Expand Down Expand Up @@ -219,3 +198,14 @@ function getWorkflowRunStatus(statusDisplayName: string): CheckSuiteStatus {
return null;
}
}

async function getLatestCommentUser(
notification: Notification,
token: string,
): Promise<User> {
const response: IssueComments = (
await apiRequestAuth(notification.subject.latest_comment_url, 'GET', token)
).data;

return response.user;
}