Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
54db6f3
change main file to lib/GitHub.js
klarkc Sep 21, 2016
f55342d
Revert "change main file to lib/GitHub.js"
klarkc Sep 21, 2016
014c241
add lib to npmignore, because we dont want this source code in the pa…
klarkc Sep 21, 2016
1e45626
add prepublish command
klarkc Sep 21, 2016
873e2a7
fix missing comma
klarkc Sep 21, 2016
9401423
add new project functions
klarkc Sep 22, 2016
b415a91
add reading tests
klarkc Sep 22, 2016
2384038
fix some lint errors
klarkc Sep 22, 2016
7721158
change variable id to number in some project functions
klarkc Sep 22, 2016
e84f050
change more id parameters to number
klarkc Sep 22, 2016
156e1b7
add preview support to __getRequestHeaders
klarkc Sep 22, 2016
7c7d036
change parameters to avoid errors
klarkc Sep 22, 2016
80855f3
add projects tests
klarkc Sep 22, 2016
c732335
fix lint fixable errors
klarkc Sep 22, 2016
ccdc2c0
fix error in url
klarkc Sep 22, 2016
36f8cd2
add better response error detection
klarkc Sep 22, 2016
87fdcf7
add missing tests
klarkc Sep 22, 2016
b7b6e8e
fix lint errors
klarkc Sep 22, 2016
8bde56b
fix lint errors
klarkc Sep 23, 2016
2092a45
add clearRepo test helper
klarkc Sep 23, 2016
152be9b
clear repo before user tests
klarkc Sep 23, 2016
e7cb3cd
change callback parameter of _request to only set true when the reque…
klarkc Sep 23, 2016
010cf2f
change POST to DELETE
klarkc Sep 23, 2016
cdc20f0
fix lint errors
klarkc Sep 23, 2016
851a823
create repo, issue and milestone before reading tests
klarkc Sep 27, 2016
9600542
change callback behavior in _request function
klarkc Sep 27, 2016
a47f426
create fixed-test-repo-1 when needed
klarkc Sep 27, 2016
39fd2ff
add helperFunctions
klarkc Sep 27, 2016
b0d27e6
add clearTeams
klarkc Sep 27, 2016
89a90ac
fix tests
klarkc Sep 27, 2016
792b726
fix uncatchable before callback
klarkc Sep 27, 2016
6a13220
fix team test errors
klarkc Sep 27, 2016
175bffe
fix eslint errors
klarkc Sep 27, 2016
f61baf6
fix team errors
klarkc Sep 27, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
docs/
coverage/
node_modules/

