diff --git a/lib/platform/vsts/index.js b/lib/platform/vsts/index.js index 3133faf91cb1696d119f9eb6169de9aece3d7a64..aae14fcb5b29845a0fa5528d0f9f08bc4990c7e3 100644 --- a/lib/platform/vsts/index.js +++ b/lib/platform/vsts/index.js @@ -49,7 +49,8 @@ module.exports = { async function getRepos(token, endpoint) { logger.debug('getRepos(token, endpoint)'); vstsHelper.setTokenAndEndpoint(token, endpoint); - const repos = await vstsApi.gitApi().getRepositories(); + const vstsApiGit = await vstsApi.gitApi(); + const repos = await vstsApiGit.getRepositories(); return repos.map(repo => `${repo.project.name}/${repo.name}`); } @@ -59,7 +60,8 @@ async function initRepo({ repository, token, endpoint }) { config.repository = repository; config.fileList = null; config.prList = null; - const repos = await vstsApi.gitApi().getRepositories(); + const vstsApiGit = await vstsApi.gitApi(); + const repos = await vstsApiGit.getRepositories(); const names = vstsHelper.getProjectAndRepo(repository); const repo = repos.filter( c => @@ -127,12 +129,11 @@ async function setBaseBranch(branchName) { } async function getBranchCommit(fullBranchName) { - const commit = await vstsApi - .gitApi() - .getBranch( - config.repoId, - vstsHelper.getBranchNameWithoutRefsheadsPrefix(fullBranchName) - ); + const vstsApiGit = await vstsApi.gitApi(); + const commit = await vstsApiGit.getBranch( + config.repoId, + vstsHelper.getBranchNameWithoutRefsheadsPrefix(fullBranchName) + ); return commit.commit.commitId; } @@ -140,7 +141,8 @@ async function getCommitMessages() { logger.debug('getCommitMessages'); try { // @ts-ignore - const res = await vstsApi.gitApi().getCommits(config.repoId); + const vstsApiGit = await vstsApi.gitApi(); + const res = await vstsApiGit.getCommits(config.repoId); const msg = res.map(commit => commit.comment); return msg; } catch (err) { @@ -168,7 +170,8 @@ async function findPr(branchName, prTitle, state = 'all') { logger.debug(`findPr(${branchName}, ${prTitle}, ${state})`); let prsFiltered = []; try { - const prs = await vstsApi.gitApi().getPullRequests(config.repoId, null); + const vstsApiGit = await vstsApi.gitApi(); + const prs = await vstsApiGit.getPullRequests(config.repoId, null); prsFiltered = prs.filter( item => item.sourceRefName === vstsHelper.getNewBranchName(branchName) @@ -207,7 +210,8 @@ async function getFileList(branchName = config.baseBranch) { if (config.fileList) { return config.fileList; } - const items = await vstsApi.gitApi().getItems( + const vstsApiGit = await vstsApi.gitApi(); + const items = await vstsApiGit.getItems( config.repoId, null, null, @@ -261,7 +265,8 @@ async function commitFilesToBranch( parentBranch ); - await vstsApi.gitApi().createPush( + const vstsApiGit = await vstsApi.gitApi(); + await vstsApiGit.createPush( { commits: [ { @@ -312,12 +317,11 @@ async function getBranchStatus(branchName, requiredStatusChecks) { async function getBranchStatusCheck(branchName, context) { logger.trace(`getBranchStatusCheck(${branchName}, ${context})`); - const branch = await vstsApi - .gitApi() - .getBranch( - config.repoId, - vstsHelper.getBranchNameWithoutRefsheadsPrefix(branchName) - ); + const vstsApiGit = await vstsApi.gitApi(); + const branch = await vstsApiGit.getBranch( + config.repoId, + vstsHelper.getBranchNameWithoutRefsheadsPrefix(branchName) + ); if (branch.aheadCount === 0) { return 'success'; } @@ -329,7 +333,8 @@ async function getPr(pullRequestId) { if (!pullRequestId) { return null; } - const prs = await vstsApi.gitApi().getPullRequests(config.repoId, null); + const vstsApiGit = await vstsApi.gitApi(); + const prs = await vstsApiGit.getPullRequests(config.repoId, null); const vstsPr = prs.filter(item => item.pullRequestId === pullRequestId); if (vstsPr.length === 0) { return null; @@ -345,7 +350,8 @@ async function createPr(branchName, title, body, labels, useDefaultBranch) { ? config.defaultBranch : config.baseBranch; const description = vstsHelper.max4000Chars(body); - const pr = await vstsApi.gitApi().createPullRequest( + const vstsApiGit = await vstsApi.gitApi(); + const pr = await vstsApiGit.createPullRequest( { sourceRefName, targetRefName, @@ -359,7 +365,8 @@ async function createPr(branchName, title, body, labels, useDefaultBranch) { async function updatePr(prNo, title, body) { logger.debug(`updatePr(${prNo}, ${title}, body)`); - await vstsApi.gitApi().updatePullRequest( + const vstsApiGit = await vstsApi.gitApi(); + await vstsApiGit.updatePullRequest( // @ts-ignore { title, description: vstsHelper.max4000Chars(body) }, config.repoId, @@ -387,7 +394,8 @@ async function isBranchStale(branchName) { async function ensureComment(issueNo, topic, content) { logger.debug(`ensureComment(${issueNo}, ${topic}, content)`); const body = `### ${topic}\n\n${content}`; - await vstsApi.gitApi().createThread( + const vstsApiGit = await vstsApi.gitApi(); + await vstsApiGit.createThread( { comments: [{ content: body, commentType: 1, parentCommentId: 0 }], status: 1, @@ -400,7 +408,8 @@ async function ensureComment(issueNo, topic, content) { async function ensureCommentRemoval(issueNo, topic) { logger.debug(`ensureCommentRemoval(issueNo, topic)(${issueNo}, ${topic})`); if (issueNo) { - const threads = await vstsApi.gitApi().getThreads(config.repoId, issueNo); + const vstsApiGit = await vstsApi.gitApi(); + const threads = await vstsApiGit.getThreads(config.repoId, issueNo); let threadIdFound = null; threads.forEach(thread => { @@ -410,7 +419,7 @@ async function ensureCommentRemoval(issueNo, topic) { }); if (threadIdFound) { - await vstsApi.gitApi().updateThread( + await vstsApiGit.updateThread( { status: 4, // close }, @@ -424,7 +433,8 @@ async function ensureCommentRemoval(issueNo, topic) { async function getAllRenovateBranches(branchPrefix) { logger.debug(`getAllRenovateBranches(branchPrefix)(${branchPrefix})`); - const branches = await vstsApi.gitApi().getBranches(config.repoId); + const vstsApiGit = await vstsApi.gitApi(); + const branches = await vstsApiGit.getBranches(config.repoId); return branches.filter(c => c.name.startsWith(branchPrefix)).map(c => c.name); } @@ -434,7 +444,8 @@ async function deleteBranch(branchName) { config.repoId, vstsHelper.getNewBranchName(branchName) ); - return vstsApi.gitApi().updateRefs( + const vstsApiGit = await vstsApi.gitApi(); + return vstsApiGit.updateRefs( [ { name: ref[0].name, @@ -450,12 +461,11 @@ async function deleteBranch(branchName) { async function getBranchLastCommitTime(branchName) { logger.debug(`getBranchLastCommitTime(branchName)(${branchName})`); - const branch = await vstsApi - .gitApi() - .getBranch( - config.repoId, - vstsHelper.getBranchNameWithoutRefsheadsPrefix(branchName) - ); + const vstsApiGit = await vstsApi.gitApi(); + const branch = await vstsApiGit.getBranch( + config.repoId, + vstsHelper.getBranchNameWithoutRefsheadsPrefix(branchName) + ); return branch.commit.committer.date; } @@ -505,14 +515,16 @@ async function addAssignees(issueNo, assignees) { */ async function addReviewers(prNo, reviewers) { logger.trace(`addReviewers(${prNo}, ${reviewers})`); - const repos = await vstsApi.gitApi().getRepositories(); + const vstsApiGit = await vstsApi.gitApi(); + const vstsApiCore = await vstsApi.getCoreApi(); + const repos = await vstsApiGit.getRepositories(); const repo = repos.filter(c => c.id === config.repoId)[0]; - const teams = await vstsApi.getCoreApi().getTeams(repo.project.id); + const teams = await vstsApiCore.getTeams(repo.project.id); const members = await Promise.all( teams.map( async t => /* eslint-disable no-return-await */ - await vstsApi.getCoreApi().getTeamMembers(repo.project.id, t.id) + await vstsApiCore.getTeamMembers(repo.project.id, t.id) ) ); @@ -534,9 +546,12 @@ async function addReviewers(prNo, reviewers) { await Promise.all( ids.map(async obj => { - await vstsApi - .gitApi() - .createPullRequestReviewer({}, config.repoId, prNo, obj.id); + await vstsApiGit.createPullRequestReviewer( + {}, + config.repoId, + prNo, + obj.id + ); logger.info(`Reviewer added: ${obj.name}`); }) ); diff --git a/lib/platform/vsts/vsts-got-wrapper.js b/lib/platform/vsts/vsts-got-wrapper.js index 6199439f670758c97432b5616157b2183bfbd489..0deb85599604fb3300058f7ccb97e22618db6897 100644 --- a/lib/platform/vsts/vsts-got-wrapper.js +++ b/lib/platform/vsts/vsts-got-wrapper.js @@ -1,6 +1,7 @@ const vsts = require('vso-node-api'); module.exports = { + vstsObj, gitApi, getCoreApi, }; diff --git a/lib/platform/vsts/vsts-helper.js b/lib/platform/vsts/vsts-helper.js index 5a08686a6925730c8c7221fff4fed43eb483d52d..90f06992d955787c6a0b9b8f392c630689a5d438 100644 --- a/lib/platform/vsts/vsts-helper.js +++ b/lib/platform/vsts/vsts-helper.js @@ -90,9 +90,12 @@ function getBranchNameWithoutRefsPrefix(branchPath) { */ async function getRefs(repoId, branchName) { logger.debug(`getRefs(${repoId}, ${branchName})`); - const refs = await vstsApi - .gitApi() - .getRefs(repoId, null, getBranchNameWithoutRefsPrefix(branchName)); + const vstsApiGit = await vstsApi.gitApi(); + const refs = await vstsApiGit.getRefs( + repoId, + null, + getBranchNameWithoutRefsPrefix(branchName) + ); return refs; } @@ -164,7 +167,8 @@ async function getChanges(files, repoId, repository, branchName) { */ async function getFile(repoId, repository, filePath, branchName) { logger.trace(`getFile(filePath=${filePath}, branchName=${branchName})`); - const item = await vstsApi.gitApi().getItemText( + const vstsApiGit = await vstsApi.gitApi(); + const item = await vstsApiGit.getItemText( repoId, filePath, null, @@ -266,7 +270,8 @@ function getRenovatePRFormat(vstsPr) { async function getCommitDetails(commit, repoId) { logger.debug(`getCommitDetails(${commit}, ${repoId})`); - const results = await vstsApi.gitApi().getCommit(commit, repoId); + const vstsApiGit = await vstsApi.gitApi(); + const results = await vstsApiGit.getCommit(commit, repoId); return results; } diff --git a/package.json b/package.json index 57bcd34cb19370a74e43b67bbe0cae49dd14eeb9..dad33a0e960e5d6b39c9d18825382b03b990dac4 100644 --- a/package.json +++ b/package.json @@ -82,7 +82,7 @@ "tmp-promise": "1.0.4", "traverse": "0.6.6", "upath": "1.0.2", - "vso-node-api": "6.2.8-preview", + "vso-node-api": "6.3.2", "yarn": "1.3.2" }, "devDependencies": { diff --git a/test/platform/vsts/__snapshots__/vsts-got-wrapper.spec.js.snap b/test/platform/vsts/__snapshots__/vsts-got-wrapper.spec.js.snap index 218e421fac101524883a70c42e858dcb6abcb5de..6e84ab82bc8993d8b3674feecae36ca3c375b591 100644 --- a/test/platform/vsts/__snapshots__/vsts-got-wrapper.spec.js.snap +++ b/test/platform/vsts/__snapshots__/vsts-got-wrapper.spec.js.snap @@ -1,76 +1,12 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`platform/vsts/vsts-got-wrapper gitApi should set token and endpoint 1`] = ` -GitApi { - "baseUrl": "myEndpoint", - "http": HttpClient { - "_certConfig": undefined, - "_httpProxy": undefined, - "_ignoreSslError": false, - "_socketTimeout": undefined, - "handlers": Array [ - PersonalAccessTokenCredentialHandler { - "token": "myToken", - }, - ], - "requestOptions": Object {}, - "userAgent": "node-Git-api", +WebApi { + "authHandler": PersonalAccessTokenCredentialHandler { + "token": "myToken", }, - "rest": RestClient { - "client": HttpClient { - "_certConfig": undefined, - "_httpProxy": undefined, - "_ignoreSslError": false, - "_socketTimeout": undefined, - "handlers": Array [ - PersonalAccessTokenCredentialHandler { - "token": "myToken", - }, - ], - "requestOptions": Object {}, - "userAgent": "node-Git-api", - }, - }, - "userAgent": "node-Git-api", - "vsoClient": VsoClient { - "_initializationPromise": Promise {}, - "_locationsByAreaPromises": Object {}, - "basePath": "myEndpoint", - "baseUrl": "myEndpoint", - "restClient": RestClient { - "client": HttpClient { - "_certConfig": undefined, - "_httpProxy": undefined, - "_ignoreSslError": false, - "_socketTimeout": undefined, - "handlers": Array [ - PersonalAccessTokenCredentialHandler { - "token": "myToken", - }, - ], - "requestOptions": Object {}, - "userAgent": "node-Git-api", - }, - }, - }, -} -`; - -exports[`platform/vsts/vsts-got-wrapper gitApi should set token and endpoint 2`] = ` -CoreApi { - "baseUrl": "myEndpoint", - "http": HttpClient { - "_certConfig": undefined, - "_httpProxy": undefined, - "_ignoreSslError": false, - "_socketTimeout": undefined, - "handlers": Array [ - PersonalAccessTokenCredentialHandler { - "token": "myToken", - }, - ], - "requestOptions": Object {}, - "userAgent": "node-Core-api", + "options": Object { + "ignoreSslError": false, }, "rest": RestClient { "client": HttpClient { @@ -83,11 +19,13 @@ CoreApi { "token": "myToken", }, ], - "requestOptions": Object {}, - "userAgent": "node-Core-api", + "requestOptions": Object { + "ignoreSslError": false, + }, + "userAgent": "vsts-node-api", }, }, - "userAgent": "node-Core-api", + "serverUrl": "myEndpoint", "vsoClient": VsoClient { "_initializationPromise": Promise {}, "_locationsByAreaPromises": Object {}, @@ -104,8 +42,10 @@ CoreApi { "token": "myToken", }, ], - "requestOptions": Object {}, - "userAgent": "node-Core-api", + "requestOptions": Object { + "ignoreSslError": false, + }, + "userAgent": "vsts-node-api", }, }, }, diff --git a/test/platform/vsts/index.spec.js b/test/platform/vsts/index.spec.js index e4f390a3e1385b1d00ed3bf08f03b46ccb5d5256..eb0a81f047f550554a793ba36b2edc16dc0bb1a7 100644 --- a/test/platform/vsts/index.spec.js +++ b/test/platform/vsts/index.spec.js @@ -598,7 +598,7 @@ describe('platform/vsts', () => { updateThread: jest.fn(), })); await vsts.ensureCommentRemoval(42, 'some-subject'); - expect(vstsApi.gitApi.mock.calls.length).toBe(4); + expect(vstsApi.gitApi.mock.calls.length).toBe(3); }); it('nothing should happen, no number', async () => { await vsts.ensureCommentRemoval(); @@ -647,22 +647,7 @@ describe('platform/vsts', () => { }); }); - describe('Not supported by VSTS (yet!)', () => { - it('setBranchStatus', () => { - const res = vsts.setBranchStatus(); - expect(res).toBeUndefined(); - }); - - it('mergeBranch', async () => { - const res = await vsts.mergeBranch(); - expect(res).toBeUndefined(); - }); - - it('mergePr', async () => { - const res = await vsts.mergePr(); - expect(res).toBeUndefined(); - }); - + describe('Assignees', () => { it('addAssignees', async () => { await initRepo({ repository: 'some/repo', token: 'token' }); vstsApi.gitApi.mockImplementation(() => ({ @@ -671,7 +656,9 @@ describe('platform/vsts', () => { await vsts.addAssignees(123, ['test@bonjour.fr']); expect(vstsApi.gitApi.mock.calls.length).toBe(3); }); + }); + describe('Reviewers', () => { it('addReviewers', async () => { await initRepo({ repository: 'some/repo', token: 'token' }); vstsApi.gitApi.mockImplementation(() => ({ @@ -685,7 +672,24 @@ describe('platform/vsts', () => { ]), })); await vsts.addReviewers(123, ['test@bonjour.fr', 'jyc']); - expect(vstsApi.gitApi.mock.calls.length).toBe(4); + expect(vstsApi.gitApi.mock.calls.length).toBe(3); + }); + }); + + describe('Not supported by VSTS (yet!)', () => { + it('setBranchStatus', () => { + const res = vsts.setBranchStatus(); + expect(res).toBeUndefined(); + }); + + it('mergeBranch', async () => { + const res = await vsts.mergeBranch(); + expect(res).toBeUndefined(); + }); + + it('mergePr', async () => { + const res = await vsts.mergePr(); + expect(res).toBeUndefined(); }); // to become async? diff --git a/test/platform/vsts/vsts-got-wrapper.spec.js b/test/platform/vsts/vsts-got-wrapper.spec.js index 3ba978392d1cfb3bdfd41d1f6f7c91e52c3a3cab..5237465d55b98a30966d6080498a59c967448680 100644 --- a/test/platform/vsts/vsts-got-wrapper.spec.js +++ b/test/platform/vsts/vsts-got-wrapper.spec.js @@ -24,7 +24,7 @@ describe('platform/vsts/vsts-got-wrapper', () => { let err; try { process.env.VSTS_TOKEN = 'myToken'; - await vsts.gitApi(); + await vsts.getCoreApi(); } catch (e) { err = e; } @@ -35,20 +35,7 @@ describe('platform/vsts/vsts-got-wrapper', () => { it('should set token and endpoint', async () => { process.env.VSTS_TOKEN = 'myToken'; process.env.VSTS_ENDPOINT = 'myEndpoint'; - const res = await vsts.gitApi(); - - // We will track if the lib vso-node-api change - expect(res).toMatchSnapshot(); - expect(process.env.VSTS_TOKEN).toBe(`myToken`); - expect(process.env.VSTS_ENDPOINT).toBe(`myEndpoint`); - }); - }); - - describe('gitApi', () => { - it('should set token and endpoint', async () => { - process.env.VSTS_TOKEN = 'myToken'; - process.env.VSTS_ENDPOINT = 'myEndpoint'; - const res = await vsts.getCoreApi(); + const res = await vsts.vstsObj(); // We will track if the lib vso-node-api change expect(res).toMatchSnapshot(); diff --git a/yarn.lock b/yarn.lock index c3d3786034343273bc65498b7a3904767db484a2..bfca4ffb68f8913c8eb085ef681cf9dd3c2dffcc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7296,9 +7296,9 @@ version-selector-type@^2.0.0: dependencies: semver "^5.4.1" -vso-node-api@6.2.8-preview: - version "6.2.8-preview" - resolved "https://registry.yarnpkg.com/vso-node-api/-/vso-node-api-6.2.8-preview.tgz#99902e626c408716ab90b042705452c88ec1c2f0" +vso-node-api@6.3.2: + version "6.3.2" + resolved "https://registry.yarnpkg.com/vso-node-api/-/vso-node-api-6.3.2.tgz#bc6d4c8a6e0351eee1c0e622dda95f0e929fdd32" dependencies: tunnel "0.0.4" typed-rest-client "^0.12.0"