From 53472e7ae69bd787842984d24622b8a60bd063fc Mon Sep 17 00:00:00 2001 From: zoenglinghou <11689106+zoenglinghou@users.noreply.github.com> Date: Sat, 17 Jul 2021 09:25:38 +0000 Subject: [PATCH] feat(route): Add gitlab releases & tags(#7873) add options for different gitlab instances --- docs/en/programming.md | 11 ++++++++++- docs/programming.md | 10 +++++++++- lib/router.js | 4 +++- lib/routes/gitlab/explore.js | 10 ++++++---- lib/routes/gitlab/release.js | 30 ++++++++++++++++++++++++++++++ lib/routes/gitlab/tag.js | 30 ++++++++++++++++++++++++++++++ 6 files changed, 88 insertions(+), 7 deletions(-) create mode 100644 lib/routes/gitlab/release.js create mode 100644 lib/routes/gitlab/tag.js diff --git a/docs/en/programming.md b/docs/en/programming.md index 9a7b3b93d7..560c2e447d 100644 --- a/docs/en/programming.md +++ b/docs/en/programming.md @@ -111,7 +111,7 @@ For instance, the `/github/topics/framework/l=php&o=desc&s=stars` route will gen ### Explore - + | Trending | Most stars | All | | -------- | ---------- | --- | @@ -119,6 +119,15 @@ For instance, the `/github/topics/framework/l=php&o=desc&s=stars` route will gen +### Releases + + + +### Tags + + + + ## Hacker News ### Section diff --git a/docs/programming.md b/docs/programming.md index a160cabf25..401ada0c33 100644 --- a/docs/programming.md +++ b/docs/programming.md @@ -177,7 +177,7 @@ GitHub 官方也提供了一些 RSS: ### Explore - + | Trending | Most stars | All | | -------- | ---------- | --- | @@ -185,6 +185,14 @@ GitHub 官方也提供了一些 RSS: +### Releases + + + +### Tags + + + ## Go 语言中文网 ### 周刊 diff --git a/lib/router.js b/lib/router.js index 3dab40ec6e..b6795b732c 100644 --- a/lib/router.js +++ b/lib/router.js @@ -597,7 +597,9 @@ router.get('/novel/zhaishuyuan/:id', require('./routes/novel/zhaishuyuan')); router.get('/weatheralarm/:province?', require('./routes/weatheralarm')); // Gitlab -router.get('/gitlab/explore/:type', require('./routes/gitlab/explore')); +router.get('/gitlab/explore/:type/:host?', require('./routes/gitlab/explore')); +router.get('/gitlab/release/:namespace/:project/:host?', require('./routes/gitlab/release')); +router.get('/gitlab/tag/:namespace/:project/:host?', require('./routes/gitlab/tag')); // 忧郁的loli router.get('/mygalgame', require('./routes/galgame/hhgal')); // 废弃 diff --git a/lib/routes/gitlab/explore.js b/lib/routes/gitlab/explore.js index 6feb623481..f77939b756 100644 --- a/lib/routes/gitlab/explore.js +++ b/lib/routes/gitlab/explore.js @@ -2,7 +2,9 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); module.exports = async (ctx) => { - const type = ctx.params.type === 'all' ? '' : ctx.params.type; + let { type, host } = ctx.params; + type = type === 'all' ? '' : type; + host = host ? host : 'gitlab.com'; const typename = { trending: 'Trending', starred: 'Most stars', @@ -11,14 +13,14 @@ module.exports = async (ctx) => { const res = await got({ method: 'get', - url: `https://gitlab.com/explore/projects/${type}`, + url: `https://${host}/explore/projects/${type}`, }); const $ = cheerio.load(res.data); const list = $('ul.projects-list').find('li'); ctx.state.data = { title: `${typename[ctx.params.type]} - Explore - Gitlab`, - link: `https://gitlab.com/explore/projects/${type}`, + link: `https://${host}/explore/projects/${type}`, item: list && list @@ -28,7 +30,7 @@ module.exports = async (ctx) => { title: item.find('.project-full-name').text(), author: item.find('.namespace-name').text().slice(0, -1).split('\n').join('').trim(), description: item.find('.description').text(), - link: `https://gitlab.com${item.find('a.text-plain').attr('href')}`, + link: `https://${host}${item.find('a.text-plain').attr('href')}`, }; }) .get(), diff --git a/lib/routes/gitlab/release.js b/lib/routes/gitlab/release.js new file mode 100644 index 0000000000..ee734df265 --- /dev/null +++ b/lib/routes/gitlab/release.js @@ -0,0 +1,30 @@ +const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const { namespace, project, host } = ctx.params; + + const host_ = host ? host : 'gitlab.com'; + const namespace_ = encodeURIComponent(namespace); + + const api_url = `https://${host_}/api/v4/projects/${namespace_}%2F${project}/releases`; + + const response = await got({ + method: 'get', + url: api_url, + }); + const data = response.data; + + ctx.state.data = { + title: `${project} - Releases - Gitlab`, + link: `https://${host_}/${namespace}/${project}/-/releases`, + description: `${namespace}/${project} Releases`, + item: data.map((item) => ({ + title: item.name, + author: item.author.name, + description: item.description_html, + pubDate: parseDate(item.released_at), + link: item._links.self, + })), + }; +}; diff --git a/lib/routes/gitlab/tag.js b/lib/routes/gitlab/tag.js new file mode 100644 index 0000000000..6e267be525 --- /dev/null +++ b/lib/routes/gitlab/tag.js @@ -0,0 +1,30 @@ +const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const { namespace, project, host } = ctx.params; + + const host_ = host ? host : 'gitlab.com'; + const namespace_ = encodeURIComponent(namespace); + + const api_url = `https://${host_}/api/v4/projects/${namespace_}%2F${project}/repository/tags`; + + const response = await got({ + method: 'get', + url: api_url, + }); + const data = response.data; + + ctx.state.data = { + title: `${project} - Tags - Gitlab`, + link: `https://${host_}/${namespace}/${project}/-/tags`, + description: `${namespace}/${project} Tags`, + item: data.map((item) => ({ + title: item.name, + author: item.commit.author_name, + description: item.message, + pubDate: parseDate(item.commit.created_at), + link: item.commit.web_url, + })), + }; +};