Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugix] Fix parsing of !reference tag #59

Merged
merged 5 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Fixed

- 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))

## v2.1.0 - 2024.12.12

### Added
Expand Down
18 changes: 16 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
"dependencies": {
"chalk": "^5.3.0",
"jest-diff": "^29.7.0",
"js-yaml": "^4.1.0",
"prettier-linter-helpers": "^1.0.0"
"prettier-linter-helpers": "^1.0.0",
"yaml": "2.7.0"
},
"peerDependencies": {
"prettier": "^3.0"
Expand Down
35 changes: 5 additions & 30 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,19 @@
#!/usr/bin/env node
import { readFileSync, mkdirSync, writeFileSync } from 'node:fs';
import { cwd, env } from 'node:process';
import { join, resolve, dirname } from 'node:path';
import yaml from 'js-yaml';
import { mkdirSync, writeFileSync } from 'node:fs';
import { env } from 'node:process';
import { dirname } from 'node:path';
import { diff } from './formatters/diff.js';
import { gitlab } from './formatters/gitlab.js';
import { parse } from './utils/parse-prettier-results.js';

const {
// Used as a fallback for local testing.
CI_CONFIG_PATH = '.gitlab-ci.yml',
CI_JOB_NAME,
CI_PROJECT_DIR = cwd(),
} = env;

/**
* Get the output path for the report file.
* @returns {string}
*/
function getOutputPath() {
const jobs = yaml.load(readFileSync(join(CI_PROJECT_DIR, CI_CONFIG_PATH), 'utf-8'));
const { artifacts } = jobs[CI_JOB_NAME];
const location = artifacts && artifacts.reports && artifacts.reports.codequality;
const msg = `Expected ${CI_JOB_NAME}.artifacts.reports.codequality to be one exact path`;
if (!location) {
throw new Error(`${msg}, but no value was found.`);
}
if (Array.isArray(location)) {
throw new Error(`${msg}, but found an array instead.`);
}
return resolve(CI_PROJECT_DIR, location);
}
import { getOutputPath } from './utils/get-output-path.js';

/**
* Format Prettier results for GitLab Code Quality Reports.
* @param {string} results
* @returns {Promise<void>}
*/
export async function prettierFormatterGitLab(results) {
const { PRETTIER_CODE_QUALITY_REPORT } = env;
const { CI_JOB_NAME, PRETTIER_CODE_QUALITY_REPORT } = env;
if (CI_JOB_NAME || PRETTIER_CODE_QUALITY_REPORT) {
const files = parse(results);

Expand Down
52 changes: 52 additions & 0 deletions src/utils/get-output-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { readFileSync, existsSync, lstatSync } from 'node:fs';
import { cwd, env } from 'node:process';
import { join, resolve } from 'node:path';
import { parseDocument } from 'yaml';

/** @type {yaml.CollectionTag} */
const referenceTag = {
tag: '!reference',
collection: 'seq',
default: false,
resolve() {
// We only allow the syntax. We don’t actually resolve the reference.
},
};

/**
* Get the output path for the report file.
* @returns {string}
*/
export function getOutputPath() {
const {
// Used as a fallback for local testing.
CI_CONFIG_PATH = '.gitlab-ci.yml',
CI_JOB_NAME,
CI_PROJECT_DIR = cwd(),
} = env;

const configPath = join(CI_PROJECT_DIR, CI_CONFIG_PATH);

if (!existsSync(configPath) || !lstatSync(configPath).isFile()) {
throw new Error(
'Could not resolve .gitlab-ci.yml to automatically detect report artifact path.' +
' Please manually provide a path via the ESLINT_CODE_QUALITY_REPORT variable.',
);
}

const doc = parseDocument(readFileSync(configPath, 'utf-8'), {
version: '1.1',
customTags: [referenceTag],
});

const path = [CI_JOB_NAME, 'artifacts', 'reports', 'codequality'];
const location = doc.getIn(path);

if (typeof location !== 'string' || !location) {
throw new TypeError(
`Expected ${path.join('.')} to be one exact path, got: ${JSON.stringify(location)}`,
);
}

return resolve(CI_PROJECT_DIR, location);
}
56 changes: 56 additions & 0 deletions test/__snapshots__/cli.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,34 @@

exports[`prettier-formatter-gitlab cli should create a code quality report file with prettier -l 1`] = `
[
{
"check_name": "prettier",
"description": "Delete ⏎",
"fingerprint": "994d9053d8e5f73c098f2bcfadec786c",
"location": {
"lines": {
"begin": 11,
"end": 12,
},
"path": "test/__stubs__/.gitlab-ci.fail.yml",
},
"severity": "minor",
"type": "issue",
},
{
"check_name": "prettier",
"description": "Delete ⏎",
"fingerprint": "20c9ca526d0a43e3075a3e0f74135991",
"location": {
"lines": {
"begin": 11,
"end": 12,
},
"path": "test/__stubs__/.gitlab-ci.yml",
},
"severity": "minor",
"type": "issue",
},
{
"check_name": "prettier",
"description": "Replace Β·argΒ·Β· with arg",
Expand Down Expand Up @@ -82,6 +110,34 @@ exports[`prettier-formatter-gitlab cli should create a code quality report file

exports[`prettier-formatter-gitlab cli should create a code quality report file with prettier -c 1`] = `
[
{
"check_name": "prettier",
"description": "Delete ⏎",
"fingerprint": "994d9053d8e5f73c098f2bcfadec786c",
"location": {
"lines": {
"begin": 11,
"end": 12,
},
"path": "test/__stubs__/.gitlab-ci.fail.yml",
},
"severity": "minor",
"type": "issue",
},
{
"check_name": "prettier",
"description": "Delete ⏎",
"fingerprint": "20c9ca526d0a43e3075a3e0f74135991",
"location": {
"lines": {
"begin": 11,
"end": 12,
},
"path": "test/__stubs__/.gitlab-ci.yml",
},
"severity": "minor",
"type": "issue",
},
{
"check_name": "prettier",
"description": "Replace Β·argΒ·Β· with arg",
Expand Down
11 changes: 11 additions & 0 deletions test/__stubs__/.gitlab-ci.fail.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.ref:
script:
- npm run lint

prettier:
script:
- !reference [.ref, script]
artifacts:
reports:
codequalit: gl-codequality.json

11 changes: 11 additions & 0 deletions test/__stubs__/.gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.ref:
script:
- npm run lint

prettier:
script:
- !reference [.ref, script]
artifacts:
reports:
codequality: gl-codequality.json

36 changes: 36 additions & 0 deletions test/utils/get-output-path.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, it, expect, beforeEach, afterEach } from 'bun:test';
import { resolve, join } from 'node:path';
import { getOutputPath } from '../../src/utils/get-output-path.js';

let env;

beforeEach(() => {
env = process.env;
});

afterEach(() => {
process.env = env;
});

describe('The getPrettierFileInfos function', () => {
it('should return the output path defined in a .gitlab-ci.yml file', () => {
process.env.CI_PROJECT_DIR = resolve(join(import.meta.dirname, '../__stubs__/'));
process.env.CI_JOB_NAME = 'prettier';
console.log(process.env.CI_PROJECT_DIR);
expect(getOutputPath()).toBe(
resolve(join(import.meta.dirname, '../__stubs__/gl-codequality.json')),
);
});

it('should throw an error if it can not find the report file path', () => {
process.env.CI_PROJECT_DIR = resolve(join(import.meta.dirname, '../__stubs__/'));
process.env.CI_JOB_NAME = 'prettier';
process.env.CI_CONFIG_PATH = '.gitlab-ci.fail.yml';
expect(getOutputPath).toThrow();
});

it('should throw an error if it can not find a .gitlab-ci.yml file', () => {
process.env.CI_PROJECT_DIR = '/tmp';
expect(getOutputPath).toThrow();
});
});