feat(route): add github all issue/pull comments (#12899)

This commit is contained in:
Arne Keller
2023-07-28 22:25:29 +02:00
committed by GitHub
parent 3428cc2beb
commit 6f13fc4cc2
6 changed files with 75 additions and 7 deletions

View File

@@ -213,7 +213,7 @@ For instance, the `/github/topics/framework/l=php&o=desc&s=stars` route will gen
### Issue / Pull Request comments
<RouteEn author="TonyRL" example="/github/comments/DIYgod/RSSHub/8116" path="/github/comments/:user/:repo/:number" :paramsDesc="['User / Org name', 'Repo name', 'Issue or pull number']" radar="1" rssbud="1"/>
<RouteEn author="TonyRL FliegendeWurst" example="/github/comments/DIYgod/RSSHub/8116" path="/github/comments/:user/:repo/:number?" :paramsDesc="['User / Org name', 'Repo name', 'Issue or pull number (if omitted: all)']" radar="1" rssbud="1"/>
### Wiki History

View File

@@ -308,7 +308,7 @@ GitHub 官方也提供了一些 RSS:
### Issue / Pull Request 评论
<Route author="TonyRL" example="/github/comments/DIYgod/RSSHub/8116" path="/github/comments/:user/:repo/:number" :paramsDesc="['用户名', '仓库名', 'Issue 或 pull 编号']" radar="1" rssbud="1"/>
<Route author="TonyRL FliegendeWurst" example="/github/comments/DIYgod/RSSHub/8116" path="/github/comments/:user/:repo/:number?" :paramsDesc="['用户名', '仓库名', 'Issue 或 pull 编号']" radar="1" rssbud="1"/>
### Wiki 历史

View File

@@ -3,12 +3,16 @@ const { parseDate } = require('@/utils/parse-date');
const md = require('markdown-it')({
html: true,
});
const rootUrl = 'https://github.com';
const apiUrl = 'https://api.github.com';
const config = require('@/config').value;
const typeDict = {
issue: {
title: 'Issue',
},
issues: {
title: 'Issue',
},
pull: {
title: 'Pull request',
},
@@ -17,8 +21,8 @@ const typeDict = {
module.exports = async (ctx) => {
const user = ctx.params.user;
const repo = ctx.params.repo;
const number = isNaN(parseInt(ctx.params.number)) ? 1 : parseInt(ctx.params.number);
const limit = ctx.query.limit ? parseInt(ctx.params.limit) : 100;
const number = ctx.params.number && isNaN(parseInt(ctx.params.number)) ? 1 : parseInt(ctx.params.number);
const limit = ctx.query.limit ? parseInt(ctx.query.limit) : 100;
const headers =
config.github && config.github.access_token
? {
@@ -29,6 +33,64 @@ module.exports = async (ctx) => {
Accept: 'application/vnd.github.v3+json',
};
if (isNaN(number)) {
await allIssues(ctx, user, repo, limit, headers);
} else {
await singleIssue(ctx, user, repo, number, limit, headers);
}
};
async function allIssues(ctx, user, repo, limit, headers) {
const response = await got(`${apiUrl}/repos/${user}/${repo}/issues/comments`, {
headers,
searchParams: {
sort: "updated",
direction: "desc",
per_page: limit,
}
});
const timeline = response.data;
const items = timeline.map((item) => {
const actor = item.actor?.login ?? item.user?.login ?? 'ghost';
const issueUrlParts = item.issue_url.split("/");
const issue = issueUrlParts[issueUrlParts.length - 1];
const urlParts = item.html_url.split("/");
const issueType = typeDict[urlParts[urlParts.length - 2]].title;
return {
title: `${actor} commented on ${user}/${repo}: ${issueType} #${issue}`,
author: actor,
pubDate: parseDate(item.created_at),
link: item.html_url,
description: item.body ? md.render(item.body) : null,
};
});
const 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']),
};
ctx.state.data = {
title: `${user}/${repo}: Issue & Pull request comments`,
link: `${rootUrl}/${user}/${repo}`,
item: items,
};
ctx.state.json = {
title: `${user}/${repo}: Issue & Pull request comments`,
link: `${rootUrl}/${user}/${repo}`,
item: items,
rateLimit,
};
}
async function singleIssue(ctx, user, repo, number, limit, headers) {
const response = await got(`${apiUrl}/repos/${user}/${repo}/issues/${number}`, {
headers,
});
@@ -124,4 +186,4 @@ module.exports = async (ctx) => {
used: parseInt(response.headers['x-ratelimit-used']),
},
};
};
}

View File

@@ -1,6 +1,6 @@
module.exports = {
'/branches/:user/:repo': ['max-arnold'],
'/comments/:user/:repo/:number': ['TonyRL'],
'/comments/:user/:repo/:number?': ['TonyRL', 'FliegendeWurst'],
'/contributors/:user/:repo/:order?/:anon?': ['zoenglinghou'],
'/file/:user/:repo/:branch/:filepath+': ['zengxs'],
'/gist/:gistId': ['TonyRL'],

View File

@@ -14,6 +14,12 @@ module.exports = {
source: ['/:user/:repo/:type/:number'],
target: '/github/comments/:user/:repo/:number',
},
{
title: 'Issue & Pull Request comments',
docs: 'https://docs.rsshub.app/programming.html#github',
source: ['/:user/:repo/:type'],
target: '/github/comments/:user/:repo',
},
{
title: '仓库 Contributors',
docs: 'https://docs.rsshub.app/programming.html#github',

View File

@@ -1,7 +1,7 @@
module.exports = function (router) {
router.get('/branches/:user/:repo', require('./branches'));
router.get('/comments/:user/:repo/:type/:number', require('./comments')); // deprecated
router.get('/comments/:user/:repo/:number', require('./comments'));
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'));