Skip to content

Commit

Permalink
Merge pull request #37 from screwdriver-cd/openedPRs
Browse files Browse the repository at this point in the history
feat: Add getOpenedPRs to required methods
  • Loading branch information
minzcmu authored Jan 12, 2017
2 parents d1cc707 + 2a94571 commit e24817c
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
25 changes: 25 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
42 changes: 42 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit e24817c

Please sign in to comment.