feat: Support subscribe GitHub file commits (#2002)

* feat: Support subscribe github single file

* refact: Use GitHub API to crawl commits

* Use GitHub API to crawl commits
* Update document

* Add access_token for github file commits api
This commit is contained in:
zengxs
2019-04-28 12:05:17 +08:00
committed by DIYgod
parent bf26680646
commit cac1a9e7ce
3 changed files with 63 additions and 0 deletions

View File

@@ -95,6 +95,21 @@ GitHub 官方也提供了一些 RSS:
<Route name="仓库 Branches" author="max-arnold" example="/github/branches/DIYgod/RSSHub" path="/github/branches/:user/:repo" :paramsDesc="['用户名', '仓库名']"/> <Route name="仓库 Branches" author="max-arnold" example="/github/branches/DIYgod/RSSHub" path="/github/branches/:user/:repo" :paramsDesc="['用户名', '仓库名']"/>
<Route name="文件 Commits" author="zengxs" example="/github/file/DIYgod/RSSHub/master/lib/router.js" path="/github/file/:user/:repo/:branch/:filepath+" :paramsDesc="['用户名', '仓库名', '分支名', '文件路径']">
| 用户名 | 仓库名 | 分支名 | 文件路径 |
| -------- | -------- | -------- | --------------- |
| `DIYgod` | `RSSHub` | `master` | `lib/router.js` |
> - **分支名**中如果有 `/` 等特殊字符需使用 urlencode 进行编码,通常 `/` 需要被替换成 `%2f`
> - **文件路径**中如果有特殊字符同样需使用 urlencode 进行编码,但文件路径可以正常识别 `/` 字符
> - **文件路径**如果以 `.rss`, `.atom`, `.json` 结尾,需要将后缀中的 `.` 替换成 `%2e`
> > Reeder 订阅 `%2erss` 或类似后缀的时候会出错,此时再在路由后面加上 `.rss` 即可正常订阅
> >
> > 如: `https://rsshub.app/github/file/DIYgod/RSSHub/master/lib/router%2ejs` 替换成 `https://rsshub.app/github/file/DIYgod/RSSHub/master/lib/router%2ejs.rss` 即可
</Route>
<Route name="搜索结果" author="LogicJake" example="/github/search/RSSHub/bestmatch/desc" path="/github/search/:query/:sort?/:order?" :paramsDesc="['搜索关键词', '排序选项默认为bestmatch','排序顺序desc和asc默认desc降序']"/> <Route name="搜索结果" author="LogicJake" example="/github/search/RSSHub/bestmatch/desc" path="/github/search/:query/:sort?/:order?" :paramsDesc="['搜索关键词', '排序选项默认为bestmatch','排序顺序desc和asc默认desc降序']"/>
| 排序选项 | sort | | 排序选项 | sort |

View File

@@ -345,6 +345,7 @@ router.get('/github/user/followers/:user', require('./routes/github/follower'));
router.get('/github/stars/:user/:repo', require('./routes/github/star')); router.get('/github/stars/:user/:repo', require('./routes/github/star'));
router.get('/github/search/:query/:sort?/:order?', require('./routes/github/search')); router.get('/github/search/:query/:sort?/:order?', require('./routes/github/search'));
router.get('/github/branches/:user/:repo', require('./routes/github/branches')); router.get('/github/branches/:user/:repo', require('./routes/github/branches'));
router.get('/github/file/:user/:repo/:branch/:filepath+', require('./routes/github/file'));
// f-droid // f-droid
router.get('/fdroid/apprelease/:app', require('./routes/fdroid/apprelease')); router.get('/fdroid/apprelease/:app', require('./routes/fdroid/apprelease'));

47
lib/routes/github/file.js Normal file
View File

@@ -0,0 +1,47 @@
const axios = require('../../utils/axios');
const config = require('../../config');
module.exports = async (ctx) => {
const user = ctx.params.user;
const repo = ctx.params.repo;
const branch = ctx.params.branch;
const filepath = ctx.params.filepath;
const fileUrl = `https://github.com/${user}/${repo}/commits/${branch}/${filepath}`;
const reqParams = {
sha: branch,
path: filepath,
};
if (config.github.access_token) {
reqParams.access_token = config.github.access_token;
}
const res = await axios.get(`https://api.github.com/repos/${user}/${repo}/commits`, {
params: reqParams,
});
const list = res.data;
const count = [];
for (let i = 0; i < Math.min(list.length, 10); i++) {
count.push(i);
}
const resultItems = await Promise.all(
count.map(async (i) => {
const each = list[i];
const item = {
title: each.commit.message.split('\n')[0],
description: `<pre>${each.commit.message}</pre>`,
link: each.html_url,
author: each.commit.author.name,
pubDate: new Date(each.commit.committer.date).toUTCString(),
};
return Promise.resolve(item);
})
);
ctx.state.data = {
title: `GitHub File - ${user}/${repo}/${branch}/${filepath}`,
link: fileUrl,
item: resultItems,
};
};