Skip to content

Commit 5fe3549

Browse files
feat: add logic for building and displaying helper url for rule
Signed-off-by: Fredrik Nordlander <fredrik.nordlander@digg.se>
1 parent 0cc5af1 commit 5fe3549

File tree

7 files changed

+954
-1228
lines changed

7 files changed

+954
-1228
lines changed

package-lock.json

Lines changed: 880 additions & 1224 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli-mode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,13 @@ export async function execCLI<T extends CliArgs>(argv: T) {
167167
}
168168
};
169169
const formatLintingResult = (result: any) => {
170-
return `allvarlighetsgrad: ${colorizeSeverity(result.allvarlighetsgrad)} \nid: ${result.id} \nkrav: ${
170+
return `\nallvarlighetsgrad: ${colorizeSeverity(result.allvarlighetsgrad)} \nid: ${result.id} \nkrav: ${
171171
result.krav
172172
} \nområde: ${result.område} \nsökväg:[${result.sökväg}] \nomfattning:${JSON.stringify(
173173
result.omfattning,
174174
null,
175175
2,
176-
)} `;
176+
)}\ndesignregel: ${result.helpUrl} `;
177177
};
178178
//Check specified option from yargs input
179179

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const RULES_DOC_REPO =
2+
'https://github.com/diggsweden/rest-api-profil-lint-processor';
3+
4+
5+
/**
6+
* Constructs a helper Url together with correct Rule
7+
* @param ruleId ( Rule identifier)
8+
* @returns Normalized help url
9+
*/
10+
export function buildRuleHelpUrl(ruleId: string): string {
11+
const ref = process.env.RULES_DOC_REF || 'main';
12+
const normalizedId = ruleId
13+
.toLowerCase()
14+
.replace('.', '');
15+
16+
return `${RULES_DOC_REPO}/blob/${ref}/GUIDELINES.md#id-${normalizedId}`;
17+
}

src/util/RapLPCustomSpectral.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ISpectralDiagnostic } from '@stoplight/spectral-core';
77
import spectralCore from '@stoplight/spectral-core';
88
const { Spectral, Document } = spectralCore;
99
import { RapLPCustomSpectralDiagnostic } from './RapLPCustomSpectralDiagnostic.js';
10+
import { buildRuleHelpUrl } from '../rulesets/util/rules-doc.config.js';
1011

1112
class RapLPCustomSpectral {
1213
private spectral: SpectralCore.Spectral;
@@ -49,9 +50,12 @@ class RapLPCustomSpectral {
4950
if (ruleClass && typeof ruleClass.getCustomProperties === 'function') {
5051
// Check for existance
5152
const customProperties = ruleClass.getCustomProperties;
53+
const ruleId = ruleClass.customProperties.id;
54+
5255
const customResult: RapLPCustomSpectralDiagnostic = {
53-
id: ruleClass.customProperties.id,
56+
id: ruleId,
5457
område: ruleClass.customProperties.område,
58+
helpUrl: ruleId ? buildRuleHelpUrl(ruleId) : undefined,
5559
...customProperties, // For more copy
5660
...this.mapResultToCustom(result),
5761
};

src/util/RapLPCustomSpectralDiagnostic.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ export interface RapLPCustomSpectralDiagnostic
1616
allvarlighetsgrad?: string;
1717
sökväg?: any;
1818
omfattning?: any;
19+
20+
/**Helper Url for guidelines */
21+
helpUrl?: string;
1922
}
2023

2124

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// SPDX-FileCopyrightText: 2025 Digg - Agency for Digital Government
2+
//
3+
// SPDX-License-Identifier: EUPL-1.2
4+
5+
import { buildRuleHelpUrl } from '../../src/rulesets/util/rules-doc.config.js';
6+
7+
describe('buildRuleHelpUrl', () => {
8+
const ORIGINAL_ENV = process.env;
9+
10+
beforeEach(() => {
11+
process.env = { ...ORIGINAL_ENV };
12+
});
13+
14+
afterAll(() => {
15+
process.env = ORIGINAL_ENV;
16+
});
17+
18+
it('bygger korrekt GitHub-länk för regel-ID med punkt (SAK.16)', () => {
19+
process.env.RULES_DOC_REF = 'main';
20+
21+
const url = buildRuleHelpUrl('SAK.16');
22+
23+
expect(url).toBe(
24+
'https://github.com/diggsweden/rest-api-profil-lint-processor/blob/main/GUIDELINES.md#id-sak16'
25+
);
26+
});
27+
28+
it('faller tillbaka till main om RULES_DOC_REF saknas', () => {
29+
delete process.env.RULES_DOC_REF;
30+
31+
const url = buildRuleHelpUrl('FOR.02');
32+
33+
expect(url).toBe(
34+
'https://github.com/diggsweden/rest-api-profil-lint-processor/blob/main/GUIDELINES.md#id-for02'
35+
);
36+
});
37+
38+
it('hanterar redan normaliserade ID:n korrekt', () => {
39+
process.env.RULES_DOC_REF = 'feature/test';
40+
41+
const url = buildRuleHelpUrl('sak16');
42+
43+
expect(url).toBe(
44+
'https://github.com/diggsweden/rest-api-profil-lint-processor/blob/feature/test/GUIDELINES.md#id-sak16'
45+
);
46+
});
47+
});

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"compilerOptions": {
3-
"types": ["node"],
43
"module": "NodeNext",
54
"moduleResolution": "NodeNext",
65
"target": "ES2020",

0 commit comments

Comments
 (0)