├── .editorconfig ├── .gitattributes ├── .gitignore ├── .npmrc ├── .travis.yml ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.js text eol=lf 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - '8' 5 | - '6' 6 | - '4' 7 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const githubRepos = require('github-repositories'); 3 | const githubTokenUser = require('github-token-user'); 4 | const ghGot = require('gh-got'); 5 | 6 | const run = (user, repos, opts) => Promise.all(repos.map(x => { 7 | const url = `repos/${x}/collaborators/${user}`; 8 | 9 | return ghGot.put(url, { 10 | token: opts.token, 11 | json: false 12 | }).then(() => x); 13 | })); 14 | 15 | const getRepos = (user, login, opts) => { 16 | return githubRepos(login, {token: opts.token}).then(data => { 17 | if (opts.addToSources) { 18 | data = data.filter(x => !x.fork); 19 | } 20 | 21 | data = data.map(x => x.full_name); 22 | 23 | return run(user, data, opts); 24 | }); 25 | }; 26 | 27 | module.exports = (user, repos, opts) => { 28 | opts = Object.assign({}, opts); 29 | 30 | if (typeof user !== 'string') { 31 | return Promise.reject(new Error('User required')); 32 | } 33 | 34 | if (typeof repos === 'object' && !Array.isArray(repos)) { 35 | opts = repos; 36 | repos = []; 37 | } 38 | 39 | if (!Array.isArray(repos)) { 40 | return Promise.reject(new TypeError(`Expected an \`Array\` of repos, got \`${typeof repos}\``)); 41 | } 42 | 43 | if (repos.length > 0 && (opts.addToAll || opts.addToSources)) { 44 | return Promise.reject(new Error('`addToAll` and `addToSources` cannot be used with `repos`')); 45 | } 46 | 47 | return githubTokenUser(opts.token).then(data => { 48 | if (repos.length === 0 && (opts.addToAll || opts.addToSources)) { 49 | return getRepos(user, data.login, opts); 50 | } 51 | 52 | repos = repos.map(x => x.split('/')[1] ? x : `${data.login}/${x}`); 53 | 54 | return run(user, repos, opts); 55 | }); 56 | }; 57 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Kevin Mårtensson (github.com/kevva) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "github-add-collab", 3 | "version": "2.0.2", 4 | "description": "Add collaborators to Github repos", 5 | "license": "MIT", 6 | "repository": "kevva/github-add-collab", 7 | "author": { 8 | "name": "Kevin Mårtensson", 9 | "email": "kevinmartensson@gmail.com", 10 | "url": "github.com/kevva" 11 | }, 12 | "engines": { 13 | "node": ">=4" 14 | }, 15 | "scripts": { 16 | "test": "xo && ava" 17 | }, 18 | "files": [ 19 | "index.js" 20 | ], 21 | "keywords": [ 22 | "api", 23 | "collab", 24 | "github", 25 | "repo", 26 | "repositories" 27 | ], 28 | "dependencies": { 29 | "gh-got": "^6.0.0", 30 | "github-repositories": "^3.0.0", 31 | "github-token-user": "^3.0.0", 32 | "nock": "^9.0.13" 33 | }, 34 | "devDependencies": { 35 | "ava": "*", 36 | "xo": "*" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # github-add-collab [![Build Status](https://travis-ci.org/kevva/github-add-collab.svg?branch=master)](https://travis-ci.org/kevva/github-add-collab) 2 | 3 | > Add collaborators to GitHub repos 4 | 5 | 6 | ## Install 7 | 8 | ``` 9 | $ npm install github-add-collab 10 | ``` 11 | 12 | 13 | ## Usage 14 | 15 | ```js 16 | const githubAddCollab = require('github-add-collab'); 17 | 18 | githubAddCollab('johndoe', ['github-add-collab', 'yeoman/yo'], { 19 | token: '523ef691191' 20 | }).then(data => { 21 | console.log('Successfully added user johndoe to github-add-collab and yeoman/yo'); 22 | }); 23 | ``` 24 | 25 | 26 | ## API 27 | 28 | ### githubAddCollab(user, [repos], options) 29 | 30 | Returns a promise for an `array`. 31 | 32 | #### user 33 | 34 | *Required*
35 | Type: `string` 36 | 37 | Username to add as collaborator. 38 | 39 | #### repos 40 | 41 | Type: `Array` 42 | 43 | Repos to add the collaborator to. 44 | 45 | #### options 46 | 47 | ##### token 48 | 49 | *Required*
50 | Type: `string` 51 | 52 | Token to authenticate with. If you don't have a token you can generate a new one [here](https://github.com/settings/tokens/new). 53 | 54 | ##### addToAll 55 | 56 | Type: `boolean` 57 | 58 | If no repos are defined and this option is set to `true` it'll add the user to all repositories that the token has access to. 59 | 60 | ##### addToSources 61 | 62 | Type: `boolean` 63 | 64 | If no repos are defined and this option is set to `true` it'll add the user to all non-forked repositories that the token has access to. 65 | 66 | 67 | ## Related 68 | 69 | * [github-add-collab-cli](https://github.com/kevva/github-add-collab-cli) - CLI for this module 70 | 71 | 72 | ## License 73 | 74 | MIT © [Kevin Mårtensson](https://github.com/kevva) 75 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import m from '.'; 3 | 4 | test('add collab to repository', async t => { 5 | const [repo] = await m('octocat', ['playground'], { 6 | token: process.env.GITHUB_TOKEN 7 | }); 8 | 9 | t.is(repo, 'bRuNoLeVeRmAnZoR/playground'); 10 | }); 11 | 12 | test('add collab to all repositories', async t => { 13 | const [download, playground] = await m('octocat', { 14 | token: process.env.GITHUB_TOKEN, 15 | addToAll: true 16 | }); 17 | 18 | t.is(download, 'bRuNoLeVeRmAnZoR/download'); 19 | t.is(playground, 'bRuNoLeVeRmAnZoR/playground'); 20 | }); 21 | 22 | test('add collab to all source repositories', async t => { 23 | const [repo] = await m('octocat', { 24 | token: process.env.GITHUB_TOKEN, 25 | addToSources: true 26 | }); 27 | 28 | t.is(repo, 'bRuNoLeVeRmAnZoR/playground'); 29 | }); 30 | --------------------------------------------------------------------------------