forked from renovatebot/renovate
/
cache.ts
147 lines (141 loc) · 3.96 KB
/
cache.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* istanbul ignore file */
import { REPOSITORY_CHANGED } from '../../constants/error-messages';
import { logger } from '../../logger';
import { platform } from '../../modules/platform';
import { scm } from '../../modules/platform/scm';
import { getCache } from '../../util/cache/repository';
import type {
BranchCache,
BranchUpgradeCache,
} from '../../util/cache/repository/types';
import { getCachedBehindBaseResult } from '../../util/git/behind-base-branch-cache';
import { getCachedConflictResult } from '../../util/git/conflicts-cache';
import { getCachedModifiedResult } from '../../util/git/modified-cache';
import { getCachedPristineResult } from '../../util/git/pristine';
import type { BranchConfig, BranchUpgradeConfig } from '../types';
import { getPrCache } from './update/pr/pr-cache';
function generateBranchUpgradeCache(
upgrade: BranchUpgradeConfig
): BranchUpgradeCache {
const {
datasource,
depName,
depType,
displayPending,
packageName,
fixedVersion,
currentVersion,
newVersion,
currentValue,
newValue,
currentDigest,
newDigest,
packageFile,
sourceUrl,
remediationNotPossible,
updateType,
} = upgrade;
const result: BranchUpgradeCache = {
datasource,
depName,
depType,
displayPending,
fixedVersion,
currentVersion,
currentValue,
newValue,
newVersion,
currentDigest,
newDigest,
packageFile,
sourceUrl,
remediationNotPossible,
updateType,
};
if (packageName) {
result.packageName = packageName;
}
return result;
}
async function generateBranchCache(
branch: BranchConfig
): Promise<BranchCache | null> {
const { baseBranch, branchName, prBlockedBy, prTitle, result } = branch;
try {
const branchSha = await scm.getBranchCommit(branchName);
const baseBranchSha = await scm.getBranchCommit(baseBranch);
const pristine = getCachedPristineResult(branchName);
let prNo = null;
let isModified: boolean | undefined;
let isBehindBase: boolean | undefined;
let isConflicted: boolean | undefined;
if (baseBranchSha && branchSha) {
const branchPr = await platform.getBranchPr(branchName);
if (branchPr) {
prNo = branchPr.number;
}
isModified = getCachedModifiedResult(branchName, branchSha) ?? undefined;
isBehindBase =
getCachedBehindBaseResult(
branchName,
branchSha,
baseBranch,
baseBranchSha
) ?? undefined;
isConflicted =
getCachedConflictResult(
branchName,
branchSha,
baseBranch,
baseBranchSha
) ?? undefined;
}
const automerge = !!branch.automerge;
const upgrades: BranchUpgradeCache[] = branch.upgrades
? branch.upgrades.map(generateBranchUpgradeCache)
: [];
const branchFingerprint = branch.branchFingerprint;
const prCache = getPrCache(branchName);
return {
automerge,
baseBranchSha,
baseBranch,
branchFingerprint,
branchName,
isBehindBase,
isConflicted,
isModified,
prBlockedBy,
pristine,
prCache,
prNo,
prTitle,
result,
sha: branchSha,
upgrades,
};
} catch (error) {
const err = error.err || error; // external host error nests err
const errCodes = [401, 404];
// istanbul ignore if
if (errCodes.includes(err.response?.statusCode)) {
logger.warn({ err, branchName }, 'HTTP error generating branch cache');
return null;
}
if (err.message === REPOSITORY_CHANGED) {
throw err;
}
logger.error({ err, branchName }, 'Error generating branch cache');
return null;
}
}
export async function setBranchCache(branches: BranchConfig[]): Promise<void> {
const branchCaches: BranchCache[] = [];
for (const branch of branches) {
const branchCache = await generateBranchCache(branch);
if (branchCache) {
branchCaches.push(branchCache);
}
}
getCache().branches = branchCaches;
}