Skip to content

Commit 576f160

Browse files
committed
fix discussions-url
1 parent f8a6ac2 commit 576f160

File tree

4 files changed

+148
-1
lines changed

4 files changed

+148
-1
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
"afterSign": "scripts/notarize.js"
107107
},
108108
"dependencies": {
109+
"@octokit/graphql": "^4.8.0",
109110
"@primer/octicons-react": "^11.2.0",
110111
"autoprefixer": "^10.1.0",
111112
"axios": "=0.21.1",

src/components/NotificationRow.tsx

+11-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { formatDistanceToNow, parseISO } from 'date-fns';
55
import { CheckIcon, MuteIcon } from '@primer/octicons-react';
66

77
import { formatReason, getNotificationTypeIcon } from '../utils/github-api';
8-
import { generateGitHubWebUrl } from '../utils/helpers';
8+
import { generateGitHubWebUrl, getDiscussionUrl } from '../utils/helpers';
99
import { Notification } from '../typesGithub';
1010
import { AppContext } from '../context/App';
1111

@@ -38,6 +38,16 @@ export const NotificationRow: React.FC<IProps> = ({
3838
accounts.user?.id
3939
);
4040
shell.openExternal(url);
41+
} else if (notification.subject.type === 'Discussion') {
42+
getDiscussionUrl(notification, accounts.token).then(url =>
43+
shell.openExternal(
44+
generateGitHubWebUrl(
45+
url || `${notification.repository.url}/discussions`,
46+
notification.id,
47+
accounts.user?.id
48+
)
49+
)
50+
);
4151
}
4252
}, [notification]);
4353

src/utils/helpers.ts

+45
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { EnterpriseAccount } from '../types';
2+
import { graphql } from "@octokit/graphql";
3+
import type { GraphQlQueryResponseData } from "@octokit/graphql";
4+
import { Notification } from '../typesGithub';
25

36
import { Constants } from './constants';
47

@@ -59,3 +62,45 @@ export function generateGitHubWebUrl(
5962

6063
return newUrl;
6164
}
65+
66+
const addHours = (date: string, hours: number) =>
67+
new Date(new Date(date).getTime() + hours * 36e5).toISOString();
68+
69+
const queryString = (repo: string, title: string, lastUpdated: string) =>
70+
`${title} in:title repo:${repo} -updated:<${addHours(lastUpdated, -2)}`;
71+
72+
export async function getDiscussionUrl(
73+
notification: Notification,
74+
token: string
75+
): Promise<string> {
76+
const graphqlResults: GraphQlQueryResponseData = await graphql({
77+
query: `{
78+
search(query:"${queryString(
79+
notification.repository.full_name,
80+
notification.subject.title,
81+
notification.updated_at
82+
)}", type: DISCUSSION, first: 10) {
83+
edges {
84+
node {
85+
... on Discussion {
86+
viewerSubscription
87+
title
88+
url
89+
}
90+
}
91+
}
92+
}
93+
}`,
94+
headers: {
95+
authorization: `token ${token}`,
96+
},
97+
});
98+
let edges = graphqlResults.search.edges.filter(
99+
edge => edge.node.title === notification.subject.title
100+
);
101+
if (edges.length > 1)
102+
edges = edges.filter(
103+
edge => edge.node.viewerSubscription === 'SUBSCRIBED'
104+
);
105+
return edges[0]?.node.url;
106+
}

yarn.lock

+91
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,57 @@
579579
lodash "^4.17.15"
580580
tmp-promise "^3.0.2"
581581

