-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from studiometa/bugfix/gitlab-reference
[Bugix] Fix parsing of `!reference` tag
- Loading branch information
Showing
9 changed files
with
193 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |