Skip to content

Commit

Permalink
Merge pull request #9 from ibarsi/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ibarsi authored Feb 20, 2017
2 parents c5bd76b + c33c81a commit 98f0b14
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 17 deletions.
6 changes: 5 additions & 1 deletion main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ function getRepositoryType() {
TYPES.GITHUB,
TYPES.BITBUCKET
],
default: repository_package ? getRepositoryTypeFromUrl(repository_package.repository) : TYPES.GITHUB
default: repository_package ?
getRepositoryTypeFromUrl(typeof repository_package.repository === 'string' ?
repository_package.repository :
repository_package.repository.type || repository_package.repository.url) :
TYPES.GITHUB
}
];

Expand Down
45 changes: 30 additions & 15 deletions main/modules/commits.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import fs from 'fs';

import { isFile, async, requestPromise } from './helpers';
import { isFile, uniq, async, requestPromise } from './helpers';

// PUBLIC

Expand Down Expand Up @@ -33,12 +33,10 @@ export function Commits(type = TYPES.GITHUB) {
return new Promise((resolve, reject) => {
async(function* () {
try {
const url = config.url.replace('{owner}', owner).replace('{repo}', repository);

const { username, password } = yield _getCreds(config.token);

const options = {
url,
url: config.commits_url.replace('{owner}', owner).replace('{repo}', repository),
config: {
headers: {
'User-Agent': owner,
Expand All @@ -49,10 +47,22 @@ export function Commits(type = TYPES.GITHUB) {

switch (type) {
case TYPES.GITHUB:
resolve(yield _requestFullGitHubResponse((result) => result.map(GitHubCommit), options));
const branches = yield requestPromise(config.branches_url.replace('{owner}', owner).replace('{repo}', repository), options.config);
const branch_commit_results = yield Promise.all(branches.data.map(branch => {
return _requestFullGitHubResponse(Object.assign({}, options, {
url: `${ options.url }?sha=${ branch.name }`
}));
}));

const github_commits = branch_commit_results.reduce((acc, list) => acc.concat(list), []);
const unique_commits = uniq(github_commits, item => item.sha);

resolve(unique_commits.map(GitHubCommit));
break;
case TYPES.BITBUCKET:
resolve(yield _requestFullBitBucketResponse((result) => result.values.map(BitBucketCommit), options));
const bitbucket_commits = yield _requestFullBitBucketResponse(options);

resolve(bitbucket_commits.map(BitBucketCommit));
break;
default:
resolve([]);
Expand Down Expand Up @@ -91,6 +101,7 @@ export default {

function BitBucketCommit(value) {
return {
id: value.sha,
author: value.author.user ? value.author.user.display_name : value.author.raw,
message: value.message,
date: value.date
Expand All @@ -99,6 +110,7 @@ function BitBucketCommit(value) {

function GitHubCommit(value) {
return {
id: value.sha,
author: value.commit.author.name,
message: value.commit.message,
date: value.commit.committer.date
Expand All @@ -109,17 +121,20 @@ function _initCommitProps(type) {
switch (type) {
case TYPES.BITBUCKET:
return {
url: 'https://api.bitbucket.org/2.0/repositories/{owner}/{repo}/commits',
commits_url: 'https://api.bitbucket.org/2.0/repositories/{owner}/{repo}/commits',
token: '.bitbucket_token'
};
case TYPES.GITHUB:
return {
url: 'https://api.github.com/repos/{owner}/{repo}/commits',
commits_url: 'https://api.github.com/repos/{owner}/{repo}/commits',
branches_url: 'https://api.github.com/repos/{owner}/{repo}/branches',
token: '.github_token'
};
default:
return {
url: ''
commits_url: '',
branches_url: '',
token: ''
};
}
}
Expand All @@ -135,16 +150,16 @@ function _getCreds(token) {
});
}

function _requestFullBitBucketResponse(func, options, values = []) {
function _requestFullBitBucketResponse(options, values = []) {
return new Promise((resolve, reject) => {
async(function* () {
try {
const { url, config } = options;
const response = yield requestPromise(url, config);
const chunked_values = values.concat(func(response.data));
const chunked_values = values.concat(response.data.values);

if (response.data.next) {
resolve(_requestFullBitBucketResponse(func, { url: response.data.next, config }, chunked_values));
resolve(_requestFullBitBucketResponse({ url: response.data.next, config }, chunked_values));
}

resolve(chunked_values);
Expand All @@ -156,21 +171,21 @@ function _requestFullBitBucketResponse(func, options, values = []) {
});
}

function _requestFullGitHubResponse(func, options, values = []) {
function _requestFullGitHubResponse(options, values = []) {
return new Promise((resolve, reject) => {
async(function* () {
try {
const { url, config } = options;
const response = yield requestPromise(url, config);
const chunked_values = values.concat(func(response.data));
const chunked_values = values.concat(response.data);

const link = response.headers.link;

if (link && link.indexOf('rel="next"') >= 0) {
const next_url = link.substring(0, link.indexOf('rel="next"'));
const next_url_formatted = next_url.trim().replace('<', '').replace('>', '').replace(';', '');

resolve(_requestFullGitHubResponse(func, { url: next_url_formatted, config }, chunked_values));
resolve(_requestFullGitHubResponse({ url: next_url_formatted, config }, chunked_values));
}

resolve(chunked_values);
Expand Down
2 changes: 1 addition & 1 deletion main/modules/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Configuration: __{{format}}__
Current Commits: __{{current_commits}}__
Previous Commits: __{{previous_commits}}__
Press \`Esc\` or \`Ctrl/Cmd-C\` to quit.`;
Press \`Esc\` or \`Ctrl-C\` to quit.`;

const listing_settings = {
fg: 'green',
Expand Down
17 changes: 17 additions & 0 deletions main/modules/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ export function isFile(path) {
}
}

export function uniq(list, func) {
const uids = [];
const unique_set = [];

list.forEach(item => {
const uid = func(item);

if (uids.indexOf(uid) < 0) {
uids.push(uid);
unique_set.push(item);
}
});

return unique_set;
}

export function wrapSpinner(promise, message = '') {
const spinner = new CLI.Spinner(message);
spinner.start();
Expand Down Expand Up @@ -72,6 +88,7 @@ export function async(gen, context = undefined) {

export default {
isFile,
uniq,
wrapSpinner,
partial,
async
Expand Down

0 comments on commit 98f0b14

Please sign in to comment.