-
Notifications
You must be signed in to change notification settings - Fork 2
/
get-github-notifications.js
executable file
·65 lines (53 loc) · 1.64 KB
/
get-github-notifications.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env node
// @ts-check
import octicons from '@primer/octicons';
import anybar from 'anybar';
import { config } from 'dotenv';
import fetch from 'node-fetch';
config({ path: new URL('.env', import.meta.url).pathname });
async function main() {
/**
* @type {import('@octokit/types').Endpoints['GET /notifications']['response']['data']}
*/
const notifications = await fetch('https://api.github.com/notifications', {
headers: {
Accept: 'application/vnd.github.v3+json',
Authorization: `token ${process.env.GITHUB_TOKEN}`,
},
}).then((res) => res.json());
const output = notifications
.map((notification) => {
const url = new URL('https://github.com');
if (notification.subject.url) {
url.pathname = new URL(notification.subject.url).pathname.replace('/repos', '');
} else {
url.pathname = '/notifications';
url.searchParams.append('query', `repo:${notification.repository.full_name}`);
}
return {
repo: notification.repository.full_name,
subject: notification.subject.title,
type: notification.subject.type,
url,
};
})
.map(
(n) =>
`<a href="${n.url.toString()}">${getOcticonSvg(n.type)}<pre><span>${n.repo}</span>: ${n.subject}</pre></a>`,
)
.join('\n');
console.log(output);
await anybar(notifications.length > 0 ? 'white' : 'black');
}
/**
* @param {string} type
*/
const getOcticonSvg = (type) => octicons[icons.get(type)].toSVG();
const icons = new Map([
['CheckSuite', 'workflow'],
['Commit', 'git-commit'],
['Issue', 'issue-opened'],
['PullRequest', 'git-pull-request'],
['RepositoryDependabotAlertsThread', 'alert'],
]);
main().catch((err) => process.exit(1));