From a4a315675cecb222278296e74a9f6d543ae90caa Mon Sep 17 00:00:00 2001 From: dastrong-codiga <100973772+dastrong-codiga@users.noreply.github.com> Date: Tue, 10 Jan 2023 08:55:38 -0800 Subject: [PATCH 01/10] chore: throw error if no token --- utils/rulesets.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/utils/rulesets.js b/utils/rulesets.js index ea0dabf..04e6c36 100644 --- a/utils/rulesets.js +++ b/utils/rulesets.js @@ -3,12 +3,8 @@ import { codigaApiFetch } from "./api"; import { ACTION_TOKEN_ADD, CODIGA_CONFIG_FILE } from "./constants"; import { getRootDirectory } from "./git"; import { GET_RULESETS_FOR_CLIENT } from "../graphql/queries"; -import { - printCommandSuggestion, - printFailure, - printInfo, - printSuggestion, -} from "./print"; +import { printCommandSuggestion, printFailure, printSuggestion } from "./print"; +import { getToken } from "./store"; /** * Gets an array of rulesets and their rules @@ -16,6 +12,9 @@ import { */ export async function getRulesetsWithRules(names) { try { + if (!getToken()) { + throw new Error("Not Authorized"); + } const resp = await codigaApiFetch(GET_RULESETS_FOR_CLIENT, { names }); const rulesetsWithRules = resp.ruleSetsForClient || []; return rulesetsWithRules; From b8bc52873445ca339bd63cb58d4eadb46e213bce Mon Sep 17 00:00:00 2001 From: dastrong-codiga <100973772+dastrong-codiga@users.noreply.github.com> Date: Tue, 10 Jan 2023 08:56:07 -0800 Subject: [PATCH 02/10] feat: extract function and test it --- tests/language-support.test.js | 46 ++++++++++++++++++++++++++++++++ utils/rosie.js | 48 ++++++++++++++++++++-------------- 2 files changed, 75 insertions(+), 19 deletions(-) create mode 100644 tests/language-support.test.js diff --git a/tests/language-support.test.js b/tests/language-support.test.js new file mode 100644 index 0000000..78d2483 --- /dev/null +++ b/tests/language-support.test.js @@ -0,0 +1,46 @@ +import { + LANGUAGE_JAVASCRIPT, + LANGUAGE_PYTHON, + LANGUAGE_TYPESCRIPT, +} from "../utils/constants"; +import { managePathsBySupportAndLanguage } from "../utils/rosie"; + +const notSupportedPaths = [ + "config.json", + "README.md", + "LICENSE", + "index.html", + "picture.png", +]; + +const pythonPaths = [ + "index.py3", + "/python.py3", + "/some-folder/nested/index.py", +]; + +const javascriptPaths = [ + "index.js", + "/javascript.jsx", + "/some-folder/nested/index.js", +]; + +const typescriptPaths = [ + "index.ts", + "/typescript.tsx", + "/some-folder/nested/index.ts", +]; + +test("path files are split correctly", async () => { + const files = managePathsBySupportAndLanguage([ + ...notSupportedPaths, + ...pythonPaths, + ...javascriptPaths, + ...typescriptPaths, + ]); + + expect(files.notSupported.length).toBe(5); + expect(files[LANGUAGE_PYTHON].length).toBe(3); + expect(files[LANGUAGE_JAVASCRIPT].length).toBe(3); + expect(files[LANGUAGE_TYPESCRIPT].length).toBe(3); +}); diff --git a/utils/rosie.js b/utils/rosie.js index 6b750e7..8fa44af 100644 --- a/utils/rosie.js +++ b/utils/rosie.js @@ -6,31 +6,41 @@ import { getRulesForRosiePerLanguage } from "./rules"; import { printEmptyLine, printFailure, printInfo, printSubItem } from "./print"; /** - * + * Used to filter out unsupported language files and + * combine similar language files together + * @param {string[]} paths + * @returns {{notSupported: string[], 'language': string[]}} + */ +export function managePathsBySupportAndLanguage(paths) { + return paths.reduce( + (acc, path) => { + const fileLanguage = getLanguageForFile(path); + if (!fileLanguage) { + acc.notSupported.push(path); + } else { + if (acc[fileLanguage]) { + acc[fileLanguage].push(path); + } else { + acc[fileLanguage] = [path]; + } + } + return acc; + }, + { + notSupported: [], + } + ); +} + +/** + * Run a Rosie check on the given paths against with the given rules * @param {string[]} paths * @param {RosieRule} rules */ export async function analyzeFiles(paths, rules) { try { // we won't analyze files for languages that aren't supported - const files = paths.reduce( - (acc, path) => { - const fileLanguage = getLanguageForFile(path); - if (!fileLanguage) { - acc.notSupported.push(path); - } else { - if (acc[fileLanguage]) { - acc[fileLanguage].push(path); - } else { - acc[fileLanguage] = [path]; - } - } - return acc; - }, - { - notSupported: [], - } - ); + const files = managePathsBySupportAndLanguage(paths); printEmptyLine(); // if there's are unsupported files we'll log that with a notice From 53546deac08c194012f136b304e2647cd7348520 Mon Sep 17 00:00:00 2001 From: dastrong-codiga <100973772+dastrong-codiga@users.noreply.github.com> Date: Tue, 10 Jan 2023 08:56:35 -0800 Subject: [PATCH 03/10] feat: test ruleset-to-rule functionality --- tests/fixtures/rulesetsMock.js | 41 ++++++++++++++++++++++++++++ tests/ruleset-to-rules.test.js | 49 ++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 tests/fixtures/rulesetsMock.js create mode 100644 tests/ruleset-to-rules.test.js diff --git a/tests/fixtures/rulesetsMock.js b/tests/fixtures/rulesetsMock.js new file mode 100644 index 0000000..e666725 --- /dev/null +++ b/tests/fixtures/rulesetsMock.js @@ -0,0 +1,41 @@ +export function getRulesetsWithRulesMock(rulesets) { + return mockedRulesets.filter((ruleset) => rulesets.includes(ruleset.name)); +} + +export const mockedRulesets = [ + { + id: 1, + name: "testing-ruleset-1", + rules: [ + { + id: 11, + name: "testing-rule-11", + content: "ZnVuY3Rpb24gdmlzaXQobm9kZSl7CiAgaWYoIW5vZGUpIHJldHVybiAKfQ==", + ruleType: "Ast", + language: "Javascript", + pattern: null, + elementChecked: "HtmlElement", + }, + ], + }, + { + id: 2, + name: "testing-ruleset-2", + rules: [ + { + id: 12, + name: "testing-rule-22", + content: "ZnVuY3Rpb24gdmlzaXQobm9kZSl7CiAgaWYoIW5vZGUpIHJldHVybiAKfQ==", + ruleType: "Ast", + language: "Javascript", + pattern: null, + elementChecked: "Assignment", + }, + ], + }, + { + id: 3, + name: "testing-ruleset-3", + rules: [], + }, +]; diff --git a/tests/ruleset-to-rules.test.js b/tests/ruleset-to-rules.test.js new file mode 100644 index 0000000..f4e11e4 --- /dev/null +++ b/tests/ruleset-to-rules.test.js @@ -0,0 +1,49 @@ +import { convertRulesetsToRules } from "../utils/rules"; +import { getRulesetsWithRulesMock } from "./fixtures/rulesetsMock"; + +test("can fetch rulesets when an API token is set", async () => { + expect( + convertRulesetsToRules( + getRulesetsWithRulesMock(["testing-ruleset-1", "testing-ruleset-2"]) + ) + ).toEqual([ + { + id: "testing-ruleset-1/testing-rule-11", + contentBase64: + "ZnVuY3Rpb24gdmlzaXQobm9kZSl7CiAgaWYoIW5vZGUpIHJldHVybiAKfQ==", + language: "javascript", + type: "ast", + entityChecked: "htmlelement", + pattern: null, + }, + { + id: "testing-ruleset-2/testing-rule-22", + contentBase64: + "ZnVuY3Rpb24gdmlzaXQobm9kZSl7CiAgaWYoIW5vZGUpIHJldHVybiAKfQ==", + language: "javascript", + type: "ast", + entityChecked: "assign", + pattern: null, + }, + ]); + + expect( + convertRulesetsToRules( + getRulesetsWithRulesMock(["testing-ruleset-1", "testing-ruleset-3"]) + ) + ).toEqual([ + { + id: "testing-ruleset-1/testing-rule-11", + contentBase64: + "ZnVuY3Rpb24gdmlzaXQobm9kZSl7CiAgaWYoIW5vZGUpIHJldHVybiAKfQ==", + language: "javascript", + type: "ast", + entityChecked: "htmlelement", + pattern: null, + }, + ]); + + expect( + convertRulesetsToRules(getRulesetsWithRulesMock(["testing-ruleset-3"])) + ).toEqual([]); +}); From 983a1bd8f3f8b05a051ef6d2839224bd22bc4411 Mon Sep 17 00:00:00 2001 From: dastrong-codiga <100973772+dastrong-codiga@users.noreply.github.com> Date: Tue, 10 Jan 2023 08:57:04 -0800 Subject: [PATCH 04/10] chore: removed unused imports --- tests/check-push.test.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/check-push.test.js b/tests/check-push.test.js index c24b4fd..a8f9baa 100644 --- a/tests/check-push.test.js +++ b/tests/check-push.test.js @@ -1,6 +1,5 @@ import { ACTION_GIT_PUSH_HOOK, BLANK_SHA } from "../utils/constants"; -import { setToken } from "../utils/store"; -import { executeCommand, SAMPLE_TOKEN } from "./test-utils"; +import { executeCommand } from "./test-utils"; describe("codiga git-push-hook", () => { test("check for same SHAs", async () => { From 0004ad7ba0e864ffd43179bb52157f7d429329da Mon Sep 17 00:00:00 2001 From: dastrong-codiga <100973772+dastrong-codiga@users.noreply.github.com> Date: Tue, 10 Jan 2023 09:21:21 -0800 Subject: [PATCH 05/10] chore: update dev doc --- DEVELOPMENT.md | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index 1bf0f9f..8ba6762 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -22,9 +22,32 @@ ### Running the tests -```bash -npm run test -``` +- Install and symlink the package as described in the Getting Started + + ```bash + npm i + npm link + ``` + +- Run the test script + + ```bash + npm run test + ``` + +> We'll automatically run the tests on `push` and `merge-request` actions. You can see past workflow runs [here](https://github.com/codiga/codiga-cli/actions/workflows/main.yml). + +### Release a new version + +- Open a MR with your new changes + - Bump the version in `package.json` and `package-lock.json` and commit it as well +- Once that's merged, go to [releases](https://github.com/codiga/codiga-cli/releases) and draft a new release + - Choose a tag > Create a new tag > `vX.X.X` (should match your new version above) + - Generate release notes + - Publish release +- Verify the following: + - The [Release Github Action](https://github.com/codiga/codiga-cli/actions/workflows/release.yml) is successful + - The [NPM package](https://www.npmjs.com/package/@codiga/cli) was updated ### Notes From c8c1405429e1b3b193af48bc173e75885a64c680 Mon Sep 17 00:00:00 2001 From: dastrong-codiga <100973772+dastrong-codiga@users.noreply.github.com> Date: Tue, 10 Jan 2023 10:26:15 -0800 Subject: [PATCH 06/10] chore: update gitignore --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 3091757..6b622a4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ node_modules -coverage \ No newline at end of file +coverage +.vscode +codiga-cli*.tgz \ No newline at end of file From 7cf20cdd2ded8fbc633211bff2298196a09fe00c Mon Sep 17 00:00:00 2001 From: dastrong-codiga <100973772+dastrong-codiga@users.noreply.github.com> Date: Tue, 10 Jan 2023 10:26:30 -0800 Subject: [PATCH 07/10] chore: removed vscode settings --- .vscode/settings.json | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index ed94f44..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "git.ignoreLimitWarning": true -} \ No newline at end of file From ffe32706e8ce7278b9052e6c1c18286a81804de5 Mon Sep 17 00:00:00 2001 From: dastrong-codiga <100973772+dastrong-codiga@users.noreply.github.com> Date: Tue, 10 Jan 2023 10:26:38 -0800 Subject: [PATCH 08/10] chore: add npmignore file --- .npmignore | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .npmignore diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..1d04daa --- /dev/null +++ b/.npmignore @@ -0,0 +1,8 @@ +DEVELOPMENT.md +tests +coverage +babel.config.js +jest.config.js +.github +.vscode +codiga-cli*.tgz \ No newline at end of file From 2ceaba762d6b60535782297590f665f172943c62 Mon Sep 17 00:00:00 2001 From: dastrong-codiga <100973772+dastrong-codiga@users.noreply.github.com> Date: Tue, 10 Jan 2023 10:42:32 -0800 Subject: [PATCH 09/10] chore: add skip notice --- src/checkPush.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/checkPush.js b/src/checkPush.js index 68dfe52..3ff66b0 100644 --- a/src/checkPush.js +++ b/src/checkPush.js @@ -145,6 +145,17 @@ export async function checkPush(remoteShaArg, localShaArg) { if (violations.length === 0 && errors.length === 0) { process.exit(0); } else { + printEmptyLine(); + printInfo("Do you consider these violations as false positives?"); + printSuggestion( + " ↳ You can add the following flag to your `git push` to bypass this check:", + "--no-verify" + ); + printSuggestion( + " ↳ Consider commenting on those rules in the Codiga Hub, so the maintainer can improve them:", + "https://app.codiga.io/hub/rulesets" + ); + printEmptyLine(); process.exit(1); } } From b2eae6e2a5211582d2c7710ec2c5de980fa0c4e5 Mon Sep 17 00:00:00 2001 From: dastrong-codiga <100973772+dastrong-codiga@users.noreply.github.com> Date: Tue, 10 Jan 2023 10:44:20 -0800 Subject: [PATCH 10/10] chore: bump version --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index caf1994..e480b9b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@codiga/cli", - "version": "1.0.4", + "version": "1.0.5", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index ac452db..fe85e46 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@codiga/cli", - "version": "1.0.4", + "version": "1.0.5", "description": "A Codiga CLI used to integrate Codiga easily in your projects", "homepage": "https://github.com/codiga/codiga-cli", "repository": {