forked from renovatebot/renovate
/
extract.ts
323 lines (306 loc) · 8.36 KB
/
extract.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* eslint no-plusplus: 0 */
import { parse as _parse } from 'url';
import parse from 'github-url-from-git';
import moo from 'moo';
import * as datasourceDocker from '../../datasource/docker';
import * as datasourceGithubReleases from '../../datasource/github-releases';
import * as datasourceGithubTags from '../../datasource/github-tags';
import * as datasourceGo from '../../datasource/go';
import { logger } from '../../logger';
import { SkipReason } from '../../types';
import * as dockerVersioning from '../../versioning/docker';
import type { PackageDependency, PackageFile } from '../types';
interface UrlParsedResult {
datasource: string;
repo: string;
currentValue: string;
}
function parseUrl(urlString: string): UrlParsedResult | null {
// istanbul ignore if
if (!urlString) {
return null;
}
const url = _parse(urlString);
if (url.host !== 'github.com') {
return null;
}
const path = url.path.split('/').slice(1);
const repo = path[0] + '/' + path[1];
let datasource: string;
let currentValue: string = null;
if (path[2] === 'releases' && path[3] === 'download') {
datasource = datasourceGithubReleases.id;
currentValue = path[4];
}
if (path[2] === 'archive') {
datasource = datasourceGithubTags.id;
currentValue = path[3];
// Strip archive extension to get hash or tag.
// Tolerates formats produced by Git(Hub|Lab) and allowed by http_archive
// Note: Order matters in suffix list to strip, e.g. .tar.gz.
for (const extension of ['.gz', '.bz2', '.xz', '.tar', '.tgz', '.zip']) {
if (currentValue.endsWith(extension)) {
currentValue = currentValue.slice(0, -extension.length);
}
}
}
if (currentValue) {
return { datasource, repo, currentValue };
}
// istanbul ignore next
return null;
}
const lexer = moo.states({
main: {
lineComment: { match: /#.*?$/ },
leftParen: { match: '(' },
rightParen: { match: ')' },
longDoubleQuoted: {
match: '"""',
push: 'longDoubleQuoted',
},
doubleQuoted: {
match: '"',
push: 'doubleQuoted',
},
longSingleQuoted: {
match: "'''",
push: 'longSingleQuoted',
},
singleQuoted: {
match: "'",
push: 'singleQuoted',
},
def: {
match: new RegExp(
[
'container_pull',
'http_archive',
'http_file',
'go_repository',
'git_repository',
].join('|')
),
},
unknown: { match: /[^]/, lineBreaks: true },
},
longDoubleQuoted: {
stringFinish: { match: '"""', pop: 1 },
char: { match: /[^]/, lineBreaks: true },
},
doubleQuoted: {
stringFinish: { match: '"', pop: 1 },
char: { match: /[^]/, lineBreaks: true },
},
longSingleQuoted: {
stringFinish: { match: "'''", pop: 1 },
char: { match: /[^]/, lineBreaks: true },
},
singleQuoted: {
stringFinish: { match: "'", pop: 1 },
char: { match: /[^]/, lineBreaks: true },
},
});
function parseContent(content: string): string[] {
lexer.reset(content);
let balance = 0;
let def: null | string = null;
const result: string[] = [];
const finishDef = (): void => {
if (def !== null) {
result.push(def);
}
def = null;
};
const startDef = (): void => {
finishDef();
def = '';
};
const updateDef = (chunk: string): void => {
if (def !== null) {
def += chunk;
}
};
let token = lexer.next();
while (token) {
const { type, value } = token;
if (type === 'def') {
startDef();
}
updateDef(value);
if (type === 'leftParen') {
balance += 1;
}
if (type === 'rightParen') {
balance -= 1;
if (balance <= 0) {
finishDef();
}
}
token = lexer.next();
}
return result;
}
export function extractPackageFile(
content: string,
fileName?: string
): PackageFile | null {
const definitions = parseContent(content);
if (!definitions.length) {
logger.debug({ fileName }, 'No matching bazel WORKSPACE definitions found');
return null;
}
logger.debug({ definitions }, `Found ${definitions.length} definitions`);
const deps: PackageDependency[] = [];
definitions.forEach((def) => {
logger.debug({ def }, 'Checking bazel definition');
const [depType] = def.split('(', 1);
const dep: PackageDependency = { depType, managerData: { def } };
let depName: string;
let importpath: string;
let remote: string;
let currentValue: string;
let commit: string;
let url: string;
let sha256: string;
let digest: string;
let repository: string;
let registry: string;
let match = /name\s*=\s*"([^"]+)"/.exec(def);
if (match) {
[, depName] = match;
}
match = /digest\s*=\s*"([^"]+)"/.exec(def);
if (match) {
[, digest] = match;
}
match = /registry\s*=\s*"([^"]+)"/.exec(def);
if (match) {
[, registry] = match;
}
match = /repository\s*=\s*"([^"]+)"/.exec(def);
if (match) {
[, repository] = match;
}
match = /remote\s*=\s*"([^"]+)"/.exec(def);
if (match) {
[, remote] = match;
}
match = /tag\s*=\s*"([^"]+)"/.exec(def);
if (match) {
[, currentValue] = match;
}
match = /url\s*=\s*"([^"]+)"/.exec(def);
if (match) {
[, url] = match;
}
match = /urls\s*=\s*\[\s*"([^\]]+)",?\s*\]/.exec(def);
if (match) {
const urls = match[1].replace(/\s/g, '').split('","');
url = urls.find(parseUrl);
}
match = /commit\s*=\s*"([^"]+)"/.exec(def);
if (match) {
[, commit] = match;
}
match = /sha256\s*=\s*"([^"]+)"/.exec(def);
if (match) {
[, sha256] = match;
}
match = /importpath\s*=\s*"([^"]+)"/.exec(def);
if (match) {
[, importpath] = match;
}
logger.debug({ dependency: depName, remote, currentValue });
if (
depType === 'git_repository' &&
depName &&
remote &&
(currentValue || commit)
) {
dep.depName = depName;
if (currentValue) {
dep.currentValue = currentValue;
}
if (commit) {
dep.currentDigest = commit;
}
// TODO: Check if we really need to use parse here or if it should always be a plain https url (#9605)
const githubURL = parse(remote);
if (githubURL) {
const repo = githubURL.substring('https://github.com/'.length);
dep.datasource = datasourceGithubReleases.id;
dep.lookupName = repo;
deps.push(dep);
}
} else if (
depType === 'go_repository' &&
depName &&
importpath &&
(currentValue || commit)
) {
dep.depName = depName;
dep.currentValue = currentValue || commit.substr(0, 7);
dep.datasource = datasourceGo.id;
dep.lookupName = importpath;
if (remote) {
const remoteMatch = /https:\/\/github\.com(?:.*\/)(([a-zA-Z]+)([-])?([a-zA-Z]+))/.exec(
remote
);
if (remoteMatch && remoteMatch[0].length === remote.length) {
dep.lookupName = remote.replace('https://', '');
} else {
dep.skipReason = SkipReason.UnsupportedRemote;
}
}
if (commit) {
dep.currentValue = 'v0.0.0';
dep.currentDigest = commit;
dep.currentDigestShort = commit.substr(0, 7);
dep.digestOneAndOnly = true;
}
deps.push(dep);
} else if (
(depType === 'http_archive' || depType === 'http_file') &&
depName &&
parseUrl(url) &&
sha256
) {
const parsedUrl = parseUrl(url);
dep.depName = depName;
dep.repo = parsedUrl.repo;
if (/^[a-f0-9]{40}$/i.test(parsedUrl.currentValue)) {
dep.currentDigest = parsedUrl.currentValue;
} else {
dep.currentValue = parsedUrl.currentValue;
}
dep.datasource = parsedUrl.datasource;
dep.lookupName = dep.repo;
deps.push(dep);
} else if (
depType === 'container_pull' &&
currentValue &&
digest &&
repository &&
registry
) {
dep.currentDigest = digest;
dep.currentValue = currentValue;
dep.depName = depName;
dep.versioning = dockerVersioning.id;
dep.datasource = datasourceDocker.id;
dep.lookupName = repository;
dep.registryUrls = [registry];
deps.push(dep);
} else {
logger.debug(
{ def },
'Failed to find dependency in bazel WORKSPACE definition'
);
}
});
if (!deps.length) {
return null;
}
return { deps };
}