-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathrules.ts
244 lines (226 loc) · 7.55 KB
/
rules.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
import is from '@sindresorhus/is';
import parseGithubUrl from 'github-url-from-git';
import { z } from 'zod';
import { logger } from '../../../logger';
import type { SkipReason } from '../../../types';
import { clone } from '../../../util/clone';
import { regEx } from '../../../util/regex';
import { BazelDatasource } from '../../datasource/bazel';
import { GithubTagsDatasource } from '../../datasource/github-tags';
import type { PackageDependency } from '../types';
import { RecordFragmentSchema, StringFragmentSchema } from './fragments';
// Rule Schemas
export interface BasePackageDep extends PackageDependency {
depType: string;
depName: string;
}
type BasePackageDepMergeKeys = Extract<keyof BasePackageDep, 'registryUrls'>;
export interface MergePackageDep extends BasePackageDep {
// The fields that should be copied from this struct to the bazel_dep
// PackageDependency.
bazelDepMergeFields: BasePackageDepMergeKeys[];
}
export interface OverridePackageDep extends BasePackageDep {
// This value is set as the skipReason on the bazel_dep PackageDependency.
bazelDepSkipReason: SkipReason;
}
export type BazelModulePackageDep =
| BasePackageDep
| OverridePackageDep
| MergePackageDep;
function isOverride(value: BazelModulePackageDep): value is OverridePackageDep {
return 'bazelDepSkipReason' in value;
}
function isMerge(value: BazelModulePackageDep): value is MergePackageDep {
return 'bazelDepMergeFields' in value;
}
// This function exists to remove properties that are specific to
// BazelModulePackageDep. In theory, there is no harm in leaving the properties
// as it does not invalidate the PackageDependency interface. However, it might
// be surprising to someone outside the bazel-module code to see the extra
// properties.
export function bazelModulePackageDepToPackageDependency(
bmpd: BazelModulePackageDep,
): PackageDependency {
const copy: BazelModulePackageDep = clone(bmpd);
if (isOverride(copy)) {
const partial = copy as Partial<OverridePackageDep>;
delete partial.bazelDepSkipReason;
}
if (isMerge(copy)) {
const partial = copy as Partial<MergePackageDep>;
delete partial.bazelDepMergeFields;
}
return copy;
}
const BazelDepToPackageDep = RecordFragmentSchema.extend({
children: z.object({
rule: StringFragmentSchema.extend({
value: z.literal('bazel_dep'),
}),
name: StringFragmentSchema,
version: StringFragmentSchema,
}),
}).transform(
({ children: { rule, name, version } }): BasePackageDep => ({
datasource: BazelDatasource.id,
depType: rule.value,
depName: name.value,
currentValue: version.value,
}),
);
const GitOverrideToPackageDep = RecordFragmentSchema.extend({
children: z.object({
rule: StringFragmentSchema.extend({
value: z.literal('git_override'),
}),
module_name: StringFragmentSchema,
remote: StringFragmentSchema,
commit: StringFragmentSchema,
}),
}).transform(
({
children: { rule, module_name: moduleName, remote, commit },
}): OverridePackageDep => {
const override: OverridePackageDep = {
depType: rule.value,
depName: moduleName.value,
bazelDepSkipReason: 'git-dependency',
currentDigest: commit.value,
};
const ghPackageName = githubPackageName(remote.value);
if (is.nonEmptyString(ghPackageName)) {
override.datasource = GithubTagsDatasource.id;
override.packageName = ghPackageName;
} else {
override.skipReason = 'unsupported-datasource';
}
return override;
},
);
const SingleVersionOverrideToPackageDep = RecordFragmentSchema.extend({
children: z.object({
rule: StringFragmentSchema.extend({
value: z.literal('single_version_override'),
}),
module_name: StringFragmentSchema,
version: StringFragmentSchema.optional(),
registry: StringFragmentSchema.optional(),
}),
}).transform(
({
children: { rule, module_name: moduleName, version, registry },
}): BasePackageDep => {
const base: BasePackageDep = {
depType: rule.value,
depName: moduleName.value,
skipReason: 'ignored',
};
// If a version is specified, then add a skipReason to bazel_dep
if (version) {
const override = base as OverridePackageDep;
override.bazelDepSkipReason = 'is-pinned';
override.currentValue = version.value;
}
// If a registry is specified, then merge it into the bazel_dep
if (registry) {
const merge = base as MergePackageDep;
merge.bazelDepMergeFields = ['registryUrls'];
merge.registryUrls = [registry.value];
}
return base;
},
);
const UnsupportedOverrideToPackageDep = RecordFragmentSchema.extend({
children: z.object({
rule: StringFragmentSchema.extend({
value: z.enum(['archive_override', 'local_path_override']),
}),
module_name: StringFragmentSchema,
}),
}).transform(
({ children: { rule, module_name: moduleName } }): OverridePackageDep => {
let bazelDepSkipReason: SkipReason = 'unsupported';
switch (rule.value) {
case 'archive_override':
bazelDepSkipReason = 'file-dependency';
break;
case 'local_path_override':
bazelDepSkipReason = 'local-dependency';
break;
}
return {
depType: rule.value,
depName: moduleName.value,
skipReason: 'unsupported-datasource',
bazelDepSkipReason,
};
},
);
export const RuleToBazelModulePackageDep = z.union([
BazelDepToPackageDep,
GitOverrideToPackageDep,
SingleVersionOverrideToPackageDep,
UnsupportedOverrideToPackageDep,
]);
const githubRemoteRegex = regEx(
/^https:\/\/github\.com\/(?<packageName>[^/]+\/.+)$/,
);
function githubPackageName(remote: string): string | undefined {
return parseGithubUrl(remote)?.match(githubRemoteRegex)?.groups?.packageName;
}
function collectByModule(
packageDeps: BazelModulePackageDep[],
): BazelModulePackageDep[][] {
const rulesByModule = new Map<string, BasePackageDep[]>();
for (const pkgDep of packageDeps) {
const bmi = rulesByModule.get(pkgDep.depName) ?? [];
bmi.push(pkgDep);
rulesByModule.set(pkgDep.depName, bmi);
}
return Array.from(rulesByModule.values());
}
export function processModulePkgDeps(
packageDeps: BazelModulePackageDep[],
): PackageDependency[] {
if (!packageDeps.length) {
return [];
}
const moduleName = packageDeps[0].depName;
const bazelDep = packageDeps.find((pd) => pd.depType === 'bazel_dep');
if (!bazelDep) {
logger.debug(`A 'bazel_dep' was not found for '${moduleName}'.`);
return [];
}
// Create a new bazelDep that will be modified. We do not want to change the
// input.
const bazelDepOut = { ...bazelDep };
const deps: PackageDependency[] = [bazelDepOut];
const merges = packageDeps.filter(isMerge);
for (const merge of merges) {
merge.bazelDepMergeFields.forEach((k) => (bazelDepOut[k] = merge[k]));
}
const overrides = packageDeps.filter(isOverride);
if (overrides.length === 0) {
return deps;
}
// It is an error for more than one override to exist for a module. We will
// ignore the overrides if there is more than one.
if (overrides.length > 1) {
const depTypes = overrides.map((o) => o.depType);
logger.debug(
{ depName: moduleName, depTypes },
'More than one override for a module was found',
);
return deps;
}
const override = overrides[0];
deps.push(bazelModulePackageDepToPackageDependency(override));
bazelDepOut.skipReason = override.bazelDepSkipReason;
return deps;
}
export function toPackageDependencies(
packageDeps: BazelModulePackageDep[],
): PackageDependency[] {
return collectByModule(packageDeps).map(processModulePkgDeps).flat();
}