From 8cdd1a20f4c2cc5679f0ef67701d7a31049a4749 Mon Sep 17 00:00:00 2001 From: Sergei Zharinov Date: Thu, 18 May 2023 17:33:11 +0300 Subject: [PATCH] refactor(http): Rename `useCache` option to `memCache` (#22300) --- .../platform/bitbucket-server/index.ts | 12 +++---- lib/modules/platform/bitbucket/index.ts | 6 ++-- lib/modules/platform/gitea/index.ts | 16 ++++----- lib/modules/platform/github/index.ts | 12 ++++--- lib/modules/platform/gitlab/index.ts | 6 ++-- lib/modules/platform/types.ts | 2 +- lib/util/http/index.ts | 34 ++++++++++--------- lib/util/http/types.ts | 4 +-- 8 files changed, 47 insertions(+), 45 deletions(-) diff --git a/lib/modules/platform/bitbucket-server/index.ts b/lib/modules/platform/bitbucket-server/index.ts index 9da5a7fff7a2eb..43c44bf84c21ea 100644 --- a/lib/modules/platform/bitbucket-server/index.ts +++ b/lib/modules/platform/bitbucket-server/index.ts @@ -258,7 +258,7 @@ export async function getPr( const res = await bitbucketServerHttp.getJson( `./rest/api/1.0/projects/${config.projectKey}/repos/${config.repositorySlug}/pull-requests/${prNo}`, - { useCache: !refreshCache } + { memCache: !refreshCache } ); const pr: BbsPr = { @@ -356,7 +356,7 @@ export async function refreshPr(number: number): Promise { async function getStatus( branchName: string, - useCache = true + memCache = true ): Promise { const branchCommit = git.getBranchCommit(branchName); @@ -364,9 +364,7 @@ async function getStatus( await bitbucketServerHttp.getJson( // TODO: types (#7154) `./rest/build-status/1.0/commits/stats/${branchCommit!}`, - { - useCache, - } + { memCache } ) ).body; } @@ -404,7 +402,7 @@ export async function getBranchStatus( function getStatusCheck( branchName: string, - useCache = true + memCache = true ): Promise { const branchCommit = git.getBranchCommit(branchName); @@ -412,7 +410,7 @@ function getStatusCheck( // TODO: types (#7154) `./rest/build-status/1.0/commits/${branchCommit!}`, 'get', - { useCache } + { memCache } ); } diff --git a/lib/modules/platform/bitbucket/index.ts b/lib/modules/platform/bitbucket/index.ts index b12eb09a114dae..de980f3b5201e4 100644 --- a/lib/modules/platform/bitbucket/index.ts +++ b/lib/modules/platform/bitbucket/index.ts @@ -80,7 +80,7 @@ export async function initPlatform({ setBaseUrl(defaults.endpoint); renovateUserUuid = null; const options: HttpOptions = { - useCache: false, + memCache: false, }; if (token) { options.token = token; @@ -362,7 +362,7 @@ export async function getBranchPr(branchName: string): Promise { async function getStatus( branchName: string, - useCache = true + memCache = true ): Promise { const sha = await getBranchCommit(branchName); return utils.accumulateValues( @@ -370,7 +370,7 @@ async function getStatus( // eslint-disable-next-line @typescript-eslint/restrict-template-expressions `/2.0/repositories/${config.repository}/commit/${sha}/statuses`, 'get', - { useCache } + { memCache } ); } // Returns the combined status for a branch. diff --git a/lib/modules/platform/gitea/index.ts b/lib/modules/platform/gitea/index.ts index 2162e5252215dc..ecc8e64bfa5c3c 100644 --- a/lib/modules/platform/gitea/index.ts +++ b/lib/modules/platform/gitea/index.ts @@ -165,7 +165,7 @@ function getLabelList(): Promise { if (config.labelList === null) { const repoLabels = helper .getRepoLabels(config.repository, { - useCache: false, + memCache: false, }) .then((labels) => { logger.debug(`Retrieved ${labels.length} repo labels`); @@ -174,7 +174,7 @@ function getLabelList(): Promise { const orgLabels = helper .getOrgLabels(config.repository.split('/')[0], { - useCache: false, + memCache: false, }) .then((labels) => { logger.debug(`Retrieved ${labels.length} org labels`); @@ -382,7 +382,7 @@ const platform: Platform = { // Refresh caches by re-fetching commit status for branch await helper.getCombinedCommitStatus(config.repository, branchName, { - useCache: false, + memCache: false, }); } catch (err) { logger.warn({ err }, 'Failed to set branch status'); @@ -449,7 +449,7 @@ const platform: Platform = { getPrList(): Promise { if (config.prList === null) { config.prList = helper - .searchPRs(config.repository, { state: 'all' }, { useCache: false }) + .searchPRs(config.repository, { state: 'all' }, { memCache: false }) .then((prs) => { const prList = prs.map(toRenovatePR).filter(is.truthy); logger.debug(`Retrieved ${prList.length} Pull Requests`); @@ -650,7 +650,7 @@ const platform: Platform = { getIssueList(): Promise { if (config.issueList === null) { config.issueList = helper - .searchIssues(config.repository, { state: 'all' }, { useCache: false }) + .searchIssues(config.repository, { state: 'all' }, { memCache: false }) .then((issues) => { const issueList = issues.map(toRenovateIssue); logger.debug(`Retrieved ${issueList.length} Issues`); @@ -661,12 +661,10 @@ const platform: Platform = { return config.issueList; }, - async getIssue(number: number, useCache = true): Promise { + async getIssue(number: number, memCache = true): Promise { try { const body = ( - await helper.getIssue(config.repository, number, { - useCache, - }) + await helper.getIssue(config.repository, number, { memCache }) ).body; return { number, diff --git a/lib/modules/platform/github/index.ts b/lib/modules/platform/github/index.ts index a82a532eb04a29..a90fb2a4acc819 100644 --- a/lib/modules/platform/github/index.ts +++ b/lib/modules/platform/github/index.ts @@ -743,7 +743,7 @@ async function ensureBranchSha(branchName: string, sha: string): Promise { let branchExists = false; try { - await githubApi.head(refUrl, { useCache: false }); + await githubApi.head(refUrl, { memCache: false }); branchExists = true; } catch (err) { if (err.statusCode !== 404) { @@ -836,7 +836,9 @@ async function getStatus( )}/status`; return ( - await githubApi.getJson(commitStatusUrl, { useCache }) + await githubApi.getJson(commitStatusUrl, { + memCache: useCache, + }) ).body; } @@ -951,7 +953,9 @@ async function getStatusCheck( const url = `repos/${config.repository}/commits/${branchCommit}/statuses`; - return (await githubApi.getJson(url, { useCache })).body; + return ( + await githubApi.getJson(url, { memCache: useCache }) + ).body; } interface GithubToRenovateStatusMapping { @@ -1077,7 +1081,7 @@ export async function getIssue( const issueBody = ( await githubApi.getJson<{ body: string }>( `repos/${config.parentRepo ?? config.repository}/issues/${number}`, - { useCache } + { memCache: useCache } ) ).body.body; return { diff --git a/lib/modules/platform/gitlab/index.ts b/lib/modules/platform/gitlab/index.ts index 4f1cdf8527a0c9..17e6a497288831 100644 --- a/lib/modules/platform/gitlab/index.ts +++ b/lib/modules/platform/gitlab/index.ts @@ -371,7 +371,7 @@ async function getStatus( return ( await gitlabApi.getJson(url, { paginate: true, - useCache, + memCache: useCache, }) ).body; } catch (err) /* istanbul ignore next */ { @@ -864,7 +864,7 @@ export async function getIssueList(): Promise { const res = await gitlabApi.getJson< { iid: number; title: string; labels: string[] }[] >(`projects/${config.repository}/issues?${query}`, { - useCache: false, + memCache: false, paginate: true, }); // istanbul ignore if @@ -889,7 +889,7 @@ export async function getIssue( const issueBody = ( await gitlabApi.getJson<{ description: string }>( `projects/${config.repository}/issues/${number}`, - { useCache } + { memCache: useCache } ) ).body.description; return { diff --git a/lib/modules/platform/types.ts b/lib/modules/platform/types.ts index 12205f74099bb2..bfc54c32251a4c 100644 --- a/lib/modules/platform/types.ts +++ b/lib/modules/platform/types.ts @@ -165,7 +165,7 @@ export type EnsureIssueResult = 'updated' | 'created'; export interface Platform { findIssue(title: string): Promise; getIssueList(): Promise; - getIssue?(number: number, useCache?: boolean): Promise; + getIssue?(number: number, memCache?: boolean): Promise; getVulnerabilityAlerts?(): Promise; getRawFile( fileName: string, diff --git a/lib/util/http/index.ts b/lib/util/http/index.ts index e5dd4ac87e98b1..20bca08a33bf96 100644 --- a/lib/util/http/index.ts +++ b/lib/util/http/index.ts @@ -153,22 +153,24 @@ export class Http { options = applyAuthorization(options); // use sha512: https://www.npmjs.com/package/hasha#algorithm - const cacheKey = hasha([ - 'got-', - JSON.stringify({ - url, - headers: options.headers, - method: options.method, - }), - ]); + const memCacheKey = + options.memCache !== false && + (options.method === 'get' || options.method === 'head') + ? hasha([ + 'got-', + JSON.stringify({ + url, + headers: options.headers, + method: options.method, + }), + ]) + : null; + let resPromise: Promise> | null = null; - // Cache GET requests unless useCache=false - if ( - (options.method === 'get' || options.method === 'head') && - options.useCache !== false - ) { - resPromise = memCache.get(cacheKey); + // Cache GET requests unless memCache=false + if (memCacheKey) { + resPromise = memCache.get(memCacheKey); } // istanbul ignore else: no cache tests @@ -195,8 +197,8 @@ export class Http { resPromise = queuedTask(); - if (options.method === 'get' || options.method === 'head') { - memCache.set(cacheKey, resPromise); // always set if it's a get or a head + if (memCacheKey) { + memCache.set(memCacheKey, resPromise); } } diff --git a/lib/util/http/types.ts b/lib/util/http/types.ts index 4cf1774683eb42..4b232982585ea2 100644 --- a/lib/util/http/types.ts +++ b/lib/util/http/types.ts @@ -19,7 +19,7 @@ export type GotExtraOptions = { token?: string; hostType?: string; enabled?: boolean; - useCache?: boolean; + memCache?: boolean; noAuth?: boolean; context?: GotContextOptions; }; @@ -62,7 +62,7 @@ export interface HttpOptions { throwHttpErrors?: boolean; token?: string; - useCache?: boolean; + memCache?: boolean; } export interface InternalHttpOptions extends HttpOptions {