diff --git a/README.md b/README.md index b3c88c7..3450fcc 100644 --- a/README.md +++ b/README.md @@ -276,6 +276,31 @@ A configuration that can be passed to the [bell][bell] OAuth module to authentic #### Expected Promise Response 1. Resolve with a valid [bell][bell] configuration +### getOpenedPRs +The parameters required are: + +| Parameter | Type | Required | Description | +| :------------- | :---- | :------- | :-------------| +| config | Object | true | Configuration Object | +| config.scmUri | String | true | The scm uri (ex: `github.com:1234:branchName`) | +| config.token | String | true | Access token for scm | + +#### Expected Outcome +The list of objects consist of PR names and refs (either a branch or a sha) for the pipeline. For example: +``` +[{ + name: 'PR-5', + ref: '73675432e1288f67332af3ecd0155cf455af1492' +}, { + name: 'PR-6', + ref: 'dfbbc032fa331a95ee5107d1f16e9ff5f7c9d2fa' +}] +``` +#### Expected Promise Response +1. Resolve with the list of objects consists of PR names and refs +2. Reject if the input or output is not valid + + ## Extending To make use of the validation functions, the functions to override are: diff --git a/index.js b/index.js index 8efe4e3..6b6ef14 100644 --- a/index.js +++ b/index.js @@ -272,6 +272,31 @@ class ScmBase { return Promise.reject('Not implemented'); } + /** + * Get list of objects which consists of opened PR names and its ref + * @method getOpenedPRs + * @param {Object} config Configuration + * @param {String} config.scmUri The scmUri to get opened PRs + * @param {String} config.token The token used to authenticate to the SCM + * @return {Promise} + */ + getOpenedPRs(config) { + return validate(config, dataSchema.plugins.scm.getCommitSha) // includes scmUri and token + .then(validConfig => this._getOpenedPRs(validConfig)) + .then(jobList => + validate(jobList, Joi.array().items( + Joi.object().keys({ + name: Joi.reach(dataSchema.models.job.base, 'name').required(), + ref: Joi.string().required() + }) + )) + ); + } + + _getOpenedPRs() { + return Promise.reject('Not implemented'); + } + /** * Return a valid Bell configuration (for OAuth) * @method getBellConfiguration diff --git a/package.json b/package.json index 63f2ef8..95e1a99 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "pretest": "eslint .", - "test": "jenkins-mocha --recursive", + "test": "jenkins-mocha --recursive --timeout 3000", "semantic-release": "semantic-release pre && npm publish && semantic-release post" }, "repository": { diff --git a/test/index.test.js b/test/index.test.js index 9e10431..f325915 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -519,6 +519,48 @@ describe('index test', () => { ); }); + describe('getOpenedPRs', () => { + const config = { + scmUri: 'github.com:repoId:branch', + token: 'token' + }; + + it('returns error when invalid input', () => + instance.getOpenedPRs({}) + .then(assert.fail, (err) => { + assert.instanceOf(err, Error); + assert.equal(err.name, 'ValidationError'); + }) + ); + + it('returns error when invalid output', () => { + instance._getOpenedPRs = () => Promise.resolve({ + invalid: 'stuff' + }); + + return instance.getOpenedPRs(config) + .then(assert.fail, (err) => { + assert.instanceOf(err, Error); + assert.equal(err.name, 'ValidationError'); + }); + }); + + it('returns not implemented', () => + instance.getOpenedPRs(config) + .then(assert.fail, (err) => { + assert.equal(err, 'Not implemented'); + }) + ); + + it('returns job list when no errors', () => { + instance._getOpenedPRs = () => Promise.resolve([{ name: 'PR-1', ref: 'pull/1/merge' }]); + + return instance.getOpenedPRs(config).then( + jobs => assert.deepEqual(jobs, [{ name: 'PR-1', ref: 'pull/1/merge' }]), + assert.fail); + }); + }); + describe('getBellConfiguration', () => { it('returns data from underlying method', () => { instance._getBellConfiguration = () => Promise.resolve({