diff --git a/lib/api/gh-got-retry.js b/lib/api/gh-got-retry.js index 6534ba74d80c9f5df7b6e9b6130af3295a018dab..8a947c2ae93228905bef5c719046f1931cf26b29 100644 --- a/lib/api/gh-got-retry.js +++ b/lib/api/gh-got-retry.js @@ -7,7 +7,7 @@ function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } -async function ghGotRetry(path, opts, retries = 5) { +async function get(path, opts, retries = 5) { try { if (appMode) { // eslint-disable-next-line no-param-reassign @@ -33,7 +33,7 @@ async function ghGotRetry(path, opts, retries = 5) { if (process.env.NODE_ENV !== 'test') { await sleep(5000 / retries); } - return ghGotRetry(path, opts, retries - 1); + return get(path, opts, retries - 1); } if ( retries > 0 && @@ -50,7 +50,7 @@ async function ghGotRetry(path, opts, retries = 5) { if (process.env.NODE_ENV !== 'test') { await sleep(180000 / (retries * retries)); } - return ghGotRetry(path, opts, retries - 1); + return get(path, opts, retries - 1); } if ( err.statusCode === 403 && @@ -66,7 +66,7 @@ async function ghGotRetry(path, opts, retries = 5) { if (process.env.NODE_ENV !== 'test') { await sleep(60000 / (retries * retries)); } - return ghGotRetry(path, opts, retries - 1); + return get(path, opts, retries - 1); } logger.info({ headers: err.headers }, 'Failed retrying request'); } @@ -77,13 +77,13 @@ async function ghGotRetry(path, opts, retries = 5) { const helpers = ['get', 'post', 'put', 'patch', 'head', 'delete']; for (const x of helpers) { - ghGotRetry[x] = async (path, opts, retries = 3) => { + get[x] = async (path, opts, retries = 3) => { try { const res = await ghGot[x](path, opts); return res; } catch (err) { if (err.statusCode === 502 && retries > 0) { - return ghGotRetry[x](path, opts, retries - 1); + return get[x](path, opts, retries - 1); } throw err; } @@ -91,8 +91,8 @@ for (const x of helpers) { } let appMode = false; -ghGotRetry.setAppMode = function setAppMode(val) { +get.setAppMode = function setAppMode(val) { appMode = val; }; -module.exports = ghGotRetry; +module.exports = get; diff --git a/lib/api/github.js b/lib/api/github.js index 3a7d4a3eb267d8d9e5209f9fc77315d0de491cbf..93b1fa398efbb90daada523241b36ebc51107e3d 100644 --- a/lib/api/github.js +++ b/lib/api/github.js @@ -1,5 +1,5 @@ let logger = require('../logger'); -const ghGotRetry = require('./gh-got-retry'); +const get = require('./gh-got-retry'); const config = {}; @@ -60,7 +60,7 @@ async function getInstallations(appToken) { authorization: `Bearer ${appToken}`, }, }; - const res = await ghGotRetry(url, options); + const res = await get(url, options); logger.debug(`Returning ${res.body.length} results`); return res.body; } catch (err) { @@ -80,7 +80,7 @@ async function getInstallationToken(appToken, installationId) { authorization: `Bearer ${appToken}`, }, }; - const res = await ghGotRetry.post(url, options); + const res = await get.post(url, options); return res.body.token; } catch (err) { logger.error({ err }, `GitHub getInstallationToken error`); @@ -99,7 +99,7 @@ async function getInstallationRepositories(userToken) { authorization: `token ${userToken}`, }, }; - const res = await ghGotRetry(url, options); + const res = await get(url, options); logger.debug( `Returning ${res.body.repositories.length} results from a total of ${res .body.total_count}` @@ -123,7 +123,7 @@ async function getRepos(token, endpoint) { process.env.GITHUB_ENDPOINT = endpoint; } try { - const res = await ghGotRetry('user/repos'); + const res = await get('user/repos'); return res.body.map(repo => repo.full_name); } catch (err) /* istanbul ignore next */ { logger.error({ err }, `GitHub getRepos error`); @@ -150,7 +150,7 @@ async function initRepo(repoName, token, endpoint, repoLogger) { config.fileList = null; const platformConfig = {}; try { - const res = await ghGotRetry(`repos/${repoName}`, { + const res = await get(`repos/${repoName}`, { headers: { accept: 'application/vnd.github.loki-preview+json', }, @@ -206,7 +206,7 @@ async function initRepo(repoName, token, endpoint, repoLogger) { } async function getBranchProtection(branchName) { - const res = await ghGotRetry( + const res = await get( `repos/${config.repoName}/branches/${branchName}/protection/required_status_checks`, { headers: { @@ -232,7 +232,7 @@ async function getFileList(branchName) { if (config.fileList) { return config.fileList; } - const res = await ghGotRetry( + const res = await get( `repos/${config.repoName}/git/trees/${branchName}?recursive=true` ); if (res.body.truncated) { @@ -261,7 +261,7 @@ async function findFilePaths(fileName, branchName = config.baseBranch) { async function branchExists(branchName) { logger.debug(`Checking if branch exists: ${branchName}`); try { - const res = await ghGotRetry( + const res = await get( `repos/${config.repoName}/git/refs/heads/${branchName}` ); if (Array.isArray(res.body)) { @@ -291,9 +291,8 @@ async function branchExists(branchName) { async function getAllRenovateBranches(branchPrefix) { logger.trace('getAllRenovateBranches'); - const allBranches = (await ghGotRetry( - `repos/${config.repoName}/git/refs/heads` - )).body; + const allBranches = (await get(`repos/${config.repoName}/git/refs/heads`)) + .body; return allBranches.reduce((arr, branch) => { if (branch.ref.indexOf(`refs/heads/${branchPrefix}`) === 0) { arr.push(branch.ref.substring('refs/heads/'.length)); @@ -321,7 +320,7 @@ async function getBranchPr(branchName) { const gotString = `repos/${config.repoName}/pulls?` + `state=open&base=${config.baseBranch}&head=${config.owner}:${branchName}`; - const res = await ghGotRetry(gotString); + const res = await get(gotString); if (!res.body.length) { return null; } @@ -342,14 +341,14 @@ async function getBranchStatus(branchName, requiredStatusChecks) { return 'failed'; } const gotString = `repos/${config.repoName}/commits/${branchName}/status`; - const res = await ghGotRetry(gotString); + const res = await get(gotString); return res.body.state; } async function getBranchStatusCheck(branchName, context) { const branchCommit = await getBranchCommit(branchName); const url = `repos/${config.repoName}/commits/${branchCommit}/statuses`; - const res = await ghGotRetry(url); + const res = await get(url); for (const check of res.body) { if (check.context === context) { return check.state; @@ -375,13 +374,11 @@ async function setBranchStatus( if (targetUrl) { options.target_url = targetUrl; } - await ghGotRetry.post(url, { body: options }); + await get.post(url, { body: options }); } async function deleteBranch(branchName) { - await ghGotRetry.delete( - `repos/${config.repoName}/git/refs/heads/${branchName}` - ); + await get.delete(`repos/${config.repoName}/git/refs/heads/${branchName}`); } async function mergeBranch(branchName, mergeType) { @@ -394,7 +391,7 @@ async function mergeBranch(branchName, mergeType) { }, }; try { - await ghGotRetry.patch(url, options); + await get.patch(url, options); } catch (err) { logger.warn({ err }, `Error pushing branch merge for ${branchName}`); throw new Error('branch-push failed'); @@ -408,7 +405,7 @@ async function mergeBranch(branchName, mergeType) { }, }; try { - await ghGotRetry.post(url, options); + await get.post(url, options); } catch (err) { logger.warn({ err }, `Error pushing branch merge for ${branchName}`); throw new Error('branch-merge-commit failed'); @@ -424,9 +421,7 @@ async function mergeBranch(branchName, mergeType) { async function getBranchLastCommitTime(branchName) { try { - const res = await ghGotRetry( - `repos/${config.repoName}/commits?sha=${branchName}` - ); + const res = await get(`repos/${config.repoName}/commits?sha=${branchName}`); return new Date(res.body[0].commit.committer.date); } catch (err) { logger.error({ err }, `getBranchLastCommitTime error`); @@ -438,19 +433,16 @@ async function getBranchLastCommitTime(branchName) { async function addAssignees(issueNo, assignees) { logger.debug(`Adding assignees ${assignees} to #${issueNo}`); - await ghGotRetry.post( - `repos/${config.repoName}/issues/${issueNo}/assignees`, - { - body: { - assignees, - }, - } - ); + await get.post(`repos/${config.repoName}/issues/${issueNo}/assignees`, { + body: { + assignees, + }, + }); } async function addReviewers(issueNo, reviewers) { logger.debug(`Adding reviewers ${reviewers} to #${issueNo}`); - const res = await ghGotRetry.post( + const res = await get.post( `repos/${config.repoName}/pulls/${issueNo}/requested_reviewers`, { headers: { @@ -466,7 +458,7 @@ async function addReviewers(issueNo, reviewers) { async function addLabels(issueNo, labels) { logger.debug(`Adding labels ${labels} to #${issueNo}`); - await ghGotRetry.post(`repos/${config.repoName}/issues/${issueNo}/labels`, { + await get.post(`repos/${config.repoName}/issues/${issueNo}/labels`, { body: labels, }); } @@ -475,7 +467,7 @@ async function findPr(branchName, prTitle, state = 'all') { logger.debug(`findPr(${branchName}, ${state})`); const urlString = `repos/${config.repoName}/pulls?head=${config.owner}:${branchName}&state=${state}`; logger.debug(`findPr urlString: ${urlString}`); - const res = await ghGotRetry(urlString); + const res = await get(urlString); let pr = null; res.body.forEach(result => { if (!prTitle || result.title === prTitle) { @@ -494,7 +486,7 @@ async function checkForClosedPr(branchName, prTitle) { logger.debug(`checkForClosedPr(${branchName}, ${prTitle})`); const url = `repos/${config.repoName}/pulls?state=closed&head=${config.owner}:${branchName}`; try { - const res = await ghGotRetry(url); + const res = await get(url); // Return true if any of the titles match exactly return res.body.some( pr => @@ -511,7 +503,7 @@ async function checkForClosedPr(branchName, prTitle) { // Creates PR and returns PR number async function createPr(branchName, title, body, useDefaultBranch) { const base = useDefaultBranch ? config.defaultBranch : config.baseBranch; - const pr = (await ghGotRetry.post(`repos/${config.repoName}/pulls`, { + const pr = (await get.post(`repos/${config.repoName}/pulls`, { body: { title, head: branchName, @@ -528,7 +520,7 @@ async function getPr(prNo) { if (!prNo) { return null; } - const pr = (await ghGotRetry(`repos/${config.repoName}/pulls/${prNo}`)).body; + const pr = (await get(`repos/${config.repoName}/pulls/${prNo}`)).body; if (!pr) { return null; } @@ -549,7 +541,7 @@ async function getPr(prNo) { } else { // Check if only one author of all commits logger.debug('Checking all commits'); - const prCommits = (await ghGotRetry( + const prCommits = (await get( `repos/${config.repoName}/pulls/${prNo}/commits` )).body; const authors = prCommits.reduce((arr, commit) => { @@ -581,8 +573,7 @@ async function getPr(prNo) { } async function getAllPrs() { - const all = (await ghGotRetry(`repos/${config.repoName}/pulls?state=open`)) - .body; + const all = (await get(`repos/${config.repoName}/pulls?state=open`)).body; return all.map(pr => ({ number: pr.number, branchName: pr.head.ref, @@ -590,7 +581,7 @@ async function getAllPrs() { } async function updatePr(prNo, title, body) { - await ghGotRetry.patch(`repos/${config.repoName}/pulls/${prNo}`, { + await get.patch(`repos/${config.repoName}/pulls/${prNo}`, { body: { title, body }, }); } @@ -605,7 +596,7 @@ async function mergePr(pr) { options.body.merge_method = config.mergeMethod; try { logger.debug({ options, url }, `mergePr`); - await ghGotRetry.put(url, options); + await get.put(url, options); } catch (err) { logger.error({ err }, `Failed to ${options.body.merge_method} PR`); return false; @@ -615,13 +606,13 @@ async function mergePr(pr) { options.body.merge_method = 'rebase'; try { logger.debug({ options, url }, `mergePr`); - await ghGotRetry.put(url, options); + await get.put(url, options); } catch (err1) { logger.debug({ err: err1 }, `Failed to ${options.body.merge_method} PR`); try { options.body.merge_method = 'squash'; logger.debug({ options, url }, `mergePr`); - await ghGotRetry.put(url, options); + await get.put(url, options); } catch (err2) { logger.debug( { err: err2 }, @@ -630,7 +621,7 @@ async function mergePr(pr) { try { options.body.merge_method = 'merge'; logger.debug({ options, url }, `mergePr`); - await ghGotRetry.put(url, options); + await get.put(url, options); } catch (err3) { logger.debug( { err: err3 }, @@ -654,7 +645,7 @@ async function mergePr(pr) { async function getFile(filePath, branchName) { logger.trace(`getFile(filePath=${filePath}, branchName=${branchName})`); - const res = await ghGotRetry( + const res = await get( `repos/${config.repoName}/contents/${filePath}?ref=${branchName || config.baseBranch}` ); @@ -694,7 +685,7 @@ async function getFileJson(filePath, branchName) { async function getSubDirectories(path) { logger.trace(`getSubDirectories(path=${path})`); - const res = await ghGotRetry(`repos/${config.repoName}/contents/${path}`); + const res = await get(`repos/${config.repoName}/contents/${path}`); const directoryList = []; res.body.forEach(item => { if (item.type === 'dir') { @@ -740,7 +731,7 @@ async function commitFilesToBranch( // Creates a new branch with provided commit async function createBranch(branchName, commit = config.baseCommitSHA) { - await ghGotRetry.post(`repos/${config.repoName}/git/refs`, { + await get.post(`repos/${config.repoName}/git/refs`, { body: { ref: `refs/heads/${branchName}`, sha: commit, @@ -751,15 +742,12 @@ async function createBranch(branchName, commit = config.baseCommitSHA) { // Internal: Updates an existing branch to new commit sha async function updateBranch(branchName, commit) { logger.debug(`Updating branch ${branchName} with commit ${commit}`); - await ghGotRetry.patch( - `repos/${config.repoName}/git/refs/heads/${branchName}`, - { - body: { - sha: commit, - force: true, - }, - } - ); + await get.patch(`repos/${config.repoName}/git/refs/heads/${branchName}`, { + body: { + sha: commit, + force: true, + }, + }); } // Low-level commit operations @@ -767,7 +755,7 @@ async function updateBranch(branchName, commit) { // Create a blob with fileContents and return sha async function createBlob(fileContents) { logger.debug('Creating blob'); - return (await ghGotRetry.post(`repos/${config.repoName}/git/blobs`, { + return (await get.post(`repos/${config.repoName}/git/blobs`, { body: { encoding: 'base64', content: Buffer.from(fileContents).toString('base64'), @@ -777,7 +765,7 @@ async function createBlob(fileContents) { // Return the commit SHA for a branch async function getBranchCommit(branchName) { - const res = await ghGotRetry( + const res = await get( `repos/${config.repoName}/git/refs/heads/${branchName}` ); return res.body.object.sha; @@ -785,17 +773,15 @@ async function getBranchCommit(branchName) { async function getCommitDetails(commit) { logger.debug(`getCommitDetails(${commit})`); - const results = await ghGotRetry( - `repos/${config.repoName}/git/commits/${commit}` - ); + const results = await get(`repos/${config.repoName}/git/commits/${commit}`); return results.body; } // Return the tree SHA for a commit async function getCommitTree(commit) { logger.debug(`getCommitTree(${commit})`); - return (await ghGotRetry(`repos/${config.repoName}/git/commits/${commit}`)) - .body.tree.sha; + return (await get(`repos/${config.repoName}/git/commits/${commit}`)).body.tree + .sha; } // Create a tree and return SHA @@ -814,14 +800,14 @@ async function createTree(baseTree, files) { }); }); logger.trace({ body }, 'createTree body'); - return (await ghGotRetry.post(`repos/${config.repoName}/git/trees`, { body })) - .body.sha; + return (await get.post(`repos/${config.repoName}/git/trees`, { body })).body + .sha; } // Create a commit and return commit SHA async function createCommit(parent, tree, message) { logger.debug(`createCommit(${parent}, ${tree}, ${message})`); - return (await ghGotRetry.post(`repos/${config.repoName}/git/commits`, { + return (await get.post(`repos/${config.repoName}/git/commits`, { body: { message, parents: [parent], @@ -833,7 +819,7 @@ async function createCommit(parent, tree, message) { async function getCommitMessages() { logger.debug('getCommitMessages'); try { - const res = await ghGotRetry(`repos/${config.repoName}/commits`); + const res = await get(`repos/${config.repoName}/commits`); return res.body.map(commit => commit.commit.message); } catch (err) { logger.error({ err }, `getCommitMessages error`); diff --git a/lib/api/gitlab.js b/lib/api/gitlab.js index 48d3f6a0eab46af0f15755aa2bc309fdf5520417..afec808201f4646d2a131e568e82834565dfab8e 100644 --- a/lib/api/gitlab.js +++ b/lib/api/gitlab.js @@ -1,5 +1,5 @@ let logger = require('../logger'); -const glGot = require('gl-got'); +const get = require('gl-got'); const config = {}; @@ -60,7 +60,7 @@ async function getRepos(token, endpoint) { let res; do { const url = `projects?membership=true&per_page=100&page=${i}`; - res = await glGot(url); + res = await get(url); projects = projects.concat( res.body.map(repo => repo.path_with_namespace) ); @@ -94,7 +94,7 @@ async function initRepo(repoName, token, endpoint, repoLogger) { try { logger.debug(`Determining Gitlab API version`); // projects/owned route deprecated in v4 - await glGot(`projects/owned`); + await get(`projects/owned`); config.apiVersion = 'v3'; } catch (err) { config.apiVersion = 'v4'; @@ -103,12 +103,12 @@ async function initRepo(repoName, token, endpoint, repoLogger) { config.repoName = repoName.replace('/', '%2F'); config.fileList = null; try { - const res = await glGot(`projects/${config.repoName}`); + const res = await get(`projects/${config.repoName}`); config.defaultBranch = res.body.default_branch; config.baseBranch = config.defaultBranch; logger.debug(`${repoName} default branch = ${config.baseBranch}`); // Discover our user email - config.email = (await glGot(`user`)).body.email; + config.email = (await get(`user`)).body.email; } catch (err) { logger.error({ err }, `GitLab init error`); throw err; @@ -129,7 +129,7 @@ async function getFileList(branchName) { if (config.fileList) { return config.fileList; } - const res = await glGot( + const res = await get( `projects/${config.repoName}/repository/tree?ref=${branchName}&recursive=1` ); config.fileList = res.body @@ -156,7 +156,7 @@ async function branchExists(branchName) { '/', '%2F' )}`; - const res = await glGot(url); + const res = await get(url); if (res.statusCode === 200) { logger.debug('Branch exists'); return true; @@ -183,7 +183,7 @@ async function getBranch(branchName) { '%2F' )}`; try { - return (await glGot(url)).body; + return (await get(url)).body; } catch (err) { logger.warn({ err }, `Failed to getBranch ${branchName}`); return null; @@ -194,7 +194,7 @@ async function getBranch(branchName) { async function getBranchPr(branchName) { logger.debug(`getBranchPr(${branchName})`); const urlString = `projects/${config.repoName}/merge_requests?state=opened`; - const res = await glGot(urlString); + const res = await get(urlString); logger.debug(`Got res with ${res.body.length} results`); let pr = null; res.body.forEach(result => { @@ -225,11 +225,11 @@ async function getBranchStatus(branchName, requiredStatusChecks) { '/', '%2F' )}`; - let res = await glGot(url); + let res = await get(url); const branchSha = res.body.commit.id; // Now, check the statuses for that commit url = `projects/${config.repoName}/repository/commits/${branchSha}/statuses`; - res = await glGot(url); + res = await get(url); logger.debug(`Got res with ${res.body.length} results`); if (res.body.length === 0) { // Return 'pending' if we have no status checks @@ -256,11 +256,11 @@ async function getBranchStatusCheck(branchName, context) { '/', '%2F' )}`; - let res = await glGot(url); + let res = await get(url); const branchSha = res.body.commit.id; // Now, check the statuses for that commit url = `projects/${config.repoName}/repository/commits/${branchSha}/statuses`; - res = await glGot(url); + res = await get(url); logger.debug(`Got res with ${res.body.length} results`); for (const check of res.body) { if (check.name === context) { @@ -282,7 +282,7 @@ async function setBranchStatus( '/', '%2F' )}`; - const res = await glGot(url); + const res = await get(url); const branchSha = res.body.commit.id; // Now, check the statuses for that commit url = `projects/${config.repoName}/statuses/${branchSha}`; @@ -294,11 +294,11 @@ async function setBranchStatus( if (targetUrl) { options.target_url = targetUrl; } - await glGot.post(url, { body: options }); + await get.post(url, { body: options }); } async function deleteBranch(branchName) { - await glGot.delete( + await get.delete( `projects/${config.repoName}/repository/branches/${branchName.replace( '/', '%2F' @@ -308,7 +308,7 @@ async function deleteBranch(branchName) { async function getBranchLastCommitTime(branchName) { try { - const res = await glGot( + const res = await get( `projects/${config.repoName}/repository/commits?ref_name=${branchName}` ); return new Date(res.body[0].committed_date); @@ -327,7 +327,7 @@ async function addAssignees(prNo, assignees) { } let url = `projects/${config.repoName}/merge_requests/${prNo}`; url = `${url}?assignee_id=${assignees[0]}`; - await glGot.put(url); + await get.put(url); } async function addReviewers(prNo, reviewers) { @@ -339,13 +339,13 @@ async function addLabels(prNo, labels) { logger.debug(`Adding labels ${labels} to #${prNo}`); let url = `projects/${config.repoName}/merge_requests/${prNo}`; url = `${url}?labels=${labels.join(',')}`; - await glGot.put(url); + await get.put(url); } async function findPr(branchName, prTitle, state = 'all') { logger.debug(`findPr(${branchName}, ${prTitle}, ${state})`); const urlString = `projects/${config.repoName}/merge_requests?state=${state}`; - const res = await glGot(urlString); + const res = await get(urlString); let pr = null; res.body.forEach(result => { if ( @@ -382,7 +382,7 @@ async function createPr(branchName, title, body, useDefaultBranch) { const description = body .replace(/Pull Request/g, 'Merge Request') .replace(/PR/g, 'MR'); - const res = await glGot.post(`projects/${config.repoName}/merge_requests`, { + const res = await get.post(`projects/${config.repoName}/merge_requests`, { body: { source_branch: branchName, target_branch: targetBranch, @@ -400,7 +400,7 @@ async function createPr(branchName, title, body, useDefaultBranch) { async function getPr(prNo) { logger.debug(`getPr(${prNo})`); const url = `projects/${config.repoName}/merge_requests/${prNo}`; - const pr = (await glGot(url)).body; + const pr = (await get(url)).body; // Harmonize fields with GitHub pr.number = config.apiVersion === 'v3' ? pr.id : pr.iid; pr.displayNumber = `Merge Request #${pr.iid}`; @@ -426,7 +426,7 @@ async function updatePr(prNo, title, body) { const description = body .replace(/Pull Request/g, 'Merge Request') .replace(/PR/g, 'MR'); - await glGot.put(`projects/${config.repoName}/merge_requests/${prNo}`, { + await get.put(`projects/${config.repoName}/merge_requests/${prNo}`, { body: { title, description, @@ -435,7 +435,7 @@ async function updatePr(prNo, title, body) { } async function mergePr(pr) { - await glGot.put( + await get.put( `projects/${config.repoName}/merge_requests/${pr.number}/merge`, { body: { @@ -460,7 +460,7 @@ async function getFile(filePath, branchName) { '%2F' )}?ref=${branchName || config.baseBranch}`; } - const res = await glGot(url); + const res = await get(url); return res.body.content; } @@ -505,7 +505,7 @@ async function createFile(branchName, filePath, fileContents, message) { content: Buffer.from(fileContents).toString('base64'), }; } - await glGot.post(url, opts); + await get.post(url, opts); } async function updateFile(branchName, filePath, fileContents, message) { @@ -530,12 +530,12 @@ async function updateFile(branchName, filePath, fileContents, message) { content: Buffer.from(fileContents).toString('base64'), }; } - await glGot.put(url, opts); + await get.put(url, opts); } async function getSubDirectories(path) { logger.trace(`getSubDirectories(path=${path})`); - const res = await glGot( + const res = await get( `projects/${config.repoName}/repository/tree?path=${path}` ); const directoryList = []; @@ -582,7 +582,7 @@ async function commitFilesToBranch( async function getCommitMessages() { logger.debug('getCommitMessages'); try { - const res = await glGot(`projects/${config.repoName}/repository/commits`); + const res = await get(`projects/${config.repoName}/repository/commits`); return res.body.map(commit => commit.title); } catch (err) { logger.error({ err }, `getCommitMessages error`); @@ -607,5 +607,5 @@ async function createBranch(branchName, ref = config.baseBranch) { ref, }; } - await glGot.post(`projects/${config.repoName}/repository/branches`, opts); + await get.post(`projects/${config.repoName}/repository/branches`, opts); } diff --git a/test/api/github.spec.js b/test/api/github.spec.js index 9d6e245dc6aa31e072af95a8a928c1b418b76aa3..8995c47a7a98ea5364609ab0d583d71683e92c11 100644 --- a/test/api/github.spec.js +++ b/test/api/github.spec.js @@ -3,7 +3,7 @@ const logger = require('../_fixtures/logger'); describe('api/github', () => { let github; let ghGot; - let ghGotRetry; + let get; beforeEach(() => { // clean up env delete process.env.GITHUB_TOKEN; @@ -12,7 +12,7 @@ describe('api/github', () => { // reset module jest.resetModules(); jest.mock('gh-got'); - ghGotRetry = require('../../lib/api/gh-got-retry'); + get = require('../../lib/api/gh-got-retry'); github = require('../../lib/api/github'); ghGot = require('gh-got'); }); @@ -22,7 +22,7 @@ describe('api/github', () => { ghGot.mockImplementationOnce(() => ({ body: ['a', 'b'], })); - ghGotRetry.setAppMode(true); + get.setAppMode(true); const installations = await github.getInstallations('sometoken'); expect(ghGot.mock.calls).toMatchSnapshot(); expect(installations).toMatchSnapshot(); diff --git a/test/api/gitlab.spec.js b/test/api/gitlab.spec.js index aaa65a26dff7fcbf2a2503d75bcc7d846e475d84..6d02a9832474e1a58ac1333ec929183ca780ff21 100644 --- a/test/api/gitlab.spec.js +++ b/test/api/gitlab.spec.js @@ -2,7 +2,7 @@ const logger = require('../_fixtures/logger'); describe('api/gitlab', () => { let gitlab; - let glGot; + let get; beforeEach(() => { // clean up env delete process.env.GITLAB_TOKEN; @@ -12,13 +12,13 @@ describe('api/gitlab', () => { jest.resetModules(); jest.mock('gl-got'); gitlab = require('../../lib/api/gitlab'); - glGot = require('gl-got'); + get = require('gl-got'); }); describe('getRepos', () => { async function getRepos(...args) { // repo info - glGot.mockImplementationOnce(() => ({ + get.mockImplementationOnce(() => ({ body: [ { path_with_namespace: 'a/b', @@ -40,7 +40,7 @@ describe('api/gitlab', () => { expect(err.message).toBe('No token found for getRepos'); }); it('should throw an error if it receives an error', async () => { - glGot.mockImplementation(() => { + get.mockImplementation(() => { throw new Error('getRepos error'); }); let err; @@ -53,12 +53,12 @@ describe('api/gitlab', () => { }); it('should return an array of repos', async () => { const repos = await getRepos('sometoken'); - expect(glGot.mock.calls).toMatchSnapshot(); + expect(get.mock.calls).toMatchSnapshot(); expect(repos).toMatchSnapshot(); }); it('should support a custom endpoint', async () => { const repos = await getRepos('sometoken', 'someendpoint'); - expect(glGot.mock.calls).toMatchSnapshot(); + expect(get.mock.calls).toMatchSnapshot(); expect(repos).toMatchSnapshot(); }); it('should fetch multiple pages', async () => { @@ -67,32 +67,32 @@ describe('api/gitlab', () => { for (let i = 0; i < repoCount; i += 1) { projects.push({ path_with_namespace: `project${i}` }); } - glGot.mockImplementationOnce(() => ({ + get.mockImplementationOnce(() => ({ body: projects.slice(0, 100), })); - glGot.mockImplementationOnce(() => ({ + get.mockImplementationOnce(() => ({ body: projects.slice(100, 200), })); - glGot.mockImplementationOnce(() => ({ + get.mockImplementationOnce(() => ({ body: projects.slice(200), })); const repos = await gitlab.getRepos('sometoken'); - expect(glGot.mock.calls).toMatchSnapshot(); + expect(get.mock.calls).toMatchSnapshot(); expect(repos.length).toBe(repoCount); }); }); async function initRepo(...args) { // projects/owned - glGot.mockImplementationOnce(); + get.mockImplementationOnce(); // projects/${config.repoName - glGot.mockImplementationOnce(() => ({ + get.mockImplementationOnce(() => ({ body: { default_branch: 'master', }, })); // user - glGot.mockImplementationOnce(() => ({ + get.mockImplementationOnce(() => ({ body: { email: 'a@b.com', }, @@ -116,7 +116,7 @@ describe('api/gitlab', () => { process.env.GITLAB_TOKEN = envToken; } const config = await initRepo('some/repo', ...args); - expect(glGot.mock.calls).toMatchSnapshot(); + expect(get.mock.calls).toMatchSnapshot(); expect(config).toMatchSnapshot(); expect(process.env.GITLAB_TOKEN).toBe(token); expect(process.env.GITLAB_ENDPOINT).toBe(endpoint); @@ -143,7 +143,7 @@ describe('api/gitlab', () => { ); }); it('should throw an error if receiving an error', async () => { - glGot.mockImplementation(() => { + get.mockImplementation(() => { throw new Error('always error'); }); let err; @@ -156,17 +156,17 @@ describe('api/gitlab', () => { }); it('should use api v4', async () => { // projects/owned - glGot.mockImplementationOnce(() => { + get.mockImplementationOnce(() => { throw new Error('any error'); }); // projects/${config.repoName - glGot.mockImplementationOnce(() => ({ + get.mockImplementationOnce(() => ({ body: { default_branch: 'master', }, })); // user - glGot.mockImplementationOnce(() => ({ + get.mockImplementationOnce(() => ({ body: { email: 'a@b.com', }, @@ -179,7 +179,7 @@ describe('api/gitlab', () => { it('sets the base branch', async () => { await initRepo('some/repo', 'token'); // getBranchCommit - glGot.mockImplementationOnce(() => ({ + get.mockImplementationOnce(() => ({ body: { object: { sha: '1238', @@ -187,13 +187,13 @@ describe('api/gitlab', () => { }, })); await gitlab.setBaseBranch('some-branch'); - expect(glGot.mock.calls).toMatchSnapshot(); + expect(get.mock.calls).toMatchSnapshot(); }); }); describe('findFilePaths(fileName)', () => { it('warns if truncated result', async () => { await initRepo('some/repo', 'token'); - glGot.mockImplementationOnce(() => ({ + get.mockImplementationOnce(() => ({ body: [], })); const files = await gitlab.findFilePaths('package.json'); @@ -201,7 +201,7 @@ describe('api/gitlab', () => { }); it('caches the result', async () => { await initRepo('some/repo', 'token'); - glGot.mockImplementationOnce(() => ({ + get.mockImplementationOnce(() => ({ body: [], })); let files = await gitlab.findFilePaths('package.json'); @@ -211,7 +211,7 @@ describe('api/gitlab', () => { }); it('should return the files matching the fileName', async () => { await initRepo('some/repo', 'token'); - glGot.mockImplementationOnce(() => ({ + get.mockImplementationOnce(() => ({ body: [ { type: 'blob', path: 'package.json' }, { @@ -228,17 +228,17 @@ describe('api/gitlab', () => { }); describe('branchExists(branchName)', () => { it('should return true if 200 OK', async () => { - glGot.mockImplementationOnce(() => ({ statusCode: 200 })); + get.mockImplementationOnce(() => ({ statusCode: 200 })); const branchExists = await gitlab.branchExists('foo'); expect(branchExists).toBe(true); }); it('should return false if not 200 OK', async () => { - glGot.mockImplementationOnce(() => ({ statusCode: 500 })); + get.mockImplementationOnce(() => ({ statusCode: 500 })); const branchExists = await gitlab.branchExists('foo'); expect(branchExists).toBe(false); }); it('should return false if 404 error received', async () => { - glGot.mockImplementationOnce(() => + get.mockImplementationOnce(() => Promise.reject({ statusCode: 404, }) @@ -247,7 +247,7 @@ describe('api/gitlab', () => { expect(branchExists).toBe(false); }); it('should return error if non-404 error thrown', async () => { - glGot.mockImplementationOnce(() => + get.mockImplementationOnce(() => Promise.reject({ statusCode: 500, }) @@ -263,12 +263,12 @@ describe('api/gitlab', () => { }); describe('getBranch', () => { it('returns a branch', async () => { - glGot.mockReturnValueOnce({ body: 'foo' }); + get.mockReturnValueOnce({ body: 'foo' }); const branch = await gitlab.getBranch('branch-name'); expect(branch).toMatchSnapshot(); }); it('nulls on error', async () => { - glGot.mockImplementationOnce(() => { + get.mockImplementationOnce(() => { throw new Error('not found'); }); const branch = await gitlab.getBranch('branch-name'); @@ -278,19 +278,19 @@ describe('api/gitlab', () => { describe('getBranchPr(branchName)', () => { it('should return null if no PR exists', async () => { await initRepo('some/repo', 'token'); - glGot.mockImplementationOnce(() => ({ + get.mockImplementationOnce(() => ({ body: [], })); const pr = await gitlab.getBranchPr('somebranch'); - expect(glGot.mock.calls).toMatchSnapshot(); + expect(get.mock.calls).toMatchSnapshot(); expect(pr).toBe(null); }); it('should return the PR object', async () => { await initRepo('some/repo', 'token'); - glGot.mockImplementationOnce(() => ({ + get.mockImplementationOnce(() => ({ body: [{ number: 91, source_branch: 'somebranch' }], })); - glGot.mockImplementationOnce(() => ({ + get.mockImplementationOnce(() => ({ body: { iid: 91, additions: 1, @@ -303,13 +303,13 @@ describe('api/gitlab', () => { }, })); const pr = await gitlab.getBranchPr('somebranch'); - expect(glGot.mock.calls).toMatchSnapshot(); + expect(get.mock.calls).toMatchSnapshot(); expect(pr).toMatchSnapshot(); }); }); describe('getBranchStatus(branchName, requiredStatusChecks)', () => { beforeEach(() => { - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: { commit: { id: 1, @@ -328,28 +328,28 @@ describe('api/gitlab', () => { expect(res).toEqual('failed'); }); it('returns pending if no results', async () => { - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: [], }); const res = await gitlab.getBranchStatus('somebranch', []); expect(res).toEqual('pending'); }); it('returns success if all are success', async () => { - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: [{ status: 'success' }, { status: 'success' }], }); const res = await gitlab.getBranchStatus('somebranch', []); expect(res).toEqual('success'); }); it('returns failure if any are failed', async () => { - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: [{ status: 'success' }, { status: 'failed' }], }); const res = await gitlab.getBranchStatus('somebranch', []); expect(res).toEqual('failure'); }); it('returns custom statuses', async () => { - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: [{ status: 'success' }, { status: 'foo' }], }); const res = await gitlab.getBranchStatus('somebranch', []); @@ -358,7 +358,7 @@ describe('api/gitlab', () => { }); describe('getBranchStatusCheck', () => { beforeEach(() => { - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: { commit: { id: 1, @@ -367,7 +367,7 @@ describe('api/gitlab', () => { }); }); it('returns null if no results', async () => { - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: [], }); const res = await gitlab.getBranchStatusCheck( @@ -377,7 +377,7 @@ describe('api/gitlab', () => { expect(res).toEqual(null); }); it('returns null if no matching results', async () => { - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: [{ name: 'context-1', status: 'pending' }], }); const res = await gitlab.getBranchStatusCheck( @@ -387,7 +387,7 @@ describe('api/gitlab', () => { expect(res).toEqual(null); }); it('returns status if name found', async () => { - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: [ { name: 'context-1', state: 'pending' }, { name: 'some-context', state: 'success' }, @@ -405,7 +405,7 @@ describe('api/gitlab', () => { it('sets branch status', async () => { await initRepo('some/repo', 'token'); // getBranchCommit - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: { commit: { id: 1, @@ -419,20 +419,20 @@ describe('api/gitlab', () => { 'some-state', 'some-url' ); - expect(glGot.post.mock.calls).toHaveLength(1); + expect(get.post.mock.calls).toHaveLength(1); }); }); describe('deleteBranch(branchName)', () => { it('should send delete', async () => { - glGot.delete = jest.fn(); + get.delete = jest.fn(); await gitlab.deleteBranch('some-branch'); - expect(glGot.delete.mock.calls.length).toBe(1); + expect(get.delete.mock.calls.length).toBe(1); }); }); describe('getBranchLastCommitTime', () => { it('should return a Date', async () => { await initRepo('some/repo', 'token'); - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: [ { id: 'ed899a2f4b50b4370feeea94676502b42383c746', @@ -467,7 +467,7 @@ describe('api/gitlab', () => { }); it('handles error', async () => { await initRepo('some/repo', 'token'); - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: [], }); const res = await gitlab.getBranchLastCommitTime('some-branch'); @@ -478,12 +478,12 @@ describe('api/gitlab', () => { it('should add the given assignees to the issue', async () => { await initRepo('some/repo', 'token'); await gitlab.addAssignees(42, ['someuser']); - expect(glGot.put.mock.calls).toMatchSnapshot(); + expect(get.put.mock.calls).toMatchSnapshot(); }); it('should log error if more than one assignee', async () => { await initRepo('some/repo', 'token'); await gitlab.addAssignees(42, ['someuser', 'someotheruser']); - expect(glGot.put.mock.calls).toMatchSnapshot(); + expect(get.put.mock.calls).toMatchSnapshot(); }); }); describe('addReviewers(issueNo, reviewers)', () => { @@ -496,19 +496,19 @@ describe('api/gitlab', () => { it('should add the given labels to the issue', async () => { await initRepo('some/repo', 'token'); await gitlab.addLabels(42, ['foo', 'bar']); - expect(glGot.put.mock.calls).toMatchSnapshot(); + expect(get.put.mock.calls).toMatchSnapshot(); }); }); describe('findPr(branchName, prTitle, state)', () => { it('returns null if no results', async () => { - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: [], }); const pr = await gitlab.findPr('some-branch'); expect(pr).toBe(null); }); it('returns null if no matching titles', async () => { - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: [ { source_branch: 'some-branch', @@ -525,7 +525,7 @@ describe('api/gitlab', () => { expect(pr).toBe(null); }); it('returns last result if multiple match', async () => { - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: [ { source_branch: 'some-branch', @@ -543,7 +543,7 @@ describe('api/gitlab', () => { }); describe('checkForClosedPr(branchName, prTitle)', () => { it('returns true if pr exists', async () => { - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: [ { source_branch: 'some-branch', @@ -559,7 +559,7 @@ describe('api/gitlab', () => { expect(res).toBe(true); }); it('returns false if pr does not exist', async () => { - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: [], }); const res = await gitlab.checkForClosedPr('some-branch'); @@ -568,7 +568,7 @@ describe('api/gitlab', () => { }); describe('createPr(branchName, title, body)', () => { it('returns the PR', async () => { - glGot.post.mockReturnValueOnce({ + get.post.mockReturnValueOnce({ body: { id: 1, iid: 12345, @@ -576,10 +576,10 @@ describe('api/gitlab', () => { }); const pr = await gitlab.createPr('some-branch', 'some-title', 'the-body'); expect(pr).toMatchSnapshot(); - expect(glGot.post.mock.calls).toMatchSnapshot(); + expect(get.post.mock.calls).toMatchSnapshot(); }); it('uses default branch', async () => { - glGot.post.mockReturnValueOnce({ + get.post.mockReturnValueOnce({ body: { id: 1, iid: 12345, @@ -592,12 +592,12 @@ describe('api/gitlab', () => { true ); expect(pr).toMatchSnapshot(); - expect(glGot.post.mock.calls).toMatchSnapshot(); + expect(get.post.mock.calls).toMatchSnapshot(); }); }); describe('getPr(prNo)', () => { it('returns the PR', async () => { - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: { id: 1, iid: 12345, @@ -607,7 +607,7 @@ describe('api/gitlab', () => { source_branch: 'some-branch', }, }); - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: { commit: {}, }, @@ -620,38 +620,38 @@ describe('api/gitlab', () => { jest.resetAllMocks(); it('updates the PR', async () => { await gitlab.updatePr(1, 'title', 'body'); - expect(glGot.put.mock.calls.length).toEqual(1); + expect(get.put.mock.calls.length).toEqual(1); }); }); describe('mergePr(pr)', () => { jest.resetAllMocks(); it('merges the PR', async () => { await gitlab.mergePr({ number: 1 }); - expect(glGot.put.mock.calls.length).toEqual(1); + expect(get.put.mock.calls.length).toEqual(1); }); }); describe('getFile(filePath, branchName)', () => { it('gets the file with v4 by default', async () => { - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: { content: 'foo', }, }); const res = await gitlab.getFile('some/path'); expect(res).toMatchSnapshot(); - expect(glGot.mock.calls[0][0].indexOf('some%2Fpath')).not.toBe(-1); + expect(get.mock.calls[0][0].indexOf('some%2Fpath')).not.toBe(-1); }); it('gets the file with v3', async () => { - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: {}, }); - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: {}, }); - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: {}, }); - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: { content: 'foo', }, @@ -660,12 +660,12 @@ describe('api/gitlab', () => { expect(config).toMatchSnapshot(); const res = await gitlab.getFile('some-path'); expect(res).toMatchSnapshot(); - expect(glGot.mock.calls[3][0].indexOf('file_path')).not.toBe(-1); + expect(get.mock.calls[3][0].indexOf('file_path')).not.toBe(-1); }); }); describe('getFileContent(filePath, branchName)', () => { it('gets the file', async () => { - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: { content: 'foo', }, @@ -674,12 +674,12 @@ describe('api/gitlab', () => { expect(res).toMatchSnapshot(); }); it('returns null for 404', async () => { - glGot.mockImplementationOnce(() => Promise.reject({ statusCode: 404 })); + get.mockImplementationOnce(() => Promise.reject({ statusCode: 404 })); const res = await gitlab.getFileContent('some-path', 'some-branch'); expect(res).toBe(null); }); it('throws error for non-404', async () => { - glGot.mockImplementationOnce(() => Promise.reject({ statusCode: 403 })); + get.mockImplementationOnce(() => Promise.reject({ statusCode: 403 })); let e; try { await gitlab.getFileContent('some-path', 'some-branch'); @@ -691,7 +691,7 @@ describe('api/gitlab', () => { }); describe('getFileJson(filePath, branchName)', () => { it('returns null for 404', async () => { - glGot.mockImplementationOnce(() => Promise.reject({ statusCode: 404 })); + get.mockImplementationOnce(() => Promise.reject({ statusCode: 404 })); const res = await gitlab.getFileJson('some-path', 'some-branch'); expect(res).toBe(null); }); @@ -704,17 +704,17 @@ describe('api/gitlab', () => { 'some-contents', 'some-message' ); - expect(glGot.post.mock.calls).toMatchSnapshot(); - expect(glGot.post.mock.calls[0][1].body.file_path).not.toBeDefined(); + expect(get.post.mock.calls).toMatchSnapshot(); + expect(get.post.mock.calls[0][1].body.file_path).not.toBeDefined(); }); it('creates file with v3', async () => { - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: {}, }); - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: {}, }); - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: {}, }); await gitlab.initRepo('some-repo', 'some-token'); @@ -724,8 +724,8 @@ describe('api/gitlab', () => { 'some-contents', 'some-message' ); - expect(glGot.post.mock.calls).toMatchSnapshot(); - expect(glGot.post.mock.calls[0][1].body.file_path).toBeDefined(); + expect(get.post.mock.calls).toMatchSnapshot(); + expect(get.post.mock.calls[0][1].body.file_path).toBeDefined(); }); describe('updateFile(branchName, filePath, fileContents, message)', () => { it('creates file with v4', async () => { @@ -735,17 +735,17 @@ describe('api/gitlab', () => { 'some-contents', 'some-message' ); - expect(glGot.put.mock.calls).toMatchSnapshot(); - expect(glGot.put.mock.calls[0][1].body.file_path).not.toBeDefined(); + expect(get.put.mock.calls).toMatchSnapshot(); + expect(get.put.mock.calls[0][1].body.file_path).not.toBeDefined(); }); it('creates file with v3', async () => { - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: {}, }); - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: {}, }); - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: {}, }); await gitlab.initRepo('some-repo', 'some-token'); @@ -755,52 +755,52 @@ describe('api/gitlab', () => { 'some-contents', 'some-message' ); - expect(glGot.put.mock.calls).toMatchSnapshot(); - expect(glGot.put.mock.calls[0][1].body.file_path).toBeDefined(); + expect(get.put.mock.calls).toMatchSnapshot(); + expect(get.put.mock.calls[0][1].body.file_path).toBeDefined(); }); }); describe('createBranch(branchName)', () => { it('creates file with v4', async () => { await gitlab.createBranch('some-branch'); - expect(glGot.post.mock.calls).toMatchSnapshot(); - expect(glGot.post.mock.calls[0][1].body.branch_name).not.toBeDefined(); + expect(get.post.mock.calls).toMatchSnapshot(); + expect(get.post.mock.calls[0][1].body.branch_name).not.toBeDefined(); }); it('creates file with v3', async () => { - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: {}, }); - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: {}, }); - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: {}, }); await gitlab.initRepo('some-repo', 'some-token'); await gitlab.createBranch('some-branch'); - expect(glGot.post.mock.calls).toMatchSnapshot(); - expect(glGot.post.mock.calls[0][1].body.branch_name).toBeDefined(); + expect(get.post.mock.calls).toMatchSnapshot(); + expect(get.post.mock.calls[0][1].body.branch_name).toBeDefined(); }); }); describe('getSubDirectories(path)', () => { it('should return subdirectories', async () => { await initRepo('some/repo', 'token'); - glGot.mockImplementationOnce(() => ({ + get.mockImplementationOnce(() => ({ body: [{ type: 'tree', name: 'a' }, { type: 'file', name: 'b' }], })); const dirList = await gitlab.getSubDirectories('some-path'); - expect(glGot.mock.calls).toMatchSnapshot(); + expect(get.mock.calls).toMatchSnapshot(); expect(dirList).toHaveLength(1); expect(dirList).toMatchSnapshot(); }); }); describe('commitFilesToBranch(branchName, files, message, parentBranch)', () => { it('creates branch', async () => { - glGot.mockReturnValueOnce({ statusCode: 404 }); + get.mockReturnValueOnce({ statusCode: 404 }); await gitlab.commitFilesToBranch('some-branch', [], 'some-message'); }); it('does not create branch and updates file', async () => { - glGot.mockReturnValueOnce({ statusCode: 200 }); - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ statusCode: 200 }); + get.mockReturnValueOnce({ body: { content: 'hello', }, @@ -817,8 +817,8 @@ describe('api/gitlab', () => { ); }); it('does not create branch and creates file', async () => { - glGot.mockReturnValueOnce({ statusCode: 200 }); - glGot.mockReturnValueOnce(Promise.reject({ statusCode: 404 })); + get.mockReturnValueOnce({ statusCode: 200 }); + get.mockReturnValueOnce(Promise.reject({ statusCode: 404 })); const file = { name: 'foo', contents: 'bar', @@ -834,7 +834,7 @@ describe('api/gitlab', () => { }); describe('getCommitMessages()', () => { it('returns commits messages', async () => { - glGot.mockReturnValueOnce({ + get.mockReturnValueOnce({ body: [ { title: 'foo', @@ -848,7 +848,7 @@ describe('api/gitlab', () => { expect(res).toMatchSnapshot(); }); it('swallows errors', async () => { - glGot.mockImplementationOnce(() => { + get.mockImplementationOnce(() => { throw new Error('some-error'); }); const res = await gitlab.getCommitMessages(); diff --git a/test/config/index.spec.js b/test/config/index.spec.js index 8d91482c9c5cb6ebce0c599477489865efe49678..e6a5a7bdf8e627801c88b6b456405a8dcd8db39b 100644 --- a/test/config/index.spec.js +++ b/test/config/index.spec.js @@ -6,7 +6,7 @@ describe('config/index', () => { let configParser; let defaultArgv; let ghGot; - let glGot; + let get; let githubApp; beforeEach(() => { jest.resetModules(); @@ -15,7 +15,7 @@ describe('config/index', () => { jest.mock('gh-got'); ghGot = require('gh-got'); jest.mock('gl-got'); - glGot = require('gl-got'); + get = require('gl-got'); jest.mock('../../lib/config/github-app'); githubApp = require('../../lib/config/github-app'); githubApp.getRepositories = jest.fn(); @@ -104,7 +104,7 @@ describe('config/index', () => { })); await configParser.parseConfigs(env, defaultArgv); expect(ghGot.mock.calls.length).toBe(1); - expect(glGot.mock.calls.length).toBe(0); + expect(get.mock.calls.length).toBe(0); }); it('autodiscovers gitlab platform', async () => { const env = {}; @@ -113,7 +113,7 @@ describe('config/index', () => { '--platform=gitlab', '--token=abc', ]); - glGot.mockImplementationOnce(() => ({ + get.mockImplementationOnce(() => ({ body: [ { path_with_namespace: 'a/b', @@ -122,7 +122,7 @@ describe('config/index', () => { })); await configParser.parseConfigs(env, defaultArgv); expect(ghGot.mock.calls.length).toBe(0); - expect(glGot.mock.calls.length).toBe(1); + expect(get.mock.calls.length).toBe(1); }); it('logs if no autodiscovered repositories', async () => { const env = { GITHUB_TOKEN: 'abc' }; @@ -132,7 +132,7 @@ describe('config/index', () => { })); await configParser.parseConfigs(env, defaultArgv); expect(ghGot.mock.calls.length).toBe(1); - expect(glGot.mock.calls.length).toBe(0); + expect(get.mock.calls.length).toBe(0); }); it('adds a log file', async () => { const env = { GITHUB_TOKEN: 'abc', RENOVATE_LOG_FILE: 'debug.log' }; @@ -142,7 +142,7 @@ describe('config/index', () => { })); await configParser.parseConfigs(env, defaultArgv); expect(ghGot.mock.calls.length).toBe(1); - expect(glGot.mock.calls.length).toBe(0); + expect(get.mock.calls.length).toBe(0); }); }); describe('mergeChildConfig(parentConfig, childConfig)', () => {