From 15485132f6372547ea810bdaf3ac53aa8a3eeb24 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Wed, 6 Mar 2024 15:48:06 -0500 Subject: [PATCH] feat: add hyperlink to workflow run --- src/utils/helpers.test.ts | 69 +++++++++++++++++++++++++++++++++++++++ src/utils/helpers.ts | 21 ++++++++++++ 2 files changed, 90 insertions(+) diff --git a/src/utils/helpers.test.ts b/src/utils/helpers.test.ts index 8cef1c3cc..c4473f226 100644 --- a/src/utils/helpers.test.ts +++ b/src/utils/helpers.test.ts @@ -310,6 +310,75 @@ describe('utils/helpers.ts', () => { ); }); + describe('Workflow Run URLs', () => { + it('approval requested', async () => { + const subject = { + title: 'some-user requested your review to deploy to an environment', + url: null, + latest_comment_url: null, + type: 'WorkflowRun' as SubjectType, + }; + + const result = await generateGitHubWebUrl( + { + ...mockedSingleNotification, + subject: subject, + }, + mockAccounts, + ); + + expect(apiRequestAuthMock).toHaveBeenCalledTimes(0); + expect(result).toBe( + `https://github.com/manosim/notifications-test/actions?query=is%3Awaiting&${mockedNotificationReferrer}`, + ); + }); + + it('unhandled status/action scenario', async () => { + const subject = { + title: + 'some-user requested your unhandled-action to deploy to an environment', + url: null, + latest_comment_url: null, + type: 'WorkflowRun' as SubjectType, + }; + + const result = await generateGitHubWebUrl( + { + ...mockedSingleNotification, + subject: subject, + }, + mockAccounts, + ); + + expect(apiRequestAuthMock).toHaveBeenCalledTimes(0); + expect(result).toBe( + `https://github.com/manosim/notifications-test/actions?${mockedNotificationReferrer}`, + ); + }); + + it('unhandled workflow scenario', async () => { + const subject = { + title: 'some unhandled scenario', + url: null, + latest_comment_url: null, + type: 'WorkflowRun' as SubjectType, + }; + + const result = await generateGitHubWebUrl( + { + ...mockedSingleNotification, + subject: subject, + }, + mockAccounts, + ); + + expect(apiRequestAuthMock).toHaveBeenCalledTimes(0); + expect(result).toBe( + `https://github.com/manosim/notifications-test/actions?${mockedNotificationReferrer}`, + ); + }); + }); + it('defaults to repository url', async () => { const subject = { title: 'generate github web url unit tests', diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index 2ccdbe9b4..20353d6d1 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -8,6 +8,7 @@ import { import { apiRequestAuth } from '../utils/api-requests'; import { openExternalLink } from '../utils/comms'; import { Constants } from './constants'; +import { getWorkflowRunAttributes } from './state'; export function getEnterpriseAccountToken( hostname: string, @@ -70,6 +71,23 @@ export async function getHtmlUrl(url: string, token: string): Promise { return response.data.html_url; } +export function getWorkflowRunUrl(notification: Notification) { + let url = `${notification.repository.html_url}/actions`; + let filters = []; + + const workflowRunAttributes = getWorkflowRunAttributes(notification); + + if (workflowRunAttributes?.status) { + filters.push(`is:${workflowRunAttributes.status}`); + } + + if (filters.length > 0) { + url += `?query=${filters.join('+')}`; + } + + return url; +} + async function getDiscussionUrl( notification: Notification, token: string, @@ -167,6 +185,9 @@ export async function generateGitHubWebUrl( case 'RepositoryInvitation': url = `${notification.repository.html_url}/invitations`; break; + case 'WorkflowRun': + url = getWorkflowRunUrl(notification); + break; default: url = notification.repository.html_url; break;