Skip to content

Commit 573e112

Browse files
authored
fix(ruleset-migrator): http/https uris not followed correctly (#2247)
1 parent 7d16080 commit 573e112

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

packages/ruleset-migrator/src/__tests__/ruleset.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,61 @@ describe('migrator', () => {
161161
});
162162
});
163163

164+
it('should follow links correctly', async () => {
165+
serveAssets({
166+
'http://domain/bitbucket/projects/API/repos/spectral-rules/raw/.spectral.yml?at=refs%2Fheads%2Fmaster': {
167+
extends: ['spectral:oas', 'oas-rules.yml'],
168+
rules: {
169+
'valid-type': 'error',
170+
},
171+
},
172+
'http://domain/bitbucket/projects/API/repos/spectral-rules/raw/oas-rules.yml': {
173+
rules: {
174+
'valid-type': {
175+
given: '$',
176+
function: {
177+
then: 'truthy',
178+
},
179+
},
180+
},
181+
},
182+
});
183+
184+
await vol.promises.writeFile(
185+
path.join(cwd, 'ruleset.json'),
186+
JSON.stringify({
187+
extends: [
188+
'http://domain/bitbucket/projects/API/repos/spectral-rules/raw/.spectral.yml?at=refs%2Fheads%2Fmaster',
189+
],
190+
}),
191+
);
192+
193+
expect(
194+
await migrateRuleset(path.join(cwd, 'ruleset.json'), {
195+
format: 'esm',
196+
fs: vol as any,
197+
}),
198+
).toEqual(`import {oas} from "@stoplight/spectral-rulesets";
199+
export default {
200+
"extends": [{
201+
"extends": [oas, {
202+
"rules": {
203+
"valid-type": {
204+
"given": "$",
205+
"function": {
206+
"then": "truthy"
207+
}
208+
}
209+
}
210+
}],
211+
"rules": {
212+
"valid-type": "error"
213+
}
214+
}]
215+
};
216+
`);
217+
});
218+
164219
describe('custom npm registry', () => {
165220
it('should be supported', async () => {
166221
serveAssets({

packages/ruleset-migrator/src/tree/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,6 @@ export class Tree {
128128
if (path.isURL(identifier) || path.isAbsolute(identifier)) {
129129
resolved = identifier;
130130
this.#resolvedPaths.add(identifier);
131-
} else if (kind === 'ruleset' && isPackageImport(identifier)) {
132-
resolved =
133-
ctx.npmRegistry !== null
134-
? path.join(ctx.npmRegistry, identifier)
135-
: requireResolve?.(identifier, { paths: [ctx.cwd] }) ?? path.join(ctx.cwd, identifier);
136131
} else if (
137132
(ctx.npmRegistry !== null && ctx.filepath.startsWith(ctx.npmRegistry)) ||
138133
isKnownNpmRegistry(ctx.filepath)
@@ -142,6 +137,11 @@ export class Tree {
142137
// <origin>/<pkg-name>
143138
// <origin>/<pkg-name>/<asset> where asset can be a custom fn, etc.
144139
resolved = path.join(ctx.filepath, identifier);
140+
} else if (kind === 'ruleset' && !path.isURL(ctx.filepath) && isPackageImport(identifier)) {
141+
resolved =
142+
ctx.npmRegistry !== null
143+
? path.join(ctx.npmRegistry, identifier)
144+
: requireResolve?.(identifier, { paths: [ctx.cwd] }) ?? path.join(ctx.cwd, identifier);
145145
} else {
146146
resolved = path.join(ctx.filepath, '..', identifier);
147147
this.#resolvedPaths.add(resolved);

0 commit comments

Comments
 (0)