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: use regex to extract workflow-run attributes #850

Merged
merged 1 commit into from Mar 3, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/hooks/useNotifications.test.ts
Expand Up @@ -237,6 +237,14 @@ describe('hooks/useNotifications.ts', () => {
url: 'https://api.github.com/5',
},
},
{
id: 6,
subject: {
title: 'This is a workflow run.',
type: 'WorkflowRun',
url: 'https://api.github.com/6',
},
},
];

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

expect(result.current.notifications[0].notifications.length).toBe(5);
expect(result.current.notifications[0].notifications.length).toBe(6);
});
});
});
Expand Down
6 changes: 6 additions & 0 deletions src/typesGithub.ts
Expand Up @@ -219,3 +219,9 @@ export interface CheckSuiteAttributes {
status: CheckSuiteStatus | null;
branchName: string;
}

export interface WorkflowRunAttributes {
user: string;
statusDisplayName: string;
status: CheckSuiteStatus | null;
}
48 changes: 48 additions & 0 deletions src/utils/state.test.ts
Expand Up @@ -8,6 +8,7 @@ import {
getDiscussionState,
getIssueState,
getPullRequestState,
getWorkflowRunAttributes,
} from './state';
describe('utils/state.ts', () => {
beforeEach(() => {
Expand Down Expand Up @@ -518,4 +519,51 @@ describe('utils/state.ts', () => {
expect(result).toBe('open');
});
});

describe('getWorkflowRunState', () => {
it('deploy review workflow run state', async () => {
const mockNotification = {
...mockedSingleNotification,
subject: {
...mockedSingleNotification.subject,
title: 'some-user requested your review to deploy to an environment',
},
};

const result = getWorkflowRunAttributes(mockNotification);

expect(result.user).toBe('some-user');
expect(result.status).toBe('waiting');
});

it('unknown workflow run state', async () => {
const mockNotification = {
...mockedSingleNotification,
subject: {
...mockedSingleNotification.subject,
title:
'some-user requested your unknown-state to deploy to an environment',
},
};

const result = getWorkflowRunAttributes(mockNotification);

expect(result.user).toBe('some-user');
expect(result.status).toBeNull();
});

it('unhandled workflow run title', async () => {
const mockNotification = {
...mockedSingleNotification,
subject: {
...mockedSingleNotification.subject,
title: 'unhandled workflow run structure',
},
};

const result = getWorkflowRunAttributes(mockNotification);

expect(result).toBeNull();
});
});
});
41 changes: 38 additions & 3 deletions src/utils/state.ts
Expand Up @@ -9,6 +9,7 @@ import {
Notification,
PullRequestStateType,
StateType,
WorkflowRunAttributes,
} from '../typesGithub';
import { apiRequestAuth } from './api-requests';

Expand All @@ -25,6 +26,8 @@ export async function getNotificationState(
return await getIssueState(notification, token);
case 'PullRequest':
return await getPullRequestState(notification, token);
case 'WorkflowRun':
return getWorkflowRunAttributes(notification)?.status;
default:
return null;
}
Expand Down Expand Up @@ -59,9 +62,7 @@ export function getCheckSuiteAttributes(
return null;
}

export function getCheckSuiteStatus(
statusDisplayName: string,
): CheckSuiteStatus {
function getCheckSuiteStatus(statusDisplayName: string): CheckSuiteStatus {
switch (statusDisplayName) {
case 'cancelled':
return 'cancelled';
Expand Down Expand Up @@ -151,3 +152,37 @@ export async function getPullRequestState(

return pr.state;
}

/**
* Ideally we would be using a GitHub API to fetch the CheckSuite / WorkflowRun state,
* but there isn't an obvious/clean way to do this currently.
*/
export function getWorkflowRunAttributes(
notification: Notification,
): WorkflowRunAttributes | null {
const regexPattern =
/^(?<user>.*?) requested your (?<statusDisplayName>.*?) to deploy to an environment$/;

const matches = regexPattern.exec(notification.subject.title);

if (matches) {
const { groups } = matches;

return {
user: groups.user,
status: getWorkflowRunStatus(groups.statusDisplayName),
statusDisplayName: groups.statusDisplayName,
};
}

return null;
}

function getWorkflowRunStatus(statusDisplayName: string): CheckSuiteStatus {
switch (statusDisplayName) {
case 'review':
return 'waiting';
default:
return null;
}
}