|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @copyright 2016 Matt Smith (Development Seed) |
| 4 | + * @license Licensed under {@link https://spdx.org/licenses/BSD-3-Clause-Clear.html BSD-3-Clause-Clear}. |
| 5 | + * Github.js is freely distributable. |
| 6 | + */ |
| 7 | + |
| 8 | +import Requestable from './Requestable'; |
| 9 | +import debug from 'debug'; |
| 10 | +const log = debug('github:team'); |
| 11 | + |
| 12 | +/** |
| 13 | + * A Team allows scoping of API requests to a particular Github Organization Team. |
| 14 | + */ |
| 15 | +class Team extends Requestable { |
| 16 | + /** |
| 17 | + * Create a Team. |
| 18 | + * @param {string} [teamId] - the id for the team |
| 19 | + * @param {Requestable.auth} [auth] - information required to authenticate to Github |
| 20 | + * @param {string} [apiBase=https://api.github.com] - the base Github API URL |
| 21 | + */ |
| 22 | + constructor(teamId, auth, apiBase) { |
| 23 | + super(auth, apiBase); |
| 24 | + this.__teamId = teamId; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Get Team information |
| 29 | + * @see https://developer.github.com/v3/orgs/teams/#get-team |
| 30 | + * @param {Requestable.callback} [cb] - will receive the team |
| 31 | + * @return {Promise} - the promise for the http request |
| 32 | + */ |
| 33 | + getTeam(cb) { |
| 34 | + log(`Fetching Team ${this.__teamId}`); |
| 35 | + return this._request('Get', `/teams/${this.__teamId}`, undefined, cb); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * List the Team's repositories |
| 40 | + * @see https://developer.github.com/v3/orgs/teams/#list-team-repos |
| 41 | + * @param {Requestable.callback} [cb] - will receive the list of repositories |
| 42 | + * @return {Promise} - the promise for the http request |
| 43 | + */ |
| 44 | + getRepos(cb) { |
| 45 | + log(`Fetching repositories for Team ${this.__teamId}`); |
| 46 | + return this._requestAllPages(`/teams/${this.__teamId}/repos`, undefined, cb); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Edit Team information |
| 51 | + * @see https://developer.github.com/v3/orgs/teams/#edit-team |
| 52 | + * @param {object} options - Parameters for team edit |
| 53 | + * @param {string} options.name - The name of the team |
| 54 | + * @param {string} [options.description] - Team description |
| 55 | + * @param {string} [options.repo_names] - Repos to add the team to |
| 56 | + * @param {string} [options.privacy=secret] - The level of privacy the team should have. Can be either one |
| 57 | + * of: `secret`, or `closed` |
| 58 | + * @param {Requestable.callback} [cb] - will receive the updated team |
| 59 | + * @return {Promise} - the promise for the http request |
| 60 | + */ |
| 61 | + editTeam(options, cb) { |
| 62 | + log(`Editing Team ${this.__teamId}`); |
| 63 | + return this._request('PATCH', `/teams/${this.__teamId}`, options, cb); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * List the users who are members of the Team |
| 68 | + * @see https://developer.github.com/v3/orgs/teams/#list-team-members |
| 69 | + * @param {object} options - Parameters for listing team users |
| 70 | + * @param {string} [options.role=all] - can be one of: `all`, `maintainer`, or `member` |
| 71 | + * @param {Requestable.callback} [cb] - will receive the list of users |
| 72 | + * @return {Promise} - the promise for the http request |
| 73 | + */ |
| 74 | + listMembers(options, cb) { |
| 75 | + log(`Getting members of Team ${this.__teamId}`); |
| 76 | + return this._requestAllPages(`/teams/${this.__teamId}/members`, options, cb); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Get Team membership status for a user |
| 81 | + * @see https://developer.github.com/v3/orgs/teams/#get-team-membership |
| 82 | + * @param {string} username - can be one of: `all`, `maintainer`, or `member` |
| 83 | + * @param {Requestable.callback} [cb] - will receive the membership status of a user |
| 84 | + * @return {Promise} - the promise for the http request |
| 85 | + */ |
| 86 | + getMembership(username, cb) { |
| 87 | + log(`Getting membership of user ${username} in Team ${this.__teamId}`); |
| 88 | + return this._request('GET', `/teams/${this.__teamId}/memberships/${username}`, undefined, cb); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Add a member to the Team |
| 93 | + * @see https://developer.github.com/v3/orgs/teams/#add-team-membership |
| 94 | + * @param {string} username - can be one of: `all`, `maintainer`, or `member` |
| 95 | + * @param {object} options - Parameters for adding a team member |
| 96 | + * @param {string} [options.role=member] - The role that this user should have in the team. Can be one |
| 97 | + * of: `member`, or `maintainer` |
| 98 | + * @param {Requestable.callback} [cb] - will receive the membership status of added user |
| 99 | + * @return {Promise} - the promise for the http request |
| 100 | + */ |
| 101 | + addMembership(username, options, cb) { |
| 102 | + log(`Adding user ${username} to Team ${this.__teamId}`); |
| 103 | + return this._request('PUT', `/teams/${this.__teamId}/memberships/${username}`, options, cb); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Get repo management status for team |
| 108 | + * @see https://developer.github.com/v3/orgs/teams/#remove-team-membership |
| 109 | + * @param {string} owner - Organization name |
| 110 | + * @param {string} repo - Repo name |
| 111 | + * @param {Requestable.callback} [cb] - will receive the membership status of added user |
| 112 | + * @return {Promise} - the promise for the http request |
| 113 | + */ |
| 114 | + isManagedRepo(owner, repo, cb) { |
| 115 | + log(`Getting repo management by Team ${this.__teamId} for repo ${owner}/${repo}`); |
| 116 | + return this._request204or404(`/teams/${this.__teamId}/repos/${owner}/${repo}`, undefined, cb); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Add or Update repo management status for team |
| 121 | + * @see https://developer.github.com/v3/orgs/teams/#add-or-update-team-repository |
| 122 | + * @param {string} owner - Organization name |
| 123 | + * @param {string} repo - Repo name |
| 124 | + * @param {object} options - Parameters for adding or updating repo management for the team |
| 125 | + * @param {string} [options.permission] - The permission to grant the team on this repository. Can be one |
| 126 | + * of: `pull`, `push`, or `admin` |
| 127 | + * @param {Requestable.callback} [cb] - will receive the membership status of added user |
| 128 | + * @return {Promise} - the promise for the http request |
| 129 | + */ |
| 130 | + manageRepo(owner, repo, options, cb) { |
| 131 | + log(`Adding or Updating repo management by Team ${this.__teamId} for repo ${owner}/${repo}`); |
| 132 | + return this._request204or404(`/teams/${this.__teamId}/repos/${owner}/${repo}`, options, cb, 'PUT'); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Remove repo management status for team |
| 137 | + * @see https://developer.github.com/v3/orgs/teams/#remove-team-repository |
| 138 | + * @param {string} owner - Organization name |
| 139 | + * @param {string} repo - Repo name |
| 140 | + * @param {Requestable.callback} [cb] - will receive the membership status of added user |
| 141 | + * @return {Promise} - the promise for the http request |
| 142 | + */ |
| 143 | + unmanageRepo(owner, repo, cb) { |
| 144 | + log(`Remove repo management by Team ${this.__teamId} for repo ${owner}/${repo}`); |
| 145 | + return this._request204or404(`/teams/${this.__teamId}/repos/${owner}/${repo}`, undefined, cb, 'DELETE'); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Delete Team |
| 150 | + * @see https://developer.github.com/v3/orgs/teams/#delete-team |
| 151 | + * @param {Requestable.callback} [cb] - will receive the list of repositories |
| 152 | + * @return {Promise} - the promise for the http request |
| 153 | + */ |
| 154 | + deleteTeam(cb) { |
| 155 | + log(`Deleting Team ${this.__teamId}`); |
| 156 | + return this._request204or404(`/teams/${this.__teamId}`, undefined, cb, 'DELETE'); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +module.exports = Team; |
0 commit comments