Skip to content

Commit

Permalink
chore: fix empty string checker to correctly count violations
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 committed Oct 8, 2024
1 parent 7a35f74 commit a9b93e5
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions .github/empty-string-checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async function main() {
const { data: pullRequest } = await octokit.pulls.get({
owner,
repo,
pull_number: parseInt(pullNumber),
pull_number: parseInt(pullNumber, 10),
});

const baseSha = pullRequest.base.sha;
Expand All @@ -37,10 +37,11 @@ async function main() {
violations.forEach(({ file, line, content }) => {
core.warning(`Empty string found: ${content}`, {
file,
startLine: parseInt(line.toString()),
startLine: line,
});
});
// core.setFailed(`${emptyStrings.length} empty string${emptyStrings.length > 1 ? "s" : ""} detected in the code.`);

// core.setFailed(`${violations.length} empty string${violations.length > 1 ? "s" : ""} detected in the code.`);

await octokit.rest.checks.create({
owner,
Expand All @@ -51,7 +52,7 @@ async function main() {
conclusion: violations.length > 0 ? "failure" : "success",
output: {
title: "Empty String Check Results",
summary: `Found ${violations.length} violations`,
summary: `Found ${violations.length} violation${violations.length !== 1 ? "s" : ""}`,
annotations: violations.map((v) => ({
path: v.file,
start_line: v.line,
Expand All @@ -75,22 +76,37 @@ function parseDiffForEmptyStrings(diff: string) {
const diffLines = diff.split("\n");

let currentFile = "";
let lineNumber = 0;
let headLine = 0;
let inHunk = false;

diffLines.forEach((line) => {
if (line.startsWith("+++ b/")) {
currentFile = line.replace("+++ b/", "");
lineNumber = 0;
} else if (line.startsWith("+") && !line.startsWith("+++")) {
if (line.includes('""')) {
violations.push({
file: currentFile,
line: lineNumber++,
content: line.substring(1),
});
const hunkHeaderMatch = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/.exec(line);
if (hunkHeaderMatch) {
headLine = parseInt(hunkHeaderMatch[1], 10);
inHunk = true;
return;
}

if (inHunk) {
if (line.startsWith("+++ b/")) {
currentFile = line.replace("+++ b/", "");
return;
}

if (line.startsWith("+") && !line.startsWith("+++")) {
if (line.includes('""')) {
violations.push({
file: currentFile,
line: headLine,
content: line.substring(1),
});
}
headLine++;
} else if (line.startsWith("-")) {
// Removed line; do not increment headLine
} else {
headLine++;
}
} else if (!line.startsWith("-")) {
lineNumber++;
}
});

Expand Down

0 comments on commit a9b93e5

Please sign in to comment.