diff --git a/lib/routes/github/repos.ts b/lib/routes/github/repos.ts index 57e02e3ec704d8..edf19af5c40dbc 100644 --- a/lib/routes/github/repos.ts +++ b/lib/routes/github/repos.ts @@ -1,13 +1,16 @@ import { Route } from '@/types'; import got from '@/utils/got'; import { config } from '@/config'; -import queryString from 'query-string'; export const route: Route = { - path: '/repos/:user', + path: '/repos/:user/:type?/:sort?', categories: ['programming'], example: '/github/repos/DIYgod', - parameters: { user: 'GitHub username' }, + parameters: { + user: 'GitHub username', + type: 'Type of repository, can be `all`, `owner`, `member`, `public`, `private`, `forks`, `sources`', + sort: 'Sort by `created`, `updated`, `pushed`, `full_name`', + }, features: { requireConfig: false, requirePuppeteer: false, @@ -28,20 +31,44 @@ export const route: Route = { async function handler(ctx) { const user = ctx.req.param('user'); - - const headers = {}; + const type = ctx.req.param('type') || 'all'; + const sort = ctx.req.param('sort') || 'created'; + let headers = {}; if (config.github && config.github.access_token) { - headers.Authorization = `token ${config.github.access_token}`; + headers = { + ...headers, + Authorization: `token ${config.github.access_token}`, + }; } const response = await got({ method: 'get', url: `https://api.github.com/users/${user}/repos`, - searchParams: queryString.stringify({ - sort: 'created', - }), + searchParams: { + type, + sort, + }, headers, }); - const data = response.data; + const data = response.data.filter((item) => { + switch (type) { + case 'all': + return true; + case 'owner': + return item.owner.login === user; + case 'member': + return item.owner.login !== user; + case 'public': + return item.private === false; + case 'private': + return item.private === true; + case 'forks': + return item.fork === true; + case 'sources': + return item.fork === false; + default: + return true; + } + }); return { allowEmpty: true, title: `${user}'s GitHub repositories`,