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

🐛 read workspace config correctly #91

Merged
merged 4 commits into from
Dec 29, 2023
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
25 changes: 14 additions & 11 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,32 @@
"@jscpd/core": "^3.5.4",
"@jscpd/finder": "^3.5.10",
"@jscpd/tokenizer": "^3.5.4",
"@octokit/rest": "^18.2.0",
"@octokit/rest": "^18.12.0",
"deepmerge": "4.3.1",
"js-yaml": "^4.1.0",
"jscpd": "^3.5.10",
"node-fetch": "^2.7.0"
},
"devDependencies": {
"@babel/cli": "^7.23.0",
"@babel/core": "^7.23.2",
"@babel/preset-env": "^7.23.3",
"@octokit/types": "^12.3.0",
"@babel/cli": "^7.23.4",
"@babel/core": "^7.23.6",
"@babel/preset-env": "^7.23.6",
"@octokit/types": "^12.4.0",
"@types/jest": "^29.5.11",
"@types/js-yaml": "^4.0.9",
"@types/node": "^18.18.8",
"@types/node-fetch": "2.6.6",
"@typescript-eslint/parser": "^6.15.0",
"@types/node": "^18.19.3",
"@types/node-fetch": "2.6.10",
"@typescript-eslint/parser": "^6.16.0",
"@vercel/ncc": "^0.38.1",
"babel-jest": "^29.7.0",
"eslint": "^8.53.0",
"eslint-config-prettier": "^9.0.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-github": "^4.10.1",
"eslint-plugin-jest": "^27.4.3",
"eslint-plugin-prettier": "^5.0.1",
"eslint-plugin-jest": "^27.6.0",
"eslint-plugin-prettier": "^5.1.2",
"jest": "^29.7.0",
"js-yaml": "^4.1.0",
"prettier": "3.0.3",
"prettier": "3.1.1",
"ts-jest": "^29.1.1",
"typescript": "^4.9.5"
}
Expand Down
22 changes: 12 additions & 10 deletions src/duplicated.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { error, info, notice, setFailed, setOutput, warning } from '@actions/core';
import * as core from '@actions/core';
import { context } from '@actions/github';
import { IClone, IOptions } from '@jscpd/core';
import { Octokit } from '@octokit/rest';
Expand Down Expand Up @@ -34,12 +34,12 @@ export async function duplicatedCheck(
fs.writeFileSync(markdownReport, message);
await git.UploadReportToArtifacts([markdownReport, jsonReport], REPORT_ARTIFACT_NAME);
const isOverThreshold = checkThreshold(jsonReport, options.threshold || 0);
jscpdCheckAsError && isOverThreshold ? setFailed('❌ DUPLICATED CODE FOUND') : warning('DUPLICATED CODE FOUND', ANNOTATION_OPTIONS);
jscpdCheckAsError && isOverThreshold ? core.setFailed('❌ DUPLICATED CODE FOUND') : core.warning('DUPLICATED CODE FOUND', ANNOTATION_OPTIONS);
showAnnotation(clones, cwd, jscpdCheckAsError && isOverThreshold);
setOutput('hasDuplicates', `${isOverThreshold}`);
core.setOutput('hasDuplicates', `${isOverThreshold}`);
} else {
setOutput('hasDuplicates', 'false');
notice('✅ NO DUPLICATED CODE FOUND', ANNOTATION_OPTIONS);
core.setOutput('hasDuplicates', 'false');
core.notice('✅ NO DUPLICATED CODE FOUND', ANNOTATION_OPTIONS);
}
await execute(`rm -rf ${cwd}/${REPORT_ARTIFACT_NAME}`);
}
Expand All @@ -52,19 +52,21 @@ function getOptions(jscpdConfigPath: string, workspace: string, cwd: string): Pa
output: `${cwd}/${REPORT_ARTIFACT_NAME}`
};
const options = { ...configOptions, ...defaultOptions };
info(`loaded options: ${inspect(options)}`);
core.startGroup('🔎 loaded options');
core.info(`${inspect(options)}`);
core.endGroup();
return options;
}

function getReportFiles(cwd: string): string[] {
const files = fs.readdirSync(`${cwd}/${REPORT_ARTIFACT_NAME}`);
const filePaths = files.map(file => `${cwd}/${REPORT_ARTIFACT_NAME}/${file}`);
info(`reportFiles: ${filePaths.join(',')}`);
core.info(`reportFiles: ${filePaths.join(',')}`);
return filePaths;
}

function checkWorkspace(workspace: string): string {
info(`workspace: ${workspace}`);
core.info(`workspace: ${workspace}`);
//check if workspace path is a file
const isFile = fs.existsSync(workspace) && fs.lstatSync(workspace).isFile();
if (isFile) {
Expand All @@ -75,7 +77,7 @@ function checkWorkspace(workspace: string): string {
}

function showAnnotation(clones: IClone[], cwd: string, isError: boolean): void {
const show = isError ? error : warning;
const show = isError ? core.error : core.warning;
for (const clone of clones) {
show(
`${clone.duplicationA.sourceId.replace(cwd, '')} (${clone.duplicationA.start.line}-${clone.duplicationA.end.line})
Expand Down Expand Up @@ -137,7 +139,7 @@ function checkThreshold(jsonReport: string, threshold: number): boolean {
// read json report
const report = JSON.parse(fs.readFileSync(jsonReport, 'utf8')) as IJsonReport;
if (report.statistics.total.percentage > threshold) {
error(`DUPLICATED CODE FOUND ${report.statistics.total.percentage}% IS OVER THRESHOLD ${threshold}%`, ANNOTATION_OPTIONS);
core.error(`DUPLICATED CODE FOUND ${report.statistics.total.percentage}% IS OVER THRESHOLD ${threshold}%`, ANNOTATION_OPTIONS);
return true;
}
return false;
Expand Down
7 changes: 4 additions & 3 deletions src/readConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ function arrayMergeDedupe<T>(source: T[], target: T[]): T[] {
return Array.from(new Set([...source, ...target]));
}

export function readConfig<T>(defaultOptions: Partial<T>, configName: string, workspace: string, defaultConfigName: string): Partial<T> {
const configFile = resolve(configName || defaultConfigName);
const workspaceConfig = resolve(workspace, configName || defaultConfigName);
export function readConfig<T>(defaultOptions: Partial<T>, mainConfigPath: string, workspace: string, defaultConfigName: string): Partial<T> {
const configFile = resolve(mainConfigPath || defaultConfigName);
const configFilename = configFile.split('/').pop();
const workspaceConfig = resolve(workspace, configFilename || defaultConfigName);

const configExists = fs.existsSync(configFile);
const workspaceConfigExists = workspaceConfig !== configFile && fs.existsSync(workspaceConfig);
Expand Down