Skip to content

Commit

Permalink
refactor(http): Rename useCache option to memCache (#22300)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed May 18, 2023
1 parent be9c642 commit 8cdd1a2
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 45 deletions.
12 changes: 5 additions & 7 deletions lib/modules/platform/bitbucket-server/index.ts
Expand Up @@ -258,7 +258,7 @@ export async function getPr(

const res = await bitbucketServerHttp.getJson<BbsRestPr>(
`./rest/api/1.0/projects/${config.projectKey}/repos/${config.repositorySlug}/pull-requests/${prNo}`,
{ useCache: !refreshCache }
{ memCache: !refreshCache }
);

const pr: BbsPr = {
Expand Down Expand Up @@ -356,17 +356,15 @@ export async function refreshPr(number: number): Promise<void> {

async function getStatus(
branchName: string,
useCache = true
memCache = true
): Promise<utils.BitbucketCommitStatus> {
const branchCommit = git.getBranchCommit(branchName);

return (
await bitbucketServerHttp.getJson<utils.BitbucketCommitStatus>(
// TODO: types (#7154)
`./rest/build-status/1.0/commits/stats/${branchCommit!}`,
{
useCache,
}
{ memCache }
)
).body;
}
Expand Down Expand Up @@ -404,15 +402,15 @@ export async function getBranchStatus(

function getStatusCheck(
branchName: string,
useCache = true
memCache = true
): Promise<utils.BitbucketStatus[]> {
const branchCommit = git.getBranchCommit(branchName);

return utils.accumulateValues(
// TODO: types (#7154)
`./rest/build-status/1.0/commits/${branchCommit!}`,
'get',
{ useCache }
{ memCache }
);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/modules/platform/bitbucket/index.ts
Expand Up @@ -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;
Expand Down Expand Up @@ -362,15 +362,15 @@ export async function getBranchPr(branchName: string): Promise<Pr | null> {

async function getStatus(
branchName: string,
useCache = true
memCache = true
): Promise<BitbucketStatus[]> {
const sha = await getBranchCommit(branchName);
return utils.accumulateValues<BitbucketStatus>(
// TODO: types (#7154)
// 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.
Expand Down
16 changes: 7 additions & 9 deletions lib/modules/platform/gitea/index.ts
Expand Up @@ -165,7 +165,7 @@ function getLabelList(): Promise<Label[]> {
if (config.labelList === null) {
const repoLabels = helper
.getRepoLabels(config.repository, {
useCache: false,
memCache: false,
})
.then((labels) => {
logger.debug(`Retrieved ${labels.length} repo labels`);
Expand All @@ -174,7 +174,7 @@ function getLabelList(): Promise<Label[]> {

const orgLabels = helper
.getOrgLabels(config.repository.split('/')[0], {
useCache: false,
memCache: false,
})
.then((labels) => {
logger.debug(`Retrieved ${labels.length} org labels`);
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -449,7 +449,7 @@ const platform: Platform = {
getPrList(): Promise<Pr[]> {
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`);
Expand Down Expand Up @@ -650,7 +650,7 @@ const platform: Platform = {
getIssueList(): Promise<Issue[]> {
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`);
Expand All @@ -661,12 +661,10 @@ const platform: Platform = {
return config.issueList;
},

async getIssue(number: number, useCache = true): Promise<Issue | null> {
async getIssue(number: number, memCache = true): Promise<Issue | null> {
try {
const body = (
await helper.getIssue(config.repository, number, {
useCache,
})
await helper.getIssue(config.repository, number, { memCache })
).body;
return {
number,
Expand Down
12 changes: 8 additions & 4 deletions lib/modules/platform/github/index.ts
Expand Up @@ -743,7 +743,7 @@ async function ensureBranchSha(branchName: string, sha: string): Promise<void> {

let branchExists = false;
try {
await githubApi.head(refUrl, { useCache: false });
await githubApi.head(refUrl, { memCache: false });
branchExists = true;
} catch (err) {
if (err.statusCode !== 404) {
Expand Down Expand Up @@ -836,7 +836,9 @@ async function getStatus(
)}/status`;

return (
await githubApi.getJson<CombinedBranchStatus>(commitStatusUrl, { useCache })
await githubApi.getJson<CombinedBranchStatus>(commitStatusUrl, {
memCache: useCache,
})
).body;
}

Expand Down Expand Up @@ -951,7 +953,9 @@ async function getStatusCheck(

const url = `repos/${config.repository}/commits/${branchCommit}/statuses`;

return (await githubApi.getJson<GhBranchStatus[]>(url, { useCache })).body;
return (
await githubApi.getJson<GhBranchStatus[]>(url, { memCache: useCache })
).body;
}

interface GithubToRenovateStatusMapping {
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions lib/modules/platform/gitlab/index.ts
Expand Up @@ -371,7 +371,7 @@ async function getStatus(
return (
await gitlabApi.getJson<GitlabBranchStatus[]>(url, {
paginate: true,
useCache,
memCache: useCache,
})
).body;
} catch (err) /* istanbul ignore next */ {
Expand Down Expand Up @@ -864,7 +864,7 @@ export async function getIssueList(): Promise<GitlabIssue[]> {
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
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/platform/types.ts
Expand Up @@ -165,7 +165,7 @@ export type EnsureIssueResult = 'updated' | 'created';
export interface Platform {
findIssue(title: string): Promise<Issue | null>;
getIssueList(): Promise<Issue[]>;
getIssue?(number: number, useCache?: boolean): Promise<Issue | null>;
getIssue?(number: number, memCache?: boolean): Promise<Issue | null>;
getVulnerabilityAlerts?(): Promise<VulnerabilityAlert[]>;
getRawFile(
fileName: string,
Expand Down
34 changes: 18 additions & 16 deletions lib/util/http/index.ts
Expand Up @@ -153,22 +153,24 @@ export class Http<Opts extends HttpOptions = HttpOptions> {
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<HttpResponse<T>> | 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
Expand All @@ -195,8 +197,8 @@ export class Http<Opts extends HttpOptions = HttpOptions> {

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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/util/http/types.ts
Expand Up @@ -19,7 +19,7 @@ export type GotExtraOptions = {
token?: string;
hostType?: string;
enabled?: boolean;
useCache?: boolean;
memCache?: boolean;
noAuth?: boolean;
context?: GotContextOptions;
};
Expand Down Expand Up @@ -62,7 +62,7 @@ export interface HttpOptions {
throwHttpErrors?: boolean;

token?: string;
useCache?: boolean;
memCache?: boolean;
}

export interface InternalHttpOptions extends HttpOptions {
Expand Down

0 comments on commit 8cdd1a2

Please sign in to comment.