From d9794f27853e2099e5a771ee12becaa3e95a7dc7 Mon Sep 17 00:00:00 2001 From: Ryosuke Nakayama Date: Thu, 30 Aug 2018 03:08:34 +0900 Subject: [PATCH] fix(1220): Reduce API calls (#101) --- index.js | 43 +++++++++++++++------- test/index.test.js | 90 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 8d31641..70d1841 100644 --- a/index.js +++ b/index.js @@ -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({ @@ -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 { @@ -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({ diff --git a/test/index.test.js b/test/index.test.js index 9f1e0a0..d0caa75 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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 }); @@ -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'); @@ -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');