|
1 | 1 | import { EnterpriseAccount } from '../types';
|
| 2 | +import { Notification } from '../typesGithub'; |
| 3 | +import { apiRequestAuth } from '../utils/api-requests'; |
2 | 4 |
|
3 | 5 | import { Constants } from './constants';
|
4 | 6 |
|
@@ -59,3 +61,42 @@ export function generateGitHubWebUrl(
|
59 | 61 |
|
60 | 62 | return newUrl;
|
61 | 63 | }
|
| 64 | + |
| 65 | +const addHours = (date: string, hours: number) => |
| 66 | + new Date(new Date(date).getTime() + hours * 36e5).toISOString(); |
| 67 | + |
| 68 | +const queryString = (repo: string, title: string, lastUpdated: string) => |
| 69 | + `${title} in:title repo:${repo} -updated:<${addHours(lastUpdated, -2)}`; |
| 70 | + |
| 71 | +export async function getDiscussionUrl( |
| 72 | + notification: Notification, |
| 73 | + token: string |
| 74 | +): Promise<string> { |
| 75 | + const response = await apiRequestAuth(`https://api.github.com/graphql`, 'POST', token, { |
| 76 | + query: `{ |
| 77 | + search(query:"${queryString( |
| 78 | + notification.repository.full_name, |
| 79 | + notification.subject.title, |
| 80 | + notification.updated_at |
| 81 | + )}", type: DISCUSSION, first: 10) { |
| 82 | + edges { |
| 83 | + node { |
| 84 | + ... on Discussion { |
| 85 | + viewerSubscription |
| 86 | + title |
| 87 | + url |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + }`, |
| 93 | + }); |
| 94 | + let edges = response?.data?.data?.search?.edges?.filter( |
| 95 | + edge => edge.node.title === notification.subject.title |
| 96 | + ) || []; |
| 97 | + if (edges.length > 1) |
| 98 | + edges = edges.filter( |
| 99 | + edge => edge.node.viewerSubscription === 'SUBSCRIBED' |
| 100 | + ); |
| 101 | + return edges[0]?.node.url; |
| 102 | +} |
0 commit comments