Skip to content

Commit a03ff12

Browse files
committed
feat: addapt to severity string enum
1 parent fccaa92 commit a03ff12

File tree

7 files changed

+123
-26
lines changed

7 files changed

+123
-26
lines changed

packages/certification-service/code/src/evaluate/graphqlEvaluate.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
const { ESLint } = require("eslint");
66
const graphqlPlugin = require("@graphql-eslint/eslint-plugin");
77
const path = require("path");
8-
const { WARN_SEVERITY } = require("./severity");
8+
const { WARN_SEVERITY, ERROR_SEVERITY } = require("./severity");
99
const { configValue } = require("../config/config");
1010
const GRAPHQL_FILE_EXTENSIONS = configValue("cerws.certification.protocol.GRAPHQL.file-extensions", [
1111
"graphql",
1212
"graphqls",
1313
"gql",
1414
]);
1515

16-
const evaluateGraphqlRepo = async (rootFolder, config) => {
16+
const evaluateGraphqlApi = async (rootFolder, config) => {
1717
return await runEslint(
1818
rootFolder,
1919
{
@@ -29,7 +29,7 @@ const evaluateGraphqlFile = async (file, config) => {
2929
path.dirname(file),
3030
{
3131
graphQLConfig: { schema: file },
32-
config,
32+
graphqlCustomConfig: config,
3333
},
3434
[file],
3535
);
@@ -45,7 +45,7 @@ const runEslint = async (cwd, config, files) => {
4545
if (config?.graphqlCustomConfig?.rulesConfig?.rules) {
4646
rules = config.graphqlCustomConfig.rulesConfig.rules;
4747
} else {
48-
rules = graphqlPlugin.configs["flat/schema-all"].rules;
48+
rules = graphqlPlugin.configs["flat/schema-recommended"].rules;
4949
}
5050

5151
const eslint = new ESLint({
@@ -71,13 +71,14 @@ const runEslint = async (cwd, config, files) => {
7171
results.forEach((file) => {
7272
file.messages.forEach((message) => {
7373
const customSeverity = config.graphqlCustomConfig?.rulesConfig?.severities[message.ruleId];
74-
message.customSeverity = typeof customSeverity === "number" ? customSeverity : WARN_SEVERITY;
74+
message.customSeverity =
75+
typeof customSeverity === "number" ? customSeverity : message.severity === 1 ? WARN_SEVERITY : ERROR_SEVERITY;
7576
});
7677
});
7778
return results;
7879
};
7980

8081
module.exports = {
81-
evaluateGraphqlRepo,
82+
evaluateGraphqlApi,
8283
evaluateGraphqlFile,
8384
};

packages/certification-service/code/src/format/issue.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
// SPDX-FileCopyrightText: 2025 Industria de Diseño Textil S.A. INDITEX
22
//
33
// SPDX-License-Identifier: Apache-2.0
4+
const { INFO_SEVERITY, WARN_SEVERITY, ERROR_SEVERITY } = require("../evaluate/severity");
45
const { cleanFileName } = require("../verify/utils");
6+
const severity = {
7+
[INFO_SEVERITY]: "INFO",
8+
[WARN_SEVERITY]: "WARN",
9+
[ERROR_SEVERITY]: "ERROR",
10+
};
511

612
const fromSpectralIssue = (issue, filePath, tempDir) => {
713
return {
814
fileName: cleanFileName(filePath, tempDir),
915
code: issue.code,
1016
message: issue.message,
11-
severity: issue.severity,
17+
severity: severity[issue.severity],
1218
range: {
1319
start: {
1420
line: issue.range?.start?.line,
@@ -28,7 +34,7 @@ const fromProtlintIssue = (issue, filePath, tempDir) => {
2834
fileName: cleanFileName(filePath, tempDir),
2935
code: issue.rule,
3036
message: issue.message,
31-
severity: issue.severity,
37+
severity: severity[issue.severity],
3238
range: {
3339
start: {
3440
line: issue.line,
@@ -48,7 +54,7 @@ const fromMarkdownlintIssue = (issue) => {
4854
fileName: issue.fileName,
4955
code: issue.ruleNames.join(", "),
5056
message: issue.ruleDescription,
51-
severity: issue.severity,
57+
severity: severity[issue.severity],
5258
range: {
5359
start: {
5460
line: issue.lineNumber,
@@ -69,7 +75,7 @@ const fromEslintIssue = (issue, filePath, tempDir) => {
6975
fileName: cleanFileName(filePath, tempDir),
7076
code: issue.messageId || issue.ruleId,
7177
message: issue.message,
72-
severity: issue.customSeverity,
78+
severity: severity[issue.customSeverity],
7379
range: {
7480
start: {
7581
line: issue.line,

packages/certification-service/code/src/verify/lint.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// SPDX-License-Identifier: Apache-2.0
44

55
const { evaluate } = require("../evaluate/spectralEvaluate");
6-
const { evaluateGraphqlRepo, evaluateGraphqlFile } = require("../evaluate/graphqlEvaluate");
6+
const { evaluateGraphqlApi, evaluateGraphqlFile } = require("../evaluate/graphqlEvaluate");
77
const evaluateProtolint = require("../evaluate/protolintEvaluate");
88
const { markdownEvaluate } = require("../evaluate/markdownEvaluate");
99
const { mkdirSync } = require("fs");
@@ -32,7 +32,7 @@ const lintFileWithMarkdownLint = async (file, ruleset) => {
3232

3333
const lintGraphql = async (rootFolder, config = {}) => {
3434
logger.info(`Linting graphql folder ${rootFolder}`);
35-
return await evaluateGraphqlRepo(rootFolder, config);
35+
return await evaluateGraphqlApi(rootFolder, config);
3636
};
3737

3838
const lintGraphqlFile = async (file, config = {}) => {

packages/certification-service/code/test/evaluate/graphqlEvaluate.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
//
33
// SPDX-License-Identifier: Apache-2.0
44

5-
const { evaluateGraphqlRepo, evaluateGraphqlFile } = require("../../src/evaluate/graphqlEvaluate");
5+
const { evaluateGraphqlApi, evaluateGraphqlFile } = require("../../src/evaluate/graphqlEvaluate");
66
const { ESLint } = require("eslint");
77
const graphqlPlugin = require("@graphql-eslint/eslint-plugin");
88
const path = require("path");
9-
const { WARN_SEVERITY, INFO_SEVERITY } = require("../../src/evaluate/severity");
9+
const { WARN_SEVERITY, INFO_SEVERITY, ERROR_SEVERITY } = require("../../src/evaluate/severity");
1010
const { getAppLogger } = require("../../src/log");
1111

1212
jest.mock("eslint");
@@ -24,7 +24,7 @@ describe("graphqlEvaluate", () => {
2424
jest.clearAllMocks();
2525
});
2626

27-
describe("evaluateGraphqlRepo", () => {
27+
describe("evaluateGraphqlApi", () => {
2828
test("should run ESLint for repository", async () => {
2929
const mockLintFiles = jest.fn().mockResolvedValue([]);
3030

@@ -35,7 +35,7 @@ describe("graphqlEvaluate", () => {
3535
const mockRootFolder = "/mock/root/folder";
3636
const mockConfig = {};
3737

38-
const result = await evaluateGraphqlRepo(mockRootFolder, mockConfig);
38+
const result = await evaluateGraphqlApi(mockRootFolder, mockConfig);
3939

4040
expect(ESLint).toHaveBeenCalledWith({
4141
overrideConfigFile: true,
@@ -53,7 +53,7 @@ describe("graphqlEvaluate", () => {
5353
plugins: {
5454
"@graphql-eslint": graphqlPlugin,
5555
},
56-
rules: graphqlPlugin.configs["flat/schema-all"].rules,
56+
rules: graphqlPlugin.configs["flat/schema-recommended"].rules,
5757
},
5858
});
5959

@@ -135,7 +135,7 @@ describe("graphqlEvaluate", () => {
135135
"/mock/root/folder/**/*.graphqls",
136136
"/mock/root/folder/**/*.gql",
137137
];
138-
const result = await evaluateGraphqlRepo(mockRootFolder, mockConfig);
138+
const result = await evaluateGraphqlApi(mockRootFolder, mockConfig);
139139

140140
expect(ESLint).toHaveBeenCalledWith({
141141
overrideConfigFile: true,
@@ -186,7 +186,7 @@ describe("graphqlEvaluate", () => {
186186
range: [1304, 1521],
187187
text: "",
188188
},
189-
customSeverity: WARN_SEVERITY,
189+
customSeverity: ERROR_SEVERITY,
190190
},
191191
],
192192
suppressedMessages: [],
@@ -234,7 +234,7 @@ describe("graphqlEvaluate", () => {
234234
plugins: {
235235
"@graphql-eslint": graphqlPlugin,
236236
},
237-
rules: graphqlPlugin.configs["flat/schema-all"].rules,
237+
rules: graphqlPlugin.configs["flat/schema-recommended"].rules,
238238
},
239239
});
240240

packages/certification-service/code/test/format/issue.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe("Issue Conversion Functions", () => {
3535
fileName: "file.yaml",
3636
code: "contact-email",
3737
message: "Definition must have a contact email",
38-
severity: 1,
38+
severity: "WARN",
3939
range: issue.range,
4040
path: issue.path,
4141
});
@@ -55,7 +55,7 @@ describe("Issue Conversion Functions", () => {
5555
fileName: "proto.proto",
5656
code: "ENUM_NAMES_LOWER_SNAKE_CASE",
5757
message: 'Enum name "Brand" must be underscore_separated_names',
58-
severity: 1,
58+
severity: "WARN",
5959
range: {
6060
start: { line: 125, character: 2 },
6161
end: { line: 125, character: 2 },
@@ -84,7 +84,7 @@ describe("Issue Conversion Functions", () => {
8484
fileName: "test/README.md",
8585
code: "MD012, no-multiple-blanks",
8686
message: "Multiple consecutive blank lines",
87-
severity: 1,
87+
severity: "WARN",
8888
range: {
8989
start: { line: 3, character: 1 },
9090
end: { line: 3, character: 1 },
@@ -116,7 +116,7 @@ describe("Issue Conversion Functions", () => {
116116
fileName: "schema.graphqls",
117117
code: "alphabetize",
118118
message: 'type "Color" should be before type "MasterData"',
119-
severity: 2,
119+
severity: "INFO",
120120
range: {
121121
start: { line: 91, character: 6 },
122122
end: { line: 91, character: 11 },
@@ -142,7 +142,7 @@ describe("Issue Conversion Functions", () => {
142142
fileName: "schema.graphql",
143143
code: "custom-rules/my-custom-rule",
144144
message: "The field `Query.executeExampleMethod` has no valid name!",
145-
severity: 2,
145+
severity: "INFO",
146146
range: {
147147
start: { line: 7, character: 3 },
148148
end: { line: 16, character: 20 },

packages/certification-service/code/test/verify/graphqlLinter.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ describe("GraphqlLinter", () => {
9191
fileName: "file1.graphql",
9292
code: "alphabetize",
9393
message: 'type "Color" should be before type "MasterData"',
94-
severity: INFO_SEVERITY,
94+
severity: "INFO",
9595
range: {
9696
start: { line: 91, character: 6 },
9797
end: { line: 91, character: 11 },
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
const { lintGraphql, lintGraphqlFile } = require("../../src/verify/lint");
2+
const { evaluateGraphqlApi, evaluateGraphqlFile } = require("../../src/evaluate/graphqlEvaluate");
3+
const { WARN_SEVERITY } = require("../../src/evaluate/severity");
4+
5+
jest.mock("../../src/evaluate/graphqlEvaluate", () => ({
6+
evaluateGraphqlApi: jest.fn(),
7+
evaluateGraphqlFile: jest.fn(),
8+
}));
9+
10+
describe("GraphQL Linter", () => {
11+
afterEach(() => {
12+
jest.clearAllMocks();
13+
});
14+
15+
describe("GraphQL Linter", () => {
16+
const graphqlLinterExpectedResult = [
17+
{
18+
filePath: "/test/folder/schema.graphql",
19+
messages: [
20+
{
21+
ruleId: "@graphql-eslint/require-description",
22+
severity: 2,
23+
message: 'Description is required for input "Article"',
24+
line: 41,
25+
column: 7,
26+
nodeType: null,
27+
messageId: "require-description",
28+
endLine: 41,
29+
endColumn: 14,
30+
customSeverity: 1,
31+
},
32+
],
33+
suppressedMessages: [],
34+
errorCount: 12,
35+
fatalErrorCount: 0,
36+
warningCount: 0,
37+
fixableErrorCount: 0,
38+
fixableWarningCount: 0,
39+
source: "",
40+
usedDeprecatedRules: [],
41+
},
42+
];
43+
44+
const linterConfig = {
45+
plugins: {},
46+
rulesConfig: {
47+
rules: {
48+
"@graphql-eslint/require-description": "error",
49+
},
50+
severities: {
51+
"@graphql-eslint/require-description": WARN_SEVERITY,
52+
},
53+
},
54+
};
55+
test("lintGraphql should call evaluateGraphqlApi without config", async () => {
56+
evaluateGraphqlApi.mockResolvedValue(graphqlLinterExpectedResult);
57+
58+
const result = await lintGraphql("/test/folder");
59+
60+
expect(evaluateGraphqlApi).toHaveBeenCalledWith("/test/folder", {});
61+
expect(result).toStrictEqual(graphqlLinterExpectedResult);
62+
});
63+
64+
test("lintGraphql should call evaluateGraphqlApi with config", async () => {
65+
evaluateGraphqlApi.mockResolvedValue(graphqlLinterExpectedResult);
66+
67+
const result = await lintGraphql("/test/folder", linterConfig);
68+
69+
expect(evaluateGraphqlApi).toHaveBeenCalledWith("/test/folder", linterConfig);
70+
expect(result).toStrictEqual(graphqlLinterExpectedResult);
71+
});
72+
73+
test("lintGraphqlFile should call evaluateGraphqlFile without config", async () => {
74+
evaluateGraphqlFile.mockResolvedValue(graphqlLinterExpectedResult);
75+
76+
const result = await lintGraphqlFile("/test/folder/schema.graphql");
77+
78+
expect(evaluateGraphqlFile).toHaveBeenCalledWith("/test/folder/schema.graphql", {});
79+
expect(result).toStrictEqual(graphqlLinterExpectedResult);
80+
});
81+
test("lintGraphqlFile should call evaluateGraphqlFile with config", async () => {
82+
evaluateGraphqlFile.mockResolvedValue(graphqlLinterExpectedResult);
83+
84+
const result = await lintGraphqlFile("/test/folder/schema.graphql", linterConfig);
85+
86+
expect(evaluateGraphqlFile).toHaveBeenCalledWith("/test/folder/schema.graphql", linterConfig);
87+
expect(result).toStrictEqual(graphqlLinterExpectedResult);
88+
});
89+
});
90+
});

0 commit comments

Comments
 (0)