Skip to content

Commit 1653666

Browse files
authored
feat: use regex to extract workflow-run attributes (#850)
1 parent cddd3c3 commit 1653666

File tree

4 files changed

+101
-4
lines changed

4 files changed

+101
-4
lines changed

src/hooks/useNotifications.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,14 @@ describe('hooks/useNotifications.ts', () => {
237237
url: 'https://api.github.com/5',
238238
},
239239
},
240+
{
241+
id: 6,
242+
subject: {
243+
title: 'This is a workflow run.',
244+
type: 'WorkflowRun',
245+
url: 'https://api.github.com/6',
246+
},
247+
},
240248
];
241249

242250
nock('https://api.github.com')
@@ -286,7 +294,7 @@ describe('hooks/useNotifications.ts', () => {
286294
expect(result.current.notifications[0].hostname).toBe('github.com');
287295
});
288296

289-
expect(result.current.notifications[0].notifications.length).toBe(5);
297+
expect(result.current.notifications[0].notifications.length).toBe(6);
290298
});
291299
});
292300
});

src/typesGithub.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,9 @@ export interface CheckSuiteAttributes {
219219
status: CheckSuiteStatus | null;
220220
branchName: string;
221221
}
222+
223+
export interface WorkflowRunAttributes {
224+
user: string;
225+
statusDisplayName: string;
226+
status: CheckSuiteStatus | null;
227+
}

src/utils/state.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
getDiscussionState,
99
getIssueState,
1010
getPullRequestState,
11+
getWorkflowRunAttributes,
1112
} from './state';
1213
describe('utils/state.ts', () => {
1314
beforeEach(() => {
@@ -518,4 +519,51 @@ describe('utils/state.ts', () => {
518519
expect(result).toBe('open');
519520
});
520521
});
522+
523+
describe('getWorkflowRunState', () => {
524+
it('deploy review workflow run state', async () => {
525+
const mockNotification = {
526+
...mockedSingleNotification,
527+
subject: {
528+
...mockedSingleNotification.subject,
529+
title: 'some-user requested your review to deploy to an environment',
530+
},
531+
};
532+
533+
const result = getWorkflowRunAttributes(mockNotification);
534+
535+
expect(result.user).toBe('some-user');
536+
expect(result.status).toBe('waiting');
537+
});
538+
539+
it('unknown workflow run state', async () => {
540+
const mockNotification = {
541+
...mockedSingleNotification,
542+
subject: {
543+
...mockedSingleNotification.subject,
544+
title:
545+
'some-user requested your unknown-state to deploy to an environment',
546+
},
547+
};
548+
549+
const result = getWorkflowRunAttributes(mockNotification);
550+
551+
expect(result.user).toBe('some-user');
552+
expect(result.status).toBeNull();
553+
});
554+
555+
it('unhandled workflow run title', async () => {
556+
const mockNotification = {
557+
...mockedSingleNotification,
558+
subject: {
559+
...mockedSingleNotification.subject,
560+
title: 'unhandled workflow run structure',
561+
},
562+
};
563+
564+
const result = getWorkflowRunAttributes(mockNotification);
565+
566+
expect(result).toBeNull();
567+
});
568+
});
521569
});

src/utils/state.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Notification,
1010
PullRequestStateType,
1111
StateType,
12+
WorkflowRunAttributes,
1213
} from '../typesGithub';
1314
import { apiRequestAuth } from './api-requests';
1415

@@ -25,6 +26,8 @@ export async function getNotificationState(
2526
return await getIssueState(notification, token);
2627
case 'PullRequest':
2728
return await getPullRequestState(notification, token);
29+
case 'WorkflowRun':
30+
return getWorkflowRunAttributes(notification)?.status;
2831
default:
2932
return null;
3033
}
@@ -59,9 +62,7 @@ export function getCheckSuiteAttributes(
5962
return null;
6063
}
6164

62-
export function getCheckSuiteStatus(
63-
statusDisplayName: string,
64-
): CheckSuiteStatus {
65+
function getCheckSuiteStatus(statusDisplayName: string): CheckSuiteStatus {
6566
switch (statusDisplayName) {
6667
case 'cancelled':
6768
return 'cancelled';
@@ -151,3 +152,37 @@ export async function getPullRequestState(
151152

152153
return pr.state;
153154
}
155+
156+
/**
157+
* Ideally we would be using a GitHub API to fetch the CheckSuite / WorkflowRun state,
158+
* but there isn't an obvious/clean way to do this currently.
159+
*/
160+
export function getWorkflowRunAttributes(
161+
notification: Notification,
162+
): WorkflowRunAttributes | null {
163+
const regexPattern =
164+
/^(?<user>.*?) requested your (?<statusDisplayName>.*?) to deploy to an environment$/;
165+
166+
const matches = regexPattern.exec(notification.subject.title);
167+
168+
if (matches) {
169+
const { groups } = matches;
170+
171+
return {
172+
user: groups.user,
173+
status: getWorkflowRunStatus(groups.statusDisplayName),
174+
statusDisplayName: groups.statusDisplayName,
175+
};
176+
}
177+
178+
return null;
179+
}
180+
181+
function getWorkflowRunStatus(statusDisplayName: string): CheckSuiteStatus {
182+
switch (statusDisplayName) {
183+
case 'review':
184+
return 'waiting';
185+
default:
186+
return null;
187+
}
188+
}

0 commit comments

Comments
 (0)