Skip to content

Commit

Permalink
fix(1220): Reduce API calls (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
catto authored and minzcmu committed Aug 29, 2018
1 parent 85c21bd commit d9794f2
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 12 deletions.
43 changes: 31 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,10 +594,16 @@ class GithubScm extends Scm {
* @return {Promise} Resolves to string containing contents of file
*/
async _getFile(config) {
const scmInfo = await this.lookupScmUri({
scmUri: config.scmUri,
token: config.token
});
let scmInfo = {};

if (!config.scmInfo) {
scmInfo = await this.lookupScmUri({
scmUri: config.scmUri,
token: config.token
});
} else {
scmInfo = config.scmInfo;
}

try {
const file = await this.breaker.runCommand({
Expand Down Expand Up @@ -752,10 +758,17 @@ class GithubScm extends Scm {
* @return {Promise} Resolves to decorated url object
*/
async _decorateUrl(config) {
const scmInfo = await this.lookupScmUri({
scmUri: config.scmUri,
token: config.token
});
let scmInfo = {};

if (!config.scmInfo) {
scmInfo = await this.lookupScmUri({
scmUri: config.scmUri,
token: config.token
});
} else {
scmInfo = config.scmInfo;
}

const baseUrl = `${scmInfo.host}/${scmInfo.owner}/${scmInfo.repo}`;

return {
Expand Down Expand Up @@ -969,10 +982,16 @@ class GithubScm extends Scm {
* @return {Promise}
*/
async _getPrInfo(config) {
const scmInfo = await this.lookupScmUri({
scmUri: config.scmUri,
token: config.token
});
let scmInfo = {};

if (!config.scmInfo) {
scmInfo = await this.lookupScmUri({
scmUri: config.scmUri,
token: config.token
});
} else {
scmInfo = config.scmInfo;
}

try {
const pullRequestInfo = await this.breaker.runCommand({
Expand Down
90 changes: 90 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,36 @@ jobs:
});
});

it('promises to get content without lookupScmUri when a ref is passed', () => {
const configWithScmInfo = Object.assign({}, config);

githubMock.repos.getContent.yieldsAsync(null, { data: returnData });
configWithScmInfo.scmInfo = {
owner: 'screwdriver-cd',
repo: 'models',
branch: 'branch',
host: 'host'
};

return scm.getFile(configWithScmInfo)
.then((data) => {
assert.deepEqual(data, expectedYaml);

assert.calledWith(githubMock.repos.getContent, {
owner: 'screwdriver-cd',
repo: 'models',
path: config.path,
ref: config.ref
});
assert.calledWith(githubMock.authenticate, {
type: 'oauth',
token: config.token
});

assert.notCalled(githubMock.repos.getById);
});
});

it('promises to get content when a ref is not passed', () => {
githubMock.repos.getContent.yieldsAsync(null, { data: returnData });

Expand Down Expand Up @@ -1385,6 +1415,30 @@ jobs:
});
});

it('decorates a scm uri with scmInfo', () => {
const scmUri = 'github.com:102498:boat';
const scmInfo = {
branch: 'boat',
host: 'github.com',
owner: 'iAm',
repo: 'theCaptain'
};

return scm.decorateUrl({
scmUri,
scmInfo,
token: 'mytokenfortesting'
}).then((data) => {
assert.deepEqual(data, {
branch: 'boat',
name: 'iAm/theCaptain',
url: 'https://github.com/iAm/theCaptain/tree/boat'
});

assert.notCalled(githubMock.repos.getById);
});
});

it('rejects when github lookup fails', () => {
const scmUri = 'github.com:102498:boat';
const testError = new Error('decorateUrlError');
Expand Down Expand Up @@ -1728,6 +1782,42 @@ jobs:
});
});

it('returns a pull request with the given prNum and scmInfo', () => {
const configWithScmInfo = Object.assign({}, config);

githubMock.pullRequests.get.yieldsAsync(null,
{ data: { html_url: 'https://github.com/repoOwner/repoName/pull/1',
number: 1,
head: { sha } } }
);

configWithScmInfo.scmInfo = {
branch: 'branch',
host: 'github.com',
owner: 'repoOwner',
repo: 'repoName'
};

return scm._getPrInfo(configWithScmInfo).then((data) => {
assert.deepEqual(data,
{
name: 'PR-1',
ref: 'pull/1/merge',
sha,
url: 'https://github.com/repoOwner/repoName/pull/1'
}
);

assert.notCalled(githubMock.repos.getById);

assert.calledWith(githubMock.pullRequests.get, {
owner: 'repoOwner',
repo: 'repoName',
number: 1
});
});
});

it('rejects when failing to lookup the SCM URI information', () => {
const testError = new Error('testError');

Expand Down

0 comments on commit d9794f2

Please sign in to comment.