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

@@ -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']),
},
};
};
}