diff --git a/lib/zenhub.js b/lib/zenhub.js index 74be874..62a2253 100644 --- a/lib/zenhub.js +++ b/lib/zenhub.js @@ -25,7 +25,7 @@ module.exports = ZenHub; * @param object parameters additional parameters * @callback complete * @memberof ZenHub - * @method get + * @method _get */ ZenHub.prototype._get = function(url, parameters, callback) { parameters = extend(parameters, this.credentials); // Add credentials to parameters @@ -52,7 +52,7 @@ ZenHub.prototype._get = function(url, parameters, callback) { * @param object body request body * @callback complete * @memberof ZenHub - * @method get + * @method _post */ ZenHub.prototype._post = function(url, parameters, body, callback) { parameters = extend(parameters, this.credentials); // Add credentials to parameters @@ -68,6 +68,31 @@ ZenHub.prototype._post = function(url, parameters, body, callback) { }); }; +/** + * Helper to handle PUT requests to the API with authorization + * + * @private + * @param string url address part after API root + * @param object parameters additional parameters + * @param object body request body + * @callback complete + * @memberof ZenHub + * @method _put + */ +ZenHub.prototype._put = function(url, parameters, body, callback) { + parameters = extend(parameters, this.credentials); // Add credentials to parameters + var putURL = API_URL + '/' + url + '?' + querystring.stringify(parameters); // Construct URL with parameters + + request.put({ + url: putURL, + strictSSL: true, + json: true, + body: body, + }, function(error, response, body) { + callback(error, body || {}); + }); +}; + /** * Show All Pipelines in a repo board * This method returns all pipelines in a repo board @@ -172,3 +197,19 @@ ZenHub.prototype.convertIssueToEpic = function (repoId, issueId, payload, callba callback(error, body); }); }; + +/** + * Set estimate in issue + * This method set estimate for an issue on ZenHub. + * @param int repoId github id of repository + * @param int issueId github id of issue to convert + * @param object payload contains estimate to set for the issue, see https://github.com/ZenHubIO/API#set-estimate-for-issue for payload format + * @callback complete + * @memberof ZenHub + * @method setEstimateForIssue + */ +ZenHub.prototype.setEstimateForIssue = function (repoId, issueId, payload, callback) { + this._put('repositories/' + repoId + '/issues/' + issueId + '/estimate', {}, payload, function (error, body) { + callback(error, body); + }); +}; diff --git a/test/zenhubWriteTest.js b/test/zenhubWriteTest.js index 6dfbc30..0d7e86d 100644 --- a/test/zenhubWriteTest.js +++ b/test/zenhubWriteTest.js @@ -64,4 +64,19 @@ describe('ZenHub Write API', function() { api.convertIssueToEpic(repoId, issueId, payload, done); }); }); + + describe('Set estimate for issue test', function() { + var issueId = 457; + + it('should send payload to the ZenHub API', function(done) { + var payload = { + estimate: 8 + }; + nock('https://api.zenhub.io/p1') + .put('/repositories/' + repoId + '/issues/' + issueId + '/estimate' + tokenQueryString, payload) + .reply(200, { status: 'OK' }); + api.setEstimateForIssue(repoId, issueId, payload, done); + }); + }); + });