Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: kevva/github-repositories
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: ddff311fc815a5299635584f430d841c770dbfd1
Choose a base ref
...
head repository: kevva/github-repositories
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: cb2e4a99fdeeff226a96fd72929afa9742933918
Choose a head ref
  • 4 commits
  • 6 files changed
  • 1 contributor

Commits on Sep 30, 2019

  1. Require Node.js 10

    kevva committed Sep 30, 2019
    Copy the full SHA
    2e31a28 View commit details
  2. Add sort option

    kevva committed Sep 30, 2019
    Copy the full SHA
    10534c7 View commit details
  3. Add direction option

    kevva committed Sep 30, 2019
    Copy the full SHA
    fe8dd68 View commit details
  4. 4.0.0

    kevva committed Sep 30, 2019
    Copy the full SHA
    cb2e4a9 View commit details
Showing with 56 additions and 129 deletions.
  1. +0 −3 .travis.yml
  2. +0 −70 cli.js
  3. +25 −21 index.js
  4. +7 −13 package.json
  5. +14 −22 readme.md
  6. +10 −0 test.js
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -3,6 +3,3 @@ language: node_js
node_js:
- '12'
- '10'
- '8'
- '6'
- '4'
70 changes: 0 additions & 70 deletions cli.js

This file was deleted.

46 changes: 25 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
@@ -2,32 +2,36 @@
const ghGot = require('gh-got');
const isGithubUserOrOrg = require('is-github-user-or-org');

module.exports = (name, options) => {
options = options || {};

let page = 1;
let returnValue = [];
const fetchRepos = async (url, options = {}, repos = [], page = 1) => {
const {body: currentRepos, headers: {link}} = await ghGot(url, {
...options,
query: {
direction: options.direction,
page,
per_page: 100, // eslint-disable-line camelcase
sort: options.sort
}
});

if (typeof name !== 'string') {
return Promise.reject(new TypeError(`Expected \`name\` to be of type \`string\` but received type \`${typeof name}\``));
if (link && link.includes('next')) {
return fetchRepos(url, options, repos.concat(currentRepos), page + 1);
}

return isGithubUserOrOrg(name, options).then(userType => {
const type = (userType === 'User') ? 'users' : 'orgs';
return repos.concat(currentRepos);
};

return (function loop() {
const url = `${type}/${name}/repos?&per_page=100&page=${page}`;
module.exports = async (name, options = {}) => {
options = {
sort: 'full_name',
...options
};

return ghGot(url, options).then(response => {
returnValue = returnValue.concat(response.body);
if (typeof name !== 'string') {
throw new TypeError(`Expected \`name\` to be of type \`string\` but received type \`${typeof name}\``);
}

if (response.headers.link && response.headers.link.includes('next')) {
page++;
return loop();
}
const type = (await isGithubUserOrOrg(name, options) === 'User') ? 'users' : 'orgs';
const url = `${type}/${name}/repos`;

return returnValue;
});
})();
});
return fetchRepos(url, options);
};
20 changes: 7 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "github-repositories",
"version": "3.1.0",
"version": "4.0.0",
"description": "Get all Github repos from a user or an organization",
"license": "MIT",
"repository": "kevva/github-repositories",
@@ -9,33 +9,27 @@
"email": "kevinmartensson@gmail.com",
"url": "github.com/kevva"
},
"bin": "cli.js",
"engines": {
"node": ">=4"
"node": ">=10"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js",
"cli.js"
"index.js"
],
"keywords": [
"cli-app",
"cli",
"api",
"github",
"repo",
"repositories"
],
"dependencies": {
"chalk": "^1.0.0",
"gh-got": "^3.0.0",
"is-github-user-or-org": "^1.0.0",
"meow": "^3.3.0"
"gh-got": "^8.1.0",
"is-github-user-or-org": "^1.0.0"
},
"devDependencies": {
"ava": "^0.25.0",
"xo": "^0.20.3"
"ava": "^2.4.0",
"xo": "^0.25.3"
}
}
36 changes: 14 additions & 22 deletions readme.md
Original file line number Diff line number Diff line change
@@ -38,6 +38,20 @@ Username or organization to fetch repos from.

Type: `object`

##### sort

Type: `string`<br>
Default: `full_name`

Can be one of `created`, `updated`, `pushed`, `full_name`.

##### direction

Type: `string`<br>
Default: `asc` when using `full_name`, otherwise `desc`

Can be one of `asc` or `desc`.

##### token

Type: `string`
@@ -56,25 +70,3 @@ Default: `https://api.github.com/`
To support [GitHub Enterprise](https://enterprise.github.com/).

Can be set globally with the `GITHUB_ENDPOINT` environment variable.


## CLI

```
$ npm install --global github-repositories
```

```
$ github-repositories --help
Usage
$ github-repositories kevva
$ github-repositories kevva --token 523ef69119eadg12
Options
-f, --forks Only list forks
-r, --repos Only display repository names
-s, --sources Only list sources
-t, --token GitHub authentication token
-u, --urls Only display URLs
```
10 changes: 10 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -31,3 +31,13 @@ test('two requests should return same data', async t => {
t.truthy(second.length);
t.deepEqual(first, second);
});

test('`sort` option', async t => {
const repos = await githubRepositories('kevva', {sort: 'created'});
t.is(repos[repos.length - 1].name, 'download');
});

test('`direction` option', async t => {
const [repo] = await githubRepositories('kevva', {direction: 'asc', sort: 'created'});
t.is(repo.name, 'download');
});