From 259bdcc0028697a94feb43b7dc4462bb7b0e9d17 Mon Sep 17 00:00:00 2001 From: mslxl Date: Sat, 25 Oct 2025 19:31:37 +0800 Subject: [PATCH] feat(route): add GitHub repo, user and org event routes (#20330) * feat(route): add GitHub repository event route Co-authored-by: LiangddBUG <113956019+LiangddCoder@users.noreply.github.com> Signed-off-by: mslxl * feat(route): add GitHub user and org event routes Signed-off-by: mslxl --------- Signed-off-by: mslxl Co-authored-by: LiangddBUG <113956019+LiangddCoder@users.noreply.github.com> --- lib/routes/github/eventapi.ts | 150 ++++++++++++++++++++++++++++++ lib/routes/github/org-event.ts | 142 ++++++++++++++++++++++++++++ lib/routes/github/private-feed.ts | 119 +----------------------- lib/routes/github/repo-event.ts | 144 ++++++++++++++++++++++++++++ lib/routes/github/user-event.ts | 142 ++++++++++++++++++++++++++++ 5 files changed, 582 insertions(+), 115 deletions(-) create mode 100644 lib/routes/github/eventapi.ts create mode 100644 lib/routes/github/org-event.ts create mode 100644 lib/routes/github/repo-event.ts create mode 100644 lib/routes/github/user-event.ts diff --git a/lib/routes/github/eventapi.ts b/lib/routes/github/eventapi.ts new file mode 100644 index 0000000000..9bda07d018 --- /dev/null +++ b/lib/routes/github/eventapi.ts @@ -0,0 +1,150 @@ +import { parseDate } from '@/utils/parse-date'; + +export const eventTypeMapping: Record = { + create: 'CreateEvent', + delete: 'DeleteEvent', + issuecomm: 'IssueCommentEvent', + fork: 'ForkEvent', + member: 'MemberEvent', + public: 'PublicEvent', + push: 'PushEvent', + pr: 'PullRequestEvent', + prcomm: 'PullRequestReviewCommentEvent', + release: 'ReleaseEvent', + star: 'WatchEvent', + issue: 'IssuesEvent', + prrev: 'PullRequestReviewEvent', + discussion: 'DiscussionEvent', + wiki: 'GollumEvent', + cmcomm: 'CommitCommentEvent', +}; + +function formatEventItem(event: any) { + const { id, type, actor, repo, payload, created_at } = event; + + let title = ''; + let description = ''; + let link = ''; + + switch (type) { + case 'PushEvent': { + title = `${actor.login} pushed to ${repo.name}`; + const branch = payload.ref ? payload.ref.replace('refs/heads/', '') : 'unknown'; + const commitCount = payload.size ? `${payload.size} commit(s) ` : ''; + description = `Pushed ${commitCount}to ${branch} in ${repo.name}`; + + if (payload.commits) { + link = payload.commits.at(-1).url.replace('api.github.com/repos/', 'github.com/').replace('/commits/', '/commit/'); + description += `
Latest commit: ${payload.commits.at(-1).message}`; + } else { + link = `https://github.com/${repo.name}/commit/${payload.head}`; + } + break; + } + case 'PullRequestEvent': + title = `${actor.login} ${payload.action} a pull request in ${repo.name}`; + description = `PR: ${payload.pull_request?.url || 'Unknown'}`; + link = payload.pull_request?.url || `https://github.com/${repo.name}`; + break; + case 'PullRequestReviewCommentEvent': + title = `${actor.login} commented on a pull request review in ${repo.name}`; + description = `Comment: ${payload.comment?.body || 'No comment'}`; + link = payload.comment?.html_url || `https://github.com/${repo.name}`; + break; + case 'PullRequestReviewEvent': + title = `${actor.login} reviewed a pull request in ${repo.name}`; + description = `${actor.login} ${payload.review?.state ?? "operated"} the PR` + (payload.review?.body ? `: ${payload.review.body}` : ''); + link = payload.review?.html_url || `https://github.com/${repo.name}`; + break; + case 'IssueCommentEvent': + title = `${actor.login} commented on an issue in ${repo.name}`; + description = `Comment: ${payload.comment?.body || 'No comment'}`; + link = payload.comment?.html_url || `https://github.com/${repo.name}`; + break; + case 'IssuesEvent': + title = `${actor.login} ${payload.action} an issue in ${repo.name}`; + description = `Issue: ${payload.issue?.title || 'Unknown'}`; + link = payload.issue?.html_url || `https://github.com/${repo.name}`; + break; + case 'CommitCommentEvent': + title = `${actor.login} commented on a commit in ${repo.name}`; + description = `Comment: ${payload.comment?.body || 'No comment'}`; + link = payload.comment?.html_url || `https://github.com/${repo.name}`; + break; + case 'WatchEvent': + title = `${actor.login} starred ${repo.name}`; + description = `Starred repository ${repo.name}`; + link = `https://github.com/${repo.name}`; + break; + case 'ForkEvent': + title = `${actor.login} forked ${repo.name}`; + description = `Forked repository ${repo.name}`; + link = `https://github.com/${repo.name}`; + break; + case 'CreateEvent': + title = `${actor.login} created ${payload.ref_type} in ${repo.name}`; + description = `Created ${payload.ref_type}: ${payload.ref || repo.name}`; + link = `https://github.com/${repo.name}`; + break; + case 'DeleteEvent': + title = `${actor.login} deleted ${payload.ref_type} in ${repo.name}`; + description = `Deleted ${payload.ref_type}: ${payload.ref}`; + link = `https://github.com/${repo.name}`; + break; + case 'ReleaseEvent': + title = `${actor.login} released ${payload.release?.name || payload.release?.tag_name} in ${repo.name}`; + description = payload.release?.body || `Released ${payload.release?.tag_name}`; + link = payload.release?.html_url || `https://github.com/${repo.name}`; + break; + case 'PublicEvent': + title = `${actor.login} made ${repo.name} public`; + description = `Repository ${repo.name} was made public`; + link = `https://github.com/${repo.name}`; + break; + case 'MemberEvent': + title = `${actor.login} ${payload.action} as a member of ${repo.name}`; + description = `Member ${payload.action} in repository ${repo.name}`; + link = `https://github.com/${repo.name}`; + break; + case 'GollumEvent': + title = `${actor.login} update the wiki in ${repo.name}`; + description = '
    '; + for (const page of payload.pages ?? []) { + description += `
  • Page ${page.page_name} ${page.action} ${page.summary ? `: ${page.summary}` : ''}
  • `; + } + description += `
`; + link = `https://github.com/${repo.name}`; + break; + case 'DiscussionEvent': + title = `${actor.login} ${payload.action} a discussion ${repo.discussion?.title ?? ''} on ${repo.name}`; + description = payload.discussion?.body ?? 'Unknown'; + link = payload.discussion?.html_url || `https://github.com/${repo.name}`; + break; + default: + title = `${actor.login} performed ${type} in ${repo?.name || 'unknown repository'}`; + description = `Activity type: ${type} ${JSON.stringify(event)}`; + link = repo ? `https://github.com/${repo.name}` : `https://github.com/${actor.login}`; + } + + return { + title, + link, + description, + pubDate: parseDate(created_at), + author: actor.login, + category: [type], + guid: id, + }; +} + +export function filterEvents(types: string, data: any) { + // Parse requested event types and map short names to full event types + let filteredEventTypes: string[] = []; + if (types !== 'all') { + filteredEventTypes = types.split(',').map((type) => { + const trimmedType = type.trim(); + return eventTypeMapping[trimmedType] || trimmedType; + }); + } + return data.filter((event) => filteredEventTypes.length === 0 || filteredEventTypes.includes(event.type)).map((event) => formatEventItem(event)); +} diff --git a/lib/routes/github/org-event.ts b/lib/routes/github/org-event.ts new file mode 100644 index 0000000000..ad016ad60a --- /dev/null +++ b/lib/routes/github/org-event.ts @@ -0,0 +1,142 @@ +import { Route, ViewType } from '@/types'; +import got from '@/utils/got'; +import { config } from '@/config'; +import { filterEvents } from './eventapi'; + +export const route: Route = { + path: '/org_event/:org/:types?', + categories: ['programming'], + example: '/github/org_event/RSSNext', + view: ViewType.Notifications, + parameters: { + org: 'Organization name', + types: { + description: 'Event types to include, comma separated', + default: 'all', + options: [ + { + label: 'All events', + value: 'all', + }, + { + label: 'Create events', + value: 'create', + }, + { + label: 'Delete events', + value: 'delete', + }, + { + label: 'Fork events', + value: 'fork', + }, + { + label: 'Issue create events', + value: 'issue', + }, + { + label: 'Issue comment events', + value: 'issuecomm', + }, + { + label: 'Member events', + value: 'member', + }, + { + label: 'Pull request events', + value: 'pr', + }, + { + label: 'Pull request review comment events', + value: 'prcomm', + }, + { + label: 'Pull request review events', + value: 'prrev', + }, + { + label: 'Public events', + value: 'public', + }, + { + label: 'Push events', + value: 'push', + }, + { + label: 'Release events', + value: 'release', + }, + { + label: 'Watch events (stars)', + value: 'star', + }, + { + label: 'Wiki item create or update events', + value: 'wiki', + }, + { + label: 'Commit comment events', + value: 'cmcomm', + }, + { + label: 'Discussion events', + value: 'discussion', + }, + ], + }, + }, + features: { + requireConfig: [ + { + name: 'GITHUB_ACCESS_TOKEN', + optional: true, + description: 'GitHub access token to avoid access limit', + }, + ], + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['github.com/orgs/:org'], + target: '/org_event/:org', + }, + ], + name: 'Organization Event', + maintainers: ['mslxl'], + handler, +}; + +async function handler(ctx) { + const org = ctx.req.param('org'); + const types = ctx.req.param('types') || 'all'; + + const isAuthenticated = config.github && config.github.access_token; + + const headers: Record = {}; + if (isAuthenticated) { + headers.Authorization = `token ${config.github.access_token}`; + } + + const response = await got({ + method: 'get', + url: `https://api.github.com/orgs/${org}/events`, + headers, + searchParams: { + per_page: 100, + }, + }); + + const items = filterEvents(types, response.data); + const typeFilter = types === 'all' ? 'All Events' : `Events: ${types}`; + + return { + title: `${org} GitHub organization Feed - ${typeFilter}`, + link: `https://github.com/${org}`, + description: `GitHub events received by ${org}`, + item: items, + }; +} diff --git a/lib/routes/github/private-feed.ts b/lib/routes/github/private-feed.ts index fcb4d3fbf8..7ee7af1f0d 100644 --- a/lib/routes/github/private-feed.ts +++ b/lib/routes/github/private-feed.ts @@ -1,26 +1,13 @@ -import { Route } from '@/types'; +import { Route, ViewType } from '@/types'; import got from '@/utils/got'; -import { parseDate } from '@/utils/parse-date'; import { config } from '@/config'; - -const typeMapping: Record = { - push: 'PushEvent', - pr: 'PullRequestEvent', - prcomm: 'PullRequestReviewCommentEvent', - issuecomm: 'IssueCommentEvent', - star: 'WatchEvent', - fork: 'ForkEvent', - create: 'CreateEvent', - delete: 'DeleteEvent', - release: 'ReleaseEvent', - public: 'PublicEvent', - member: 'MemberEvent', -}; +import { filterEvents } from './eventapi'; export const route: Route = { path: '/feed/:user/:types?', categories: ['programming'], example: '/github/feed/yihong0618/star,release,pr', + view: ViewType.Notifications, parameters: { user: 'GitHub username', types: { @@ -103,95 +90,6 @@ export const route: Route = { handler, }; -function formatEventItem(event: any) { - const { id, type, actor, repo, payload, created_at } = event; - - let title = ''; - let description = ''; - let link = ''; - - switch (type) { - case 'PushEvent': { - title = `${actor.login} pushed to ${repo.name}`; - const branch = payload.ref ? payload.ref.replace('refs/heads/', '') : 'unknown'; - const commitCount = payload.size ? `${payload.size} commit(s) ` : ''; - description = `Pushed ${commitCount}to ${branch} in ${repo.name}`; - - if (payload.commits) { - link = payload.commits.at(-1).url.replace('api.github.com/repos/', 'github.com/').replace('/commits/', '/commit/'); - description += `
Latest commit: ${payload.commits.at(-1).message}`; - } else { - link = `https://github.com/${repo.name}/commit/${payload.head}`; - } - break; - } - case 'PullRequestEvent': - title = `${actor.login} ${payload.action} a pull request in ${repo.name}`; - description = `PR: ${payload.pull_request?.title || 'Unknown'}`; - link = payload.pull_request?.html_url || `https://github.com/${repo.name}`; - break; - case 'PullRequestReviewCommentEvent': - title = `${actor.login} commented on a pull request review in ${repo.name}`; - description = `Comment: ${payload.comment?.body || 'No comment'}`; - link = payload.comment?.html_url || `https://github.com/${repo.name}`; - break; - case 'IssueCommentEvent': - title = `${actor.login} commented on an issue in ${repo.name}`; - description = `Comment: ${payload.comment?.body || 'No comment'}`; - link = payload.comment?.html_url || `https://github.com/${repo.name}`; - break; - case 'WatchEvent': - title = `${actor.login} starred ${repo.name}`; - description = `Starred repository ${repo.name}`; - link = `https://github.com/${repo.name}`; - break; - case 'ForkEvent': - title = `${actor.login} forked ${repo.name}`; - description = `Forked repository ${repo.name}`; - link = `https://github.com/${repo.name}`; - break; - case 'CreateEvent': - title = `${actor.login} created ${payload.ref_type} in ${repo.name}`; - description = `Created ${payload.ref_type}: ${payload.ref || repo.name}`; - link = `https://github.com/${repo.name}`; - break; - case 'DeleteEvent': - title = `${actor.login} deleted ${payload.ref_type} in ${repo.name}`; - description = `Deleted ${payload.ref_type}: ${payload.ref}`; - link = `https://github.com/${repo.name}`; - break; - case 'ReleaseEvent': - title = `${actor.login} released ${payload.release?.name || payload.release?.tag_name} in ${repo.name}`; - description = payload.release?.body || `Released ${payload.release?.tag_name}`; - link = payload.release?.html_url || `https://github.com/${repo.name}`; - break; - case 'PublicEvent': - title = `${actor.login} made ${repo.name} public`; - description = `Repository ${repo.name} was made public`; - link = `https://github.com/${repo.name}`; - break; - case 'MemberEvent': - title = `${actor.login} ${payload.action} as a member of ${repo.name}`; - description = `Member ${payload.action} in repository ${repo.name}`; - link = `https://github.com/${repo.name}`; - break; - default: - title = `${actor.login} performed ${type} in ${repo?.name || 'unknown repository'}`; - description = `Activity type: ${type}`; - link = repo ? `https://github.com/${repo.name}` : `https://github.com/${actor.login}`; - } - - return { - title, - link, - description, - pubDate: parseDate(created_at), - author: actor.login, - category: [type], - guid: id, - }; -} - async function handler(ctx) { const user = ctx.req.param('user'); const types = ctx.req.param('types') || 'all'; @@ -212,16 +110,7 @@ async function handler(ctx) { }, }); - // Parse requested event types and map short names to full event types - let filteredEventTypes: string[] = []; - if (types !== 'all') { - filteredEventTypes = types.split(',').map((type) => { - const trimmedType = type.trim(); - return typeMapping[trimmedType] || trimmedType; - }); - } - - const items = response.data.filter((event) => filteredEventTypes.length === 0 || filteredEventTypes.includes(event.type)).map((event) => formatEventItem(event)); + const items = filterEvents(types, response.data); const typeFilter = types === 'all' ? 'All Events' : `Events: ${types}`; const feedType = isAuthenticated ? 'Private Feed' : 'Public Feed'; diff --git a/lib/routes/github/repo-event.ts b/lib/routes/github/repo-event.ts new file mode 100644 index 0000000000..5bdb5bb3f0 --- /dev/null +++ b/lib/routes/github/repo-event.ts @@ -0,0 +1,144 @@ +import { Route, ViewType } from '@/types'; +import got from '@/utils/got'; +import { config } from '@/config'; +import { filterEvents } from './eventapi'; + +export const route: Route = { + path: '/repo_event/:owner/:repo/:types?', + categories: ['programming'], + example: '/github/repo_event/DIYgod/RSSHub', + view: ViewType.Notifications, + parameters: { + owner: 'Username or organization name', + repo: 'Repository name', + types: { + description: 'Event types to include, comma separated', + default: 'all', + options: [ + { + label: 'All events', + value: 'all', + }, + { + label: 'Create events', + value: 'create', + }, + { + label: 'Delete events', + value: 'delete', + }, + { + label: 'Fork events', + value: 'fork', + }, + { + label: 'Issue create events', + value: 'issue', + }, + { + label: 'Issue comment events', + value: 'issuecomm', + }, + { + label: 'Member events', + value: 'member', + }, + { + label: 'Pull request events', + value: 'pr', + }, + { + label: 'Pull request review comment events', + value: 'prcomm', + }, + { + label: 'Pull request review events', + value: 'prrev', + }, + { + label: 'Public events', + value: 'public', + }, + { + label: 'Push events', + value: 'push', + }, + { + label: 'Release events', + value: 'release', + }, + { + label: 'Watch events (stars)', + value: 'star', + }, + { + label: 'Wiki item create or update events', + value: 'wiki', + }, + { + label: 'Commit comment events', + value: 'cmcomm', + }, + { + label: 'Discussion events', + value: 'discussion', + }, + ], + }, + }, + features: { + requireConfig: [ + { + name: 'GITHUB_ACCESS_TOKEN', + optional: true, + description: 'GitHub access token to access private repository events', + }, + ], + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['github.com/:owner/:repo'], + target: '/repo_event/:owner/:repo', + }, + ], + name: 'Repository Event', + maintainers: ['mslxl'], + handler, +}; + +async function handler(ctx) { + const owner = ctx.req.param('owner'); + const repo = ctx.req.param('repo'); + const types = ctx.req.param('types') || 'all'; + + const isAuthenticated = config.github && config.github.access_token; + + const headers: Record = {}; + if (isAuthenticated) { + headers.Authorization = `token ${config.github.access_token}`; + } + + const response = await got({ + method: 'get', + url: `https://api.github.com/repos/${owner}/${repo}/events`, + headers, + searchParams: { + per_page: 100, + }, + }); + + const items = filterEvents(types, response.data); + const typeFilter = types === 'all' ? 'All Events' : `Events: ${types}`; + + return { + title: `${owner}/${repo} GitHub Repo Feed - ${typeFilter}`, + link: `https://github.com/${owner}/${repo}`, + description: `GitHub events received by ${owner}/${repo}${types === 'all' ? '' : ` (filtered: ${types})`}${isAuthenticated ? ' - includes private events' : ' - public events only'}`, + item: items, + }; +} diff --git a/lib/routes/github/user-event.ts b/lib/routes/github/user-event.ts new file mode 100644 index 0000000000..2bc3ec3eb5 --- /dev/null +++ b/lib/routes/github/user-event.ts @@ -0,0 +1,142 @@ +import { Route, ViewType } from '@/types'; +import got from '@/utils/got'; +import { config } from '@/config'; +import { filterEvents } from './eventapi'; + +export const route: Route = { + path: '/user_event/:username/:types?', + categories: ['programming'], + example: '/github/user_event/mslxl', + view: ViewType.Notifications, + parameters: { + username: 'Username', + types: { + description: 'Event types to include, comma separated', + default: 'all', + options: [ + { + label: 'All events', + value: 'all', + }, + { + label: 'Create events', + value: 'create', + }, + { + label: 'Delete events', + value: 'delete', + }, + { + label: 'Fork events', + value: 'fork', + }, + { + label: 'Issue create events', + value: 'issue', + }, + { + label: 'Issue comment events', + value: 'issuecomm', + }, + { + label: 'Member events', + value: 'member', + }, + { + label: 'Pull request events', + value: 'pr', + }, + { + label: 'Pull request review comment events', + value: 'prcomm', + }, + { + label: 'Pull request review events', + value: 'prrev', + }, + { + label: 'Public events', + value: 'public', + }, + { + label: 'Push events', + value: 'push', + }, + { + label: 'Release events', + value: 'release', + }, + { + label: 'Watch events (stars)', + value: 'star', + }, + { + label: 'Wiki item create or update events', + value: 'wiki', + }, + { + label: 'Commit comment events', + value: 'cmcomm', + }, + { + label: 'Discussion events', + value: 'discussion', + }, + ], + }, + }, + features: { + requireConfig: [ + { + name: 'GITHUB_ACCESS_TOKEN', + optional: true, + description: 'GitHub access token to access private repository events', + }, + ], + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['github.com/:username'], + target: '/user_event/:username', + }, + ], + name: 'User Event', + maintainers: ['mslxl'], + handler, +}; + +async function handler(ctx) { + const username = ctx.req.param('username'); + const types = ctx.req.param('types') || 'all'; + + const isAuthenticated = config.github && config.github.access_token; + + const headers: Record = {}; + if (isAuthenticated) { + headers.Authorization = `token ${config.github.access_token}`; + } + + const response = await got({ + method: 'get', + url: `https://api.github.com/users/${username}/events` + (isAuthenticated ? '' : '/public'), + headers, + searchParams: { + per_page: 100, + }, + }); + + const items = filterEvents(types, response.data); + const typeFilter = types === 'all' ? 'All Events' : `Events: ${types}`; + + return { + title: `${username} GitHub User Feed - ${typeFilter}`, + link: `https://github.com/${username}`, + description: `GitHub events received by ${username}${types === 'all' ? '' : ` (filtered: ${types})`}${isAuthenticated ? ' - includes private events' : ' - public events only'}`, + item: items, + }; +}