diff --git a/lib/platform/azure/azure-got-wrapper.js b/lib/platform/azure/azure-got-wrapper.ts similarity index 58% rename from lib/platform/azure/azure-got-wrapper.js rename to lib/platform/azure/azure-got-wrapper.ts index fbff8d0fba39cd3039cf34d3515d20ec50e91b2b..f0d71ba3406caab4953b2164cb61d09d98264bf6 100644 --- a/lib/platform/azure/azure-got-wrapper.js +++ b/lib/platform/azure/azure-got-wrapper.ts @@ -1,17 +1,10 @@ -const azure = require('azure-devops-node-api'); -const hostRules = require('../../util/host-rules'); +import * as azure from 'azure-devops-node-api'; +import * as hostRules from '../../util/host-rules'; const hostType = 'azure'; -let endpoint; +let endpoint: string; -module.exports = { - azureObj, - gitApi, - getCoreApi, - setEndpoint, -}; - -function azureObj() { +export function azureObj() { const config = hostRules.find({ hostType, url: endpoint }); if (!(config && config.token)) { throw new Error(`No token found for azure`); @@ -20,14 +13,14 @@ function azureObj() { return new azure.WebApi(endpoint, authHandler); } -function gitApi() { +export function gitApi() { return azureObj().getGitApi(); } -function getCoreApi() { +export function getCoreApi() { return azureObj().getCoreApi(); } -function setEndpoint(e) { +export function setEndpoint(e: string) { endpoint = e; } diff --git a/lib/platform/azure/azure-helper.js b/lib/platform/azure/azure-helper.ts similarity index 80% rename from lib/platform/azure/azure-helper.js rename to lib/platform/azure/azure-helper.ts index a71df18ae10dd819b8291fadb3cd29c3b2c4fef3..7f65c90b591e96b701115da12d514d34ed3e7f62 100644 --- a/lib/platform/azure/azure-helper.js +++ b/lib/platform/azure/azure-helper.ts @@ -1,25 +1,10 @@ -// @ts-nocheck - -const azureApi = require('./azure-got-wrapper'); - -module.exports = { - getBranchNameWithoutRefsheadsPrefix, - getRefs, - getAzureBranchObj, - getChanges, - getNewBranchName, - getFile, - max4000Chars, - getRenovatePRFormat, - getCommitDetails, - getProjectAndRepo, -}; +import * as azureApi from './azure-got-wrapper'; /** * * @param {string} branchName */ -function getNewBranchName(branchName) { +export function getNewBranchName(branchName?: string) { if (branchName && !branchName.startsWith('refs/heads/')) { return `refs/heads/${branchName}`; } @@ -30,10 +15,10 @@ function getNewBranchName(branchName) { * * @param {string} branchPath */ -function getBranchNameWithoutRefsheadsPrefix(branchPath) { +export function getBranchNameWithoutRefsheadsPrefix(branchPath: string) { if (!branchPath) { logger.error(`getBranchNameWithoutRefsheadsPrefix(${branchPath})`); - return null; + return undefined; } if (!branchPath.startsWith('refs/heads/')) { logger.trace( @@ -48,10 +33,10 @@ function getBranchNameWithoutRefsheadsPrefix(branchPath) { * * @param {string} branchPath */ -function getBranchNameWithoutRefsPrefix(branchPath) { +function getBranchNameWithoutRefsPrefix(branchPath?: string) { if (!branchPath) { logger.error(`getBranchNameWithoutRefsPrefix(${branchPath})`); - return null; + return undefined; } if (!branchPath.startsWith('refs/')) { logger.trace( @@ -67,12 +52,12 @@ function getBranchNameWithoutRefsPrefix(branchPath) { * @param {string} repoId * @param {string} branchName */ -async function getRefs(repoId, branchName) { +export async function getRefs(repoId: string, branchName?: string) { logger.debug(`getRefs(${repoId}, ${branchName})`); const azureApiGit = await azureApi.gitApi(); const refs = await azureApiGit.getRefs( repoId, - null, + undefined, getBranchNameWithoutRefsPrefix(branchName) ); return refs; @@ -83,7 +68,11 @@ async function getRefs(repoId, branchName) { * @param {string} branchName * @param {string} from */ -async function getAzureBranchObj(repoId, branchName, from) { +export async function getAzureBranchObj( + repoId: string, + branchName: string, + from?: string +) { const fromBranchName = getNewBranchName(from); const refs = await getRefs(repoId, fromBranchName); if (refs.length === 0) { @@ -107,7 +96,12 @@ async function getAzureBranchObj(repoId, branchName, from) { * @param {string} repository * @param {string} branchName */ -async function getChanges(files, repoId, repository, branchName) { +export async function getChanges( + files: any, + repoId: any, + repository: any, + branchName: any +) { const changes = []; for (const file of files) { // Add or update @@ -144,14 +138,19 @@ async function getChanges(files, repoId, repository, branchName) { * @param {string} filePath * @param {string} branchName */ -async function getFile(repoId, repository, filePath, branchName) { +export async function getFile( + repoId: string, + repository: any, + filePath: string, + branchName: any +) { logger.trace(`getFile(filePath=${filePath}, branchName=${branchName})`); const azureApiGit = await azureApi.gitApi(); const item = await azureApiGit.getItemText( repoId, filePath, - null, - null, + undefined, + undefined, 0, // because we look for 1 file false, false, @@ -183,11 +182,11 @@ async function getFile(repoId, repository, filePath, branchName) { return null; // no file found } -async function streamToString(stream) { - const chunks = []; +async function streamToString(stream: NodeJS.ReadableStream) { + const chunks: string[] = []; /* eslint-disable promise/avoid-new */ - const p = await new Promise(resolve => { - stream.on('data', chunk => { + const p = await new Promise<string>(resolve => { + stream.on('data', (chunk: any) => { chunks.push(chunk.toString()); }); stream.on('end', () => { @@ -201,15 +200,20 @@ async function streamToString(stream) { * * @param {string} str */ -function max4000Chars(str) { +export function max4000Chars(str: string) { if (str && str.length >= 4000) { return str.substring(0, 3999); } return str; } -function getRenovatePRFormat(azurePr) { - const pr = azurePr; +export function getRenovatePRFormat(azurePr: { + pullRequestId: any; + description: any; + status: number; + mergeStatus: number; +}) { + const pr = azurePr as any; pr.displayNumber = `Pull Request #${azurePr.pullRequestId}`; pr.number = azurePr.pullRequestId; @@ -249,7 +253,7 @@ function getRenovatePRFormat(azurePr) { return pr; } -async function getCommitDetails(commit, repoId) { +export async function getCommitDetails(commit: string, repoId: string) { logger.debug(`getCommitDetails(${commit}, ${repoId})`); const azureApiGit = await azureApi.gitApi(); const results = await azureApiGit.getCommit(commit, repoId); @@ -260,7 +264,7 @@ async function getCommitDetails(commit, repoId) { * * @param {string} str */ -function getProjectAndRepo(str) { +export function getProjectAndRepo(str: string) { logger.trace(`getProjectAndRepo(${str})`); const strSplited = str.split(`/`); if (strSplited.length === 1) { diff --git a/lib/platform/azure/index.js b/lib/platform/azure/index.ts similarity index 70% rename from lib/platform/azure/index.js rename to lib/platform/azure/index.ts index d511b8ed1869d9b82ee216a2c4a30af1c8fc7a9b..c7a4378537169d8a10166222faf14dd679a5eb5b 100644 --- a/lib/platform/azure/index.js +++ b/lib/platform/azure/index.ts @@ -1,69 +1,37 @@ -// @ts-nocheck //because of logger, we can't ts-check -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').Storage; - -let config = {}; - -const defaults = { +import * as azureHelper from './azure-helper'; +import * as azureApi from './azure-got-wrapper'; +import * as hostRules from '../../util/host-rules'; +import { appSlug } from '../../config/app-strings'; +import GitStorage from '../git/storage'; + +interface RepoConfig { + storage: GitStorage; + repoForceRebase: boolean; + mergeMethod: string; + baseCommitSHA: string | undefined; + baseBranch: string; + defaultBranch: string; + owner: string; + repoId: string; + azureWorkItemId: any; + prList: null; + fileList: null; + repository: string; +} + +let config: RepoConfig = {} as any; + +const defaults: any = { hostType: 'azure', }; -module.exports = { - // Initialization - initPlatform, - getRepos, - cleanRepo, - initRepo, - getRepoStatus, - getRepoForceRebase, - setBaseBranch, - setBranchPrefix, - // Search - getFileList, - // Branch - branchExists, - getAllRenovateBranches, - isBranchStale, - getBranchPr, - getBranchStatus, - getBranchStatusCheck, - setBranchStatus, - deleteBranch, - mergeBranch, - getBranchLastCommitTime, - // issue - findIssue, - ensureIssue, - ensureIssueClosing, - addAssignees, - addReviewers, - deleteLabel, - getIssueList, - // Comments - ensureComment, - ensureCommentRemoval, - // PR - getPrList, - findPr, - createPr, - getPr, - getPrFiles, - updatePr, - mergePr, - getPrBody, - // file - commitFilesToBranch, - getFile, - // Commits - getCommitMessages, - // vulnerability alerts - getVulnerabilityAlerts, -}; - -function initPlatform({ endpoint, token }) { +export function initPlatform({ + endpoint, + token, +}: { + endpoint: string; + token: string; +}) { if (!endpoint) { throw new Error('Init: You must configure an Azure DevOps endpoint'); } @@ -79,14 +47,22 @@ function initPlatform({ endpoint, token }) { return res; } -async function getRepos() { +export async function getRepos() { logger.info('Autodiscovering Azure DevOps repositories'); const azureApiGit = await azureApi.gitApi(); const repos = await azureApiGit.getRepositories(); - return repos.map(repo => `${repo.project.name}/${repo.name}`); -} - -async function initRepo({ repository, localDir, azureWorkItemId }) { + return repos.map(repo => `${repo.project!.name}/${repo.name}`); +} + +export async function initRepo({ + repository, + localDir, + azureWorkItemId, +}: { + repository: string; + localDir: string; + azureWorkItemId: any; +}) { logger.debug(`initRepo("${repository}")`); config.repository = repository; config.fileList = null; @@ -97,15 +73,15 @@ async function initRepo({ repository, localDir, azureWorkItemId }) { const names = azureHelper.getProjectAndRepo(repository); const repo = repos.filter( c => - c.name.toLowerCase() === names.repo.toLowerCase() && - c.project.name.toLowerCase() === names.project.toLowerCase() + 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.repoId = repo.id!; config.owner = '?owner?'; logger.debug(`${repository} owner = ${config.owner}`); // Use default branch as PR target unless later overridden - config.defaultBranch = repo.defaultBranch.replace('refs/heads/', ''); + config.defaultBranch = repo.defaultBranch!.replace('refs/heads/', ''); config.baseBranch = config.defaultBranch; logger.debug(`${repository} default branch = ${config.defaultBranch}`); config.baseCommitSHA = await getBranchCommit(config.baseBranch); @@ -132,12 +108,12 @@ async function initRepo({ repository, localDir, azureWorkItemId }) { return platformConfig; } -function getRepoForceRebase() { +export function getRepoForceRebase() { return false; } // istanbul ignore next -async function setBaseBranch(branchName = config.baseBranch) { +export async function setBaseBranch(branchName = config.baseBranch) { logger.debug(`Setting baseBranch to ${branchName}`); config.baseBranch = branchName; delete config.baseCommitSHA; @@ -147,41 +123,44 @@ async function setBaseBranch(branchName = config.baseBranch) { } // istanbul ignore next -function setBranchPrefix(branchPrefix) { +export function setBranchPrefix(branchPrefix: string) { return config.storage.setBranchPrefix(branchPrefix); } // Search // istanbul ignore next -function getFileList(branchName) { +export function getFileList(branchName: string) { return config.storage.getFileList(branchName); } // Branch // istanbul ignore next -function branchExists(branchName) { +export function branchExists(branchName: string) { return config.storage.branchExists(branchName); } // istanbul ignore next -function getAllRenovateBranches(branchPrefix) { +export function getAllRenovateBranches(branchPrefix: string) { return config.storage.getAllRenovateBranches(branchPrefix); } // istanbul ignore next -function isBranchStale(branchName) { +export function isBranchStale(branchName: string) { return config.storage.isBranchStale(branchName); } // istanbul ignore next -function getFile(filePath, branchName) { +export function getFile(filePath: string, branchName: string) { return config.storage.getFile(filePath, branchName); } // istanbul ignore next -async function deleteBranch(branchName, abandonAssociatedPr = false) { +export async function deleteBranch( + branchName: string, + abandonAssociatedPr = false +) { await config.storage.deleteBranch(branchName); // istanbul ignore if if (abandonAssociatedPr) { @@ -191,25 +170,25 @@ async function deleteBranch(branchName, abandonAssociatedPr = false) { } // istanbul ignore next -function getBranchLastCommitTime(branchName) { +export function getBranchLastCommitTime(branchName: string) { return config.storage.getBranchLastCommitTime(branchName); } // istanbul ignore next -function getRepoStatus() { +export function getRepoStatus() { return config.storage.getRepoStatus(); } // istanbul ignore next -function mergeBranch(branchName) { +export function mergeBranch(branchName: string) { return config.storage.mergeBranch(branchName); } // istanbul ignore next -function commitFilesToBranch( - branchName, - files, - message, +export function commitFilesToBranch( + branchName: string, + files: any[], + message: string, parentBranch = config.baseBranch ) { return config.storage.commitFilesToBranch( @@ -221,26 +200,30 @@ function commitFilesToBranch( } // istanbul ignore next -function getCommitMessages() { +export function getCommitMessages() { return config.storage.getCommitMessages(); } -async function getBranchCommit(fullBranchName) { +async function getBranchCommit(fullBranchName: string) { const azureApiGit = await azureApi.gitApi(); const commit = await azureApiGit.getBranch( config.repoId, - azureHelper.getBranchNameWithoutRefsheadsPrefix(fullBranchName) + azureHelper.getBranchNameWithoutRefsheadsPrefix(fullBranchName)! ); - return commit.commit.commitId; + return commit.commit!.commitId; } -function getPrList() { +export function getPrList() { return []; } -async function findPr(branchName, prTitle, state = 'all') { +export async function findPr( + branchName: string, + prTitle: string | null, + state = 'all' +) { logger.debug(`findPr(${branchName}, ${prTitle}, ${state})`); - let prsFiltered = []; + let prsFiltered: any[] = []; try { const azureApiGit = await azureApi.gitApi(); const prs = await azureApiGit.getPullRequests(config.repoId, { status: 4 }); @@ -278,13 +261,16 @@ async function findPr(branchName, prTitle, state = 'all') { return prsFiltered[0]; } -async function getBranchPr(branchName) { +export async function getBranchPr(branchName: string) { logger.debug(`getBranchPr(${branchName})`); const existingPr = await findPr(branchName, null, 'open'); return existingPr ? getPr(existingPr.pullRequestId) : null; } -async function getBranchStatus(branchName, requiredStatusChecks) { +export async function getBranchStatus( + branchName: string, + requiredStatusChecks: any +) { logger.debug(`getBranchStatus(${branchName})`); if (!requiredStatusChecks) { // null means disable status checks, so it always succeeds @@ -299,12 +285,15 @@ async function getBranchStatus(branchName, requiredStatusChecks) { return branchStatusCheck; } -async function getBranchStatusCheck(branchName, context) { +export async function getBranchStatusCheck( + branchName: string, + context?: string +) { logger.trace(`getBranchStatusCheck(${branchName}, ${context})`); const azureApiGit = await azureApi.gitApi(); const branch = await azureApiGit.getBranch( config.repoId, - azureHelper.getBranchNameWithoutRefsheadsPrefix(branchName) + azureHelper.getBranchNameWithoutRefsheadsPrefix(branchName)! ); if (branch.aheadCount === 0) { return 'success'; @@ -312,36 +301,36 @@ async function getBranchStatusCheck(branchName, context) { return 'pending'; } -async function getPr(pullRequestId) { +export async function getPr(pullRequestId: number) { logger.debug(`getPr(${pullRequestId})`); if (!pullRequestId) { return null; } const azureApiGit = await azureApi.gitApi(); const prs = await azureApiGit.getPullRequests(config.repoId, { status: 4 }); - const azurePr = prs.filter(item => item.pullRequestId === pullRequestId); - if (azurePr.length === 0) { + const azurePr: any = prs.find(item => item.pullRequestId === pullRequestId); + if (!azurePr) { return null; } const labels = await azureApiGit.getPullRequestLabels( config.repoId, pullRequestId ); - azurePr[0].labels = labels + azurePr.labels = labels .filter(label => label.active) .map(label => label.name); - logger.debug(`pr: (${azurePr[0]})`); - const pr = azureHelper.getRenovatePRFormat(azurePr[0]); + logger.debug(`pr: (${azurePr})`); + const pr = azureHelper.getRenovatePRFormat(azurePr); return pr; } -async function createPr( - branchName, - title, - body, - labels, - useDefaultBranch, - platformOptions = {} +export async function createPr( + branchName: string, + title: string, + body: string, + labels: string[], + useDefaultBranch?: boolean, + platformOptions: any = {} ) { const sourceRefName = azureHelper.getNewBranchName(branchName); const targetRefName = azureHelper.getNewBranchName( @@ -354,7 +343,7 @@ async function createPr( id: config.azureWorkItemId, }, ]; - let pr = await azureApiGit.createPullRequest( + let pr: any = await azureApiGit.createPullRequest( { sourceRefName, targetRefName, @@ -368,7 +357,7 @@ async function createPr( pr = await azureApiGit.updatePullRequest( { autoCompleteSetBy: { - id: pr.createdBy.id, + id: pr.createdBy!.id, }, completionOptions: { squashMerge: true, @@ -376,7 +365,7 @@ async function createPr( }, }, config.repoId, - pr.pullRequestId + pr.pullRequestId! ); } await labels.forEach(async label => { @@ -385,17 +374,17 @@ async function createPr( name: label, }, config.repoId, - pr.pullRequestId + pr.pullRequestId! ); }); pr.branchName = branchName; return azureHelper.getRenovatePRFormat(pr); } -async function updatePr(prNo, title, body) { +export async function updatePr(prNo: number, title: string, body?: string) { logger.debug(`updatePr(${prNo}, ${title}, body)`); const azureApiGit = await azureApi.gitApi(); - const objToUpdate = { + const objToUpdate: any = { title, }; if (body) { @@ -404,7 +393,11 @@ async function updatePr(prNo, title, body) { await azureApiGit.updatePullRequest(objToUpdate, config.repoId, prNo); } -async function ensureComment(issueNo, topic, content) { +export async function ensureComment( + issueNo: number, + topic: string | null, + content: string +) { logger.debug(`ensureComment(${issueNo}, ${topic}, content)`); const body = `### ${topic}\n\n${content}`; const azureApiGit = await azureApi.gitApi(); @@ -418,7 +411,7 @@ async function ensureComment(issueNo, topic, content) { ); } -async function ensureCommentRemoval(issueNo, topic) { +export async function ensureCommentRemoval(issueNo: number, topic: string) { logger.debug(`ensureCommentRemoval(issueNo, topic)(${issueNo}, ${topic})`); if (issueNo) { const azureApiGit = await azureApi.gitApi(); @@ -426,7 +419,7 @@ async function ensureCommentRemoval(issueNo, topic) { let threadIdFound = null; threads.forEach(thread => { - if (thread.comments[0].content.startsWith(`### ${topic}\n\n`)) { + if (thread.comments![0].content!.startsWith(`### ${topic}\n\n`)) { threadIdFound = thread.id; } }); @@ -445,7 +438,7 @@ async function ensureCommentRemoval(issueNo, topic) { } // istanbul ignore next -async function abandonPr(prNo) { +async function abandonPr(prNo: number) { logger.debug(`abandonPr(prNo)(${prNo})`); const azureApiGit = await azureApi.gitApi(); await azureApiGit.updatePullRequest( @@ -457,18 +450,24 @@ async function abandonPr(prNo) { ); } -function setBranchStatus(branchName, context, description, state, targetUrl) { +export function setBranchStatus( + branchName: string, + context: string, + description: string, + state: string, + targetUrl: string +) { logger.debug( `setBranchStatus(${branchName}, ${context}, ${description}, ${state}, ${targetUrl}) - Not supported by Azure DevOps (yet!)` ); } -async function mergePr(pr) { +export async function mergePr(pr: number) { logger.info(`mergePr(pr)(${pr}) - Not supported by Azure DevOps (yet!)`); await null; } -function getPrBody(input) { +export function getPrBody(input: string) { // Remove any HTML we use return input .replace(new RegExp(`\n---\n\n.*?<!-- ${appSlug}-rebase -->.*?\n`), '') @@ -479,20 +478,20 @@ function getPrBody(input) { } // istanbul ignore next -function findIssue() { +export function findIssue() { logger.warn(`findIssue() is not implemented`); } // istanbul ignore next -function ensureIssue() { +export function ensureIssue() { logger.warn(`ensureIssue() is not implemented`); } // istanbul ignore next -function ensureIssueClosing() {} +export function ensureIssueClosing() {} // istanbul ignore next -function getIssueList() { +export function getIssueList() { logger.debug(`getIssueList()`); // TODO: Needs implementation return []; @@ -503,7 +502,7 @@ function getIssueList() { * @param {number} issueNo * @param {string[]} assignees */ -async function addAssignees(issueNo, assignees) { +export async function addAssignees(issueNo: number, assignees: string[]) { logger.trace(`addAssignees(${issueNo}, ${assignees})`); await ensureComment( issueNo, @@ -517,34 +516,34 @@ async function addAssignees(issueNo, assignees) { * @param {number} prNo * @param {string[]} reviewers */ -async function addReviewers(prNo, reviewers) { +export async function addReviewers(prNo: number, reviewers: string[]) { logger.trace(`addReviewers(${prNo}, ${reviewers})`); const azureApiGit = await azureApi.gitApi(); const azureApiCore = await azureApi.getCoreApi(); const repos = await azureApiGit.getRepositories(); const repo = repos.filter(c => c.id === config.repoId)[0]; - const teams = await azureApiCore.getTeams(repo.project.id); + const teams = await azureApiCore.getTeams(repo!.project!.id!); const members = await Promise.all( teams.map( async t => /* eslint-disable no-return-await */ await azureApiCore.getTeamMembersWithExtendedProperties( - repo.project.id, - t.id + repo!.project!.id!, + t.id! ) ) ); - const ids = []; + const ids: any[] = []; members.forEach(listMembers => { listMembers.forEach(m => { reviewers.forEach(r => { if ( - r.toLowerCase() === m.identity.displayName.toLowerCase() || - r.toLowerCase() === m.identity.uniqueName.toLowerCase() + r.toLowerCase() === m.identity!.displayName!.toLowerCase() || + r.toLowerCase() === m.identity!.uniqueName!.toLowerCase() ) { - if (ids.filter(c => c.id === m.identity.id).length === 0) { - ids.push({ id: m.identity.id, name: r }); + if (ids.filter(c => c.id === m.identity!.id).length === 0) { + ids.push({ id: m.identity!.id, name: r }); } } }); @@ -553,7 +552,7 @@ async function addReviewers(prNo, reviewers) { teams.forEach(t => { reviewers.forEach(r => { - if (r.toLowerCase() === t.name.toLowerCase()) { + if (r.toLowerCase() === t.name!.toLowerCase()) { if (ids.filter(c => c.id === t.id).length === 0) { ids.push({ id: t.id, name: r }); } @@ -575,28 +574,28 @@ async function addReviewers(prNo, reviewers) { } // istanbul ignore next -async function deleteLabel(prNumber, label) { +export async function deleteLabel(prNumber: number, label: string) { logger.debug(`Deleting label ${label} from #${prNumber}`); const azureApiGit = await azureApi.gitApi(); await azureApiGit.deletePullRequestLabels(config.repoId, prNumber, label); } // to become async? -function getPrFiles(prNo) { +export function getPrFiles(prNo: number) { logger.info( `getPrFiles(prNo)(${prNo}) - Not supported by Azure DevOps (yet!)` ); return []; } -function getVulnerabilityAlerts() { +export function getVulnerabilityAlerts() { return []; } -function cleanRepo() { +export function cleanRepo() { // istanbul ignore if if (config.storage && config.storage.cleanRepo) { config.storage.cleanRepo(); } - config = {}; + config = {} as any; } diff --git a/test/platform/__snapshots__/index.spec.js.snap b/test/platform/__snapshots__/index.spec.js.snap index 85414a719a0da38681c173215da98867e7fd06ce..079eeee37eda8ac8d6306f80a9f9c3c39972c7ac 100644 --- a/test/platform/__snapshots__/index.spec.js.snap +++ b/test/platform/__snapshots__/index.spec.js.snap @@ -2,46 +2,46 @@ exports[`platform has a list of supported methods for azure 1`] = ` Array [ - "initPlatform", - "getRepos", - "cleanRepo", - "initRepo", - "getRepoStatus", - "getRepoForceRebase", - "setBaseBranch", - "setBranchPrefix", - "getFileList", - "branchExists", - "getAllRenovateBranches", - "isBranchStale", - "getBranchPr", - "getBranchStatus", - "getBranchStatusCheck", - "setBranchStatus", - "deleteBranch", - "mergeBranch", - "getBranchLastCommitTime", - "findIssue", - "ensureIssue", - "ensureIssueClosing", "addAssignees", "addReviewers", + "branchExists", + "cleanRepo", + "commitFilesToBranch", + "createPr", + "deleteBranch", "deleteLabel", - "getIssueList", "ensureComment", "ensureCommentRemoval", - "getPrList", + "ensureIssue", + "ensureIssueClosing", + "findIssue", "findPr", - "createPr", + "getAllRenovateBranches", + "getBranchLastCommitTime", + "getBranchPr", + "getBranchStatus", + "getBranchStatusCheck", + "getCommitMessages", + "getFile", + "getFileList", + "getIssueList", "getPr", - "getPrFiles", - "updatePr", - "mergePr", "getPrBody", - "commitFilesToBranch", - "getFile", - "getCommitMessages", + "getPrFiles", + "getPrList", + "getRepoForceRebase", + "getRepoStatus", + "getRepos", "getVulnerabilityAlerts", + "initPlatform", + "initRepo", + "isBranchStale", + "mergeBranch", + "mergePr", + "setBaseBranch", + "setBranchPrefix", + "setBranchStatus", + "updatePr", ] `; diff --git a/test/platform/azure/__snapshots__/azure-got-wrapper.spec.js.snap b/test/platform/azure/__snapshots__/azure-got-wrapper.spec.ts.snap similarity index 100% rename from test/platform/azure/__snapshots__/azure-got-wrapper.spec.js.snap rename to test/platform/azure/__snapshots__/azure-got-wrapper.spec.ts.snap diff --git a/test/platform/azure/__snapshots__/azure-helper.spec.js.snap b/test/platform/azure/__snapshots__/azure-helper.spec.ts.snap similarity index 100% rename from test/platform/azure/__snapshots__/azure-helper.spec.js.snap rename to test/platform/azure/__snapshots__/azure-helper.spec.ts.snap diff --git a/test/platform/azure/__snapshots__/index.spec.js.snap b/test/platform/azure/__snapshots__/index.spec.ts.snap similarity index 100% rename from test/platform/azure/__snapshots__/index.spec.js.snap rename to test/platform/azure/__snapshots__/index.spec.ts.snap diff --git a/test/platform/azure/azure-got-wrapper.spec.js b/test/platform/azure/azure-got-wrapper.spec.ts similarity index 87% rename from test/platform/azure/azure-got-wrapper.spec.js rename to test/platform/azure/azure-got-wrapper.spec.ts index d764772efa6b95d8544e8a6a4e06b7c6fd33defd..fc7edd7775198a21afaf765aafff9032f3c6b46b 100644 --- a/test/platform/azure/azure-got-wrapper.spec.js +++ b/test/platform/azure/azure-got-wrapper.spec.ts @@ -1,6 +1,6 @@ describe('platform/azure/azure-got-wrapper', () => { - let hostRules; - let azure; + let hostRules: typeof import('../../../lib/util/host-rules'); + let azure: typeof import('../../../lib/platform/azure/azure-got-wrapper'); beforeEach(() => { // reset module jest.resetModules(); diff --git a/test/platform/azure/azure-helper.spec.js b/test/platform/azure/azure-helper.spec.ts similarity index 74% rename from test/platform/azure/azure-helper.spec.js rename to test/platform/azure/azure-helper.spec.ts index d8000a80c40cbe095b24ee38b97ee936e5950cf4..128235168d0700db2c6dfee05556d3cddd6d7533 100644 --- a/test/platform/azure/azure-helper.spec.js +++ b/test/platform/azure/azure-helper.spec.ts @@ -1,8 +1,10 @@ const { Readable } = require('stream'); describe('platform/azure/helpers', () => { - let azureHelper; - let azureApi; + let azureHelper: typeof import('../../../lib/platform/azure/azure-helper'); + let azureApi: jest.Mocked< + typeof import('../../../lib/platform/azure/azure-got-wrapper') + >; beforeEach(() => { // reset module @@ -30,9 +32,11 @@ describe('platform/azure/helpers', () => { ); expect(res).toBe(`testBB`); }); - it('should log error and return null', () => { - const res = azureHelper.getBranchNameWithoutRefsheadsPrefix(); - expect(res).toBeNull(); + it('should log error and return undefined', () => { + const res = azureHelper.getBranchNameWithoutRefsheadsPrefix( + undefined as any + ); + expect(res).toBeUndefined(); }); it('should return the input', () => { const res = azureHelper.getBranchNameWithoutRefsheadsPrefix('testBB'); @@ -42,23 +46,32 @@ describe('platform/azure/helpers', () => { describe('getRef', () => { it('should get the ref', async () => { - azureApi.gitApi.mockImplementationOnce(() => ({ - getRefs: jest.fn(() => [{ objectId: 132 }]), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getRefs: jest.fn(() => [{ objectId: 132 }]), + } as any) + ); const res = await azureHelper.getRefs('123', 'branch'); expect(res).toMatchSnapshot(); }); it('should get 0 ref', async () => { - azureApi.gitApi.mockImplementationOnce(() => ({ - getRefs: jest.fn(() => []), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getRefs: jest.fn(() => []), + } as any) + ); const res = await azureHelper.getRefs('123'); expect(res.length).toBe(0); }); it('should get the ref', async () => { - azureApi.gitApi.mockImplementationOnce(() => ({ - getRefs: jest.fn(() => [{ objectId: '132' }]), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getRefs: jest.fn(() => [{ objectId: '132' }]), + } as any) + ); const res = await azureHelper.getRefs('123', 'refs/head/branch1'); expect(res).toMatchSnapshot(); }); @@ -66,9 +79,12 @@ describe('platform/azure/helpers', () => { describe('getAzureBranchObj', () => { it('should be the branch object formated', async () => { - azureApi.gitApi.mockImplementationOnce(() => ({ - getRefs: jest.fn(() => [{ objectId: '132' }]), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getRefs: jest.fn(() => [{ objectId: '132' }]), + } as any) + ); const res = await azureHelper.getAzureBranchObj( '123', 'branchName', @@ -77,9 +93,12 @@ describe('platform/azure/helpers', () => { expect(res).toMatchSnapshot(); }); it('should be the branch object formated', async () => { - azureApi.gitApi.mockImplementationOnce(() => ({ - getRefs: jest.fn(() => []), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getRefs: jest.fn(() => []), + } as any) + ); const res = await azureHelper.getAzureBranchObj('123', 'branchName'); expect(res).toMatchSnapshot(); }); @@ -101,9 +120,12 @@ describe('platform/azure/helpers', () => { }, }); - azureApi.gitApi.mockImplementationOnce(() => ({ - getItemText: jest.fn(() => mockEventStream), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getItemText: jest.fn(() => mockEventStream), + } as any) + ); const res = await azureHelper.getChanges( [ @@ -119,9 +141,12 @@ describe('platform/azure/helpers', () => { expect(res).toMatchSnapshot(); }); it('should be get the commit obj formated (file to create)', async () => { - azureApi.gitApi.mockImplementationOnce(() => ({ - getItemText: jest.fn(() => null), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getItemText: jest.fn(() => null), + } as any) + ); const res = await azureHelper.getChanges( [ @@ -154,9 +179,12 @@ describe('platform/azure/helpers', () => { }, }); - azureApi.gitApi.mockImplementationOnce(() => ({ - getItemText: jest.fn(() => mockEventStream), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getItemText: jest.fn(() => mockEventStream), + } as any) + ); const res = await azureHelper.getFile( '123', @@ -182,9 +210,12 @@ describe('platform/azure/helpers', () => { }, }); - azureApi.gitApi.mockImplementationOnce(() => ({ - getItemText: jest.fn(() => mockEventStream), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getItemText: jest.fn(() => mockEventStream), + } as any) + ); const res = await azureHelper.getFile( '123', @@ -210,9 +241,12 @@ describe('platform/azure/helpers', () => { }, }); - azureApi.gitApi.mockImplementationOnce(() => ({ - getItemText: jest.fn(() => mockEventStream), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getItemText: jest.fn(() => mockEventStream), + } as any) + ); const res = await azureHelper.getFile( '123', @@ -224,11 +258,14 @@ describe('platform/azure/helpers', () => { }); it('should return null because the file is not readable', async () => { - azureApi.gitApi.mockImplementationOnce(() => ({ - getItemText: jest.fn(() => ({ - readable: false, - })), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getItemText: jest.fn(() => ({ + readable: false, + })), + } as any) + ); const res = await azureHelper.getFile( '123', @@ -257,33 +294,36 @@ describe('platform/azure/helpers', () => { describe('getRenovatePRFormat', () => { it('should be formated (closed)', () => { - const res = azureHelper.getRenovatePRFormat({ status: 2 }); + const res = azureHelper.getRenovatePRFormat({ status: 2 } as any); expect(res).toMatchSnapshot(); }); it('should be formated (closed v2)', () => { - const res = azureHelper.getRenovatePRFormat({ status: 3 }); + const res = azureHelper.getRenovatePRFormat({ status: 3 } as any); expect(res).toMatchSnapshot(); }); it('should be formated (not closed)', () => { - const res = azureHelper.getRenovatePRFormat({ status: 1 }); + const res = azureHelper.getRenovatePRFormat({ status: 1 } as any); expect(res).toMatchSnapshot(); }); it('should be formated (isConflicted)', () => { - const res = azureHelper.getRenovatePRFormat({ mergeStatus: 2 }); + const res = azureHelper.getRenovatePRFormat({ mergeStatus: 2 } as any); expect(res).toMatchSnapshot(); }); }); describe('getCommitDetails', () => { it('should get commit details', async () => { - azureApi.gitApi.mockImplementationOnce(() => ({ - getCommit: jest.fn(() => ({ - parents: ['123456'], - })), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getCommit: jest.fn(() => ({ + parents: ['123456'], + })), + } as any) + ); const res = await azureHelper.getCommitDetails('123', '123456'); expect(res).toMatchSnapshot(); }); diff --git a/test/platform/azure/index.spec.js b/test/platform/azure/index.spec.ts similarity index 63% rename from test/platform/azure/index.spec.js rename to test/platform/azure/index.spec.ts index 43bfc394309e93a53f9474a0d597e6764d148d17..570dbd81576ca15b2c11245f8a849fa59d8924aa 100644 --- a/test/platform/azure/index.spec.js +++ b/test/platform/azure/index.spec.ts @@ -1,8 +1,12 @@ describe('platform/azure', () => { - let azure; - let azureApi; - let azureHelper; - let hostRules; + let azure: jest.Mocked<typeof import('../../../lib/platform/azure')>; + let azureApi: jest.Mocked< + typeof import('../../../lib/platform/azure/azure-got-wrapper') + >; + let azureHelper: jest.Mocked< + typeof import('../../../lib/platform/azure/azure-helper') + >; + let hostRules: jest.Mocked<typeof import('../../../lib/util/host-rules')>; let GitStorage; beforeEach(() => { // reset module @@ -45,35 +49,42 @@ describe('platform/azure', () => { azure.cleanRepo(); }); - function getRepos(token, endpoint) { - azureApi.gitApi.mockImplementationOnce(() => ({ - getRepositories: jest.fn(() => [ - { - name: 'repo1', - project: { - name: 'prj1', - }, - }, - { - name: 'repo2', - project: { - name: 'prj1', - }, - }, - ]), - })); - return azure.getRepos(token, endpoint); + // do we need the args? + // eslint-disable-next-line @typescript-eslint/no-unused-vars + function getRepos(_token: string, _endpoint: string) { + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getRepositories: jest.fn(() => [ + { + name: 'repo1', + project: { + name: 'prj1', + }, + }, + { + name: 'repo2', + project: { + name: 'prj1', + }, + }, + ]), + } as any) + ); + return azure.getRepos(); } describe('initPlatform()', () => { it('should throw if no endpoint', () => { expect(() => { - azure.initPlatform({}); + azure.initPlatform({} as any); }).toThrow(); }); it('should throw if no token', () => { expect(() => { - azure.initPlatform({ endpoint: 'https://dev.azure.com/renovate12345' }); + azure.initPlatform({ + endpoint: 'https://dev.azure.com/renovate12345', + } as any); }).toThrow(); }); it('should init', () => { @@ -109,30 +120,36 @@ describe('platform/azure', () => { azure.cleanRepo(); }); }); - function initRepo(...args) { - azureApi.gitApi.mockImplementationOnce(() => ({ - getRepositories: jest.fn(() => [ - { - name: 'some-repo', - id: '1', - privateRepo: true, - isFork: false, - defaultBranch: 'defBr', - project: { - name: 'some-repo', - }, - }, - { - name: 'repo2', - project: { - name: 'prj2', - }, - }, - ]), - })); - azureApi.gitApi.mockImplementationOnce(() => ({ - getBranch: jest.fn(() => ({ commit: { commitId: '1234' } })), - })); + function initRepo(...args: any[]) { + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getRepositories: jest.fn(() => [ + { + name: 'some-repo', + id: '1', + privateRepo: true, + isFork: false, + defaultBranch: 'defBr', + project: { + name: 'some-repo', + }, + }, + { + name: 'repo2', + project: { + name: 'prj2', + }, + }, + ]), + } as any) + ); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getBranch: jest.fn(() => ({ commit: { commitId: '1234' } })), + } as any) + ); azureHelper.getProjectAndRepo.mockImplementationOnce(() => ({ project: 'some-repo', repo: 'some-repo', @@ -140,10 +157,8 @@ describe('platform/azure', () => { if (typeof args[0] === 'string') { return azure.initRepo({ - repository: args[0], - token: args[1], - endpoint: 'https://dev.azure.com/renovate12345', - }); + repository: args[0] as string, + } as any); } return azure.initRepo({ @@ -173,16 +188,19 @@ describe('platform/azure', () => { describe('findPr(branchName, prTitle, state)', () => { it('returns pr if found it open', async () => { - azureApi.gitApi.mockImplementationOnce(() => ({ - getPullRequests: jest.fn(() => [ - { - pullRequestId: 1, - sourceRefName: 'refs/heads/branch-a', - title: 'branch a pr', - state: 'open', - }, - ]), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getPullRequests: jest.fn(() => [ + { + pullRequestId: 1, + sourceRefName: 'refs/heads/branch-a', + title: 'branch a pr', + state: 'open', + }, + ]), + } as any) + ); azureHelper.getNewBranchName.mockImplementationOnce( () => 'refs/heads/branch-a' ); @@ -196,16 +214,19 @@ describe('platform/azure', () => { expect(res).toMatchSnapshot(); }); it('returns pr if found not open', async () => { - azureApi.gitApi.mockImplementationOnce(() => ({ - getPullRequests: jest.fn(() => [ - { - pullRequestId: 1, - sourceRefName: 'refs/heads/branch-a', - title: 'branch a pr', - state: 'closed', - }, - ]), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getPullRequests: jest.fn(() => [ + { + pullRequestId: 1, + sourceRefName: 'refs/heads/branch-a', + title: 'branch a pr', + state: 'closed', + }, + ]), + } as any) + ); azureHelper.getNewBranchName.mockImplementationOnce( () => 'refs/heads/branch-a' ); @@ -219,16 +240,19 @@ describe('platform/azure', () => { expect(res).toMatchSnapshot(); }); it('returns pr if found it close', async () => { - azureApi.gitApi.mockImplementationOnce(() => ({ - getPullRequests: jest.fn(() => [ - { - pullRequestId: 1, - sourceRefName: 'refs/heads/branch-a', - title: 'branch a pr', - state: 'closed', - }, - ]), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getPullRequests: jest.fn(() => [ + { + pullRequestId: 1, + sourceRefName: 'refs/heads/branch-a', + title: 'branch a pr', + state: 'closed', + }, + ]), + } as any) + ); azureHelper.getNewBranchName.mockImplementationOnce( () => 'refs/heads/branch-a' ); @@ -242,16 +266,19 @@ describe('platform/azure', () => { expect(res).toMatchSnapshot(); }); it('returns pr if found it all state', async () => { - azureApi.gitApi.mockImplementationOnce(() => ({ - getPullRequests: jest.fn(() => [ - { - pullRequestId: 1, - sourceRefName: 'refs/heads/branch-a', - title: 'branch a pr', - state: 'closed', - }, - ]), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getPullRequests: jest.fn(() => [ + { + pullRequestId: 1, + sourceRefName: 'refs/heads/branch-a', + title: 'branch a pr', + state: 'closed', + }, + ]), + } as any) + ); azureHelper.getNewBranchName.mockImplementationOnce( () => 'refs/heads/branch-a' ); @@ -274,27 +301,33 @@ describe('platform/azure', () => { describe('getBranchPr(branchName)', () => { it('should return null if no PR exists', async () => { await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementationOnce(() => ({ - findPr: jest.fn(() => false), - getPr: jest.fn(() => { - 'myPRName'; - }), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + findPr: jest.fn(() => false), + getPr: jest.fn(() => { + 'myPRName'; + }), + } as any) + ); const pr = await azure.getBranchPr('somebranch'); expect(pr).toBeNull(); }); it('should return the pr', async () => { await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementation(() => ({ - getPullRequests: jest.fn(() => [ - { - pullRequestId: 1, - sourceRefName: 'refs/heads/branch-a', - title: 'branch a pr', - status: 2, - }, - ]), - })); + azureApi.gitApi.mockImplementation( + () => + ({ + getPullRequests: jest.fn(() => [ + { + pullRequestId: 1, + sourceRefName: 'refs/heads/branch-a', + title: 'branch a pr', + status: 2, + }, + ]), + } as any) + ); azureHelper.getNewBranchName.mockImplementation( () => 'refs/heads/branch-a' ); @@ -323,17 +356,23 @@ describe('platform/azure', () => { }); it('should pass through success', async () => { await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementationOnce(() => ({ - getBranch: jest.fn(() => ({ aheadCount: 0 })), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getBranch: jest.fn(() => ({ aheadCount: 0 })), + } as any) + ); const res = await azure.getBranchStatus('somebranch', []); expect(res).toEqual('success'); }); it('should pass through failed', async () => { await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementationOnce(() => ({ - getBranch: jest.fn(() => ({ aheadCount: 123 })), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getBranch: jest.fn(() => ({ aheadCount: 123 })), + } as any) + ); const res = await azure.getBranchStatus('somebranch', []); expect(res).toEqual('pending'); }); @@ -341,25 +380,31 @@ describe('platform/azure', () => { describe('getPr(prNo)', () => { it('should return null if no prNo is passed', async () => { - const pr = await azure.getPr(null); + const pr = await azure.getPr(0); expect(pr).toBeNull(); }); it('should return null if no PR is returned from azure', async () => { await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementationOnce(() => ({ - getPullRequests: jest.fn(() => []), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getPullRequests: jest.fn(() => []), + } as any) + ); const pr = await azure.getPr(1234); expect(pr).toBeNull(); }); it('should return a pr in the right format', async () => { await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementationOnce(() => ({ - getPullRequests: jest.fn(() => [{ pullRequestId: 1234 }]), - getPullRequestLabels: jest.fn(() => [ - { active: true, name: 'renovate' }, - ]), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getPullRequests: jest.fn(() => [{ pullRequestId: 1234 }]), + getPullRequestLabels: jest.fn(() => [ + { active: true, name: 'renovate' }, + ]), + } as any) + ); azureHelper.getRenovatePRFormat.mockImplementation(() => ({ pullRequestId: 1234, labels: ['renovate'], @@ -372,13 +417,16 @@ describe('platform/azure', () => { describe('createPr()', () => { it('should create and return a PR object', async () => { await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementationOnce(() => ({ - createPullRequest: jest.fn(() => ({ - pullRequestId: 456, - displayNumber: `Pull Request #456`, - })), - createPullRequestLabel: jest.fn(() => ({})), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + createPullRequest: jest.fn(() => ({ + pullRequestId: 456, + displayNumber: `Pull Request #456`, + })), + createPullRequestLabel: jest.fn(() => ({})), + } as any) + ); azureHelper.getRenovatePRFormat.mockImplementation(() => ({ displayNumber: 'Pull Request #456', number: 456, @@ -394,13 +442,16 @@ describe('platform/azure', () => { }); it('should create and return a PR object from base branch', async () => { await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementationOnce(() => ({ - createPullRequest: jest.fn(() => ({ - pullRequestId: 456, - displayNumber: `Pull Request #456`, - })), - createPullRequestLabel: jest.fn(() => ({})), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + createPullRequest: jest.fn(() => ({ + pullRequestId: 456, + displayNumber: `Pull Request #456`, + })), + createPullRequestLabel: jest.fn(() => ({})), + } as any) + ); azureHelper.getRenovatePRFormat.mockImplementation(() => ({ displayNumber: 'Pull Request #456', number: 456, @@ -437,11 +488,14 @@ describe('platform/azure', () => { const updateFn = jest .fn(() => prUpdateResult) .mockName('updatePullRequest'); - azureApi.gitApi.mockImplementationOnce(() => ({ - createPullRequest: jest.fn(() => prResult), - createPullRequestLabel: jest.fn(() => ({})), - updatePullRequest: updateFn, - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + createPullRequest: jest.fn(() => prResult), + createPullRequestLabel: jest.fn(() => ({})), + updatePullRequest: updateFn, + } as any) + ); azureHelper.getRenovatePRFormat.mockImplementation(x => x); const pr = await azure.createPr( 'some-branch', @@ -459,18 +513,24 @@ describe('platform/azure', () => { describe('updatePr(prNo, title, body)', () => { it('should update the PR', async () => { await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementationOnce(() => ({ - updatePullRequest: jest.fn(), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + updatePullRequest: jest.fn(), + } as any) + ); await azure.updatePr(1234, 'The New Title', 'Hello world again'); expect(azureApi.gitApi.mock.calls).toMatchSnapshot(); }); it('should update the PR without description', async () => { await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementationOnce(() => ({ - updatePullRequest: jest.fn(), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + updatePullRequest: jest.fn(), + } as any) + ); await azure.updatePr(1234, 'The New Title - autoclose'); expect(azureApi.gitApi.mock.calls).toMatchSnapshot(); }); @@ -479,9 +539,12 @@ describe('platform/azure', () => { describe('ensureComment', () => { it('add comment', async () => { await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementation(() => ({ - createThread: jest.fn(() => [{ id: 123 }]), - })); + azureApi.gitApi.mockImplementation( + () => + ({ + createThread: jest.fn(() => [{ id: 123 }]), + } as any) + ); await azure.ensureComment(42, 'some-subject', 'some\ncontent'); expect(azureApi.gitApi.mock.calls).toMatchSnapshot(); }); @@ -490,27 +553,36 @@ describe('platform/azure', () => { describe('ensureCommentRemoval', () => { it('deletes comment if found', async () => { await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementation(() => ({ - getThreads: jest.fn(() => [ - { comments: [{ content: '### some-subject\n\nblabla' }], id: 123 }, - ]), - updateThread: jest.fn(), - })); + azureApi.gitApi.mockImplementation( + () => + ({ + getThreads: jest.fn(() => [ + { + comments: [{ content: '### some-subject\n\nblabla' }], + id: 123, + }, + ]), + updateThread: jest.fn(), + } as any) + ); await azure.ensureCommentRemoval(42, 'some-subject'); expect(azureApi.gitApi).toHaveBeenCalledTimes(3); }); it('nothing should happen, no number', async () => { - await azure.ensureCommentRemoval(); + await azure.ensureCommentRemoval(0, 'test'); expect(azureApi.gitApi).toHaveBeenCalledTimes(0); }); it('comment not found', async () => { await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementation(() => ({ - getThreads: jest.fn(() => [ - { comments: [{ content: 'stupid comment' }], id: 123 }, - ]), - updateThread: jest.fn(), - })); + azureApi.gitApi.mockImplementation( + () => + ({ + getThreads: jest.fn(() => [ + { comments: [{ content: 'stupid comment' }], id: 123 }, + ]), + updateThread: jest.fn(), + } as any) + ); await azure.ensureCommentRemoval(42, 'some-subject'); expect(azureApi.gitApi).toHaveBeenCalledTimes(3); }); @@ -519,9 +591,12 @@ describe('platform/azure', () => { describe('Assignees', () => { it('addAssignees', async () => { await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementation(() => ({ - createThread: jest.fn(() => [{ id: 123 }]), - })); + azureApi.gitApi.mockImplementation( + () => + ({ + createThread: jest.fn(() => [{ id: 123 }]), + } as any) + ); await azure.addAssignees(123, ['test@bonjour.fr']); expect(azureApi.gitApi).toHaveBeenCalledTimes(3); }); @@ -530,19 +605,25 @@ describe('platform/azure', () => { describe('Reviewers', () => { it('addReviewers', async () => { await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementation(() => ({ - getRepositories: jest.fn(() => [{ id: '1', project: { id: 2 } }]), - createPullRequestReviewer: jest.fn(), - })); - azureApi.getCoreApi.mockImplementation(() => ({ - getTeams: jest.fn(() => [ - { id: 3, name: 'abc' }, - { id: 4, name: 'def' }, - ]), - getTeamMembersWithExtendedProperties: jest.fn(() => [ - { identity: { displayName: 'jyc', uniqueName: 'jyc', id: 123 } }, - ]), - })); + azureApi.gitApi.mockImplementation( + () => + ({ + getRepositories: jest.fn(() => [{ id: '1', project: { id: 2 } }]), + createPullRequestReviewer: jest.fn(), + } as any) + ); + azureApi.getCoreApi.mockImplementation( + () => + ({ + getTeams: jest.fn(() => [ + { id: 3, name: 'abc' }, + { id: 4, name: 'def' }, + ]), + getTeamMembersWithExtendedProperties: jest.fn(() => [ + { identity: { displayName: 'jyc', uniqueName: 'jyc', id: 123 } }, + ]), + } as any) + ); await azure.addReviewers(123, ['test@bonjour.fr', 'jyc', 'def']); expect(azureApi.gitApi).toHaveBeenCalledTimes(3); }); @@ -558,12 +639,12 @@ describe('platform/azure', () => { describe('Not supported by Azure DevOps (yet!)', () => { it('setBranchStatus', () => { - const res = azure.setBranchStatus(); + const res = azure.setBranchStatus('test', 'test', 'test', 'test', 'test'); expect(res).toBeUndefined(); }); it('mergePr', async () => { - const res = await azure.mergePr(); + const res = await azure.mergePr(0); expect(res).toBeUndefined(); }); @@ -584,9 +665,12 @@ describe('platform/azure', () => { describe('deleteLabel()', () => { it('Should delete a label', async () => { await initRepo({ repository: 'some/repo', token: 'token' }); - azureApi.gitApi.mockImplementationOnce(() => ({ - deletePullRequestLabels: jest.fn(), - })); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + deletePullRequestLabels: jest.fn(), + } as any) + ); await azure.deleteLabel(1234, 'rebase'); expect(azureApi.gitApi.mock.calls).toMatchSnapshot(); }); diff --git a/test/platform/index.spec.js b/test/platform/index.spec.js index a17328c698dc5a652ab5b366629e6441f38f9587..2d3b3e52cc59ead3a7888bcdf4a2aff953910bc1 100644 --- a/test/platform/index.spec.js +++ b/test/platform/index.spec.js @@ -31,7 +31,7 @@ describe('platform', () => { }); it('has a list of supported methods for azure', () => { - const azureMethods = Object.keys(azure); + const azureMethods = Object.keys(azure).sort(); expect(azureMethods).toMatchSnapshot(); });