diff --git a/lib/platform/azure/azure-helper.js b/lib/platform/azure/azure-helper.js index dce52b71daa1c297723015c71d255be367104a1c..a71df18ae10dd819b8291fadb3cd29c3b2c4fef3 100644 --- a/lib/platform/azure/azure-helper.js +++ b/lib/platform/azure/azure-helper.js @@ -213,6 +213,7 @@ function getRenovatePRFormat(azurePr) { pr.displayNumber = `Pull Request #${azurePr.pullRequestId}`; pr.number = azurePr.pullRequestId; + pr.body = azurePr.description; // status // export declare enum PullRequestStatus { diff --git a/lib/platform/azure/index.js b/lib/platform/azure/index.js index ca4c5b2ffdb78094f125ad05affa3cf1ced81505..55d54188ec811680101e4a2c6ad9946ce1617f1d 100644 --- a/lib/platform/azure/index.js +++ b/lib/platform/azure/index.js @@ -3,15 +3,16 @@ const azureHelper = require('./azure-helper'); const azureApi = require('./azure-got-wrapper'); const hostRules = require('../../util/host-rules'); const { appSlug } = require('../../config/app-strings'); +const GitStorage = require('../git/storage'); -const config = {}; +let config = {}; module.exports = { // Initialization getRepos, - cleanRepo: () => undefined, + cleanRepo, initRepo, - getRepoStatus: () => ({}), + getRepoStatus, getRepoForceRebase, setBaseBranch, // Search @@ -65,9 +66,13 @@ async function getRepos(token, endpoint) { return repos.map(repo => `${repo.project.name}/${repo.name}`); } -async function initRepo({ repository, token, endpoint }) { +async function initRepo({ repository, endpoint, localDir }) { logger.debug(`initRepo("${repository}")`); - const opts = hostRules.find({ platform: 'azure' }, { token, endpoint }); + const opts = hostRules.find({ platform: 'azure' }, { endpoint }); + // istanbul ignore next + if (!opts.token) { + throw new Error('No token found for getRepos'); + } hostRules.update({ ...opts, platform: 'azure', default: true }); config.repository = repository; config.fileList = null; @@ -82,12 +87,10 @@ async function initRepo({ repository, token, endpoint }) { )[0]; logger.debug({ repositoryDetails: repo }, 'Repository details'); config.repoId = repo.id; - config.privateRepo = true; - config.isFork = false; config.owner = '?owner?'; logger.debug(`${repository} owner = ${config.owner}`); // Use default branch as PR target unless later overridden - config.defaultBranch = repo.defaultBranch; + config.defaultBranch = repo.defaultBranch.replace('refs/heads/', ''); config.baseBranch = config.defaultBranch; logger.debug(`${repository} default branch = ${config.defaultBranch}`); config.baseCommitSHA = await getBranchCommit(config.baseBranch); @@ -125,53 +128,121 @@ async function initRepo({ repository, token, endpoint }) { // throw err; // } // } - return config; + // Always gitFs + config.storage = new GitStorage(); + const [projectName, repoName] = repository.split('/'); + const url = + endpoint.replace('https://', `https://token:${opts.token}@`) + + `${projectName}/_git/${repoName}`; + await config.storage.initRepo({ + ...config, + localDir, + url, + }); + const platformConfig = { + privateRepo: true, + isFork: false, + }; + return platformConfig; } function getRepoForceRebase() { return false; } +// istanbul ignore next async function setBaseBranch(branchName) { if (branchName) { logger.debug(`Setting baseBranch to ${branchName}`); config.baseBranch = branchName; - config.baseCommitSHA = await getBranchCommit(config.baseBranch); + delete config.baseCommitSHA; + delete config.fileList; + await config.storage.setBaseBranch(branchName); + await getFileList(branchName); } } -async function getBranchCommit(fullBranchName) { - const azureApiGit = await azureApi.gitApi(); - const commit = await azureApiGit.getBranch( - config.repoId, - azureHelper.getBranchNameWithoutRefsheadsPrefix(fullBranchName) - ); - return commit.commit.commitId; +// Search + +// istanbul ignore next +function getFileList(branchName) { + return config.storage.getFileList(branchName); } -async function getCommitMessages() { - logger.debug('getCommitMessages'); - try { - // @ts-ignore - const azureApiGit = await azureApi.gitApi(); - const res = await azureApiGit.getCommits(config.repoId); - const msg = res.map(commit => commit.comment); - return msg; - } catch (err) { - logger.error({ err }, `getCommitMessages error`); - return []; +// Branch + +// istanbul ignore next +function branchExists(branchName) { + return config.storage.branchExists(branchName); +} + +// istanbul ignore next +function getAllRenovateBranches(branchPrefix) { + return config.storage.getAllRenovateBranches(branchPrefix); +} + +// istanbul ignore next +function isBranchStale(branchName) { + return config.storage.isBranchStale(branchName); +} + +// istanbul ignore next +function getFile(filePath, branchName) { + return config.storage.getFile(filePath, branchName); +} + +// istanbul ignore next +async function deleteBranch(branchName, abandonAssociatedPr = false) { + await config.storage.deleteBranch(branchName); + // istanbul ignore if + if (abandonAssociatedPr) { + const pr = await getBranchPr(branchName); + await abandonPr(pr.number); } } -async function getFile(filePath, branchName = config.baseBranch) { - logger.trace(`getFile(filePath=${filePath}, branchName=${branchName})`); - const f = await azureHelper.getFile( +// istanbul ignore next +function getBranchLastCommitTime(branchName) { + return config.storage.getBranchLastCommitTime(branchName); +} + +// istanbul ignore next +function getRepoStatus() { + return config.storage.getRepoStatus(); +} + +// istanbul ignore next +function mergeBranch(branchName) { + return config.storage.mergeBranch(branchName); +} + +// istanbul ignore next +function commitFilesToBranch( + branchName, + files, + message, + parentBranch = config.baseBranch +) { + return config.storage.commitFilesToBranch( + branchName, + files, + message, + parentBranch + ); +} + +// istanbul ignore next +function getCommitMessages() { + return config.storage.getCommitMessages(); +} + +async function getBranchCommit(fullBranchName) { + const azureApiGit = await azureApi.gitApi(); + const commit = await azureApiGit.getBranch( config.repoId, - config.name, - filePath, - branchName + azureHelper.getBranchNameWithoutRefsheadsPrefix(fullBranchName) ); - return f; + return commit.commit.commitId; } function getPrList() { @@ -218,96 +289,6 @@ async function findPr(branchName, prTitle, state = 'all') { return prsFiltered[0]; } -async function getFileList(branchName = config.baseBranch) { - logger.trace(`getFileList('${branchName})'`); - try { - if (config.fileList) { - return config.fileList; - } - const azureApiGit = await azureApi.gitApi(); - const items = await azureApiGit.getItems( - config.repoId, - null, - null, - 120, // full - null, - null, - null, - false - ); - config.fileList = items - .filter(c => !c.isFolder) - .map(c => c.path.substring(1, c.path.length)) - .sort(); - return config.fileList; - } catch (error) { - logger.error(`getFileList('${branchName})'`); - return []; - } -} - -async function commitFilesToBranch( - branchName, - files, - message, - parentBranch = config.baseBranch -) { - logger.debug( - `commitFilesToBranch('${branchName}', files, message, '${parentBranch})'` - ); - - // Create the new Branch - let branchRef = await azureHelper.getAzureBranchObj( - config.repoId, - branchName, - parentBranch - ); - - const isBranchExisting = await branchExists(`refs/heads/${branchName}`); - if (isBranchExisting) { - branchRef = await azureHelper.getAzureBranchObj( - config.repoId, - branchName, - branchName - ); - } - - const changesInCommit = await azureHelper.getChanges( - files, - config.repoId, - config.name, - parentBranch - ); - - const azureApiGit = await azureApi.gitApi(); - await azureApiGit.createPush( - { - commits: [ - { - comment: message, - changes: changesInCommit, - }, - ], - refUpdates: [branchRef], - }, - config.repoId - ); -} - -async function branchExists(branchName) { - logger.debug(`Checking if branch exists: ${branchName}`); - - const branchNameToUse = !branchName.startsWith('refs/heads/') - ? `refs/heads/${branchName}` - : branchName; - - const branchs = await azureHelper.getRefs(config.repoId, branchNameToUse); - if (branchs.length === 0) { - return false; - } - return true; -} - async function getBranchPr(branchName) { logger.debug(`getBranchPr(${branchName})`); const existingPr = await findPr(branchName, null, 'open'); @@ -360,9 +341,9 @@ async function getPr(pullRequestId) { async function createPr(branchName, title, body, labels, useDefaultBranch) { const sourceRefName = azureHelper.getNewBranchName(branchName); - const targetRefName = useDefaultBranch - ? config.defaultBranch - : config.baseBranch; + const targetRefName = azureHelper.getNewBranchName( + useDefaultBranch ? config.defaultBranch : config.baseBranch + ); const description = azureHelper.max4000Chars(body); const azureApiGit = await azureApi.gitApi(); const pr = await azureApiGit.createPullRequest( @@ -390,23 +371,6 @@ async function updatePr(prNo, title, body) { await azureApiGit.updatePullRequest(objToUpdate, config.repoId, prNo); } -async function isBranchStale(branchName) { - logger.info(`isBranchStale(${branchName})`); - // Check if branch's parent SHA = master SHA - const branchCommit = await getBranchCommit(branchName); - logger.debug(`branchCommit=${branchCommit}`); - const commitDetails = await azureHelper.getCommitDetails( - branchCommit, - config.repoId - ); - logger.debug({ commitDetails }, `commitDetails`); - const parentSha = commitDetails.parents[0]; - logger.debug(`parentSha=${parentSha}`); - logger.debug(`config.baseCommitSHA=${config.baseCommitSHA}`); - // Return true if the SHAs don't match - return parentSha !== config.baseCommitSHA; -} - async function ensureComment(issueNo, topic, content) { logger.debug(`ensureComment(${issueNo}, ${topic}, content)`); const body = `### ${topic}\n\n${content}`; @@ -447,37 +411,6 @@ async function ensureCommentRemoval(issueNo, topic) { } } -async function getAllRenovateBranches(branchPrefix) { - logger.debug(`getAllRenovateBranches(branchPrefix)(${branchPrefix})`); - const azureApiGit = await azureApi.gitApi(); - const branches = await azureApiGit.getBranches(config.repoId); - return branches.filter(c => c.name.startsWith(branchPrefix)).map(c => c.name); -} - -async function deleteBranch(branchName, abandonAssociatedPr = false) { - logger.debug(`deleteBranch(branchName)(${branchName})`); - const ref = await azureHelper.getRefs( - config.repoId, - azureHelper.getNewBranchName(branchName) - ); - const azureApiGit = await azureApi.gitApi(); - await azureApiGit.updateRefs( - [ - { - name: ref[0].name, - oldObjectId: ref[0].objectId, - newObjectId: '0000000000000000000000000000000000000000', - }, - ], - config.repoId - ); - // istanbul ignore if - if (abandonAssociatedPr) { - const pr = await getBranchPr(branchName); - await abandonPr(pr.number); - } -} - // istanbul ignore next async function abandonPr(prNo) { logger.debug(`abandonPr(prNo)(${prNo})`); @@ -491,29 +424,12 @@ async function abandonPr(prNo) { ); } -async function getBranchLastCommitTime(branchName) { - logger.debug(`getBranchLastCommitTime(branchName)(${branchName})`); - const azureApiGit = await azureApi.gitApi(); - const branch = await azureApiGit.getBranch( - config.repoId, - azureHelper.getBranchNameWithoutRefsheadsPrefix(branchName) - ); - return branch.commit.committer.date; -} - function setBranchStatus(branchName, context, description, state, targetUrl) { logger.debug( `setBranchStatus(${branchName}, ${context}, ${description}, ${state}, ${targetUrl}) - Not supported by Azure DevOps (yet!)` ); } -async function mergeBranch(branchName) { - logger.info( - `mergeBranch(branchName)(${branchName}) - Not supported by Azure DevOps (yet!)` - ); - await null; -} - async function mergePr(pr) { logger.info(`mergePr(pr)(${pr}) - Not supported by Azure DevOps (yet!)`); await null; @@ -529,16 +445,17 @@ function getPrBody(input) { .replace('</details>', ''); } +// istanbul ignore next function findIssue() { - // istanbul ignore next logger.warn(`findIssue() is not implemented`); } +// istanbul ignore next function ensureIssue() { - // istanbul ignore next logger.warn(`ensureIssue() is not implemented`); } +// istanbul ignore next function ensureIssueClosing() {} /** @@ -620,3 +537,11 @@ function getPrFiles(prNo) { function getVulnerabilityAlerts() { return []; } + +function cleanRepo() { + // istanbul ignore if + if (config.storage && config.storage.cleanRepo) { + config.storage.cleanRepo(); + } + config = {}; +} diff --git a/test/platform/azure/__snapshots__/azure-helper.spec.js.snap b/test/platform/azure/__snapshots__/azure-helper.spec.js.snap index 7e992189e33e74a8f70e13336be3b0fcf67912fe..2934db296bf8af0086ba46933e1a25d5d15b48b2 100644 --- a/test/platform/azure/__snapshots__/azure-helper.spec.js.snap +++ b/test/platform/azure/__snapshots__/azure-helper.spec.js.snap @@ -86,6 +86,7 @@ Array [ exports[`platform/azure/helpers getRenovatePRFormat should be formated (closed v2) 1`] = ` Object { + "body": undefined, "canRebase": true, "displayNumber": "Pull Request #undefined", "number": undefined, @@ -96,6 +97,7 @@ Object { exports[`platform/azure/helpers getRenovatePRFormat should be formated (closed) 1`] = ` Object { + "body": undefined, "canRebase": true, "displayNumber": "Pull Request #undefined", "number": undefined, @@ -106,6 +108,7 @@ Object { exports[`platform/azure/helpers getRenovatePRFormat should be formated (isConflicted) 1`] = ` Object { + "body": undefined, "canRebase": true, "displayNumber": "Pull Request #undefined", "isConflicted": true, @@ -117,6 +120,7 @@ Object { exports[`platform/azure/helpers getRenovatePRFormat should be formated (not closed) 1`] = ` Object { + "body": undefined, "canRebase": true, "displayNumber": "Pull Request #undefined", "number": undefined, diff --git a/test/platform/azure/__snapshots__/index.spec.js.snap b/test/platform/azure/__snapshots__/index.spec.js.snap index 8c67594f064093e32f6413313ccef8e7e8804298..c6f72b6b2284b56171d8d2a3c27173d43a6d5895 100644 --- a/test/platform/azure/__snapshots__/index.spec.js.snap +++ b/test/platform/azure/__snapshots__/index.spec.js.snap @@ -68,36 +68,8 @@ Object { } `; -exports[`platform/azure getAllRenovateBranches() should return all renovate branches 1`] = ` -Array [ - "renovate/a", - "renovate/b", -] -`; - -exports[`platform/azure getBranchLastCommitTime should return a Date 1`] = `"1986-11-07T00:00:00Z"`; - exports[`platform/azure getBranchPr(branchName) should return the pr 1`] = `null`; -exports[`platform/azure getCommitMessages() returns commits messages 1`] = ` -Array [ - "com1", - "com2", - "com3", -] -`; - -exports[`platform/azure getFile(filePatch, branchName) should return the encoded file content 1`] = `"Hello Renovate!"`; - -exports[`platform/azure getFileList should return the files matching the fileName 1`] = ` -Array [ - "package.json", - "src/app/package.json", - "src/otherapp/package.json", - "symlinks/package.json", -] -`; - exports[`platform/azure getPr(prNo) should return a pr in the right format 1`] = ` Object { "pullRequestId": 1234, @@ -106,13 +78,13 @@ Object { exports[`platform/azure getPrBody(input) returns updated pr body 1`] = `"https://github.com/foo/bar/issues/5 plus also [a link](https://github.com/foo/bar/issues/5)"`; -exports[`platform/azure getRepos should return an array of repos 1`] = ` +exports[`platform/azure getRepos() should return an array of repos 1`] = ` Array [ Array [], ] `; -exports[`platform/azure getRepos should return an array of repos 2`] = ` +exports[`platform/azure getRepos() should return an array of repos 2`] = ` Array [ "prj1/repo1", "prj1/repo2", @@ -128,36 +100,11 @@ Array [ exports[`platform/azure initRepo should initialise the config for a repo 2`] = ` Object { - "baseBranch": "defBr", - "baseCommitSHA": "1234", - "defaultBranch": "defBr", - "fileList": null, "isFork": false, - "mergeMethod": "merge", - "owner": "?owner?", - "prList": null, "privateRepo": true, - "repoForceRebase": false, - "repoId": "1", - "repository": "some-repo", } `; -exports[`platform/azure setBaseBranch(branchName) sets the base branch 1`] = ` -Array [ - Array [], - Array [], - Array [], -] -`; - -exports[`platform/azure setBaseBranch(branchName) sets the base branch 2`] = ` -Array [ - Array [], - Array [], -] -`; - exports[`platform/azure updatePr(prNo, title, body) should update the PR 1`] = ` Array [ Array [], diff --git a/test/platform/azure/index.spec.js b/test/platform/azure/index.spec.js index fcc25b60176881dc0f54db3d51292a004bdfaf50..8f4d68dbf0639557643cd3eb10316419fc187e1d 100644 --- a/test/platform/azure/index.spec.js +++ b/test/platform/azure/index.spec.js @@ -1,20 +1,48 @@ -const hostRules = require('../../../lib/util/host-rules'); - describe('platform/azure', () => { let azure; let azureApi; let azureHelper; + let hostRules; + let GitStorage; beforeEach(() => { - // clean up hostRules - hostRules.clear(); - // reset module jest.resetModules(); jest.mock('../../../lib/platform/azure/azure-got-wrapper'); jest.mock('../../../lib/platform/azure/azure-helper'); + jest.mock('../../../lib/platform/git/storage'); + hostRules = require('../../../lib/util/host-rules'); azure = require('../../../lib/platform/azure'); azureApi = require('../../../lib/platform/azure/azure-got-wrapper'); azureHelper = require('../../../lib/platform/azure/azure-helper'); + GitStorage = require('../../../lib/platform/git/storage'); + GitStorage.mockImplementation(() => ({ + initRepo: jest.fn(), + cleanRepo: jest.fn(), + getFileList: jest.fn(), + branchExists: jest.fn(() => true), + isBranchStale: jest.fn(() => false), + setBaseBranch: jest.fn(), + getBranchLastCommitTime: jest.fn(), + getAllRenovateBranches: jest.fn(), + getCommitMessages: jest.fn(), + getFile: jest.fn(), + commitFilesToBranch: jest.fn(), + mergeBranch: jest.fn(), + deleteBranch: jest.fn(), + getRepoStatus: jest.fn(), + })); + + // clean up hostRules + hostRules.clear(); + hostRules.update({ + platform: 'azure', + endpoint: 'https://dev.azure.com/renovate12345', + token: 'token', + }); + }); + + afterEach(() => { + azure.cleanRepo(); }); function getRepos(token, endpoint) { @@ -37,21 +65,24 @@ describe('platform/azure', () => { return azure.getRepos(token, endpoint); } - describe('getRepos', () => { + describe('getRepos()', () => { it('should return an array of repos', async () => { const repos = await getRepos( 'sometoken', - 'https://fabrikam.VisualStudio.com/DefaultCollection' + 'https://dev.azure.com/renovate12345' ); expect(azureApi.gitApi.mock.calls).toMatchSnapshot(); expect(repos).toMatchSnapshot(); }); }); + describe('getRepoStatus()', () => { it('exists', async () => { - expect(await azure.getRepoStatus()).toEqual({}); + await initRepo(); + expect(await azure.getRepoStatus()).toBeUndefined(); }); }); + describe('cleanRepo()', () => { it('exists', () => { azure.cleanRepo(); @@ -90,12 +121,13 @@ describe('platform/azure', () => { return azure.initRepo({ repository: args[0], token: args[1], - endpoint: 'https://my.custom.endpoint/', + endpoint: 'https://dev.azure.com/renovate12345', }); } return azure.initRepo({ - endpoint: 'https://my.custom.endpoint/', + endpoint: 'https://dev.azure.com/renovate12345', + repository: 'some/repo', ...args[0], }); } @@ -105,7 +137,7 @@ describe('platform/azure', () => { const config = await initRepo({ repository: 'some-repo', token: 'token', - endpoint: 'https://my.custom.endpoint/', + endpoint: 'https://dev.azure.com/renovate12345', }); expect(azureApi.gitApi.mock.calls).toMatchSnapshot(); expect(config).toMatchSnapshot(); @@ -118,68 +150,6 @@ describe('platform/azure', () => { }); }); - describe('setBaseBranch(branchName)', () => { - it('sets the base branch', async () => { - await initRepo('some-repo', 'token'); - // getBranchCommit - azureApi.gitApi.mockImplementationOnce(() => ({ - getBranch: jest.fn(() => ({ - commit: { commitId: '1234' }, - })), - })); - await azure.setBaseBranch('some-branch'); - expect(azureApi.gitApi.mock.calls).toMatchSnapshot(); - }); - it('sets the base branch', async () => { - await initRepo('some-repo', 'token'); - // getBranchCommit - azureApi.gitApi.mockImplementationOnce(() => ({ - getBranch: jest.fn(() => ({ - commit: { commitId: '1234' }, - })), - })); - await azure.setBaseBranch(); - expect(azureApi.gitApi.mock.calls).toMatchSnapshot(); - }); - }); - - describe('getCommitMessages()', () => { - it('returns commits messages', async () => { - const config = await initRepo( - 'some-repo', - 'token', - 'https://my.custom.endpoint/' - ); - expect(config.repoId).toBe('1'); - azureApi.gitApi.mockImplementationOnce(() => ({ - getCommits: jest.fn(() => [ - { comment: 'com1' }, - { comment: 'com2' }, - { comment: 'com3' }, - ]), - })); - const msg = await azure.getCommitMessages(); - expect(msg).toMatchSnapshot(); - }); - it('returns empty array if error', async () => { - await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementationOnce(() => { - throw new Error('some error'); - }); - const msgs = await azure.getCommitMessages(); - expect(msgs).toEqual([]); - }); - }); - - describe('getFile(filePatch, branchName)', () => { - it('should return the encoded file content', async () => { - await initRepo('some-repo', 'token'); - azureHelper.getFile.mockImplementationOnce(() => `Hello Renovate!`); - const content = await azure.getFile('package.json'); - expect(content).toMatchSnapshot(); - }); - }); - describe('findPr(branchName, prTitle, state)', () => { it('returns pr if found it open', async () => { azureApi.gitApi.mockImplementationOnce(() => ({ @@ -273,139 +243,12 @@ describe('platform/azure', () => { const res = await azure.findPr('branch-a', 'branch a pr'); expect(res).toMatchSnapshot(); }); - /* - it('returns pr if found it but add an error', async () => { - azureApi.gitApi.mockImplementationOnce(() => ({ - getPullRequests: jest.fn(() => [ - { - pullRequestId: 1, - sourceRefName: 'refs/heads/branch-a', - title: 'branch a pr', - status: 2, - }, - ]), - })); - azureHelper.getNewBranchName.mockImplementationOnce( - () => 'refs/heads/branch-a' - ); - azureHelper.getRenovatePRFormat.mockImplementationOnce(() => ({ - number: 1, - head: { ref: 'branch-a' }, - title: 'branch a pr', - isClosed: true, - })); - const res = await azure.findPr('branch-a', 'branch a pr', 'blabla'); - expect(res).toMatchSnapshot(); - }); - it('returns null if error', async () => { - await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementationOnce(() => { - throw new Error('some error'); - }); - const pr = await azure.findPr('branch-a', 'branch a pr'); - expect(pr).toBeNull(); - }); - */ }); describe('getPrList()', () => { it('returns empty array', () => { expect(azure.getPrList()).toEqual([]); }); }); - describe('getFileList', () => { - it('returns empty array if error', async () => { - await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementationOnce(() => { - throw new Error('some error'); - }); - const files = await azure.getFileList(); - expect(files).toEqual([]); - }); - it('caches the result', async () => { - await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementationOnce(() => ({ - getItems: jest.fn(() => [ - { path: '/symlinks/package.json' }, - { isFolder: false, path: '/package.json' }, - { isFolder: true, path: '/some-dir' }, - { type: 'blob', path: '/src/app/package.json' }, - { type: 'blob', path: '/src/otherapp/package.json' }, - ]), - })); - let files = await azure.getFileList(); - expect(files.length).toBe(4); - files = await azure.getFileList(); - expect(files.length).toBe(4); - }); - it('should return the files matching the fileName', async () => { - await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementationOnce(() => ({ - getItems: jest.fn(() => [ - { path: '/symlinks/package.json' }, - { isFolder: false, path: '/package.json' }, - { isFolder: true, path: '/some-dir' }, - { type: 'blob', path: '/src/app/package.json' }, - { type: 'blob', path: '/src/otherapp/package.json' }, - ]), - })); - const files = await azure.getFileList(); - expect(files).toMatchSnapshot(); - }); - }); - - describe('commitFilesToBranch(branchName, files, message, parentBranch)', () => { - it('should add a new commit to the branch', async () => { - await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementationOnce(() => ({ - createPush: jest.fn(() => true), - })); - azureHelper.getAzureBranchObj.mockImplementationOnce(() => 'newBranch'); - azureHelper.getRefs.mockImplementation(() => [{ objectId: '123' }]); - - const files = [ - { - name: 'package.json', - contents: 'hello world', - }, - ]; - await azure.commitFilesToBranch( - 'package.json', - files, - 'my commit message' - ); - expect(azureApi.gitApi.mock.calls.length).toBe(3); - }); - it('should add a new commit to an existing branch', async () => { - await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementationOnce(() => ({ - createPush: jest.fn(() => true), - })); - azureHelper.getAzureBranchObj.mockImplementationOnce(() => 'newBranch'); - azureHelper.getRefs.mockImplementation(() => []); - - const files = [ - { - name: 'package.json', - contents: 'hello world', - }, - ]; - await azure.commitFilesToBranch( - 'package.json', - files, - 'my commit message' - ); - expect(azureApi.gitApi.mock.calls.length).toBe(3); - }); - }); - - describe('branchExists(branchName)', () => { - it('should return false if the branch does not exist', async () => { - await initRepo('some-repo', 'token'); - azureHelper.getRefs.mockImplementation(() => []); - const exists = await azure.branchExists('thebranchname'); - expect(exists).toBe(false); - }); - }); describe('getBranchPr(branchName)', () => { it('should return null if no PR exists', async () => { @@ -578,46 +421,6 @@ describe('platform/azure', () => { }); }); - describe('isBranchStale', () => { - it('should return true', async () => { - await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementationOnce(() => ({ - getBranch: jest.fn(() => ({ commit: { commitId: '123456' } })), - })); - azureHelper.getCommitDetails.mockImplementation(() => ({ - parents: ['789654'], - })); - const res = await azure.isBranchStale(); - expect(res).toBe(true); - }); - it('should return false', async () => { - await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementationOnce(() => ({ - getBranch: jest.fn(() => ({ commit: { commitId: '123457' } })), - })); - azureHelper.getCommitDetails.mockImplementation(() => ({ - parents: ['1234'], - })); - const res = await azure.isBranchStale('branch'); - expect(res).toBe(false); - }); - }); - - describe('getAllRenovateBranches()', () => { - it('should return all renovate branches', async () => { - await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementationOnce(() => ({ - getBranches: jest.fn(() => [ - { name: 'master' }, - { name: 'renovate/a' }, - { name: 'renovate/b' }, - ]), - })); - const res = await azure.getAllRenovateBranches('renovate/'); - expect(res).toMatchSnapshot(); - }); - }); - describe('ensureCommentRemoval', () => { it('deletes comment if found', async () => { await initRepo({ repository: 'some/repo', token: 'token' }); @@ -647,35 +450,6 @@ describe('platform/azure', () => { }); }); - describe('getBranchLastCommitTime', () => { - it('should return a Date', async () => { - await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementationOnce(() => ({ - getBranch: jest.fn(() => ({ - commit: { committer: { date: '1986-11-07T00:00:00Z' } }, - })), - })); - const res = await azure.getBranchLastCommitTime('some-branch'); - expect(res).toMatchSnapshot(); - }); - }); - - describe('deleteBranch and abandon PR', () => { - it('should delete the branch', async () => { - azureHelper.getRefs.mockImplementation(() => [{ objectId: '123' }]); - azureApi.gitApi.mockImplementationOnce(() => ({ - updateRefs: jest.fn(() => [ - { - name: 'refs/head/testBranch', - oldObjectId: '123456', - newObjectId: '0000000000000000000000000000000000000000', - }, - ]), - })); - await azure.deleteBranch(); - }); - }); - describe('Assignees', () => { it('addAssignees', async () => { await initRepo({ repository: 'some/repo', token: 'token' }); @@ -704,6 +478,7 @@ describe('platform/azure', () => { expect(azureApi.gitApi.mock.calls.length).toBe(3); }); }); + describe('getPrBody(input)', () => { it('returns updated pr body', () => { const input = @@ -711,17 +486,13 @@ describe('platform/azure', () => { expect(azure.getPrBody(input)).toMatchSnapshot(); }); }); + describe('Not supported by Azure DevOps (yet!)', () => { it('setBranchStatus', () => { const res = azure.setBranchStatus(); expect(res).toBeUndefined(); }); - it('mergeBranch', async () => { - const res = await azure.mergeBranch(); - expect(res).toBeUndefined(); - }); - it('mergePr', async () => { const res = await azure.mergePr(); expect(res).toBeUndefined(); @@ -733,6 +504,7 @@ describe('platform/azure', () => { expect(res.length).toBe(0); }); }); + describe('getVulnerabilityAlerts()', () => { it('returns empty', async () => { const res = await azure.getVulnerabilityAlerts(); diff --git a/test/workers/repository/init/apis.spec.js b/test/workers/repository/init/apis.spec.js index fcae6948e59c93db202acdc8b5a5b9d7323f9f75..17fe4b9569cef292e546f608b85fc425856d5216 100644 --- a/test/workers/repository/init/apis.spec.js +++ b/test/workers/repository/init/apis.spec.js @@ -24,17 +24,5 @@ describe('workers/repository/init/apis', () => { glGot.mockReturnValueOnce({ body: [] }); await initApis(config); }); - it('runs azure', async () => { - config.platform = 'azure'; - config.repository = 'some/name'; - // config.endpoint = 'https://fabrikam.visualstudio.com/DefaultCollection'; - try { - await initApis(config); - } catch (error) { - expect(error.message).toBe( - `Failed to configure platform 'azure': no endpoint defined` - ); - } - }); }); });