From ff5c45c361b34d5e5c81bdff3d53430db7d99dc3 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Tue, 19 Mar 2024 04:37:05 -0400 Subject: [PATCH 1/2] feat: add release user --- src/typesGithub.ts | 16 +++++++ src/utils/subject.test.ts | 88 ++++++++++++++++++++++++++++----------- src/utils/subject.ts | 69 ++++++++++++++++-------------- 3 files changed, 117 insertions(+), 56 deletions(-) diff --git a/src/typesGithub.ts b/src/typesGithub.ts index 49f3354f0..8649b543d 100644 --- a/src/typesGithub.ts +++ b/src/typesGithub.ts @@ -245,6 +245,22 @@ export interface IssueComments { body: string; } +export interface ReleaseComments { + url: string; + assets_url: string; + html_url: string; + id: number; + author: User; + node_id: string; + tag_name: string; + name: string; + draft: boolean; + prerelease: boolean; + created_at: string; + published_at: string; + body: string; +} + export interface GraphQLSearch { data: { data: { diff --git a/src/utils/subject.test.ts b/src/utils/subject.test.ts index d2eb357cf..8d3654252 100644 --- a/src/utils/subject.test.ts +++ b/src/utils/subject.test.ts @@ -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 @@ -142,6 +141,7 @@ describe('utils/state.ts', () => { subject: { ...mockedSingleNotification.subject, title: 'This is an answered discussion', + type: 'Discussion' as SubjectType, }, }; @@ -165,7 +165,7 @@ describe('utils/state.ts', () => { }, }); - const result = await getGitifySubjectForDiscussion( + const result = await getGitifySubjectDetails( mockNotification, mockAccounts.token, ); @@ -180,6 +180,7 @@ describe('utils/state.ts', () => { subject: { ...mockedSingleNotification.subject, title: 'This is a duplicate discussion', + type: 'Discussion' as SubjectType, }, }; @@ -203,7 +204,7 @@ describe('utils/state.ts', () => { }, }); - const result = await getGitifySubjectForDiscussion( + const result = await getGitifySubjectDetails( mockNotification, mockAccounts.token, ); @@ -218,6 +219,7 @@ describe('utils/state.ts', () => { subject: { ...mockedSingleNotification.subject, title: 'This is an open discussion', + type: 'Discussion' as SubjectType, }, }; @@ -241,7 +243,7 @@ describe('utils/state.ts', () => { }, }); - const result = await getGitifySubjectForDiscussion( + const result = await getGitifySubjectDetails( mockNotification, mockAccounts.token, ); @@ -256,6 +258,7 @@ describe('utils/state.ts', () => { subject: { ...mockedSingleNotification.subject, title: 'This is an outdated discussion', + type: 'Discussion' as SubjectType, }, }; @@ -279,7 +282,7 @@ describe('utils/state.ts', () => { }, }); - const result = await getGitifySubjectForDiscussion( + const result = await getGitifySubjectDetails( mockNotification, mockAccounts.token, ); @@ -294,6 +297,7 @@ describe('utils/state.ts', () => { subject: { ...mockedSingleNotification.subject, title: 'This is a reopened discussion', + type: 'Discussion' as SubjectType, }, }; @@ -317,7 +321,7 @@ describe('utils/state.ts', () => { }, }); - const result = await getGitifySubjectForDiscussion( + const result = await getGitifySubjectDetails( mockNotification, mockAccounts.token, ); @@ -332,6 +336,7 @@ describe('utils/state.ts', () => { subject: { ...mockedSingleNotification.subject, title: 'This is a resolved discussion', + type: 'Discussion' as SubjectType, }, }; @@ -355,7 +360,7 @@ describe('utils/state.ts', () => { }, }); - const result = await getGitifySubjectForDiscussion( + const result = await getGitifySubjectDetails( mockNotification, mockAccounts.token, ); @@ -370,6 +375,7 @@ describe('utils/state.ts', () => { subject: { ...mockedSingleNotification.subject, title: 'This is a discussion', + type: 'Discussion' as SubjectType, }, }; @@ -402,7 +408,7 @@ describe('utils/state.ts', () => { }, }); - const result = await getGitifySubjectForDiscussion( + const result = await getGitifySubjectDetails( mockNotification, mockAccounts.token, ); @@ -410,8 +416,6 @@ describe('utils/state.ts', () => { expect(result.state).toBe('OPEN'); expect(result.user).toBe(null); }); - - it('handles unknown or missing results', async () => {}); }); describe('getGitifySubjectForIssue', () => { @@ -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, ); @@ -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, ); @@ -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, ); @@ -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, ); @@ -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, ); @@ -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') @@ -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, ); @@ -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, ); @@ -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, ); @@ -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, ); @@ -580,6 +592,32 @@ describe('utils/state.ts', () => { }); }); + describe('getGitifySubjectForRelease', () => { + it('release notification', async () => { + const mockNotification = { + ...mockedSingleNotification, + subject: { + ...mockedSingleNotification.subject, + type: 'Release' as SubjectType, + url: 'https://api.github.com/repos/manosim/notifications-test/releases/1', + latest_comment_url: + 'https://api.github.com/repos/manosim/notifications-test/releases/1', + }, + }; + + nock('https://api.github.com') + .get('/repos/manosim/notifications-test/releases/1') + .reply(200, { author: { login: 'some-user' } }); + + const result = await getGitifySubjectDetails( + mockNotification, + mockAccounts.token, + ); + + expect(result.user).toBe('some-user'); + }); + }); + describe('getWorkflowRunState', () => { it('deploy review workflow run state', async () => { const mockNotification = { diff --git a/src/utils/subject.ts b/src/utils/subject.ts index c58f54fd6..e4d2a051a 100644 --- a/src/utils/subject.ts +++ b/src/utils/subject.ts @@ -9,6 +9,8 @@ import { Notification, PullRequest, PullRequestStateType, + ReleaseComments, + User, WorkflowRunAttributes, } from '../typesGithub'; import { apiRequestAuth } from './api-requests'; @@ -26,6 +28,8 @@ export async function getGitifySubjectDetails( return await getGitifySubjectForIssue(notification, token); case 'PullRequest': return await getGitifySubjectForPullRequest(notification, token); + case 'Release': + return await getGitifySubjectForRelease(notification, token); case 'WorkflowRun': return getGitifySubjectForWorkflowRun(notification); default: @@ -77,7 +81,7 @@ function getCheckSuiteStatus(statusDisplayName: string): CheckSuiteStatus { } } -export function getGitifySubjectForCheckSuite( +function getGitifySubjectForCheckSuite( notification: Notification, ): GitifySubject { return { @@ -86,7 +90,7 @@ export function getGitifySubjectForCheckSuite( }; } -export async function getGitifySubjectForDiscussion( +async function getGitifySubjectForDiscussion( notification: Notification, token: string, ): Promise { @@ -116,7 +120,7 @@ export async function getGitifySubjectForDiscussion( }; } -export async function getGitifySubjectForIssue( +async function getGitifySubjectForIssue( notification: Notification, token: string, ): Promise { @@ -124,26 +128,15 @@ export async function getGitifySubjectForIssue( 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 { @@ -151,18 +144,7 @@ export async function getGitifySubjectForPullRequest( 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) { @@ -173,11 +155,23 @@ export async function getGitifySubjectForPullRequest( return { state: prState, - user: prCommentUser, + user: prCommentUser.login, + }; +} + +async function getGitifySubjectForRelease( + notification: Notification, + token: string, +): Promise { + const releaseCommentUser = await getLatestCommentUser(notification, token); + + return { + state: null, + user: releaseCommentUser.login, }; } -export function getGitifySubjectForWorkflowRun( +function getGitifySubjectForWorkflowRun( notification: Notification, ): GitifySubject { return { @@ -219,3 +213,16 @@ function getWorkflowRunStatus(statusDisplayName: string): CheckSuiteStatus { return null; } } + +async function getLatestCommentUser( + notification: Notification, + token: string, +): Promise { + const response: IssueComments | ReleaseComments = ( + await apiRequestAuth(notification.subject.latest_comment_url, 'GET', token) + ).data; + + return ( + (response as IssueComments).user ?? (response as ReleaseComments).author + ); +} From 77ead0ce2ed3bfdcd3d52c71fc007b899bfeb47c Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Tue, 19 Mar 2024 05:01:01 -0400 Subject: [PATCH 2/2] Merge remote-tracking branch 'origin/main' into feature/support-release-subject --- src/utils/subject.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/utils/subject.ts b/src/utils/subject.ts index 43324e240..e4d2a051a 100644 --- a/src/utils/subject.ts +++ b/src/utils/subject.ts @@ -164,6 +164,7 @@ async function getGitifySubjectForRelease( token: string, ): Promise { const releaseCommentUser = await getLatestCommentUser(notification, token); + return { state: null, user: releaseCommentUser.login,