From 54db6f3a1bab9b3bc0a20735f31951a5e7499453 Mon Sep 17 00:00:00 2001 From: klarkc Date: Wed, 21 Sep 2016 14:33:09 -0300 Subject: [PATCH 01/34] change main file to lib/GitHub.js --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c942525d..4147bea1 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "2.4.0", "license": "BSD-3-Clause-Clear", "description": "A higher-level wrapper around the Github API.", - "main": "dist/components/GitHub.js", + "main": "lib/GitHub.js", "contributors": [ "Ændrew Rininsland (http://www.aendrew.com)", "Aurelio De Rosa (http://www.audero.it/)", From f55342d0dc4c83b66f5dfb9c06b91c42c003a30d Mon Sep 17 00:00:00 2001 From: klarkc Date: Wed, 21 Sep 2016 15:33:49 -0300 Subject: [PATCH 02/34] Revert "change main file to lib/GitHub.js" This reverts commit 54db6f3a1bab9b3bc0a20735f31951a5e7499453. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4147bea1..c942525d 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "2.4.0", "license": "BSD-3-Clause-Clear", "description": "A higher-level wrapper around the Github API.", - "main": "lib/GitHub.js", + "main": "dist/components/GitHub.js", "contributors": [ "Ændrew Rininsland (http://www.aendrew.com)", "Aurelio De Rosa (http://www.audero.it/)", From 014c241a4354abfde58f70be5d824047df6d582f Mon Sep 17 00:00:00 2001 From: klarkc Date: Wed, 21 Sep 2016 15:36:03 -0300 Subject: [PATCH 03/34] add lib to npmignore, because we dont want this source code in the package --- .npmignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.npmignore b/.npmignore index 15fff57d..1b903b90 100644 --- a/.npmignore +++ b/.npmignore @@ -1,6 +1,6 @@ docs/ coverage/ node_modules/ - +lib/ .DS_Store sauce.json From 1e456267f3ffe15ccd771a3e82b75ea3107b0bd8 Mon Sep 17 00:00:00 2001 From: klarkc Date: Wed, 21 Sep 2016 15:38:35 -0300 Subject: [PATCH 04/34] add prepublish command --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index c942525d..7a3b7fa1 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "lint": "gulp lint", "make-docs": "node_modules/.bin/jsdoc -c .jsdoc.json --verbose", "release": "./release.sh" + "prepublish": "npm run build" }, "babel": { "presets": [ From 873e2a7405041d459ffa57847c7e439b6f512676 Mon Sep 17 00:00:00 2001 From: klarkc Date: Wed, 21 Sep 2016 15:40:18 -0300 Subject: [PATCH 05/34] fix missing comma --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7a3b7fa1..c7aacc35 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "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": { From 9401423a0cf0ec0c0691639fcaadb9cf5f306c9c Mon Sep 17 00:00:00 2001 From: klarkc Date: Thu, 22 Sep 2016 10:49:22 -0300 Subject: [PATCH 06/34] add new project functions --- lib/Repository.js | 195 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 195 insertions(+) diff --git a/lib/Repository.js b/lib/Repository.js index 301e1230..b00af23a 100644 --- a/lib/Repository.js +++ b/lib/Repository.js @@ -803,6 +803,201 @@ 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} id - the id of the project + * @param {Requestable.callback} cb - will receive the project information + * @return {Promise} - the promise for the http request + */ + getProject(id, cb) { + return this._request('GET', `/repos/${this.__fullname}/projects/${id}`, 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} id - the id 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(id, options, cb) { + return this._request('PATCH', `/repos/${this.__fullname}/projects/${id}`, options, cb); + } + + /** + * Delete a project + * @see https://developer.github.com/v3/repos/projects/#delete-a-project + * @param {string} id - 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(id, cb) { + return this._request('DELETE', `/repos/${this.__fullname}/projects/${id}`, null, cb); + } + + /** + * Get information about all columns of a project + * @see https://developer.github.com/v3/repos/projects/#list-columns + * @param {string} id - the id of the project + * @param {Requestable.callback} [cb] - will receive the list of columns + * @return {Promise} - the promise for the http request + */ + listProjectColumns(id, cb) { + return this._request('GET', `/repos/${this.__fullname}/projects/${id}/columns`, null, cb); + } + + /** + * Get information about a column + * @see https://developer.github.com/v3/repos/projects/#get-a-column + * @param {string} id - the id of the column + * @param {Requestable.callback} cb - will receive the column information + * @return {Promise} - the promise for the http request + */ + getProjectColumn(id, cb) { + return this._request('GET', `/repos/${this.__fullname}/projects/columns/${id}`, null, cb) + } + + /** + * Create a new column + * @see https://developer.github.com/v3/repos/projects/#create-a-column + * @param {string} id - the project id + * @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(id, options, cb) { + return this._request('POST', `/repos/${this.__fullname}/projects/${id}/columns`, options, cb); + } + + /** + * Edit a column + * @see https://developer.github.com/v3/repos/projects/#update-a-column + * @param {string} id - 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(id, options, cb) { + return this._request('PATCH', `/repos/${this.__fullname}/projects/columns/${id}`, options, cb); + } + + /** + * Delete a column + * @see https://developer.github.com/v3/repos/projects/#delete-a-column + * @param {string} id - 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(id, cb) { + return this._request('DELETE', `/repos/${this.__fullname}/projects/columns/${id}`, null, cb); + } + + /** + * Move a column + * @see https://developer.github.com/v3/repos/projects/#move-a-column + * @param {string} id - the column to be moved + * @param {string} position - can be one of first, last, or after:, where 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(id, position, cb) { + return this._request('POST', `/repos/${this.__fullname}/projects/columns/${id}/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} id - the id of the column + * @param {Requestable.callback} [cb] - will receive the list of cards + * @return {Promise} - the promise for the http request + */ + listProjectCards(id, cb) { + return this._request('GET', `/repos/${this.__fullname}/projects/columns/${id}/cards`, null, cb); + } + + /** + * Get information about a card + * @see https://developer.github.com/v3/repos/projects/#list-a-project-card + * @param {string} id - the id of the card + * @param {Requestable.callback} cb - will receive the card information + * @return {Promise} - the promise for the http request + */ + getProjectCard(id, cb) { + return this._request('GET', `/repos/${this.__fullname}/projects/columns/cards/${id}`, null, cb) + } + + /** + * Create a new card + * @see https://developer.github.com/v3/repos/projects/#create-a-project-card + * @param {string} id - 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(id, options, cb) { + return this._request('POST', `/repos/${this.__fullname}/projects/columns/${id}/cards`, options, cb); + } + + /** + * Edit a card + * @see https://developer.github.com/v3/repos/projects/#update-a-project-card + * @param {string} id - 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(id, options, cb) { + return this._request('PATCH', `/repos/${this.__fullname}/projects/columns/cards/${id}`, options, cb); + } + + /** + * Delete a card + * @see https://developer.github.com/v3/repos/projects/#delete-a-project-card + * @param {string} id - 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(id, cb) { + return this._request('DELETE', `/repos/${this.__fullname}/projects/columns/cards/${id}`, null, cb); + } + + /** + * Move a card + * @see https://developer.github.com/v3/repos/projects/#move-a-project-card + * @param {string} id - the card to be moved + * @param {string} position - can be one of top, bottom, or after:, where is the id value of a card in the same project. + * @param {string} column_id - 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(id, position, col_id, cb) { + return this._request('POST', `/repos/${this.__fullname}/projects/columns/${id}/moves`, {"position": position, "column_id": column_id}, cb); + } + } module.exports = Repository; From b415a91af2fbc61555cb166bcf8d2ae480010341 Mon Sep 17 00:00:00 2001 From: klarkc Date: Thu, 22 Sep 2016 11:15:49 -0300 Subject: [PATCH 07/34] add reading tests --- test/repository.spec.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/test/repository.spec.js b/test/repository.spec.js index f747878d..1203504b 100644 --- a/test/repository.spec.js +++ b/test/repository.spec.js @@ -240,6 +240,45 @@ describe('Repository', function() { done(); })); }); + + it('should list repo projects', function(done) { + remoteRepo.listProjects(assertSuccessful(done, function(err, projects) { + expect(projects).to.be.an.array(); + expect(projects.length).to.equal(1); + })); + }); + + it('should get repo project', function(done) { + remoteRepo.getProject(1, assertSuccessful(done, function(err, project) { + expect(project).to.own('name', 'test-project'); + })); + }); + + it('should list repo project columns', function(done) { + remoteRepo.listProjectColumns(1, assertSuccessful(done, function(err, columns) { + expect(columns).to.be.an.array(); + expect(columns.length).to.equal(1); + })); + }); + + it('should get repo project column', function(done) { + remoteRepo.getProjectColumn(1, assertSuccessful(done, function(err, project) { + expect(project).to.own('name', 'test-column'); + })); + }); + + it('should list repo project cards', function(done) { + remoteRepo.listProjectCards(1, assertSuccessful(done, function(err, cards) { + expect(cards).to.be.an.array(); + expect(cards.length).to.equal(1); + })); + }); + + it('should get repo project card', function(done) { + remoteRepo.getProjectColumn(1, assertSuccessful(done, function(err, card) { + expect(card).to.own('note', 'test-card'); + })); + }); }); describe('creating/modifiying', function() { From 2384038deadb4ca38b864c7d6508330bc409a61f Mon Sep 17 00:00:00 2001 From: klarkc Date: Thu, 22 Sep 2016 11:37:44 -0300 Subject: [PATCH 08/34] fix some lint errors --- lib/Repository.js | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/Repository.js b/lib/Repository.js index b00af23a..763a2af4 100644 --- a/lib/Repository.js +++ b/lib/Repository.js @@ -822,7 +822,7 @@ class Repository extends Requestable { * @return {Promise} - the promise for the http request */ getProject(id, cb) { - return this._request('GET', `/repos/${this.__fullname}/projects/${id}`, null, cb) + return this._request('GET', `/repos/${this.__fullname}/projects/${id}`, null, cb); } /** @@ -878,7 +878,7 @@ class Repository extends Requestable { * @return {Promise} - the promise for the http request */ getProjectColumn(id, cb) { - return this._request('GET', `/repos/${this.__fullname}/projects/columns/${id}`, null, cb) + return this._request('GET', `/repos/${this.__fullname}/projects/columns/${id}`, null, cb); } /** @@ -920,12 +920,18 @@ class Repository extends Requestable { * Move a column * @see https://developer.github.com/v3/repos/projects/#move-a-column * @param {string} id - the column to be moved - * @param {string} position - can be one of first, last, or after:, where is the id value of a column in the same project. + * @param {string} position - can be one of first, last, or after:, + * where 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(id, position, cb) { - return this._request('POST', `/repos/${this.__fullname}/projects/columns/${id}/moves`, {"position": position}, cb); + return this._request( + 'POST', + `/repos/${this.__fullname}/projects/columns/${id}/moves`, + {"position": position}, + cb + ); } /** @@ -947,7 +953,7 @@ class Repository extends Requestable { * @return {Promise} - the promise for the http request */ getProjectCard(id, cb) { - return this._request('GET', `/repos/${this.__fullname}/projects/columns/cards/${id}`, null, cb) + return this._request('GET', `/repos/${this.__fullname}/projects/columns/cards/${id}`, null, cb); } /** @@ -989,13 +995,19 @@ class Repository extends Requestable { * Move a card * @see https://developer.github.com/v3/repos/projects/#move-a-project-card * @param {string} id - the card to be moved - * @param {string} position - can be one of top, bottom, or after:, where is the id value of a card in the same project. - * @param {string} column_id - the id value of a column in the same project. + * @param {string} position - can be one of top, bottom, or after:, + * where 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(id, position, col_id, cb) { - return this._request('POST', `/repos/${this.__fullname}/projects/columns/${id}/moves`, {"position": position, "column_id": column_id}, cb); + moveProjectCard(id, position, colId, cb) { + return this._request( + 'POST', + `/repos/${this.__fullname}/projects/columns/${id}/moves`, + {"position": position, "column_id": colId}, + cb + ); } } From 7721158f24d00980afeb896d12d9113c5bf98d58 Mon Sep 17 00:00:00 2001 From: klarkc Date: Thu, 22 Sep 2016 13:10:22 -0300 Subject: [PATCH 09/34] change variable id to number in some project functions --- lib/Repository.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/Repository.js b/lib/Repository.js index 763a2af4..4eec007d 100644 --- a/lib/Repository.js +++ b/lib/Repository.js @@ -817,12 +817,12 @@ class Repository extends Requestable { /** * Get information about a project * @see https://developer.github.com/v3/repos/projects/#list-a-project - * @param {string} id - the id of the project + * @param {string} number - the number of the project * @param {Requestable.callback} cb - will receive the project information * @return {Promise} - the promise for the http request */ - getProject(id, cb) { - return this._request('GET', `/repos/${this.__fullname}/projects/${id}`, null, cb); + getProject(number, cb) { + return this._request('GET', `/repos/${this.__fullname}/projects/${number}`, null, cb); } /** @@ -839,24 +839,24 @@ class Repository extends Requestable { /** * Edit a project * @see https://developer.github.com/v3/repos/projects/#update-a-project - * @param {string} id - the id of the project + * @param {string} number - 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(id, options, cb) { - return this._request('PATCH', `/repos/${this.__fullname}/projects/${id}`, options, cb); + updateProject(number, options, cb) { + return this._request('PATCH', `/repos/${this.__fullname}/projects/${number}`, options, cb); } /** * Delete a project * @see https://developer.github.com/v3/repos/projects/#delete-a-project - * @param {string} id - the project to be deleted + * @param {string} number - 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(id, cb) { - return this._request('DELETE', `/repos/${this.__fullname}/projects/${id}`, null, cb); + deleteProject(number, cb) { + return this._request('DELETE', `/repos/${this.__fullname}/projects/${number}`, null, cb); } /** From e84f05008e4d77d91bdcb350cd1c6d65c425e218 Mon Sep 17 00:00:00 2001 From: klarkc Date: Thu, 22 Sep 2016 13:13:38 -0300 Subject: [PATCH 10/34] change more id parameters to number --- lib/Repository.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/Repository.js b/lib/Repository.js index 4eec007d..2a0ffb1f 100644 --- a/lib/Repository.js +++ b/lib/Repository.js @@ -862,12 +862,12 @@ class Repository extends Requestable { /** * Get information about all columns of a project * @see https://developer.github.com/v3/repos/projects/#list-columns - * @param {string} id - the id of the project + * @param {string} number - the number of the project * @param {Requestable.callback} [cb] - will receive the list of columns * @return {Promise} - the promise for the http request */ - listProjectColumns(id, cb) { - return this._request('GET', `/repos/${this.__fullname}/projects/${id}/columns`, null, cb); + listProjectColumns(number, cb) { + return this._request('GET', `/repos/${this.__fullname}/projects/${number}/columns`, null, cb); } /** @@ -884,13 +884,13 @@ class Repository extends Requestable { /** * Create a new column * @see https://developer.github.com/v3/repos/projects/#create-a-column - * @param {string} id - the project id + * @param {string} number - 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(id, options, cb) { - return this._request('POST', `/repos/${this.__fullname}/projects/${id}/columns`, options, cb); + createProjectColumn(number, options, cb) { + return this._request('POST', `/repos/${this.__fullname}/projects/${number}/columns`, options, cb); } /** From 156e1b72d4447726fd0c178db767e8899d491e56 Mon Sep 17 00:00:00 2001 From: klarkc Date: Thu, 22 Sep 2016 13:14:26 -0300 Subject: [PATCH 11/34] add preview support to __getRequestHeaders --- lib/Requestable.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/Requestable.js b/lib/Requestable.js index 95c23bbd..6fc573e6 100644 --- a/lib/Requestable.js +++ b/lib/Requestable.js @@ -88,14 +88,20 @@ 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) { + headers.Accept = (raw) ? 'application/vnd.github.inertia-preview.raw+json' : '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; } @@ -152,7 +158,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); From 7c7d036d406b683dd0c1dcfa2a89e84a2ff24be5 Mon Sep 17 00:00:00 2001 From: klarkc Date: Thu, 22 Sep 2016 13:27:57 -0300 Subject: [PATCH 12/34] change parameters to avoid errors --- lib/Repository.js | 90 +++++++++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/lib/Repository.js b/lib/Repository.js index 2a0ffb1f..832dcc89 100644 --- a/lib/Repository.js +++ b/lib/Repository.js @@ -817,12 +817,12 @@ class Repository extends Requestable { /** * Get information about a project * @see https://developer.github.com/v3/repos/projects/#list-a-project - * @param {string} number - the number of the 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(number, cb) { - return this._request('GET', `/repos/${this.__fullname}/projects/${number}`, null, cb); + getProject(projectNumber, cb) { + return this._request('GET', `/repos/${this.__fullname}/projects/${projectNumber}`, null, cb); } /** @@ -839,96 +839,96 @@ class Repository extends Requestable { /** * Edit a project * @see https://developer.github.com/v3/repos/projects/#update-a-project - * @param {string} number - the number of the 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(number, options, cb) { - return this._request('PATCH', `/repos/${this.__fullname}/projects/${number}`, options, cb); + 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} number - the project to be deleted + * @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(number, cb) { - return this._request('DELETE', `/repos/${this.__fullname}/projects/${number}`, null, cb); + 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} number - the number of the project + * @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(number, cb) { - return this._request('GET', `/repos/${this.__fullname}/projects/${number}/columns`, null, cb); + 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} id - the id of the 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(id, cb) { - return this._request('GET', `/repos/${this.__fullname}/projects/columns/${id}`, null, cb); + 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} number - the project number + * @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(number, options, cb) { - return this._request('POST', `/repos/${this.__fullname}/projects/${number}/columns`, options, cb); + 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} id - the column id + * @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(id, options, cb) { - return this._request('PATCH', `/repos/${this.__fullname}/projects/columns/${id}`, options, cb); + 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} id - the column to be deleted + * @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(id, cb) { - return this._request('DELETE', `/repos/${this.__fullname}/projects/columns/${id}`, null, cb); + 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} id - the column to be moved + * @param {string} colId - the column to be moved * @param {string} position - can be one of first, last, or after:, * where 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(id, position, cb) { + moveProjectColumn(colId, position, cb) { return this._request( 'POST', - `/repos/${this.__fullname}/projects/columns/${id}/moves`, + `/repos/${this.__fullname}/projects/columns/${colId}/moves`, {"position": position}, cb ); @@ -937,74 +937,74 @@ class Repository extends Requestable { /** * Get information about all cards of a column * @see https://developer.github.com/v3/repos/projects/#list-projects-cards - * @param {string} id - the id of the column + * @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(id, cb) { - return this._request('GET', `/repos/${this.__fullname}/projects/columns/${id}/cards`, null, cb); + 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} id - the id of the 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(id, cb) { - return this._request('GET', `/repos/${this.__fullname}/projects/columns/cards/${id}`, null, cb); + 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} id - the column id + * @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(id, options, cb) { - return this._request('POST', `/repos/${this.__fullname}/projects/columns/${id}/cards`, options, cb); + 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} id - the card id + * @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(id, options, cb) { - return this._request('PATCH', `/repos/${this.__fullname}/projects/columns/cards/${id}`, options, cb); + 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} id - the card to be deleted + * @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(id, cb) { - return this._request('DELETE', `/repos/${this.__fullname}/projects/columns/cards/${id}`, null, cb); + 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} id - the card to be moved + * @param {string} cardId - the card to be moved * @param {string} position - can be one of top, bottom, or after:, * where 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(id, position, colId, cb) { + moveProjectCard(cardId, position, colId, cb) { return this._request( 'POST', - `/repos/${this.__fullname}/projects/columns/${id}/moves`, + `/repos/${this.__fullname}/projects/columns/${cardId}/moves`, {"position": position, "column_id": colId}, cb ); From 80855f3c8a418ba97d439899b2b27cde85bee8fa Mon Sep 17 00:00:00 2001 From: klarkc Date: Thu, 22 Sep 2016 13:28:12 -0300 Subject: [PATCH 13/34] add projects tests --- test/repository.spec.js | 169 ++++++++++++++++++++++++++++++---------- 1 file changed, 130 insertions(+), 39 deletions(-) diff --git a/test/repository.spec.js b/test/repository.spec.js index 1203504b..9917abb8 100644 --- a/test/repository.spec.js +++ b/test/repository.spec.js @@ -240,45 +240,6 @@ describe('Repository', function() { done(); })); }); - - it('should list repo projects', function(done) { - remoteRepo.listProjects(assertSuccessful(done, function(err, projects) { - expect(projects).to.be.an.array(); - expect(projects.length).to.equal(1); - })); - }); - - it('should get repo project', function(done) { - remoteRepo.getProject(1, assertSuccessful(done, function(err, project) { - expect(project).to.own('name', 'test-project'); - })); - }); - - it('should list repo project columns', function(done) { - remoteRepo.listProjectColumns(1, assertSuccessful(done, function(err, columns) { - expect(columns).to.be.an.array(); - expect(columns.length).to.equal(1); - })); - }); - - it('should get repo project column', function(done) { - remoteRepo.getProjectColumn(1, assertSuccessful(done, function(err, project) { - expect(project).to.own('name', 'test-column'); - })); - }); - - it('should list repo project cards', function(done) { - remoteRepo.listProjectCards(1, assertSuccessful(done, function(err, cards) { - expect(cards).to.be.an.array(); - expect(cards.length).to.equal(1); - })); - }); - - it('should get repo project card', function(done) { - remoteRepo.getProjectColumn(1, assertSuccessful(done, function(err, card) { - expect(card).to.own('note', 'test-card'); - })); - }); }); describe('creating/modifiying', function() { @@ -304,6 +265,9 @@ describe('Repository', function() { const releaseBody = 'This is my 49 character long release description.'; let sha; let releaseId; + let projectNumber; + let columnId; + let cardId; before(function() { user = github.getUser(); @@ -628,6 +592,133 @@ describe('Repository', function() { it('should delete a release', function(done) { remoteRepo.deleteRelease(releaseId, assertSuccessful(done)); }); + + it('should create a project', function(done) { + remoteRepo.createProject({ + name: 'test-project', + body: 'body' + }, assertSuccessful(done, function(err, project){ + expect(project).to.own('name', 'test-project'); + expect(project).to.own('body', 'body'); + projectNumber = project.number; + done(); + })); + }); + + it('should list repo projects', function(done) { + remoteRepo.listProjects(assertSuccessful(done, function(err, projects) { + expect(projects).to.be.an.array(); + expect(projects.length).to.equal(1); + done(); + })); + }); + + it('should get repo project', function(done) { + remoteRepo.getProject(projectNumber, assertSuccessful(done, function(err, project) { + expect(project).to.own('name', 'test-project'); + done(); + })); + }); + + it('should update a project', function(done) { + remoteRepo.updateProject(projectNumber, { + name: 'another-test-project', + body: 'another-body' + }, assertSuccessful(done, function(err, project){ + expect(project).to.own('name', 'another-test-project'); + expect(project).to.own('body', 'another-body'); + done(); + })); + }); + + it('should create a repo project column', function(done) { + remoteRepo.createProjectColumn(projectNumber, { + name: 'test-column' + }, assertSuccessful(done, function(err, column) { + expect(column).to.own('name', 'test-column'); + columnId = column.id; + done(); + })); + }); + + it('should list repo project columns', function(done) { + remoteRepo.listProjectColumns(projectNumber, assertSuccessful(done, function(err, columns) { + expect(columns).to.be.an.array(); + expect(columns.length).to.equal(1); + done(); + })); + }); + + it('should get repo project column', function(done) { + remoteRepo.getProjectColumn(columnId, assertSuccessful(done, function(err, project) { + expect(project).to.own('name', 'test-column'); + done(); + })); + }); + + it('should update a repo project column', function(done) { + remoteRepo.updateProjectColumn(columnId, { + name: 'another-test-column' + }, assertSuccessful(done, function(err, column) { + expect(column).to.own('name', 'another-test-column'); + done(); + })); + }); + + it('should create repo project card', function(done){ + remoteRepo.createProjectCard(columnId, { + note: "test-card" + }, assertSuccessful(done, function(err, card) { + expect(card).to.own('note', 'test-card'); + cardId = card.id; + done(); + })); + }); + + it('should list repo project cards', function(done) { + remoteRepo.listProjectCards(columnId, assertSuccessful(done, function(err, cards) { + expect(cards).to.be.an.array(); + expect(cards.length).to.equal(1); + done(); + })); + }); + + it('should get repo project card', function(done) { + remoteRepo.getProjectCard(cardId, assertSuccessful(done, function(err, card) { + expect(card).to.own('note', 'test-card'); + done(); + })); + }); + + it('should update repo project card', function(done) { + remoteRepo.updateProjectCard(cardId, { + note: 'another-test-card' + }, assertSuccessful(done, function(err, card){ + expect(card).to.own('note', 'another-test-card'); + done(); + })); + }); + + it('should delete repo project card', function(done) { + remoteRepo.deleteProjectCard(cardId, assertSuccessful(done, function(err, result){ + expect(result).to.be(true); + done(); + })); + }); + + it('should delete repo project column', function(done) { + remoteRepo.deleteProjectColumn(columnId, assertSuccessful(done, function(err, result){ + expect(result).to.be(true); + done(); + })); + }); + + it('should delete repo project', function(done) { + remoteRepo.deleteProject(projectNumber, assertSuccessful(done, function(err, result){ + expect(result).to.be(true); + done(); + })); + }); }); describe('deleting', function() { From c7323351d3d5fe834c368aed025e2fc8eac945cb Mon Sep 17 00:00:00 2001 From: klarkc Date: Thu, 22 Sep 2016 13:33:24 -0300 Subject: [PATCH 14/34] fix lint fixable errors --- lib/Repository.js | 4 ++-- lib/Requestable.js | 6 ++++-- test/repository.spec.js | 16 ++++++++-------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/Repository.js b/lib/Repository.js index 832dcc89..eba482c6 100644 --- a/lib/Repository.js +++ b/lib/Repository.js @@ -929,7 +929,7 @@ class Repository extends Requestable { return this._request( 'POST', `/repos/${this.__fullname}/projects/columns/${colId}/moves`, - {"position": position}, + {position: position}, cb ); } @@ -1005,7 +1005,7 @@ class Repository extends Requestable { return this._request( 'POST', `/repos/${this.__fullname}/projects/columns/${cardId}/moves`, - {"position": position, "column_id": colId}, + {position: position, column_id: colId}, cb ); } diff --git a/lib/Requestable.js b/lib/Requestable.js index 6fc573e6..bdd11b88 100644 --- a/lib/Requestable.js +++ b/lib/Requestable.js @@ -96,8 +96,10 @@ class Requestable { 'Content-Type': 'application/json;charset=UTF-8' }; - if (preview) { - headers.Accept = (raw) ? 'application/vnd.github.inertia-preview.raw+json' : 'application/vnd.github.inertia-preview+json'; + 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'; } diff --git a/test/repository.spec.js b/test/repository.spec.js index 9917abb8..b4a96773 100644 --- a/test/repository.spec.js +++ b/test/repository.spec.js @@ -597,7 +597,7 @@ describe('Repository', function() { remoteRepo.createProject({ name: 'test-project', body: 'body' - }, assertSuccessful(done, function(err, project){ + }, assertSuccessful(done, function(err, project) { expect(project).to.own('name', 'test-project'); expect(project).to.own('body', 'body'); projectNumber = project.number; @@ -624,7 +624,7 @@ describe('Repository', function() { remoteRepo.updateProject(projectNumber, { name: 'another-test-project', body: 'another-body' - }, assertSuccessful(done, function(err, project){ + }, assertSuccessful(done, function(err, project) { expect(project).to.own('name', 'another-test-project'); expect(project).to.own('body', 'another-body'); done(); @@ -665,9 +665,9 @@ describe('Repository', function() { })); }); - it('should create repo project card', function(done){ + it('should create repo project card', function(done) { remoteRepo.createProjectCard(columnId, { - note: "test-card" + note: 'test-card' }, assertSuccessful(done, function(err, card) { expect(card).to.own('note', 'test-card'); cardId = card.id; @@ -693,28 +693,28 @@ describe('Repository', function() { it('should update repo project card', function(done) { remoteRepo.updateProjectCard(cardId, { note: 'another-test-card' - }, assertSuccessful(done, function(err, card){ + }, assertSuccessful(done, function(err, card) { expect(card).to.own('note', 'another-test-card'); done(); })); }); it('should delete repo project card', function(done) { - remoteRepo.deleteProjectCard(cardId, assertSuccessful(done, function(err, result){ + remoteRepo.deleteProjectCard(cardId, assertSuccessful(done, function(err, result) { expect(result).to.be(true); done(); })); }); it('should delete repo project column', function(done) { - remoteRepo.deleteProjectColumn(columnId, assertSuccessful(done, function(err, result){ + remoteRepo.deleteProjectColumn(columnId, assertSuccessful(done, function(err, result) { expect(result).to.be(true); done(); })); }); it('should delete repo project', function(done) { - remoteRepo.deleteProject(projectNumber, assertSuccessful(done, function(err, result){ + remoteRepo.deleteProject(projectNumber, assertSuccessful(done, function(err, result) { expect(result).to.be(true); done(); })); From ccdc2c0b6c5acb388b1d46d78bc46548c82908b5 Mon Sep 17 00:00:00 2001 From: klarkc Date: Thu, 22 Sep 2016 13:51:22 -0300 Subject: [PATCH 15/34] fix error in url --- lib/Repository.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Repository.js b/lib/Repository.js index eba482c6..ccd75f45 100644 --- a/lib/Repository.js +++ b/lib/Repository.js @@ -1004,7 +1004,7 @@ class Repository extends Requestable { moveProjectCard(cardId, position, colId, cb) { return this._request( 'POST', - `/repos/${this.__fullname}/projects/columns/${cardId}/moves`, + `/repos/${this.__fullname}/projects/columns/cards/${cardId}/moves`, {position: position, column_id: colId}, cb ); From 36f8cd2af1973f621db0e2befc29d59dcb1fe5aa Mon Sep 17 00:00:00 2001 From: klarkc Date: Thu, 22 Sep 2016 14:10:15 -0300 Subject: [PATCH 16/34] add better response error detection --- lib/Requestable.js | 8 +++++++- package.json | 3 +-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/Requestable.js b/lib/Requestable.js index bdd11b88..b7f5ecc4 100644 --- a/lib/Requestable.js +++ b/lib/Requestable.js @@ -191,7 +191,13 @@ 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 { + // True when success + cb(null, (response.status < 300), response); + } }); } diff --git a/package.json b/package.json index c7aacc35..d112a372 100644 --- a/package.json +++ b/package.json @@ -42,8 +42,7 @@ } }, "files": [ - "dist/*", - "lib/*" + "dist/*" ], "dependencies": { "axios": "^0.10.0", From 87fdcf72cce4c89826317d240f5a9e4a1df5706c Mon Sep 17 00:00:00 2001 From: klarkc Date: Thu, 22 Sep 2016 14:10:29 -0300 Subject: [PATCH 17/34] add missing tests --- test/repository.spec.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/repository.spec.js b/test/repository.spec.js index b4a96773..6657799b 100644 --- a/test/repository.spec.js +++ b/test/repository.spec.js @@ -699,6 +699,20 @@ describe('Repository', function() { })); }); + it('should move repo project card', function(done) { + remoteRepo.moveProjectCard(cardId, 'top', columnId, assertSuccessful(done, function(err, result) { + expect(result).to.be(true); + done(); + })); + }); + + it('should move repo project column', function(done) { + remoteRepo.moveProjectColumn(columnId, 'first', assertSuccessful(done, function(err, result) { + expect(result).to.be(true); + done(); + })); + }); + it('should delete repo project card', function(done) { remoteRepo.deleteProjectCard(cardId, assertSuccessful(done, function(err, result) { expect(result).to.be(true); From b7b6e8ee23568a829d68a557503841a8225b08f6 Mon Sep 17 00:00:00 2001 From: klarkc Date: Thu, 22 Sep 2016 14:11:12 -0300 Subject: [PATCH 18/34] fix lint errors --- lib/Requestable.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Requestable.js b/lib/Requestable.js index b7f5ecc4..a4a988b7 100644 --- a/lib/Requestable.js +++ b/lib/Requestable.js @@ -191,7 +191,7 @@ class Requestable { if (cb) { requestPromise.then((response) => { - if(response.data && Object.keys(response.data).length > 0) { + if (response.data && Object.keys(response.data).length > 0) { // When data has results cb(null, response.data, response); } else { From 8bde56be0df76fd78130df6b6f5a3e4bdb46ca1b Mon Sep 17 00:00:00 2001 From: klarkc Date: Fri, 23 Sep 2016 10:25:07 -0300 Subject: [PATCH 19/34] fix lint errors --- lib/Repository.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/Repository.js b/lib/Repository.js index ccd75f45..cad8e089 100644 --- a/lib/Repository.js +++ b/lib/Repository.js @@ -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); } @@ -1002,12 +1005,14 @@ class Repository extends Requestable { * @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}, + {'position': position, 'column_id': colId}, cb ); + /* eslint-enable */ } } From 2092a45073146a1cdebe43ef4ed6949ea9e91e08 Mon Sep 17 00:00:00 2001 From: klarkc Date: Fri, 23 Sep 2016 18:31:00 -0300 Subject: [PATCH 20/34] add clearRepo test helper --- test/helpers/clearRepo.js | 51 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 test/helpers/clearRepo.js diff --git a/test/helpers/clearRepo.js b/test/helpers/clearRepo.js new file mode 100644 index 00000000..d8e4ffa7 --- /dev/null +++ b/test/helpers/clearRepo.js @@ -0,0 +1,51 @@ +function getNextPage(linksHeader = '') { + const links = linksHeader.split(/\s*,\s*/); // splits and strips the urls + return links.reduce(function(nextUrl, link) { + if (link.search(/rel="next"/) !== -1) { + return (link.match(/<(.*)>/) || [])[1]; + } + + return nextUrl; + }, undefined); +} + +function deleteRepo(repo, github) { + return new Promise((resolve, reject) => { + github + .getRepo(repo.owner.login, repo.name) + .deleteRepo() + .then((removed) => { + if(removed) console.log(repo.full_name, 'deleted'); + resolve(); + }); + }); +} + +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); +} From 152be9b83c44012c012a9f28ce3e359603902ea8 Mon Sep 17 00:00:00 2001 From: klarkc Date: Fri, 23 Sep 2016 18:32:43 -0300 Subject: [PATCH 21/34] clear repo before user tests --- test/user.spec.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/user.spec.js b/test/user.spec.js index 81181389..4c38337a 100644 --- a/test/user.spec.js +++ b/test/user.spec.js @@ -1,18 +1,21 @@ import Github from '../lib/GitHub'; import testUser from './fixtures/user.json'; import {assertSuccessful, assertArray} from './helpers/callbacks'; +import clearRepo from './helpers/clearRepo'; describe('User', function() { let github; let user; - before(function() { + before(function(done) { github = new Github({ username: testUser.USERNAME, password: testUser.PASSWORD, auth: 'basic' }); user = github.getUser(); + + clearRepo(github, done); }); it('should get user repos', function(done) { From e7cb3cdc14975214c6f2538a1b2333f58cfe12be Mon Sep 17 00:00:00 2001 From: klarkc Date: Fri, 23 Sep 2016 18:39:15 -0300 Subject: [PATCH 22/34] change callback parameter of _request to only set true when the request was POST --- lib/Requestable.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/Requestable.js b/lib/Requestable.js index a4a988b7..f54e01a2 100644 --- a/lib/Requestable.js +++ b/lib/Requestable.js @@ -194,9 +194,11 @@ class Requestable { if (response.data && Object.keys(response.data).length > 0) { // When data has results cb(null, response.data, response); - } else { - // True when success + } else if(config.method === 'POST'){ + // True when success a POST request cb(null, (response.status < 300), response); + } else { + cb(null, response.data, response); } }); } From 010cf2f0bcb2d237c269ce921874a76ca35bcacf Mon Sep 17 00:00:00 2001 From: klarkc Date: Fri, 23 Sep 2016 18:50:04 -0300 Subject: [PATCH 23/34] change POST to DELETE --- lib/Requestable.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/Requestable.js b/lib/Requestable.js index f54e01a2..23b5f1eb 100644 --- a/lib/Requestable.js +++ b/lib/Requestable.js @@ -191,11 +191,12 @@ class Requestable { if (cb) { requestPromise.then((response) => { + console.log(config.method); if (response.data && Object.keys(response.data).length > 0) { // When data has results cb(null, response.data, response); - } else if(config.method === 'POST'){ - // True when success a POST request + } else if(config.method === 'DELETE'){ + // True when success a DELETE request cb(null, (response.status < 300), response); } else { cb(null, response.data, response); From cdc20f0e84fe1a43a7d3da4f17ca39b35d965e35 Mon Sep 17 00:00:00 2001 From: klarkc Date: Fri, 23 Sep 2016 19:01:26 -0300 Subject: [PATCH 24/34] fix lint errors --- lib/Requestable.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/Requestable.js b/lib/Requestable.js index 23b5f1eb..bf280a4e 100644 --- a/lib/Requestable.js +++ b/lib/Requestable.js @@ -191,11 +191,10 @@ class Requestable { if (cb) { requestPromise.then((response) => { - console.log(config.method); if (response.data && Object.keys(response.data).length > 0) { // When data has results cb(null, response.data, response); - } else if(config.method === 'DELETE'){ + } else if (config.method === 'DELETE') { // True when success a DELETE request cb(null, (response.status < 300), response); } else { From 851a823132088206a4f9c873f90d3138d4fabf0a Mon Sep 17 00:00:00 2001 From: klarkc Date: Tue, 27 Sep 2016 09:43:29 -0300 Subject: [PATCH 25/34] create repo, issue and milestone before reading tests --- test/issue.spec.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/test/issue.spec.js b/test/issue.spec.js index e3d0bb78..08d88cfa 100644 --- a/test/issue.spec.js +++ b/test/issue.spec.js @@ -3,19 +3,37 @@ import expect from 'must'; import Github from '../lib/GitHub'; import testUser from './fixtures/user.json'; import {assertSuccessful} from './helpers/callbacks'; +import getTestRepoName from './helpers/getTestRepoName'; describe('Issue', function() { let github; + const testRepoName = getTestRepoName(); let remoteIssues; - before(function() { + before(function(done) { github = new Github({ username: testUser.USERNAME, password: testUser.PASSWORD, auth: 'basic' }); - remoteIssues = github.getIssues(testUser.USERNAME, 'TestRepo'); + + github + .getUser() + .createRepo({name: testRepoName}) + .then(function(){ + remoteIssues = github.getIssues(testUser.USERNAME, testRepoName); + return remoteIssues.createIssue({ + title: 'Test issue', + body: 'Test issue body' + }); + }) + .then(function(){ + remoteIssues.createMilestone({ + title: 'Default Milestone', + description: 'Test' + }, done); + }); }); describe('reading', function() { From 9600542c1eba0d7b266c99c60d00f91e4f516576 Mon Sep 17 00:00:00 2001 From: klarkc Date: Tue, 27 Sep 2016 10:43:49 -0300 Subject: [PATCH 26/34] change callback behavior in _request function --- lib/Requestable.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Requestable.js b/lib/Requestable.js index bf280a4e..3d9bc3e0 100644 --- a/lib/Requestable.js +++ b/lib/Requestable.js @@ -194,8 +194,8 @@ class Requestable { if (response.data && Object.keys(response.data).length > 0) { // When data has results cb(null, response.data, response); - } else if (config.method === 'DELETE') { - // True when success a DELETE request + } 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); From a47f426cdbec3bb2076d7889cb985d4509781de1 Mon Sep 17 00:00:00 2001 From: klarkc Date: Tue, 27 Sep 2016 12:04:00 -0300 Subject: [PATCH 27/34] create fixed-test-repo-1 when needed --- test/team.spec.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/test/team.spec.js b/test/team.spec.js index 1f712617..ca11a8f1 100644 --- a/test/team.spec.js +++ b/test/team.spec.js @@ -20,7 +20,8 @@ function createTestTeam() { const org = github.getOrganization(testUser.ORGANIZATION); - return org.createTeam({ + return org + .createTeam({ name, privacy: 'closed' }).then(({data: result}) => { @@ -33,14 +34,24 @@ let team; let name; describe('Team', function() { // Isolate tests that are based on a fixed team - before(function() { + before(function(done) { const github = new Github({ username: testUser.USERNAME, password: testUser.PASSWORD, auth: 'basic' }); + const org = github.getOrganization(testUser.ORGANIZATION); team = github.getTeam(2027812); // github-api-tests/fixed-test-team-1 + org + .createRepo({name: 'fixed-test-repo-1'}) + .then(() => { + return team.manageRepo(testUser.ORGANIZATION, 'fixed-test-repo-1'); + }) + .catch(() => { + console.log('skipping fixed-test-repo-1 creation'); + done(); + }); }); it('should get membership for a given user', function() { From 39fd2ffa26644e2b8ab13a88f905af6c5faae43a Mon Sep 17 00:00:00 2001 From: klarkc Date: Tue, 27 Sep 2016 12:11:09 -0300 Subject: [PATCH 28/34] add helperFunctions --- test/helpers/clearRepo.js | 23 +---------------------- test/helpers/helperFunctions.js | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 22 deletions(-) create mode 100644 test/helpers/helperFunctions.js diff --git a/test/helpers/clearRepo.js b/test/helpers/clearRepo.js index d8e4ffa7..566e1ed7 100644 --- a/test/helpers/clearRepo.js +++ b/test/helpers/clearRepo.js @@ -1,25 +1,4 @@ -function getNextPage(linksHeader = '') { - const links = linksHeader.split(/\s*,\s*/); // splits and strips the urls - return links.reduce(function(nextUrl, link) { - if (link.search(/rel="next"/) !== -1) { - return (link.match(/<(.*)>/) || [])[1]; - } - - return nextUrl; - }, undefined); -} - -function deleteRepo(repo, github) { - return new Promise((resolve, reject) => { - github - .getRepo(repo.owner.login, repo.name) - .deleteRepo() - .then((removed) => { - if(removed) console.log(repo.full_name, 'deleted'); - resolve(); - }); - }); -} +import {getNextPage, deleteRepo} from './helperFunctions.js'; module.exports = function(github, cb) { let user = github.getUser(); diff --git a/test/helpers/helperFunctions.js b/test/helpers/helperFunctions.js new file mode 100644 index 00000000..e5243346 --- /dev/null +++ b/test/helpers/helperFunctions.js @@ -0,0 +1,22 @@ +export function getNextPage(linksHeader = '') { + const links = linksHeader.split(/\s*,\s*/); // splits and strips the urls + return links.reduce(function(nextUrl, link) { + if (link.search(/rel="next"/) !== -1) { + return (link.match(/<(.*)>/) || [])[1]; + } + + return nextUrl; + }, undefined); +} + +export function deleteRepo(repo, github) { + return new Promise((resolve, reject) => { + github + .getRepo(repo.owner.login, repo.name) + .deleteRepo() + .then((removed) => { + if(removed) console.log(repo.full_name, 'deleted'); + resolve(); + }); + }); +} From b0d27e65bc8f09df45d39bf89d4413468457b3e7 Mon Sep 17 00:00:00 2001 From: klarkc Date: Tue, 27 Sep 2016 12:29:01 -0300 Subject: [PATCH 29/34] add clearTeams --- test/helpers/clearTeams.js | 32 ++++++++++++++++++++++++++++++++ test/helpers/helperFunctions.js | 12 ++++++++++++ test/organization.spec.js | 4 +++- 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 test/helpers/clearTeams.js diff --git a/test/helpers/clearTeams.js b/test/helpers/clearTeams.js new file mode 100644 index 00000000..adebbf34 --- /dev/null +++ b/test/helpers/clearTeams.js @@ -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); +} diff --git a/test/helpers/helperFunctions.js b/test/helpers/helperFunctions.js index e5243346..5b8dbc1c 100644 --- a/test/helpers/helperFunctions.js +++ b/test/helpers/helperFunctions.js @@ -20,3 +20,15 @@ export function deleteRepo(repo, github) { }); }); } + +export function deleteTeam(team, github) { + return new Promise((resolve, reject) => { + github + .getTeam(team.id) + .deleteTeam() + .then((removed) => { + if(removed) console.log('team', team.name, 'deleted'); + resolve(); + }); + }); +} diff --git a/test/organization.spec.js b/test/organization.spec.js index 9d5f75f6..b44cce2a 100644 --- a/test/organization.spec.js +++ b/test/organization.spec.js @@ -4,19 +4,21 @@ import Github from '../lib/GitHub'; import testUser from './fixtures/user.json'; import {assertSuccessful, assertArray} from './helpers/callbacks'; import getTestRepoName from './helpers/getTestRepoName'; +import clearTeams from './helpers/clearTeams'; describe('Organization', function() { let github; const ORG_NAME = 'github-tools'; const MEMBER_NAME = 'clayreimann'; - before(function() { + before(function(done) { github = new Github({ username: testUser.USERNAME, password: testUser.PASSWORD, auth: 'basic' }); + clearTeams(github, testUser.ORGANIZATION, done); }); describe('reading', function() { From 89a90acab63aeae1f29cb32774738c6e9717701d Mon Sep 17 00:00:00 2001 From: klarkc Date: Tue, 27 Sep 2016 12:32:03 -0300 Subject: [PATCH 30/34] fix tests --- test/organization.spec.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/test/organization.spec.js b/test/organization.spec.js index b44cce2a..a78682b1 100644 --- a/test/organization.spec.js +++ b/test/organization.spec.js @@ -78,18 +78,6 @@ describe('Organization', function() { })); }); - // TODO: The longer this is in place the slower it will get if we don't cleanup random test teams - it('should list the teams in the organization', function() { - return organization.getTeams() - .then(({data}) => { - const hasTeam = data.reduce( - (found, member) => member.slug === 'fixed-test-team-1' || found, - false); - - expect(hasTeam).to.be.true(); - }); - }); - it('should create an organization team', function(done) { const options = { name: testRepoName, @@ -103,5 +91,16 @@ describe('Organization', function() { done(); })); }); + + it('should list the teams in the organization', function() { + return organization.getTeams() + .then(({data}) => { + const hasTeam = data.reduce( + (found, member) => member.slug === testRepoName || found, + false); + + expect(hasTeam).to.be.true(); + }); + }); }); }); From 792b726ddd4dbd808a0f13f96f5d949347226b51 Mon Sep 17 00:00:00 2001 From: klarkc Date: Tue, 27 Sep 2016 12:34:36 -0300 Subject: [PATCH 31/34] fix uncatchable before callback --- test/issue.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/issue.spec.js b/test/issue.spec.js index 08d88cfa..a0722d08 100644 --- a/test/issue.spec.js +++ b/test/issue.spec.js @@ -33,7 +33,7 @@ describe('Issue', function() { title: 'Default Milestone', description: 'Test' }, done); - }); + }).catch(done); }); describe('reading', function() { From 6a13220187b5446cf671e4b92dd82a55864a8009 Mon Sep 17 00:00:00 2001 From: klarkc Date: Tue, 27 Sep 2016 13:57:47 -0300 Subject: [PATCH 32/34] fix team test errors --- test/team.spec.js | 55 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/test/team.spec.js b/test/team.spec.js index ca11a8f1..830b2d7c 100644 --- a/test/team.spec.js +++ b/test/team.spec.js @@ -34,7 +34,7 @@ let team; let name; describe('Team', function() { // Isolate tests that are based on a fixed team - before(function(done) { + before(function() { const github = new Github({ username: testUser.USERNAME, password: testUser.PASSWORD, @@ -42,16 +42,53 @@ describe('Team', function() { // Isolate tests that are based on a fixed team }); const org = github.getOrganization(testUser.ORGANIZATION); - team = github.getTeam(2027812); // github-api-tests/fixed-test-team-1 - org - .createRepo({name: 'fixed-test-repo-1'}) - .then(() => { - return team.manageRepo(testUser.ORGANIZATION, 'fixed-test-repo-1'); + // The code below add a fixed-test-repo-1 + let promiseRepo = new Promise((resolve, reject) => { + org + .createRepo({name: 'fixed-test-repo-1'}) + .then(resolve, () => { + console.log('skiped fixed-test-repo-1 creation') + resolve(); + }); + }); + + // The code below add a Fixed Test Team 1 + let promiseTeam = new Promise((resolve, reject) => { + org + .createTeam({ + name: 'Fixed Test Team 1', + repo_names: [testUser.ORGANIZATION + '/fixed-test-repo-1'] + }) + .then(({data: team}) => resolve(team), (err) => { + console.log('skiped Fixed Test Team 1 creation') + // Team already exists, fetch the team + return org.getTeams(); + }) + .then(({data: teams}) => { + let team = teams + .filter((team) => team.name === 'Fixed Test Team 1') + .pop(); + if(team) { + resolve(team) + } else { + reject(new Error('missing Fixed Test Team 1')); + } + }); + }); + + return promiseRepo.then(() => { + return promiseTeam + .then((t) => { + return team = github.getTeam(t.id); }) - .catch(() => { - console.log('skipping fixed-test-repo-1 creation'); - done(); + .then((team) => { + let addUsers = [ + team.addMembership(altUser.USERNAME), + team.addMembership(testUser.USERNAME) + ]; + return Promise.all(addUsers); }); + }); }); it('should get membership for a given user', function() { From 175bffee8559430baa4682b46ddbe2c29fd379a9 Mon Sep 17 00:00:00 2001 From: klarkc Date: Tue, 27 Sep 2016 14:06:24 -0300 Subject: [PATCH 33/34] fix eslint errors --- test/issue.spec.js | 5 ++--- test/team.spec.js | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/test/issue.spec.js b/test/issue.spec.js index a0722d08..5299bee3 100644 --- a/test/issue.spec.js +++ b/test/issue.spec.js @@ -17,18 +17,17 @@ describe('Issue', function() { auth: 'basic' }); - github .getUser() .createRepo({name: testRepoName}) - .then(function(){ + .then(function() { remoteIssues = github.getIssues(testUser.USERNAME, testRepoName); return remoteIssues.createIssue({ title: 'Test issue', body: 'Test issue body' }); }) - .then(function(){ + .then(function() { remoteIssues.createMilestone({ title: 'Default Milestone', description: 'Test' diff --git a/test/team.spec.js b/test/team.spec.js index 830b2d7c..3c74c676 100644 --- a/test/team.spec.js +++ b/test/team.spec.js @@ -42,12 +42,13 @@ describe('Team', function() { // Isolate tests that are based on a fixed team }); const org = github.getOrganization(testUser.ORGANIZATION); + /* eslint-disable no-console */ // The code below add a fixed-test-repo-1 - let promiseRepo = new Promise((resolve, reject) => { + let promiseRepo = new Promise((resolve) => { org .createRepo({name: 'fixed-test-repo-1'}) .then(resolve, () => { - console.log('skiped fixed-test-repo-1 creation') + console.log('skiped fixed-test-repo-1 creation'); resolve(); }); }); @@ -57,10 +58,10 @@ describe('Team', function() { // Isolate tests that are based on a fixed team org .createTeam({ name: 'Fixed Test Team 1', - repo_names: [testUser.ORGANIZATION + '/fixed-test-repo-1'] + repo_names: [testUser.ORGANIZATION + '/fixed-test-repo-1'] // eslint-disable-line camelcase }) - .then(({data: team}) => resolve(team), (err) => { - console.log('skiped Fixed Test Team 1 creation') + .then(({data: team}) => resolve(team), () => { + console.log('skiped Fixed Test Team 1 creation'); // Team already exists, fetch the team return org.getTeams(); }) @@ -68,18 +69,20 @@ describe('Team', function() { // Isolate tests that are based on a fixed team let team = teams .filter((team) => team.name === 'Fixed Test Team 1') .pop(); - if(team) { - resolve(team) + if (team) { + resolve(team); } else { reject(new Error('missing Fixed Test Team 1')); } }); }); + /* eslint-enable no-console */ return promiseRepo.then(() => { return promiseTeam .then((t) => { - return team = github.getTeam(t.id); + team = github.getTeam(t.id); + return team; }) .then((team) => { let addUsers = [ From f61baf684b6249bdea0c8cb656ec4d237de6c8ad Mon Sep 17 00:00:00 2001 From: klarkc Date: Tue, 27 Sep 2016 14:17:50 -0300 Subject: [PATCH 34/34] fix team errors --- test/team.spec.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/test/team.spec.js b/test/team.spec.js index 3c74c676..0a7e725f 100644 --- a/test/team.spec.js +++ b/test/team.spec.js @@ -53,26 +53,26 @@ describe('Team', function() { // Isolate tests that are based on a fixed team }); }); - // The code below add a Fixed Test Team 1 + // The code below add a fixed-test-repo-1 let promiseTeam = new Promise((resolve, reject) => { org .createTeam({ - name: 'Fixed Test Team 1', + name: 'fixed-test-repo-1', repo_names: [testUser.ORGANIZATION + '/fixed-test-repo-1'] // eslint-disable-line camelcase }) .then(({data: team}) => resolve(team), () => { - console.log('skiped Fixed Test Team 1 creation'); + console.log('skiped fixed-test-repo-1 creation'); // Team already exists, fetch the team return org.getTeams(); }) .then(({data: teams}) => { let team = teams - .filter((team) => team.name === 'Fixed Test Team 1') + .filter((team) => team.name === 'fixed-test-repo-1') .pop(); if (team) { resolve(team); } else { - reject(new Error('missing Fixed Test Team 1')); + reject(new Error('missing fixed-test-repo-1')); } }); }); @@ -85,11 +85,12 @@ describe('Team', function() { // Isolate tests that are based on a fixed team return team; }) .then((team) => { - let addUsers = [ + let setupTeam = [ team.addMembership(altUser.USERNAME), - team.addMembership(testUser.USERNAME) + team.addMembership(testUser.USERNAME), + team.manageRepo(testUser.ORGANIZATION, 'fixed-test-repo-1') ]; - return Promise.all(addUsers); + return Promise.all(setupTeam); }); }); }); @@ -131,7 +132,7 @@ describe('Team', function() { // Isolate tests that are based on a fixed team it('should get team', function() { return team.getTeam() .then(({data}) => { - expect(data.name).to.equal('Fixed Test Team 1'); + expect(data.name).to.equal('fixed-test-repo-1'); }); });