lib/
.DS_Store
sauce.json
214 changes: 213 additions & 1 deletion lib/Repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,10 @@ class Repository extends Requestable {
* @return {Promise} - the promise for the http request
*/
updatePullRequst(number, options, cb) {
log('Deprecated: This method contains a typo and it has been deprecated. It will be removed in next major version. Use updatePullRequest() instead.');
log(
'Deprecated: This method contains a typo and it has been deprecated.' +
'It will be removed in next major version. Use updatePullRequest() instead.'
);

return this.updatePullRequest(number, options, cb);
}
Expand Down Expand Up @@ -803,6 +806,215 @@ class Repository extends Requestable {
mergePullRequest(number, options, cb) {
return this._request('PUT', `/repos/${this.__fullname}/pulls/${number}/merge`, options, cb);
}

/**
* Get information about all projects
* @see https://developer.github.com/v3/repos/projects/#list-projects
* @param {Requestable.callback} [cb] - will receive the list of projects
* @return {Promise} - the promise for the http request
*/
listProjects(cb) {
return this._request('GET', `/repos/${this.__fullname}/projects`, null, cb);
}

/**
* Get information about a project
* @see https://developer.github.com/v3/repos/projects/#list-a-project
* @param {string} projectNumber - the number of the project
* @param {Requestable.callback} cb - will receive the project information
* @return {Promise} - the promise for the http request
*/
getProject(projectNumber, cb) {
return this._request('GET', `/repos/${this.__fullname}/projects/${projectNumber}`, null, cb);
}

/**
* Create a new project
* @see https://developer.github.com/v3/repos/projects/#create-a-project
* @param {Object} options - the description of the project
* @param {Requestable.callback} cb - will receive the newly created project
* @return {Promise} - the promise for the http request
*/
createProject(options, cb) {
return this._request('POST', `/repos/${this.__fullname}/projects`, options, cb);
}

/**
* Edit a project
* @see https://developer.github.com/v3/repos/projects/#update-a-project
* @param {string} projectNumber - the number of the project
* @param {Object} options - the description of the project
* @param {Requestable.callback} cb - will receive the modified project
* @return {Promise} - the promise for the http request
*/
updateProject(projectNumber, options, cb) {
return this._request('PATCH', `/repos/${this.__fullname}/projects/${projectNumber}`, options, cb);
}

/**
* Delete a project
* @see https://developer.github.com/v3/repos/projects/#delete-a-project
* @param {string} projectNumber - the project to be deleted
* @param {Requestable.callback} cb - will receive true if the operation is successful
* @return {Promise} - the promise for the http request
*/
deleteProject(projectNumber, cb) {
return this._request('DELETE', `/repos/${this.__fullname}/projects/${projectNumber}`, null, cb);
}

/**
* Get information about all columns of a project
* @see https://developer.github.com/v3/repos/projects/#list-columns
* @param {string} projectNumber - the number of the project
* @param {Requestable.callback} [cb] - will receive the list of columns
* @return {Promise} - the promise for the http request
*/
listProjectColumns(projectNumber, cb) {
return this._request('GET', `/repos/${this.__fullname}/projects/${projectNumber}/columns`, null, cb);
}

/**
* Get information about a column
* @see https://developer.github.com/v3/repos/projects/#get-a-column
* @param {string} colId - the id of the column
* @param {Requestable.callback} cb - will receive the column information
* @return {Promise} - the promise for the http request
*/
getProjectColumn(colId, cb) {
return this._request('GET', `/repos/${this.__fullname}/projects/columns/${colId}`, null, cb);
}

/**
* Create a new column
* @see https://developer.github.com/v3/repos/projects/#create-a-column
* @param {string} projectNumber - the project number
* @param {Object} options - the description of the column
* @param {Requestable.callback} cb - will receive the newly created column
* @return {Promise} - the promise for the http request
*/
createProjectColumn(projectNumber, options, cb) {
return this._request('POST', `/repos/${this.__fullname}/projects/${projectNumber}/columns`, options, cb);
}

/**
* Edit a column
* @see https://developer.github.com/v3/repos/projects/#update-a-column
* @param {string} colId - the column id
* @param {Object} options - the description of the column
* @param {Requestable.callback} cb - will receive the modified column
* @return {Promise} - the promise for the http request
*/
updateProjectColumn(colId, options, cb) {
return this._request('PATCH', `/repos/${this.__fullname}/projects/columns/${colId}`, options, cb);
}

/**
* Delete a column
* @see https://developer.github.com/v3/repos/projects/#delete-a-column
* @param {string} colId - the column to be deleted
* @param {Requestable.callback} cb - will receive true if the operation is successful
* @return {Promise} - the promise for the http request
*/
deleteProjectColumn(colId, cb) {
return this._request('DELETE', `/repos/${this.__fullname}/projects/columns/${colId}`, null, cb);
}

/**
* Move a column
* @see https://developer.github.com/v3/repos/projects/#move-a-column
* @param {string} colId - the column to be moved
* @param {string} position - can be one of first, last, or after:<column-id>,
* where <column-id> is the id value of a column in the same project.
* @param {Requestable.callback} cb - will receive true if the operation is successful
* @return {Promise} - the promise for the http request
*/
moveProjectColumn(colId, position, cb) {
return this._request(
'POST',
`/repos/${this.__fullname}/projects/columns/${colId}/moves`,
{position: position},
cb
);
}

/**
* Get information about all cards of a column
* @see https://developer.github.com/v3/repos/projects/#list-projects-cards
* @param {string} colId - the id of the column
* @param {Requestable.callback} [cb] - will receive the list of cards
* @return {Promise} - the promise for the http request
*/
listProjectCards(colId, cb) {
return this._request('GET', `/repos/${this.__fullname}/projects/columns/${colId}/cards`, null, cb);
}

/**
* Get information about a card
* @see https://developer.github.com/v3/repos/projects/#list-a-project-card
* @param {string} cardId - the id of the card
* @param {Requestable.callback} cb - will receive the card information
* @return {Promise} - the promise for the http request
*/
getProjectCard(cardId, cb) {
return this._request('GET', `/repos/${this.__fullname}/projects/columns/cards/${cardId}`, null, cb);
}

/**
* Create a new card
* @see https://developer.github.com/v3/repos/projects/#create-a-project-card
* @param {string} colId - the column id
* @param {Object} options - the description of the card
* @param {Requestable.callback} cb - will receive the newly created card
* @return {Promise} - the promise for the http request
*/
createProjectCard(colId, options, cb) {
return this._request('POST', `/repos/${this.__fullname}/projects/columns/${colId}/cards`, options, cb);
}

/**
* Edit a card
* @see https://developer.github.com/v3/repos/projects/#update-a-project-card
* @param {string} cardId - the card id
* @param {Object} options - the description of the card
* @param {Requestable.callback} cb - will receive the modified card
* @return {Promise} - the promise for the http request
*/
updateProjectCard(cardId, options, cb) {
return this._request('PATCH', `/repos/${this.__fullname}/projects/columns/cards/${cardId}`, options, cb);
}

/**
* Delete a card
* @see https://developer.github.com/v3/repos/projects/#delete-a-project-card
* @param {string} cardId - the card to be deleted
* @param {Requestable.callback} cb - will receive true if the operation is successful
* @return {Promise} - the promise for the http request
*/
deleteProjectCard(cardId, cb) {
return this._request('DELETE', `/repos/${this.__fullname}/projects/columns/cards/${cardId}`, null, cb);
}

/**
* Move a card
* @see https://developer.github.com/v3/repos/projects/#move-a-project-card
* @param {string} cardId - the card to be moved
* @param {string} position - can be one of top, bottom, or after:<card-id>,
* where <card-id> is the id value of a card in the same project.
* @param {string} colId - the id value of a column in the same project.
* @param {Requestable.callback} cb - will receive true if the operation is successful
* @return {Promise} - the promise for the http request
*/
moveProjectCard(cardId, position, colId, cb) {
/* eslint-disable quote-props*/
return this._request(
'POST',
`/repos/${this.__fullname}/projects/columns/cards/${cardId}/moves`,
{'position': position, 'column_id': colId},
cb
);
/* eslint-enable */
}

}

module.exports = Repository;
32 changes: 28 additions & 4 deletions lib/Requestable.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,22 @@ class Requestable {
* Compute the headers required for an API request.
* @private
* @param {boolean} raw - if the request should be treated as JSON or as a raw request
* @param {boolean} preview - set true to allow the preview api
* @return {Object} - the headers to use in the request
*/
__getRequestHeaders(raw) {
__getRequestHeaders(raw, preview) {
let headers = {
'Accept': raw ? 'application/vnd.github.v3.raw+json' : 'application/vnd.github.v3+json',
'Content-Type': 'application/json;charset=UTF-8'
};

if (preview && raw) {
headers.Accept = 'application/vnd.github.inertia-preview.raw+json';
} else if (preview) {
headers.Accept = 'application/vnd.github.inertia-preview+json';
} else {
headers.Accept = (raw) ? 'application/vnd.github.v3.raw+json' : 'application/vnd.github.v3+json';
}

if (this.__authorizationHeader) {
headers.Authorization = this.__authorizationHeader;
}
Expand Down Expand Up @@ -152,7 +160,15 @@ class Requestable {
*/
_request(method, path, data, cb, raw) {
const url = this.__getURL(path);
const headers = this.__getRequestHeaders(raw);

let headers;

// Enable preview api only for projects calls
if (path.includes('projects')) {
headers = this.__getRequestHeaders(raw, true);
} else {
headers = this.__getRequestHeaders(raw);
}
let queryParams = {};

const shouldUseDataAsParams = data && (typeof data === 'object') && methodHasNoBody(method);
Expand All @@ -175,7 +191,15 @@ class Requestable {

if (cb) {
requestPromise.then((response) => {
cb(null, response.data || true, response);
if (response.data && Object.keys(response.data).length > 0) {
// When data has results
cb(null, response.data, response);
} else if (config.method !== 'GET' && Object.keys(response.data).length < 1) {
// True when successful submit a request and receive a empty object
cb(null, (response.status < 300), response);
} else {
cb(null, response.data, response);
}
});
}

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"test-verbose": "DEBUG=github* npm test",
"lint": "gulp lint",
"make-docs": "node_modules/.bin/jsdoc -c .jsdoc.json --verbose",
"release": "./release.sh"
"release": "./release.sh",
"prepublish": "npm run build"
},
"babel": {
"presets": [
Expand All @@ -41,8 +42,7 @@
}
},
"files": [
"dist/*",
"lib/*"
"dist/*"
],
"dependencies": {
"axios": "^0.10.0",
Expand Down
30 changes: 30 additions & 0 deletions test/helpers/clearRepo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {getNextPage, deleteRepo} from './helperFunctions.js';

module.exports = function(github, cb) {
let user = github.getUser();

// Override default function to delete repo on each loop
user._requestAllPages = function(path, options, cb) {
return this._request('GET', path, options)
.then((response) => {
if (response.data instanceof Array) {
let deletions = response.data
.map((repo) => deleteRepo(repo, github))

Promise.all(deletions).then(() => {
console.log('all user repos removed');
const nextUrl = getNextPage(response.headers.link);
if (nextUrl) {
console.log('next page');
return this._requestAllPages(nextUrl, options, cb);
}

cb(null);
});
}
}).catch(cb);
};

// Read all repos (so delete with _requestAllPages inner call);
user.listRepos(cb);
}
32 changes: 32 additions & 0 deletions test/helpers/clearTeams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {getNextPage, deleteTeam} from './helperFunctions.js';

module.exports = function(github, organization, cb) {
let org = github.getOrganization(organization);

// Override default function to delete team on each loop
org._requestAllPages = function(path, options, cb) {
return this._request('GET', path, options)
.then((response) => {
if (response.data instanceof Array) {
let deletions = response.data
.map((team) => {
deleteTeam(team, github)
});

Promise.all(deletions).then(() => {
console.log('all org teams removed');
const nextUrl = getNextPage(response.headers.link);
if (nextUrl) {
console.log('next page');
return this._requestAllPages(nextUrl, options, cb);
}

cb(null);
});
}
}).catch(cb);
};

// Read all repos (so delete with _requestAllPages inner call);
org.getTeams(cb);
}
Loading