Skip to content

Commit

Permalink
fix: annotation level (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoehnelt authored Oct 6, 2020
1 parent b55c155 commit b9dd8bb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
37 changes: 37 additions & 0 deletions src/annotate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,40 @@ test("should get correct level from annotations", () => {
] as any)
).toBe(Level.FAILURE);
});

test("should annotate with correct level", () => {
const files = parse(fs.readFileSync("./fixtures/pull.failing.diff", "utf8"));
const annotations = annotate(
{
...DEFAULT_CONFIGURATION,
rules: {
whitelist: {
regex: DEFAULT_CONFIGURATION.rules.whitelist.regex,
level: Level.FAILURE,
},
master: {
regex: DEFAULT_CONFIGURATION.rules.master.regex,
level: Level.OFF,
},
},
},
files
);
expect(annotations).toMatchInlineSnapshot(`
Array [
Object {
"annotation_level": "failure",
"end_column": 8,
"end_line": 2,
"message": "
Please consider an alternative to \`whitelist\`.
",
"path": "README.md",
"raw_details": "/white[_-]*list/gi",
"start_column": 0,
"start_line": 2,
"title": "Match Found",
},
]
`);
});
17 changes: 14 additions & 3 deletions src/annotate.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Context, Logger } from "probot";
/**
* Copyright 2020 Google LLC
*
Expand Down Expand Up @@ -53,6 +54,10 @@ export const annotate = (
if (change.isInsert) {
for (const k in config.rules) {
for (const pattern of config.rules[k].regex) {
if (config.rules[k].level == "off") {
continue;
}

// @ts-ignore matchAll may not be available
for (const match of change.content.matchAll(pattern)) {
const context: MessageContext = {
Expand All @@ -62,8 +67,12 @@ export const annotate = (
content: change.content,
alternatives: config.rules[k].alternatives || [],
};
annotations.push({
annotation_level: "warning",

const annotation = {
annotation_level: config.rules[k].level as
| "notice"
| "warning"
| "failure",
end_column: match.index + match[0].length - 1,
end_line: (change.lineNumber ||
change.newLineNumber) as number,
Expand All @@ -76,7 +85,9 @@ export const annotate = (
start_line: (change.lineNumber ||
change.newLineNumber) as number,
title: "Match Found",
});
};

annotations.push(annotation);
}
}
}
Expand Down

0 comments on commit b9dd8bb

Please sign in to comment.