From 979ab55ba3f1ca3076c7aef31235c094ac64bb14 Mon Sep 17 00:00:00 2001 From: kadiryazici Date: Wed, 28 Feb 2024 15:28:06 +0300 Subject: [PATCH] Add open support for CI Activity --- src-tauri/Cargo.toml | 15 +-------------- src/pages/HomePage.vue | 4 ++-- src/utils/github.ts | 39 ++++++++++++++++++++++++--------------- 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index a2b6a39..ccfc053 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -16,20 +16,7 @@ tauri-build = { version = "1.2", features = [] } [dependencies] serde_json = "1.0" serde = { version = "1.0", features = [ "derive" ] } -tauri = { version = "1.2.4", features = [ - "dialog-confirm", - "http-request", - "icon-ico", - "icon-png", - "macos-private-api", - "notification-all", - "os-all", - "process-exit", - "process-relaunch", - "shell-open", - "system-tray", - "updater" -] } +tauri = { version = "1.2.4", features = ["dialog-confirm", "http-request", "icon-ico", "icon-png", "macos-private-api", "notification-all", "os-all", "process-exit", "process-relaunch", "shell-open", "system-tray", "updater"] } window-vibrancy = "0.3.2" tiny_http = "0.12.0" ascii = "1.1.0" diff --git a/src/pages/HomePage.vue b/src/pages/HomePage.vue index 3f90c20..62d6e84 100644 --- a/src/pages/HomePage.vue +++ b/src/pages/HomePage.vue @@ -6,7 +6,7 @@ import type { ItemRenderList } from 'vue-selectable-items' import { useStore } from '../stores/store' import NotificationItem from '../components/NotificationItem.vue' import { type MinimalRepository, type Thread, markNotificationAsRead, unsubscribeNotification } from '../api/notifications' -import { toGithubWebURL } from '../utils/github' +import { createGithubWebURL } from '../utils/github' import { AppStorage } from '../storage' import NotificationSkeleton from '../components/NotificationSkeleton.vue' import { useElementNavigation } from '../composables/useElementNavigation' @@ -29,7 +29,7 @@ if (route.state.fetchOnEnter) store.fetchNotifications(true) function handleOpenNotification(thread: Thread) { - const url = toGithubWebURL({ notification: thread, userId: AppStorage.get('user')!.id }) + const url = createGithubWebURL({ notification: thread, userId: AppStorage.get('user')!.id }) open(url) } diff --git a/src/utils/github.ts b/src/utils/github.ts index 2b98190..a9e6323 100644 --- a/src/utils/github.ts +++ b/src/utils/github.ts @@ -1,6 +1,6 @@ import { GITHUB_AUTHORIZE_ENDPOINT, GITHUB_AUTH_SCOPES } from '../api/constants' import type { Thread } from '../api/notifications' -import { NotificationSubject } from '../constants' +import { NotificationReason, NotificationSubject } from '../constants' import { createURL } from './url' const NOTIFICATION_REFERRER_ID_KEY = 'notification_referrer_id' @@ -13,33 +13,42 @@ export function createNotificationReferrerId( return window.btoa(`018:NotificationThread${notificationId}:${userId}`) } -export type ToGithubWebURLArgs = { +export type CreateGithubWebUrlParams = { notification: Thread userId: number } -export function toGithubWebURL({ notification, userId }: ToGithubWebURLArgs) { +export function createGithubWebURL({ notification, userId }: CreateGithubWebUrlParams) { const notificationReferrerId = createNotificationReferrerId(notification.id, userId) + let url: string + if (notification.subject.type === NotificationSubject.Discussion) { - let newURL = `https://github.com/${notification.repository.full_name}/discussions` + url = `https://github.com/${notification.repository.full_name}/discussions` + url += `?${DISCUSSIONS_QUERY_KEY}=${decodeURIComponent(notification.subject.title)}` + } + else if (notification.reason === NotificationReason.CiActivity) { + // We cannot produce link to CiActivity so target to repo name + url = `https://github.com/${notification.repository.full_name}` + } + else { + url = notification.subject.url.replace('api.github.com/repos', 'github.com') - newURL = `?${newURL}&${DISCUSSIONS_QUERY_KEY}=${decodeURIComponent(notification.subject.title)}` + if (url.includes('/pulls/')) + url = url.replace('/pulls/', '/pull/') - return newURL + if (url.includes('/releases/')) { + url = url.replace('/repos', '') + url = url.slice(0, url.lastIndexOf('/')) + } } - let newUrl = notification.subject.url.replace('api.github.com/repos', 'github.com') + const refer = `${NOTIFICATION_REFERRER_ID_KEY}=${notificationReferrerId}` - if (newUrl.includes('/pulls/')) - newUrl = newUrl.replace('/pulls/', '/pull/') - - if (newUrl.includes('/releases/')) { - newUrl = newUrl.replace('/repos', '') - newUrl = newUrl.slice(0, newUrl.lastIndexOf('/')) - } + if (url.includes('?')) + return `${url}&${refer}` - return `${newUrl}?${NOTIFICATION_REFERRER_ID_KEY}=${notificationReferrerId}` + return `${url}?${refer}` } export function createAuthURL(port: number) {