Skip to content

Commit

Permalink
Merge pull request #39 from screwdriver-cd/pr
Browse files Browse the repository at this point in the history
feat: add getPrInfo function
  • Loading branch information
d2lam authored May 8, 2017
2 parents cc985e0 + 363cc6e commit 0f55817
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
27 changes: 26 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
});

0 comments on commit 0f55817

Please sign in to comment.