/
extract.ts
187 lines (180 loc) · 6.09 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
import is from '@sindresorhus/is';
import { load } from 'js-yaml';
import { PlatformId } from '../../constants';
import { id as githubTagsId } from '../../datasource/github-tags';
import { id as gitlabTagsId } from '../../datasource/gitlab-tags';
import { logger } from '../../logger';
import { SkipReason } from '../../types';
import { find } from '../../util/host-rules';
import { regEx } from '../../util/regex';
import type { PackageDependency, PackageFile } from '../types';
import {
matchesPrecommitConfigHeuristic,
matchesPrecommitDependencyHeuristic,
} from './parsing';
import { PreCommitConfig } from './types';
function isEmptyObject(obj: any): boolean {
return Object.keys(obj).length === 0 && obj.constructor === Object;
}
/**
* Determines the datasource(id) to be used for this dependency
* @param repository the full git url, ie git@github.com/user/project.
* Used in debug statements to clearly indicate the related dependency.
* @param hostname the hostname (ie github.com)
* Used to determine which renovate datasource should be used.
* Is matched literally against `github.com` and `gitlab.com`.
* If that doesn't match, `hostRules.find()` is used to find related sources.
* In that case, the hostname is passed on as registryUrl to the corresponding datasource.
*/
function determineDatasource(
repository: string,
hostname: string
): { datasource?: string; registryUrls?: string[]; skipReason?: SkipReason } {
if (hostname === 'github.com') {
logger.debug({ repository, hostname }, 'Found github dependency');
return { datasource: githubTagsId };
}
if (hostname === 'gitlab.com') {
logger.debug({ repository, hostname }, 'Found gitlab dependency');
return { datasource: gitlabTagsId };
}
const hostUrl = 'https://' + hostname;
const res = find({ url: hostUrl });
if (isEmptyObject(res)) {
// 1 check, to possibly prevent 3 failures in combined query of hostType & url.
logger.debug(
{ repository, hostUrl },
'Provided hostname does not match any hostRules. Ignoring'
);
return { skipReason: SkipReason.UnknownRegistry, registryUrls: [hostname] };
}
for (const [hostType, sourceId] of [
[PlatformId.Gitea, gitlabTagsId],
[PlatformId.Github, githubTagsId],
[PlatformId.Gitlab, gitlabTagsId],
]) {
if (!isEmptyObject(find({ hostType, url: hostUrl }))) {
logger.debug(
{ repository, hostUrl, hostType },
`Provided hostname matches a ${hostType} hostrule.`
);
return { datasource: sourceId, registryUrls: [hostname] };
}
}
logger.debug(
{ repository, registry: hostUrl },
'Provided hostname did not match any of the hostRules of hostType gitea,github nor gitlab'
);
return { skipReason: SkipReason.UnknownRegistry, registryUrls: [hostname] };
}
function extractDependency(
tag: string,
repository: string
): {
depName?: string;
depType?: string;
datasource?: string;
lookupName?: string;
skipReason?: SkipReason;
currentValue?: string;
} {
logger.debug({ tag }, 'Found version');
const urlMatchers = [
// This splits "http://my.github.com/user/repo" -> "my.github.com" "user/repo
regEx('^https?:\\/\\/(?<hostname>[^\\/]+)\\/(?<depName>\\S*)'),
// This splits "git@private.registry.com:user/repo" -> "private.registry.com" "user/repo
regEx('^git@(?<hostname>[^:]+):(?<depName>\\S*)'),
// This split "git://github.com/pre-commit/pre-commit-hooks" -> "github.com" "pre-commit/pre-commit-hooks"
/^git:\/\/(?<hostname>[^/]+)\/(?<depName>\S*)/,
];
for (const urlMatcher of urlMatchers) {
const match = urlMatcher.exec(repository);
if (match) {
const hostname = match.groups.hostname;
const depName = match.groups.depName.replace(/\.git$/i, '');
const sourceDef = determineDatasource(repository, hostname);
return {
...sourceDef,
depName,
depType: 'repository',
lookupName: depName,
currentValue: tag,
};
}
}
logger.info(
{ repository },
'Could not separate hostname from full dependency url.'
);
return {
depName: undefined,
depType: 'repository',
datasource: undefined,
lookupName: undefined,
skipReason: SkipReason.InvalidUrl,
currentValue: tag,
};
}
/**
* Find all supported dependencies in the pre-commit yaml object.
*
* @param precommitFile the parsed yaml config file
*/
function findDependencies(
precommitFile: PreCommitConfig
): Array<PackageDependency> {
if (!precommitFile.repos) {
logger.debug(`No repos section found, skipping file`);
return [];
}
const packageDependencies = [];
precommitFile.repos.forEach((item) => {
if (matchesPrecommitDependencyHeuristic(item)) {
logger.trace(item, 'Matched pre-commit dependency spec');
const repository = String(item.repo);
const tag = String(item.rev);
const dep = extractDependency(tag, repository);
packageDependencies.push(dep);
} else {
logger.trace(item, 'Did not find pre-commit repo spec');
}
});
return packageDependencies;
}
export function extractPackageFile(
content: string,
filename: string
): PackageFile | null {
type ParsedContent = Record<string, unknown> | PreCommitConfig;
let parsedContent: ParsedContent;
try {
parsedContent = load(content, { json: true }) as ParsedContent;
} catch (err) {
logger.debug({ filename, err }, 'Failed to parse pre-commit config YAML');
return null;
}
if (!is.plainObject<Record<string, unknown>>(parsedContent)) {
logger.warn(
{ filename },
`Parsing of pre-commit config YAML returned invalid result`
);
return null;
}
if (!matchesPrecommitConfigHeuristic(parsedContent)) {
logger.debug(
{ filename },
`File does not look like a pre-commit config file`
);
return null;
}
try {
const deps = findDependencies(parsedContent);
if (deps.length) {
logger.trace({ deps }, 'Found dependencies in pre-commit config');
return { deps };
}
} catch (err) /* istanbul ignore next */ {
logger.error({ filename, err }, 'Error scanning parsed pre-commit config');
}
return null;
}