582+
"@octokit/endpoint@^6.0.1":
583+
version "6.0.12"
584+
resolved "https://registry.yarnpkg.com/@octokit/endpoint/-/endpoint-6.0.12.tgz#3b4d47a4b0e79b1027fb8d75d4221928b2d05658"
585+
integrity sha512-lF3puPwkQWGfkMClXb4k/eUT/nZKQfxinRWJrdZaJO85Dqwo/G0yOC434Jr2ojwafWJMYqFGFa5ms4jJUgujdA==
586+
dependencies:
587+
"@octokit/types" "^6.0.3"
588+
is-plain-object "^5.0.0"
589+
universal-user-agent "^6.0.0"
590+
591+
"@octokit/graphql@^4.8.0":
592+
version "4.8.0"
593+
resolved "https://registry.yarnpkg.com/@octokit/graphql/-/graphql-4.8.0.tgz#664d9b11c0e12112cbf78e10f49a05959aa22cc3"
594+
integrity sha512-0gv+qLSBLKF0z8TKaSKTsS39scVKF9dbMxJpj3U0vC7wjNWFuIpL/z76Qe2fiuCbDRcJSavkXsVtMS6/dtQQsg==
595+
dependencies:
596+
"@octokit/request" "^5.6.0"
597+
"@octokit/types" "^6.0.3"
598+
universal-user-agent "^6.0.0"
599+
600+
"@octokit/openapi-types@^11.2.0":
601+
version "11.2.0"
602+
resolved "https://registry.yarnpkg.com/@octokit/openapi-types/-/openapi-types-11.2.0.tgz#b38d7fc3736d52a1e96b230c1ccd4a58a2f400a6"
603+
integrity sha512-PBsVO+15KSlGmiI8QAzaqvsNlZlrDlyAJYcrXBCvVUxCp7VnXjkwPoFHgjEJXx3WF9BAwkA6nfCUA7i9sODzKA==
604+
605+
"@octokit/request-error@^2.1.0":
606+
version "2.1.0"
607+
resolved "https://registry.yarnpkg.com/@octokit/request-error/-/request-error-2.1.0.tgz#9e150357831bfc788d13a4fd4b1913d60c74d677"
608+
integrity sha512-1VIvgXxs9WHSjicsRwq8PlR2LR2x6DwsJAaFgzdi0JfJoGSO8mYI/cHJQ+9FbN21aa+DrgNLnwObmyeSC8Rmpg==
609+
dependencies:
610+
"@octokit/types" "^6.0.3"
611+
deprecation "^2.0.0"
612+
once "^1.4.0"
613+
614+
"@octokit/request@^5.6.0":
615+
version "5.6.3"
616+
resolved "https://registry.yarnpkg.com/@octokit/request/-/request-5.6.3.tgz#19a022515a5bba965ac06c9d1334514eb50c48b0"
617+
integrity sha512-bFJl0I1KVc9jYTe9tdGGpAMPy32dLBXXo1dS/YwSCTL/2nd9XeHsY616RE3HPXDVk+a+dBuzyz5YdlXwcDTr2A==
618+
dependencies:
619+
"@octokit/endpoint" "^6.0.1"
620+
"@octokit/request-error" "^2.1.0"
621+
"@octokit/types" "^6.16.1"
622+
is-plain-object "^5.0.0"
623+
node-fetch "^2.6.7"
624+
universal-user-agent "^6.0.0"
625+
626+
"@octokit/types@^6.0.3", "@octokit/types@^6.16.1":
627+
version "6.34.0"
628+
resolved "https://registry.yarnpkg.com/@octokit/types/-/types-6.34.0.tgz#c6021333334d1ecfb5d370a8798162ddf1ae8218"
629+
integrity sha512-s1zLBjWhdEI2zwaoSgyOFoKSl109CUcVBCc7biPJ3aAf6LGLU6szDvi31JPU7bxfla2lqfhjbbg/5DdFNxOwHw==
630+
dependencies:
631+
"@octokit/openapi-types" "^11.2.0"
632+
582633
"@primer/octicons-react@^11.2.0":
583634
version "11.2.0"
584635
resolved "https://registry.yarnpkg.com/@primer/octicons-react/-/octicons-react-11.2.0.tgz#af60301257cdbc1561c2647678282d3bce5a6c54"
@@ -2245,6 +2296,11 @@ delayed-stream@~1.0.0:
22452296
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
22462297
integrity sha1-3zrhmayt+31ECqrgsp4icrJOxhk=
22472298

2299+
deprecation@^2.0.0:
2300+
version "2.3.1"
2301+
resolved "https://registry.yarnpkg.com/deprecation/-/deprecation-2.3.1.tgz#6368cbdb40abf3373b525ac87e4a260c3a700919"
2302+
integrity sha512-xmHIy4F3scKVwMsQ4WnVaS8bHOx0DmVwRywosKhaILI0ywMDWPtBSku2HNxRvF7jtwDRsoEwYQSfbxj8b7RlJQ==
2303+
22482304
detect-newline@^3.0.0:
22492305
version "3.1.0"
22502306
resolved "https://registry.yarnpkg.com/detect-newline/-/detect-newline-3.1.0.tgz#576f5dfc63ae1a192ff192d8ad3af6308991b651"
@@ -3408,6 +3464,11 @@ is-plain-object@^2.0.3, is-plain-object@^2.0.4:
34083464
dependencies:
34093465
isobject "^3.0.1"
34103466

