From 14199e1a31bdee7fa0934b070b4f9c25a2161eb4 Mon Sep 17 00:00:00 2001 From: Zhiyuan Zheng Date: Thu, 30 Mar 2023 21:07:34 +0800 Subject: [PATCH] feat(route): Add GitHub Notification (#12197) * Add GitHub notification * add docs and radar. * add parseDate. * Apply suggestions from code review * Update router.js * Update maintainer.js --------- --- docs/en/programming.md | 4 +++ docs/programming.md | 4 +++ lib/v2/github/maintainer.js | 1 + lib/v2/github/notifications.js | 47 ++++++++++++++++++++++++++++++++++ lib/v2/github/radar.js | 6 +++++ lib/v2/github/router.js | 1 + 6 files changed, 63 insertions(+) create mode 100644 lib/v2/github/notifications.js diff --git a/docs/en/programming.md b/docs/en/programming.md index 00556f7820..1d7da9555f 100644 --- a/docs/en/programming.md +++ b/docs/en/programming.md @@ -219,6 +219,10 @@ For instance, the `/github/topics/framework/l=php&o=desc&s=stars` route will gen +### Notifications + + + ## GitLab ### Explore diff --git a/docs/programming.md b/docs/programming.md index 69ac0ee266..d31a60f73c 100644 --- a/docs/programming.md +++ b/docs/programming.md @@ -314,6 +314,10 @@ GitHub 官方也提供了一些 RSS: +### 通知 + + + ## GitLab ### Explore diff --git a/lib/v2/github/maintainer.js b/lib/v2/github/maintainer.js index 0996afa329..6e0c2f26e0 100644 --- a/lib/v2/github/maintainer.js +++ b/lib/v2/github/maintainer.js @@ -4,6 +4,7 @@ module.exports = { '/contributors/:user/:repo/:order?/:anon?': ['zoenglinghou'], '/file/:user/:repo/:branch/:filepath+': ['zengxs'], '/issue/:user/:repo/:state?/:labels?': ['HenryQW', 'AndreyMZ'], + '/notifications': ['zhzy0077'], '/pull/:user/:repo/:state?': ['hashman', 'TonyRL'], '/repos/:user': ['DIYgod'], '/search/:query/:sort?/:order?': ['LogicJake'], diff --git a/lib/v2/github/notifications.js b/lib/v2/github/notifications.js new file mode 100644 index 0000000000..47e57770fd --- /dev/null +++ b/lib/v2/github/notifications.js @@ -0,0 +1,47 @@ +const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); + +const apiUrl = 'https://api.github.com'; +const config = require('@/config').value; + +module.exports = async (ctx) => { + if (!config.github || !config.github.access_token) { + throw Error('GitHub trending RSS is disabled due to the lack of relevant config'); + } + const headers = { + Accept: 'application/vnd.github.v3+json', + Authorization: `Bearer ${config.github.access_token}`, + 'X-GitHub-Api-Version': '2022-11-28', + }; + + const response = await got(`${apiUrl}/notifications`, { + headers, + }); + const notifications = response.data; + + const items = notifications.map((item) => ({ + title: item.subject.title, + description: item.subject.title, + pubDate: parseDate(item.updated_at), // item.updated_at follows ISO 8601. + guid: item.id, + link: item.subject.url, + })); + + ctx.state.data = { + title: 'Github Notifications', + link: 'https://github.com/notifications', + item: items, + }; + + ctx.state.json = { + title: 'Github Notifications', + item: notifications, + rateLimit: { + limit: parseInt(response.headers['X-RateLimit-Limit']), + remaining: parseInt(response.headers['X-RateLimit-Remaining']), + reset: parseDate(parseInt(response.headers['X-RateLimit-Reset']) * 1000), + resoure: response.headers['X-RateLimit-Resource'], + used: parseInt(response.headers['X-RateLimit-Used']), + }, + }; +}; diff --git a/lib/v2/github/radar.js b/lib/v2/github/radar.js index 7c52391b5a..ae0c01231a 100644 --- a/lib/v2/github/radar.js +++ b/lib/v2/github/radar.js @@ -80,6 +80,12 @@ module.exports = { source: ['/:user/:repo/wiki/:page/_history', '/:user/:repo/wiki/:page', '/:user/:repo/wiki/_history', '/:user/:repo/wiki'], target: '/github/wiki/:user/:repo/:page', }, + { + title: 'Notifications 通知', + docs: 'https://docs.rsshub.app/programming.html#github', + source: ['/notifications'], + target: '/github/notifications', + }, ], }, }; diff --git a/lib/v2/github/router.js b/lib/v2/github/router.js index 43aade1a48..6be5904330 100644 --- a/lib/v2/github/router.js +++ b/lib/v2/github/router.js @@ -5,6 +5,7 @@ module.exports = function (router) { router.get('/contributors/:user/:repo/:order?/:anon?', require('./contributors')); router.get('/file/:user/:repo/:branch/:filepath+', require('./file')); router.get('/issue/:user/:repo/:state?/:labels?', require('./issue')); + router.get('/notifications', require('./notifications')); router.get('/pull/:user/:repo/:state?/:labels?', require('./pulls')); router.get('/repos/:user', require('./repos')); router.get('/search/:query/:sort?/:order?', require('./search'));