Skip to content

Commit

Permalink
Add github contributors
Browse files Browse the repository at this point in the history
done: "New" contributors, ordered by commit number ascended
todo: Optional "top" contributors
  • Loading branch information
zoenglinghou committed Mar 21, 2020
1 parent af20226 commit 17de3a1
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
6 changes: 6 additions & 0 deletions assets/radar-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@
source: '/:user',
target: '/github/starred_repos/:user',
},
{
title: '仓库 Contributors',
docs: 'https://docs.rsshub.app/programming.html#github',
source: ['/:user/:repo/graphs/contributors', '/:user/:repo'],
target: '/github/contributors/:user/:repo',
},
],
},
'zhihu.com': {
Expand Down
4 changes: 4 additions & 0 deletions docs/en/programming.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ GitHub provides some official RSS feeds:

<RouteEn author="LanceZhu" example="/github/starred_repos/DIYgod" path="/github/starred_repos/:user" :paramsDesc="['User name']" radar="1"/>

### Repo Contributors

<RouteEn author="zoenglinghou" example="/github/contributors/DIYgod/RSSHub" path="/github/contributors/:user/:repo/:anon?" :paramsDesc="['User name', 'Repo name', 'If anonymous users are included. Leave blank for no, use any values for yes.']" radar="1"/>

## GitLab

### Explore
Expand Down
4 changes: 4 additions & 0 deletions docs/programming.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ GitHub 官方也提供了一些 RSS:

<Route author="LanceZhu" example="/github/starred_repos/DIYgod" path="/github/starred_repos/:user" :paramsDesc="['用户名']" radar="1"/>

### 仓库 Contirbutors

<Route author="zoenglinghou" example="/github/contributors/DIYgod/RSSHub" path="/github/contributors/:user/:repo/:anon?" :paramsDesc="['用户名', '仓库名', '是否包括匿名用户。留空默认不包含,任意值包含匿名用户']" radar="1"/>

## GitLab

### Explore
Expand Down
2 changes: 2 additions & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ router.get('/github/search/:query/:sort?/:order?', require('./routes/github/sear
router.get('/github/branches/:user/:repo', require('./routes/github/branches'));
router.get('/github/file/:user/:repo/:branch/:filepath+', require('./routes/github/file'));
router.get('/github/starred_repos/:user', require('./routes/github/starred_repos'));
router.get('/github/contributors/:user/:repo/:anon?', require('./routes/github/contributors'));

// f-droid
router.get('/fdroid/apprelease/:app', require('./routes/fdroid/apprelease'));

Expand Down
85 changes: 85 additions & 0 deletions lib/routes/github/contributors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const got = require('@/utils/got');
const config = require('@/config').value;

module.exports = async (ctx) => {
const user = ctx.params.user;
const repo = ctx.params.repo;
const anon = ctx.params.anon;

const host = `https://github.com/${user}/${repo}`;
const url = `https://api.github.com/repos/${user}/${repo}/contributors?` + (anon ? 'anon=1' : '');

const headers = {};
if (config.github && config.github.access_token) {
headers.Authorization = `token ${config.github.access_token}`;
}

const response = await got({
method: 'get',
url,
headers,
});
let data = response.data;

try {
const last_page_link = response.headers.link.split(',').find(function(elem) {
return elem.includes('"last"');
});
const url_base = last_page_link.match(/<(.*)page=\d*/)[1];
const page_count = Number(last_page_link.match(/page=(\d*)/)[1]);

for (let page = 2; page <= page_count; page++) {
// eslint-disable-next-line no-await-in-loop
const response = await got({
method: 'get',
url: `${url_base}page=${page}`,
headers,
});
data = data.concat(response.data);
}
} catch (err) {
if (!(err instanceof TypeError)) {
throw err;
}
}

let title;
let description;
let link;
let guid;

const items = [];
let index = 0;

data.forEach(function(item) {
const time = new Date();
time.setMinutes(time.getMinutes() - data.length + index);
index++;

if (item.type === 'Anonymous') {
title = `Contributor: ${item.name}`;
description = `<p>Anonymous contributor</p><p>Name: ${item.name}</p><p>E-mail: ${item.email}</p><p>Contributions: ${item.contributions}</p>`;
link = '';
guid = `anon-${item.name}`;
} else {
title = `Contributor: ${item.login}`;
description = `<img src="${item.avatar_url}"></img><p><a href="${item.html_url}">${item.login}</a></p><p>Contributions: ${item.contributions}</p>`;
link = item.html_url;
guid = item.id;
}

items.push({
title: title,
description: description,
guid: guid,
link: link,
pubDate: time,
});
}),
(ctx.state.data = {
title: `${user}/${repo} Contributors`,
link: `${host}/graphs/contributors`,
description: `New contributors for ${user}/${repo}`,
item: items,
});
};

0 comments on commit 17de3a1

Please sign in to comment.