Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(route): github discussion add category filter #16983

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 75 additions & 16 deletions lib/routes/github/discussions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,47 @@ const md = MarkdownIt({
import { parseDate } from '@/utils/parse-date';

export const route: Route = {
path: '/discussion/:user/:repo/:state?',
path: '/discussion/:user/:repo/:state?/:category?',
categories: ['programming'],
example: '/github/discussion/DIYgod/RSSHub',
parameters: {
user: 'User name',
repo: 'Repo name',
state: 'The state of discussions. Can be either `open`, `closed`, `answered`, `unanswered`, `locked`, `unlocked` or `all`. Default: `all`.',
state: {
description: 'The state of discussions',
default: 'open',
options: [
{
label: 'Open',
value: 'open',
},
{
label: 'Closed',
value: 'closed',
},
{
label: 'Answered',
value: 'answered',
},
{
label: 'Unanswered',
value: 'unanswered',
},
{
label: 'Locked',
value: 'locked',
},
{
label: 'Unlocked',
value: 'unlocked',
},
{
label: 'All',
value: 'all',
},
],
},
category: 'Category Name (case-sensitive). Default: `null`.',
},
features: {
requireConfig: [
Expand All @@ -41,7 +75,7 @@ async function handler(ctx) {
if (!config.github || !config.github.access_token) {
throw new ConfigNotFoundError('GitHub Discussions RSS is disabled due to the lack of <a href="https://docs.rsshub.app/deploy/config#route-specific-configurations">relevant config</a>');
}
const { user, repo, limit, state = 'all' } = ctx.req.param();
const { user, repo, limit, state = 'open', category = null } = ctx.req.param();
const { answered, closed, locked } = mapStateToBooleans(state);
const perPage = Math.min(Number.parseInt(limit) || 100, 100);

Expand All @@ -52,6 +86,31 @@ async function handler(ctx) {
if (answered !== null) {
filters += `, answered: ${answered}`;
}
if (category !== null) {
const response = await got({
method: 'post',
url,
headers: {
Authorization: `bearer ${config.github.access_token}`,
},
json: {
query: `
{
repository(owner: "${user}", name: "${repo}") {
discussionCategories(first: 25) {
nodes {
id,
name,
}
},
}
}
`,
},
});
const categoryItem = response.data.data.repository.discussionCategories.nodes.find((item) => item.name === category);
filters += categoryItem?.id ? `, categoryId: "${categoryItem.id}"` : '';
}

const response = await got({
method: 'post',
Expand All @@ -63,19 +122,19 @@ async function handler(ctx) {
query: `
{
repository(owner: "${user}", name: "${repo}") {
discussions(${filters}) {
nodes {
title,
author {
login
},
createdAt,
closed,
isAnswered,
locked,
body,
url
}
discussions(${filters}) {
nodes {
title,
author {
login
},
createdAt,
closed,
isAnswered,
locked,
body,
url
}
},
}
}
Expand Down