Skip to content

Commit

Permalink
feat: support new repo api
Browse files Browse the repository at this point in the history
  • Loading branch information
xudafeng committed Apr 21, 2018
1 parent a03fcf3 commit 0599561
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
49 changes: 49 additions & 0 deletions lib/git-contributor.js
Expand Up @@ -4,6 +4,9 @@ const _ = require('xutil');
const {
execSync
} = require('child_process');
const {
parse
} = require('url');
const path = require('path');
const Promise = require('bluebird');
const request = require('request-promise');
Expand Down Expand Up @@ -45,6 +48,21 @@ const getInfoFromGithub = maillist => {
});
};

const getInfoFromGithubNewAPI = info => {
const uri = `https://api.github.com/repos/${info.groupName}/${info.repoName}/contributors`;
const options = {
uri,
json: true,
headers: {
'user-agent': Date.now()
}
};
if (process.env.OAUTH_TOKEN) {
options.headers['Authorization'] = `token ${process.env.OAUTH_TOKEN}`;
}
return request(options);
};

const format = list => {
return list.filter(item => item).map(item => {
return {
Expand All @@ -55,10 +73,41 @@ const format = list => {
});
};

const getRepoInfo = async (url) => {
// git@github.com:xudafeng/git-contributor.git
// https://github.com/xudafeng/git-contributor
if (url.slice(0, 4) === 'git@') {
url = url
.replace(':', '/')
.replace('git@', 'http://');
}
url = url
.replace(/\.git/g, '');
const obj = parse(url);
const arr = obj.pathname.split('/');
return {
groupName: arr[1],
repoName: arr[2],
};
};

exports.getAuthor = async (options = {}) => {
const cwd = path.resolve(options.cwd || process.cwd());
const originPkg = path.join(cwd, 'package.json');
const dotGitDir = path.join(cwd, '.git');

if (_.isExistedFile(originPkg)) {
try {
const pkg = require(originPkg);
if (pkg.repository && pkg.repository.url) {
const info = await getRepoInfo(pkg.repository.url);
const infoList = await getInfoFromGithubNewAPI(info);
return format(infoList);
}
} catch (e) {
}
}

if (!_.isExistedDir(dotGitDir)) {
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "git-contributor",
"version": "1.0.5",
"version": "1.0.6",
"description": "git-contributor",
"keywords": [
"contributor"
Expand Down

0 comments on commit 0599561

Please sign in to comment.