Skip to content

Commit

Permalink
fix rules; use three dot actions; fix fail state
Browse files Browse the repository at this point in the history
  • Loading branch information
Flaque committed Jun 15, 2023
1 parent 20419a6 commit b031917
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 37 deletions.
6 changes: 3 additions & 3 deletions .rules/no-bugs.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Make sure our code follows these best practices, UNLESS there's a comment explai

1. Avoid typos.
2. Avoid things that could be infinite loops.
3. Do not log user data. User data is things like files, or secrets.
3. Do not log secrets.
4. Don't store secrets in code.
6. This codebase is Deno, try to follow the conventions of Deno.
7. Avoid dangerous stuff, like things that would show up as a CVE somewhere.
5. This codebase is Deno, try to follow the conventions of Deno.
6. Avoid dangerous stuff, like things that would show up as a CVE somewhere.
63 changes: 36 additions & 27 deletions cli/cmds/check/cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { walkTextFiles } from "../../walkTextFiles.ts";
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
import { relative } from "https://deno.land/[email protected]/path/mod.ts";
import { readConfig } from "../../config.ts";
import { Change, getChangesAsFiles, getChangesAsHunks } from "../../git.ts";
import { Change, getChangesAsHunks } from "../../git.ts";
import * as frontmatter from "https://deno.land/x/[email protected]/mod.ts";
import { globToRegExp } from "https://deno.land/[email protected]/path/glob.ts";
import { exists } from "https://deno.land/[email protected]/fs/mod.ts";
Expand Down Expand Up @@ -66,13 +66,13 @@ async function checkAndLogRuleFileAgainst(props: {
);
return true;
} else {
console.log(
` ${colors.bgRed(
colors.brightWhite(" FAIL ")
)} ${relativeEntry} ${relativeRuleEntry}\n${result.message} ${colors.dim(
`(${totalTime}ms)`
)}`
);
const tag = colors.bgRed(colors.brightWhite(" x FAIL "));

const header = ` ${tag} ${relativeRuleEntry} ${colors.dim(
"=>"
)} ${relativeEntry} ${colors.dim(`(${totalTime}ms)`)}`;

console.log(`${header}\n \n${result.message}\n`);
return false;
}
}
Expand Down Expand Up @@ -132,7 +132,7 @@ async function checkAndLogMessageAgainstFileAndSnippet(props: {
} else {
console.log(
` ${colors.bgRed(
colors.brightWhite(" FAIL ")
colors.brightWhite(" x FAIL ")
)} ${relativeEntry} ${msg}\n${result.message} ${colors.dim(
`(${totalTime}ms)`
)}`
Expand Down Expand Up @@ -220,21 +220,7 @@ export async function checkRulesAgainstDiff(props: {

// If there's no files found, explain to the user about diffs
if (files.length === 0) {
console.log(`
No changes found.
Lintrule runs on diffs by default and skips large files
or things in your .gitignore. You can run Lintrule against
more changes with --diff.
For example:
# Changes since since two commits ago
rules check --diff HEAD^^
# Changes between a branch
rules check --diff main..feature
`);
printNoChangesFound();
Deno.exit(0);
}

Expand All @@ -256,12 +242,30 @@ For example:
const results = await Promise.all(promises);
const failed = results.filter((r) => !r);
if (failed.length > 0) {
console.log(colors.bgRed(`\n ${failed.length} rules failed. `), "\n");
console.log(`\n${colors.bgRed(` ${failed.length} rules failed. `)}\n`);
Deno.exit(1);
}
console.log(colors.dim(`\nFinished. (${Date.now() - now}ms)\n`));
}

function printNoChangesFound() {
console.log(`
No changes found.
Lintrule runs on diffs by default and skips large files
or things in your .gitignore. You can run Lintrule against
more changes with --diff.
For example:
# Changes since since two commits ago
rules check --diff HEAD^^
# Changes between a branch
rules check --diff main..feature
`);
}

async function checkMessageAgainstDiff(props: {
host: string;
accessToken: string;
Expand All @@ -281,6 +285,11 @@ async function checkMessageAgainstDiff(props: {
throw new Error("Too many files to check at once. Please check less files");
}

if (files.length === 0) {
printNoChangesFound();
Deno.exit(0);
}

console.log(colors.dim(`\nFound ${files.length} changed files...\n`));

const now = Date.now();
Expand All @@ -298,7 +307,7 @@ async function checkMessageAgainstDiff(props: {
const results = await Promise.all(promises);
const failed = results.filter((r) => !r);
if (failed.length > 0) {
console.log(colors.bgRed(`\n ${failed.length} rules failed. `), "\n");
console.log(`\n${colors.bgRed(` ${failed.length} rules failed. `)}\n`);
Deno.exit(1);
}
console.log(colors.dim(`\nFinished. (${Date.now() - now}ms)\n`));
Expand Down Expand Up @@ -379,7 +388,7 @@ async function checkMessageAgainstFiles(props: {
const results = await Promise.all(promises);
const failed = results.filter((r) => !r);
if (failed.length > 0) {
console.log(colors.bgRed(`\n ${failed.length} rules failed. `), "\n");
console.log(`\n${colors.bgRed(` ${failed.length} rules failed. `)}\n`);
Deno.exit(1);
}
console.log(colors.dim(`\nFinished. (${Date.now() - now}ms)\n`));
Expand Down
8 changes: 2 additions & 6 deletions cli/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ export async function getDiffInGithubAction() {
await gitFetch(ref);

const p = new Deno.Command("git", {
args: ["diff", `${ref}..${head}`],
args: ["diff", `${ref}...${head}`],
stdout: "piped",
});

console.log(colors.dim(`\n$ git diff ${ref}..${head}`));
console.log(colors.dim(`\n$ git diff ${ref}...${head}`));
const { code, stdout, stderr } = await p.output(); // "p.output()" returns a promise that resolves with the raw output

if (code !== 0) {
Expand Down Expand Up @@ -194,10 +194,6 @@ export async function* getChangesAsHunks(diff?: string) {

for (const file of files) {
try {
if (file.type === "delete") {
continue;
}

let snippet = "";
for (const hunk of file.hunks) {
snippet += hunk.content + "\n";
Expand Down
2 changes: 1 addition & 1 deletion cli/walkTextFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export async function* walkTextFiles(
continue;
}

if (await isBeyondMaxLines(entry.path, 5000)) {
if (await isBeyondMaxLines(entry.path, 10000)) {
console.warn(
colors.red(`Skipping file because it is too long: ${entry.path}`)
);
Expand Down

0 comments on commit b031917

Please sign in to comment.