/
update.ts
213 lines (205 loc) · 5.89 KB
/
update.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { fromStream } from 'hasha';
import { coerce } from 'semver';
import { logger } from '../../logger';
import { Http } from '../../util/http';
import type { UpdateDependencyConfig } from '../types';
import { parseUrlPath } from './extract';
import { isSpace, removeComments, skip } from './util';
const http = new Http('homebrew');
function replaceUrl(
idx: number,
content: string,
oldUrl: string,
newUrl: string
): string | null {
let i = idx;
i += 'url'.length;
i = skip(i, content, (c) => isSpace(c));
const chr = content[i];
if (chr !== '"' && chr !== "'") {
return null;
}
i += 1;
const newContent =
content.substring(0, i) + content.substring(i).replace(oldUrl, newUrl);
return newContent;
}
function getUrlTestContent(
content: string,
oldUrl: string,
newUrl: string
): string {
const urlRegExp = /(^|\s)url(\s)/;
const cleanContent = removeComments(content);
let j = cleanContent.search(urlRegExp);
if (isSpace(cleanContent[j])) {
j += 1;
}
const testContent = replaceUrl(j, cleanContent, oldUrl, newUrl);
return testContent;
}
function updateUrl(
content: string,
oldUrl: string,
newUrl: string
): string | null {
const urlRegExp = /(^|\s)url(\s)/;
let i = content.search(urlRegExp);
if (i === -1) {
return null;
}
if (isSpace(content[i])) {
i += 1;
}
let newContent = replaceUrl(i, content, oldUrl, newUrl);
const testContent = getUrlTestContent(content, oldUrl, newUrl);
if (!newContent || !testContent) {
return null;
}
while (removeComments(newContent) !== testContent) {
i += 'url'.length;
i += content.substring(i).search(urlRegExp);
if (isSpace(content[i])) {
i += 1;
}
newContent = replaceUrl(i, content, oldUrl, newUrl);
}
return newContent;
}
function replaceSha256(
idx: number,
content: string,
oldSha256: string,
newSha256: string
): string | null {
let i = idx;
i += 'sha256'.length;
i = skip(i, content, (c) => isSpace(c));
const chr = content[i];
if (chr !== '"' && chr !== "'") {
return null;
}
i += 1;
const newContent =
content.substring(0, i) +
content.substring(i).replace(oldSha256, newSha256);
return newContent;
}
function getSha256TestContent(
content: string,
oldSha256: string,
newSha256: string
): string | null {
const sha256RegExp = /(^|\s)sha256(\s)/;
const cleanContent = removeComments(content);
let j = cleanContent.search(sha256RegExp);
if (isSpace(cleanContent[j])) {
j += 1;
}
const testContent = replaceSha256(j, cleanContent, oldSha256, newSha256);
return testContent;
}
function updateSha256(
content: string,
oldSha256: string,
newSha256: string
): string | null {
const sha256RegExp = /(^|\s)sha256(\s)/;
let i = content.search(sha256RegExp);
if (i === -1) {
return null;
}
if (isSpace(content[i])) {
i += 1;
}
let newContent = replaceSha256(i, content, oldSha256, newSha256);
const testContent = getSha256TestContent(content, oldSha256, newSha256);
if (!newContent || !testContent) {
return null;
}
while (removeComments(newContent) !== testContent) {
i += 'sha256'.length;
i += content.substring(i).search(sha256RegExp);
if (isSpace(content[i])) {
i += 1;
}
newContent = replaceSha256(i, content, oldSha256, newSha256);
}
return newContent;
}
// TODO: Refactor (#9591)
export async function updateDependency({
fileContent,
upgrade,
}: UpdateDependencyConfig): Promise<string> {
logger.trace('updateDependency()');
/*
1. Update url field 2. Update sha256 field
*/
let newUrl: string;
// Example urls:
// "https://github.com/bazelbuild/bazel-watcher/archive/v0.8.2.tar.gz"
// "https://github.com/aide/aide/releases/download/v0.16.1/aide-0.16.1.tar.gz"
const oldParsedUrlPath = parseUrlPath(upgrade.managerData.url);
if (!oldParsedUrlPath) {
logger.debug(
`Failed to update - upgrade.managerData.url is invalid ${upgrade.depName}`
);
return fileContent;
}
let newSha256: string;
try {
const ownerName = String(upgrade.managerData.ownerName);
const repoName = String(upgrade.managerData.repoName);
newUrl = `https://github.com/${ownerName}/${repoName}/releases/download/${
upgrade.newValue
}/${repoName}-${String(coerce(upgrade.newValue))}.tar.gz`;
newSha256 = await fromStream(http.stream(newUrl), {
algorithm: 'sha256',
});
} catch (errOuter) {
logger.debug(
`Failed to download release download for ${upgrade.depName} - trying archive instead`
);
try {
const ownerName = String(upgrade.managerData.ownerName);
const repoName = String(upgrade.managerData.repoName);
newUrl = `https://github.com/${ownerName}/${repoName}/archive/${upgrade.newValue}.tar.gz`;
newSha256 = await fromStream(http.stream(newUrl), {
algorithm: 'sha256',
});
} catch (errInner) {
logger.debug(
`Failed to download archive download for ${upgrade.depName} - update failed`
);
return fileContent;
}
}
// istanbul ignore next
if (!newSha256) {
logger.debug(
`Failed to generate new sha256 for ${upgrade.depName} - update failed`
);
return fileContent;
}
const newParsedUrlPath = parseUrlPath(newUrl);
if (!newParsedUrlPath) {
logger.debug(`Failed to update url for dependency ${upgrade.depName}`);
return fileContent;
}
if (upgrade.newValue !== newParsedUrlPath.currentValue) {
logger.debug(`Failed to update url for dependency ${upgrade.depName}`);
return fileContent;
}
let newContent = updateUrl(fileContent, upgrade.managerData.url, newUrl);
if (!newContent) {
logger.debug(`Failed to update url for dependency ${upgrade.depName}`);
return fileContent;
}
newContent = updateSha256(newContent, upgrade.managerData.sha256, newSha256);
if (!newContent) {
logger.debug(`Failed to update sha256 for dependency ${upgrade.depName}`);
return fileContent;
}
return newContent;
}