3467+
is-plain-object@^5.0.0:
3468+
version "5.0.0"
3469+
resolved "https://registry.yarnpkg.com/is-plain-object/-/is-plain-object-5.0.0.tgz#4427f50ab3429e9025ea7d52e9043a9ef4159344"
3470+
integrity sha512-VRSzKkbMm5jMDoKLbltAkFQ5Qr7VDiTFGXxYFXXowVj387GeGNOCsOH6Msy00SGZ3Fp84b1Naa1psqgcCIEP5Q==
3471+
34113472
is-potential-custom-element-name@^1.0.0:
34123473
version "1.0.0"
34133474
resolved "https://registry.yarnpkg.com/is-potential-custom-element-name/-/is-potential-custom-element-name-1.0.0.tgz#0c52e54bcca391bb2c494b21e8626d7336c6e397"
@@ -4448,6 +4509,13 @@ node-emoji@^1.8.1:
44484509
dependencies:
44494510
lodash.toarray "^4.4.0"
44504511

4512+
node-fetch@^2.6.7:
4513+
version "2.6.7"
4514+
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad"
4515+
integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==
4516+
dependencies:
4517+
whatwg-url "^5.0.0"
4518+
44514519
node-int64@^0.4.0:
44524520
version "0.4.0"
44534521
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
@@ -6050,6 +6118,11 @@ tr46@^2.0.2:
60506118
dependencies:
60516119
punycode "^2.1.1"
60526120

6121+
tr46@~0.0.3:
6122+
version "0.0.3"
6123+
resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"
6124+
integrity sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=
6125+
60536126
truncate-utf8-bytes@^1.0.0:
60546127
version "1.0.2"
60556128
resolved "https://registry.yarnpkg.com/truncate-utf8-bytes/-/truncate-utf8-bytes-1.0.2.tgz#405923909592d56f78a5818434b0b78489ca5f2b"
@@ -6193,6 +6266,11 @@ unique-string@^2.0.0:
61936266
dependencies:
61946267
crypto-random-string "^2.0.0"
61956268

6269+
universal-user-agent@^6.0.0:
6270+
version "6.0.0"
6271+
resolved "https://registry.yarnpkg.com/universal-user-agent/-/universal-user-agent-6.0.0.tgz#3381f8503b251c0d9cd21bc1de939ec9df5480ee"
6272+
integrity sha512-isyNax3wXoKaulPDZWHQqbmIx1k2tb9fb3GGDBRxCscfYV2Ch7WxPArBsFEG8s/safwXTT7H4QGhaIkTp9447w==
6273+
61966274
universalify@^0.1.0:
61976275
version "0.1.2"
61986276
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
@@ -6345,6 +6423,11 @@ watchpack@^2.0.0:
63456423
glob-to-regexp "^0.4.1"
63466424
graceful-fs "^4.1.2"
63476425

6426+
webidl-conversions@^3.0.0:
6427+
version "3.0.1"
6428+
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"
6429+
integrity sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE=
6430+
63486431
webidl-conversions@^5.0.0:
63496432
version "5.0.0"
63506433
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-5.0.0.tgz#ae59c8a00b121543a2acc65c0434f57b0fc11aff"
@@ -6439,6 +6522,14 @@ whatwg-mimetype@^2.3.0:
64396522
resolved "https://registry.yarnpkg.com/whatwg-mimetype/-/whatwg-mimetype-2.3.0.tgz#3d4b1e0312d2079879f826aff18dbeeca5960fbf"
64406523
integrity sha512-M4yMwr6mAnQz76TbJm914+gPpB/nCwvZbJU28cUD6dR004SAxDLOOSUaB1JDRqLtaOV/vi0IC5lEAGFgrjGv/g==
64416524

6525+
whatwg-url@^5.0.0:
6526+
version "5.0.0"
6527+
resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d"
6528+
integrity sha1-lmRU6HZUYuN2RNNib2dCzotwll0=
6529+
dependencies:
6530+
tr46 "~0.0.3"
6531+
webidl-conversions "^3.0.0"
6532+
64426533
whatwg-url@^8.0.0:
64436534
version "8.4.0"
64446535
resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-8.4.0.tgz#50fb9615b05469591d2b2bd6dfaed2942ed72837"

0 commit comments

Comments
 (0)