Skip to content

Commit 8906615

Browse files
Merge pull request #59 from studiometa/bugfix/gitlab-reference
[Bugix] Fix parsing of `!reference` tag
2 parents b18d073 + f06af33 commit 8906615

9 files changed

+193
-34
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66

77
## [Unreleased]
88

9+
### Fixed
10+
11+
- Fix parsing of !reference tag ([#50](https://github.com/studiometa/prettier-formatter-gitlab/issues/50), [#59](https://github.com/studiometa/prettier-formatter-gitlab/pull/59), [a937d55](https://github.com/studiometa/prettier-formatter-gitlab/commit/a937d55))
12+
913
## v2.1.0 - 2024.12.12
1014

1115
### Added

package-lock.json

+16-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
"dependencies": {
4949
"chalk": "^5.3.0",
5050
"jest-diff": "^29.7.0",
51-
"js-yaml": "^4.1.0",
52-
"prettier-linter-helpers": "^1.0.0"
51+
"prettier-linter-helpers": "^1.0.0",
52+
"yaml": "2.7.0"
5353
},
5454
"peerDependencies": {
5555
"prettier": "^3.0"

src/index.js

+5-30
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,19 @@
11
#!/usr/bin/env node
2-
import { readFileSync, mkdirSync, writeFileSync } from 'node:fs';
3-
import { cwd, env } from 'node:process';
4-
import { join, resolve, dirname } from 'node:path';
5-
import yaml from 'js-yaml';
2+
import { mkdirSync, writeFileSync } from 'node:fs';
3+
import { env } from 'node:process';
4+
import { dirname } from 'node:path';
65
import { diff } from './formatters/diff.js';
76
import { gitlab } from './formatters/gitlab.js';
87
import { parse } from './utils/parse-prettier-results.js';
9-
10-
const {
11-
// Used as a fallback for local testing.
12-
CI_CONFIG_PATH = '.gitlab-ci.yml',
13-
CI_JOB_NAME,
14-
CI_PROJECT_DIR = cwd(),
15-
} = env;
16-
17-
/**
18-
* Get the output path for the report file.
19-
* @returns {string}
20-
*/
21-
function getOutputPath() {
22-
const jobs = yaml.load(readFileSync(join(CI_PROJECT_DIR, CI_CONFIG_PATH), 'utf-8'));
23-
const { artifacts } = jobs[CI_JOB_NAME];
24-
const location = artifacts && artifacts.reports && artifacts.reports.codequality;
25-
const msg = `Expected ${CI_JOB_NAME}.artifacts.reports.codequality to be one exact path`;
26-
if (!location) {
27-
throw new Error(`${msg}, but no value was found.`);
28-
}
29-
if (Array.isArray(location)) {
30-
throw new Error(`${msg}, but found an array instead.`);
31-
}
32-
return resolve(CI_PROJECT_DIR, location);
33-
}
8+
import { getOutputPath } from './utils/get-output-path.js';
349

3510
/**
3611
* Format Prettier results for GitLab Code Quality Reports.
3712
* @param {string} results
3813
* @returns {Promise<void>}
3914
*/
4015
export async function prettierFormatterGitLab(results) {
41-
const { PRETTIER_CODE_QUALITY_REPORT } = env;
16+
const { CI_JOB_NAME, PRETTIER_CODE_QUALITY_REPORT } = env;
4217
if (CI_JOB_NAME || PRETTIER_CODE_QUALITY_REPORT) {
4318
const files = parse(results);
4419

src/utils/get-output-path.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { readFileSync, existsSync, lstatSync } from 'node:fs';
2+
import { cwd, env } from 'node:process';
3+
import { join, resolve } from 'node:path';
4+
import { parseDocument } from 'yaml';
5+
6+
/** @type {yaml.CollectionTag} */
7+
const referenceTag = {
8+
tag: '!reference',
9+
collection: 'seq',
10+
default: false,
11+
resolve() {
12+
// We only allow the syntax. We don’t actually resolve the reference.
13+
},
14+
};
15+
16+
/**
17+
* Get the output path for the report file.
18+
* @returns {string}
19+
*/
20+
export function getOutputPath() {
21+
const {
22+
// Used as a fallback for local testing.
23+
CI_CONFIG_PATH = '.gitlab-ci.yml',
24+
CI_JOB_NAME,
25+
CI_PROJECT_DIR = cwd(),
26+
} = env;
27+
28+
const configPath = join(CI_PROJECT_DIR, CI_CONFIG_PATH);
29+
30+
if (!existsSync(configPath) || !lstatSync(configPath).isFile()) {
31+
throw new Error(
32+
'Could not resolve .gitlab-ci.yml to automatically detect report artifact path.' +
33+
' Please manually provide a path via the ESLINT_CODE_QUALITY_REPORT variable.',
34+
);
35+
}
36+
37+
const doc = parseDocument(readFileSync(configPath, 'utf-8'), {
38+
version: '1.1',
39+
customTags: [referenceTag],
40+
});
41+
42+
const path = [CI_JOB_NAME, 'artifacts', 'reports', 'codequality'];
43+
const location = doc.getIn(path);
44+
45+
if (typeof location !== 'string' || !location) {
46+
throw new TypeError(
47+
`Expected ${path.join('.')} to be one exact path, got: ${JSON.stringify(location)}`,
48+
);
49+
}
50+
51+
return resolve(CI_PROJECT_DIR, location);
52+
}

test/__snapshots__/cli.spec.js.snap

+56
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,34 @@
22

33
exports[`prettier-formatter-gitlab cli should create a code quality report file with prettier -l 1`] = `
44
[
5+
{
6+
"check_name": "prettier",
7+
"description": "Delete ⏎",
8+
"fingerprint": "994d9053d8e5f73c098f2bcfadec786c",
9+
"location": {
10+
"lines": {
11+
"begin": 11,
12+
"end": 12,
13+
},
14+
"path": "test/__stubs__/.gitlab-ci.fail.yml",
15+
},
16+
"severity": "minor",
17+
"type": "issue",
18+
},
19+
{
20+
"check_name": "prettier",
21+
"description": "Delete ⏎",
22+
"fingerprint": "20c9ca526d0a43e3075a3e0f74135991",
23+
"location": {
24+
"lines": {
25+
"begin": 11,
26+
"end": 12,
27+
},
28+
"path": "test/__stubs__/.gitlab-ci.yml",
29+
},
30+
"severity": "minor",
31+
"type": "issue",
32+
},
533
{
634
"check_name": "prettier",
735
"description": "Replace ·arg·· with arg",
@@ -82,6 +110,34 @@ exports[`prettier-formatter-gitlab cli should create a code quality report file
82110
83111
exports[`prettier-formatter-gitlab cli should create a code quality report file with prettier -c 1`] = `
84112
[
113+
{
114+
"check_name": "prettier",
115+
"description": "Delete ⏎",
116+
"fingerprint": "994d9053d8e5f73c098f2bcfadec786c",
117+
"location": {
118+
"lines": {
119+
"begin": 11,
120+
"end": 12,
121+
},
122+
"path": "test/__stubs__/.gitlab-ci.fail.yml",
123+
},
124+
"severity": "minor",
125+
"type": "issue",
126+
},
127+
{
128+
"check_name": "prettier",
129+
"description": "Delete ⏎",
130+
"fingerprint": "20c9ca526d0a43e3075a3e0f74135991",
131+
"location": {
132+
"lines": {
133+
"begin": 11,
134+
"end": 12,
135+
},
136+
"path": "test/__stubs__/.gitlab-ci.yml",
137+
},
138+
"severity": "minor",
139+
"type": "issue",
140+
},
85141
{
86142
"check_name": "prettier",
87143
"description": "Replace ·arg·· with arg",

test/__stubs__/.gitlab-ci.fail.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.ref:
2+
script:
3+
- npm run lint
4+
5+
prettier:
6+
script:
7+
- !reference [.ref, script]
8+
artifacts:
9+
reports:
10+
codequalit: gl-codequality.json
11+

test/__stubs__/.gitlab-ci.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.ref:
2+
script:
3+
- npm run lint
4+
5+
prettier:
6+
script:
7+
- !reference [.ref, script]
8+
artifacts:
9+
reports:
10+
codequality: gl-codequality.json
11+

test/utils/get-output-path.spec.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { describe, it, expect, beforeEach, afterEach } from 'bun:test';
2+
import { resolve, join } from 'node:path';
3+
import { getOutputPath } from '../../src/utils/get-output-path.js';
4+
5+
let env;
6+
7+
beforeEach(() => {
8+
env = process.env;
9+
});
10+
11+
afterEach(() => {
12+
process.env = env;
13+
});
14+
15+
describe('The getPrettierFileInfos function', () => {
16+
it('should return the output path defined in a .gitlab-ci.yml file', () => {
17+
process.env.CI_PROJECT_DIR = resolve(join(import.meta.dirname, '../__stubs__/'));
18+
process.env.CI_JOB_NAME = 'prettier';
19+
console.log(process.env.CI_PROJECT_DIR);
20+
expect(getOutputPath()).toBe(
21+
resolve(join(import.meta.dirname, '../__stubs__/gl-codequality.json')),
22+
);
23+
});
24+
25+
it('should throw an error if it can not find the report file path', () => {
26+
process.env.CI_PROJECT_DIR = resolve(join(import.meta.dirname, '../__stubs__/'));
27+
process.env.CI_JOB_NAME = 'prettier';
28+
process.env.CI_CONFIG_PATH = '.gitlab-ci.fail.yml';
29+
expect(getOutputPath).toThrow();
30+
});
31+
32+
it('should throw an error if it can not find a .gitlab-ci.yml file', () => {
33+
process.env.CI_PROJECT_DIR = '/tmp';
34+
expect(getOutputPath).toThrow();
35+
});
36+
});

0 commit comments

Comments
 (0)