Skip to content

Commit 39c3cc3

Browse files
feat: add logic for building and displaying helper url for rule (#486)
* feat: add logic for building and displaying helper url for rule * fix: add copyright and licensing information --------- Signed-off-by: Fredrik Nordlander <fredrik.nordlander@digg.se>
1 parent 0cc5af1 commit 39c3cc3

File tree

11 files changed

+975
-1228
lines changed

11 files changed

+975
-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

src/model/SpecValidationRequestDto.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2+
// SPDX-FileCopyrightText: 2025 Digg - Agency for Digital Government
3+
//
4+
// SPDX-License-Identifier: EUPL-1.2
5+
16
export class SpecValidationRequestDto {
27
/** Base 64 encoded OpenAPI spec (YAML or JSON) */
38
spec!: string;

src/model/ValidationResponse.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// SPDX-FileCopyrightText: 2025 Digg - Agency for Digital Government
2+
//
3+
// SPDX-License-Identifier: EUPL-1.2
4+
15
import { ValidationResponseDto } from './ValidationResponseDto.js';
26
import { Issue } from '../util/Issue.js';
37

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

src/util/Issue.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// SPDX-FileCopyrightText: 2025 Digg - Agency for Digital Government
2+
//
3+
// SPDX-License-Identifier: EUPL-1.2
4+
15
/**
26
* Issue type to use from client to validate parsing
37
*/

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

tests/unit/api/endpointValidation.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// SPDX-FileCopyrightText: 2025 Digg - Agency for Digital Government
2+
//
3+
// SPDX-License-Identifier: EUPL-1.2
4+
15
/**
26
* CONTRACT TESTS for /api/v1/validation/validatespec
37
* - NOTE:
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+
});

0 commit comments

Comments
 (0)