diff --git a/lib/platform/vsts/index.js b/lib/platform/vsts/index.js index 67fc2f0eb47d4b10ffc0cdc806b40fc7d90370bd..521219751ee1472e7d346cfe9b927db29366c305 100644 --- a/lib/platform/vsts/index.js +++ b/lib/platform/vsts/index.js @@ -47,7 +47,7 @@ async function getRepos(token, endpoint) { logger.debug('getRepos(token, endpoint)'); vstsHelper.setTokenAndEndpoint(token, endpoint); const repos = await gitApi().getRepositories(); - return repos.map(repo => repo.name); + return repos.map(repo => `${repo.project.name}/${repo.name}`); } async function initRepo(repoName, token, endpoint) { @@ -57,7 +57,12 @@ async function initRepo(repoName, token, endpoint) { config.fileList = null; config.prList = null; const repos = await gitApi().getRepositories(); - const repo = repos.filter(c => c.name === repoName)[0]; + const names = vstsHelper.getProjectAndRepo(repoName); + const repo = repos.filter( + c => + c.name.toLowerCase() === names.repo.toLowerCase() && + c.project.name.toLowerCase() === names.project.toLowerCase() + )[0]; logger.debug({ repositoryDetails: repo }, 'Repository details'); config.repoId = repo.id; config.privateRepo = true; diff --git a/lib/platform/vsts/vsts-helper.js b/lib/platform/vsts/vsts-helper.js index f81de601f6de0cb540a72effdaca56547d980dcd..eab060472b855c83868fad425c53ab48db5506a2 100644 --- a/lib/platform/vsts/vsts-helper.js +++ b/lib/platform/vsts/vsts-helper.js @@ -13,6 +13,7 @@ module.exports = { max4000Chars, getRenovatePRFormat, getCommitDetails, + getProjectAndRepo, }; /** @@ -261,3 +262,28 @@ async function getCommitDetails(commit, repoId) { const results = await gitApi().getCommit(commit, repoId); return results; } + +/** + * + * @param {string} str + */ +function getProjectAndRepo(str) { + logger.trace(`getProjectAndRepo(${str})`); + const strSplited = str.split(`/`); + if (strSplited.length === 1) { + return { + project: str, + repo: str, + }; + } else if (strSplited.length === 2) { + return { + project: strSplited[0], + repo: strSplited[1], + }; + } + const msg = `${ + str + } can be only structured this way : 'repoName' or 'projectName/repoName'!`; + logger.error(msg); + throw new Error(msg); +} diff --git a/test/config/index.spec.js b/test/config/index.spec.js index 4e306243c01700c178cc84215a6212beaff3d29a..869b7d4de7225cc011e43bf798f7e72e038e9c7b 100644 --- a/test/config/index.spec.js +++ b/test/config/index.spec.js @@ -119,13 +119,23 @@ describe('config/index', () => { gitApi.mockImplementationOnce(() => ({ getRepositories: jest.fn(() => [ { - name: 'a/b', + name: 'repo1', + project: { + name: 'prj1', + }, }, { - name: 'c/d', + name: 'repo2', + project: { + name: 'prj1', + }, }, ]), })); + vstsHelper.getProjectAndRepo.mockImplementationOnce(() => ({ + project: 'prj1', + repo: 'repo1', + })); await configParser.parseConfigs(env, defaultArgv); expect(ghGot.mock.calls.length).toBe(0); expect(get.mock.calls.length).toBe(0); diff --git a/test/platform/vsts/__snapshots__/index.spec.js.snap b/test/platform/vsts/__snapshots__/index.spec.js.snap index 1b1c18bd19fa3228f94a191801111e5eb6914b25..6d45ee369c04b32f445e5639685faee74c470afd 100644 --- a/test/platform/vsts/__snapshots__/index.spec.js.snap +++ b/test/platform/vsts/__snapshots__/index.spec.js.snap @@ -132,8 +132,8 @@ Array [ exports[`platform/vsts getRepos should return an array of repos 2`] = ` Array [ - "a/b", - "c/d", + "prj1/repo1", + "prj1/repo2", ] `; @@ -157,7 +157,7 @@ Object { "privateRepo": true, "repoForceRebase": false, "repoId": "1", - "repoName": "some/repo", + "repoName": "some-repo", } `; diff --git a/test/platform/vsts/__snapshots__/vsts-helper.spec.js.snap b/test/platform/vsts/__snapshots__/vsts-helper.spec.js.snap index 85c4eb8a956637fb38fef39940a95290a30a7e4b..8a2237563a24b75b7ba54ad20646e3def2acabe5 100644 --- a/test/platform/vsts/__snapshots__/vsts-helper.spec.js.snap +++ b/test/platform/vsts/__snapshots__/vsts-helper.spec.js.snap @@ -40,6 +40,20 @@ Object { exports[`platform/vsts/helpers getFile should return the file content because it is not a json 1`] = `"{\\"hello\\"= \\"test\\"}"`; +exports[`platform/vsts/helpers getProjectAndRepo should return the object with project and repo 1`] = ` +Object { + "project": "prjName", + "repo": "myRepoName", +} +`; + +exports[`platform/vsts/helpers getProjectAndRepo should return the object with same strings 1`] = ` +Object { + "project": "myRepoName", + "repo": "myRepoName", +} +`; + exports[`platform/vsts/helpers getRef should get the ref 1`] = ` Array [ Object { diff --git a/test/platform/vsts/index.spec.js b/test/platform/vsts/index.spec.js index f7a965b0afff69e6b67f415afd10ddb31b4452bd..66ea3579bcd5996d60e27b2d434990404003d5e5 100644 --- a/test/platform/vsts/index.spec.js +++ b/test/platform/vsts/index.spec.js @@ -20,10 +20,16 @@ describe('platform/vsts', () => { gitApi.mockImplementationOnce(() => ({ getRepositories: jest.fn(() => [ { - name: 'a/b', + name: 'repo1', + project: { + name: 'prj1', + }, }, { - name: 'c/d', + name: 'repo2', + project: { + name: 'prj1', + }, }, ]), })); @@ -45,20 +51,30 @@ describe('platform/vsts', () => { gitApi.mockImplementationOnce(() => ({ getRepositories: jest.fn(() => [ { - name: 'some/repo', + name: 'some-repo', id: '1', privateRepo: true, isFork: false, defaultBranch: 'defBr', + project: { + name: 'some-repo', + }, }, { - name: 'c/d', + name: 'repo2', + project: { + name: 'prj2', + }, }, ]), })); gitApi.mockImplementationOnce(() => ({ getBranch: jest.fn(() => ({ commit: { commitId: '1234' } })), })); + vstsHelper.getProjectAndRepo.mockImplementationOnce(() => ({ + project: 'some-repo', + repo: 'some-repo', + })); return vsts.initRepo(...args); } @@ -66,7 +82,7 @@ describe('platform/vsts', () => { describe('initRepo', () => { it(`should initialise the config for a repo`, async () => { const config = await initRepo( - 'some/repo', + 'some-repo', 'token', 'https://my.custom.endpoint/' ); @@ -83,7 +99,7 @@ describe('platform/vsts', () => { describe('setBaseBranch(branchName)', () => { it('sets the base branch', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); // getBranchCommit gitApi.mockImplementationOnce(() => ({ getBranch: jest.fn(() => ({ @@ -94,7 +110,7 @@ describe('platform/vsts', () => { expect(gitApi.mock.calls).toMatchSnapshot(); }); it('sets the base branch', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); // getBranchCommit gitApi.mockImplementationOnce(() => ({ getBranch: jest.fn(() => ({ @@ -109,7 +125,7 @@ describe('platform/vsts', () => { describe('getCommitMessages()', () => { it('returns commits messages', async () => { const config = await initRepo( - 'some/repo', + 'some-repo', 'token', 'https://my.custom.endpoint/' ); @@ -125,7 +141,7 @@ describe('platform/vsts', () => { expect(msg).toMatchSnapshot(); }); it('returns empty array if error', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); gitApi.mockImplementationOnce(() => { throw new Error('some error'); }); @@ -136,7 +152,7 @@ describe('platform/vsts', () => { describe('getFile(filePatch, branchName)', () => { it('should return the encoded file content', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); vstsHelper.getFile.mockImplementationOnce(() => `Hello Renovate!`); const content = await vsts.getFile('package.json'); expect(content).toMatchSnapshot(); @@ -237,7 +253,7 @@ describe('platform/vsts', () => { expect(res).toMatchSnapshot(); }); it('returns null if error', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); gitApi.mockImplementationOnce(() => { throw new Error('some error'); }); @@ -248,7 +264,7 @@ describe('platform/vsts', () => { describe('getFileList', () => { it('returns empty array if error', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); gitApi.mockImplementationOnce(() => { throw new Error('some error'); }); @@ -256,7 +272,7 @@ describe('platform/vsts', () => { expect(files).toEqual([]); }); it('caches the result', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); gitApi.mockImplementationOnce(() => ({ getItems: jest.fn(() => [ { path: '/symlinks/package.json' }, @@ -272,7 +288,7 @@ describe('platform/vsts', () => { expect(files.length).toBe(4); }); it('should return the files matching the fileName', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); gitApi.mockImplementationOnce(() => ({ getItems: jest.fn(() => [ { path: '/symlinks/package.json' }, @@ -289,7 +305,7 @@ describe('platform/vsts', () => { describe('commitFilesToBranch(branchName, files, message, parentBranch)', () => { it('should add a new commit to the branch', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); gitApi.mockImplementationOnce(() => ({ createPush: jest.fn(() => true), })); @@ -310,7 +326,7 @@ describe('platform/vsts', () => { expect(gitApi.mock.calls.length).toBe(3); }); it('should add a new commit to an existing branch', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); gitApi.mockImplementationOnce(() => ({ createPush: jest.fn(() => true), })); @@ -334,7 +350,7 @@ describe('platform/vsts', () => { describe('branchExists(branchName)', () => { it('should return false if the branch does not exist', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); vstsHelper.getRefs.mockImplementation(() => []); const exists = await vsts.branchExists('thebranchname'); expect(exists).toBe(false); @@ -343,7 +359,7 @@ describe('platform/vsts', () => { describe('getBranchPr(branchName)', () => { it('should return null if no PR exists', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); gitApi.mockImplementationOnce(() => ({ findPr: jest.fn(() => false), getPr: jest.fn(() => { @@ -354,7 +370,7 @@ describe('platform/vsts', () => { expect(pr).toBe(null); }); it('should return the pr', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); gitApi.mockImplementation(() => ({ getPullRequests: jest.fn(() => [ { @@ -382,17 +398,17 @@ describe('platform/vsts', () => { describe('getBranchStatus(branchName, requiredStatusChecks)', () => { it('return success if requiredStatusChecks null', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); const res = await vsts.getBranchStatus('somebranch', null); expect(res).toEqual('success'); }); it('return failed if unsupported requiredStatusChecks', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); const res = await vsts.getBranchStatus('somebranch', ['foo']); expect(res).toEqual('failed'); }); it('should pass through success', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); gitApi.mockImplementationOnce(() => ({ getBranch: jest.fn(() => ({ aheadCount: 0 })), })); @@ -400,7 +416,7 @@ describe('platform/vsts', () => { expect(res).toEqual('success'); }); it('should pass through failed', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); gitApi.mockImplementationOnce(() => ({ getBranch: jest.fn(() => ({ aheadCount: 123 })), })); @@ -415,7 +431,7 @@ describe('platform/vsts', () => { expect(pr).toBe(null); }); it('should return null if no PR is returned from vsts', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); gitApi.mockImplementationOnce(() => ({ getPullRequests: jest.fn(() => []), })); @@ -423,7 +439,7 @@ describe('platform/vsts', () => { expect(pr).toBe(null); }); it('should return a pr in the right format', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); gitApi.mockImplementationOnce(() => ({ getPullRequests: jest.fn(() => [{ pullRequestId: 1234 }]), })); @@ -437,7 +453,7 @@ describe('platform/vsts', () => { describe('createPr()', () => { it('should create and return a PR object', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); gitApi.mockImplementationOnce(() => ({ createPullRequest: jest.fn(() => ({ pullRequestId: 456, @@ -458,7 +474,7 @@ describe('platform/vsts', () => { expect(pr).toMatchSnapshot(); }); it('should create and return a PR object from base branch', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); gitApi.mockImplementationOnce(() => ({ createPullRequest: jest.fn(() => ({ pullRequestId: 456, @@ -483,7 +499,7 @@ describe('platform/vsts', () => { describe('updatePr(prNo, title, body)', () => { it('should update the PR', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); gitApi.mockImplementationOnce(() => ({ updatePullRequest: jest.fn(), })); @@ -494,7 +510,7 @@ describe('platform/vsts', () => { describe('ensureComment', () => { it('add comment', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); gitApi.mockImplementation(() => ({ createThread: jest.fn(() => [{ id: 123 }]), })); @@ -505,7 +521,7 @@ describe('platform/vsts', () => { describe('isBranchStale', () => { it('should return true', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); gitApi.mockImplementationOnce(() => ({ getBranch: jest.fn(() => ({ commit: { commitId: '123456' } })), })); @@ -516,7 +532,7 @@ describe('platform/vsts', () => { expect(res).toBe(true); }); it('should return false', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); gitApi.mockImplementationOnce(() => ({ getBranch: jest.fn(() => ({ commit: { commitId: '123457' } })), })); @@ -530,7 +546,7 @@ describe('platform/vsts', () => { describe('getAllRenovateBranches()', () => { it('should return all renovate branches', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); gitApi.mockImplementationOnce(() => ({ getBranches: jest.fn(() => [ { name: 'master' }, @@ -545,7 +561,7 @@ describe('platform/vsts', () => { describe('ensureCommentRemoval', () => { it('deletes comment if found', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); gitApi.mockImplementation(() => ({ getThreads: jest.fn(() => [ { comments: [{ content: '### some-subject\n\nblabla' }], id: 123 }, @@ -560,7 +576,7 @@ describe('platform/vsts', () => { expect(gitApi.mock.calls.length).toBe(0); }); it('comment not found', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); gitApi.mockImplementation(() => ({ getThreads: jest.fn(() => [ { comments: [{ content: 'stupid comment' }], id: 123 }, @@ -574,7 +590,7 @@ describe('platform/vsts', () => { describe('getBranchLastCommitTime', () => { it('should return a Date', async () => { - await initRepo('some/repo', 'token'); + await initRepo('some-repo', 'token'); gitApi.mockImplementationOnce(() => ({ getBranch: jest.fn(() => ({ commit: { committer: { date: '1986-11-07T00:00:00Z' } }, diff --git a/test/platform/vsts/vsts-helper.spec.js b/test/platform/vsts/vsts-helper.spec.js index 4e52bb22b5c4338af2d45be06fb19840da4b580f..8f3eb70a502a83d788ae9a513c884899d7fa8419 100644 --- a/test/platform/vsts/vsts-helper.spec.js +++ b/test/platform/vsts/vsts-helper.spec.js @@ -298,4 +298,26 @@ describe('platform/vsts/helpers', () => { expect(res).toMatchSnapshot(); }); }); + + describe('getProjectAndRepo', () => { + it('should return the object with same strings', async () => { + const res = await vstsHelper.getProjectAndRepo('myRepoName'); + expect(res).toMatchSnapshot(); + }); + it('should return the object with project and repo', async () => { + const res = await vstsHelper.getProjectAndRepo('prjName/myRepoName'); + expect(res).toMatchSnapshot(); + }); + it('should return an error', async () => { + let err; + try { + await vstsHelper.getProjectAndRepo('prjName/myRepoName/blalba'); + } catch (error) { + err = error; + } + expect(err.message).toBe( + `prjName/myRepoName/blalba can be only structured this way : 'repoName' or 'projectName/repoName'!` + ); + }); + }); });