From 363cc6effe867ce6bf96ab38bb616ca5ab5b50f1 Mon Sep 17 00:00:00 2001 From: Min Zhang Date: Mon, 8 May 2017 15:47:33 -0700 Subject: [PATCH] feat: add getPrInfo function --- index.js | 27 ++++++++++++++++++++++++++- test/index.test.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 6b6ef14..9e30e1b 100644 --- a/index.js +++ b/index.js @@ -217,11 +217,12 @@ class ScmBase { } /** - * Get a commit sha for a specific repo#branch + * Get a commit sha for a specific repo#branch or pull request * @method getCommitSha * @param {Object} config Configuration * @param {String} config.scmUri The scmUri to get commit sha of * @param {String} config.token The token used to authenticate to the SCM + * @param {Integer} [config.prNum] The PR number used to fetch the sha * @return {Promise} */ getCommitSha(config) { @@ -310,6 +311,30 @@ class ScmBase { return Promise.reject('Not implemented'); } + /** + * Resolve a pull request object based on the config + * @method getPrRef + * @param {Object} config Configuration + * @param {String} config.scmUri The scmUri to get PR info of + * @param {String} config.token The token used to authenticate to the SCM + * @param {Integer} config.prNum The PR number used to fetch the PR + * @return {Promise} + */ + getPrInfo(config) { + return validate(config, dataSchema.plugins.scm.getCommitSha) // includes scmUri and token + .then(validConfig => this._getPrInfo(validConfig)) + .then(pr => validate(pr, Joi.object().keys({ + name: Joi.reach(dataSchema.models.job.base, 'name').required(), + sha: Joi.reach(dataSchema.models.build.base, 'sha').required(), + ref: Joi.string().required() + })) + ); + } + + _getPrInfo() { + return Promise.reject('Not implemented'); + } + /** * Return statistics on the executor * @method stats diff --git a/test/index.test.js b/test/index.test.js index f325915..30aebfb 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -622,4 +622,32 @@ describe('index test', () => { }) ); }); + + describe('getPrInfo', () => { + const config = { + scmUri: 'github.com:repoId:branch', + token: 'token', + prNum: 123 + }; + + it('returns error when invalid input', () => + instance.getPrInfo({}) + .then(assert.fail, (err) => { + assert.instanceOf(err, Error); + assert.equal(err.name, 'ValidationError'); + }) + ); + + it('returns error when invalid output', () => { + instance._getPrInfo = () => Promise.resolve({ + invalid: 'stuff' + }); + + return instance.getPrInfo(config) + .then(assert.fail, (err) => { + assert.instanceOf(err, Error); + assert.equal(err.name, 'ValidationError'); + }); + }); + }); });