From 61a303a1de7487f27b945fccdddc01638b08cc0c Mon Sep 17 00:00:00 2001 From: Tony Date: Sun, 7 May 2023 20:51:53 -0600 Subject: [PATCH] feat(route): gist commits (#12470) --- docs/en/programming.md | 4 ++++ docs/programming.md | 4 ++++ lib/v2/github/gist.js | 35 +++++++++++++++++++++++++++++++++++ lib/v2/github/maintainer.js | 1 + lib/v2/github/radar.js | 8 ++++++++ lib/v2/github/router.js | 1 + 6 files changed, 53 insertions(+) create mode 100644 lib/v2/github/gist.js diff --git a/docs/en/programming.md b/docs/en/programming.md index e21f13072..84af60df1 100644 --- a/docs/en/programming.md +++ b/docs/en/programming.md @@ -223,6 +223,10 @@ For instance, the `/github/topics/framework/l=php&o=desc&s=stars` route will gen +### Gist Commits + + + ## GitLab ### Explore diff --git a/docs/programming.md b/docs/programming.md index daf3f6590..868f11ac6 100644 --- a/docs/programming.md +++ b/docs/programming.md @@ -318,6 +318,10 @@ GitHub 官方也提供了一些 RSS: +### Gist Commits + + + ## GitLab ### Explore diff --git a/lib/v2/github/gist.js b/lib/v2/github/gist.js new file mode 100644 index 000000000..25d274353 --- /dev/null +++ b/lib/v2/github/gist.js @@ -0,0 +1,35 @@ +const got = require('@/utils/got'); +const config = require('@/config').value; +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const { gistId } = ctx.params; + + const headers = { Accept: 'application/vnd.github.v3+json' }; + if (config.github && config.github.access_token) { + headers.Authorization = `Bearer ${config.github.access_token}`; + } + + const host = 'https://gist.github.com'; + const apiUrl = `https://api.github.com/gists/${gistId}`; + + const { data: response } = await got(apiUrl, { + headers, + }); + + const items = response.history.map((item, index) => ({ + title: `${item.user.login} ${index === response.history.length - 1 ? 'created' : 'revised'} this gist`, + description: item.change_status.total ? `${item.change_status.additions} additions and ${item.change_status.deletions} deletions` : null, + link: `${host}/${gistId}/${item.version}`, + pubDate: parseDate(item.committed_at), // e.g. 2022-09-02T11:09:56Z + })); + + ctx.state.data = { + allowEmpty: true, + title: `${response.owner.login} / ${Object.values(response.files)[0].filename}`, + description: response.description, + image: response.owner.avatar_url, + link: `${response.html_url}/revisions`, + item: items, + }; +}; diff --git a/lib/v2/github/maintainer.js b/lib/v2/github/maintainer.js index 77acea474..9874c6581 100644 --- a/lib/v2/github/maintainer.js +++ b/lib/v2/github/maintainer.js @@ -3,6 +3,7 @@ module.exports = { '/comments/:user/:repo/:number': ['TonyRL'], '/contributors/:user/:repo/:order?/:anon?': ['zoenglinghou'], '/file/:user/:repo/:branch/:filepath+': ['zengxs'], + '/gist/:gistId': ['TonyRL'], '/issue/:user/:repo/:state?/:labels?': ['HenryQW', 'AndreyMZ'], '/notifications': ['zhzy0077'], '/pull/:user/:repo/:state?': ['hashman', 'TonyRL'], diff --git a/lib/v2/github/radar.js b/lib/v2/github/radar.js index ae0c01231..0f9848f48 100644 --- a/lib/v2/github/radar.js +++ b/lib/v2/github/radar.js @@ -87,5 +87,13 @@ module.exports = { target: '/github/notifications', }, ], + gist: [ + { + title: 'Gist Commits', + docs: 'https://docs.rsshub.app/programming.html#github', + source: ['/:owner/:gistId/revisions', '/:owner/:gistId/stargazers', '/:owner/:gistId/forks', '/:owner/:gistId'], + target: '/github/gist/:gistId', + }, + ], }, }; diff --git a/lib/v2/github/router.js b/lib/v2/github/router.js index 6be590433..3c16200c3 100644 --- a/lib/v2/github/router.js +++ b/lib/v2/github/router.js @@ -4,6 +4,7 @@ module.exports = function (router) { router.get('/comments/:user/:repo/:number', require('./comments')); router.get('/contributors/:user/:repo/:order?/:anon?', require('./contributors')); router.get('/file/:user/:repo/:branch/:filepath+', require('./file')); + router.get('/gist/:gistId', require('./gist')); router.get('/issue/:user/:repo/:state?/:labels?', require('./issue')); router.get('/notifications', require('./notifications')); router.get('/pull/:user/:repo/:state?/:labels?', require('./pulls'));