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

Code review warning annotation #57

Closed
Closed
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
67 changes: 67 additions & 0 deletions .github/workflows/empty-string-warning.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Warn for Empty Strings

on:
pull_request:
paths:
- "**/*.ts"
- "**/*.tsx"

permissions:
issues: write
pull-requests: write

jobs:
check-empty-strings:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Find empty strings in TypeScript files
id: find_empty_strings
run: |
# Find empty strings and mark them explicitly
output=$(grep -Pnr --include=\*.ts "(?<!\\w)(''|\"\")" --exclude-dir={node_modules,dist,out,tests} . || true)

if [ -z "$output" ]; then
echo "results=No empty strings found." >> $GITHUB_OUTPUT
exit 0
else
output=$(echo "$output" | sed 's/""/"[EMPTY STRING]"/g' | sed "s/''/'[EMPTY STRING]'/g")
echo "results=${output//$'\n'/%0A}" >> $GITHUB_OUTPUT
fi

- name: findings
if: steps.find_empty_strings.outputs.results != 'No empty strings found.'
run: |
if [ "${{ steps.find_empty_strings.outputs.results }}" == "No empty strings found." ]; then
echo "No empty strings found. No action required."
else
echo "::warning::Empty strings found in the following files:"
echo "${{ steps.find_empty_strings.outputs.results }}"
fi

- name: Post review comments for findings
if: steps.find_empty_strings.outputs.results != 'No empty strings found.'
uses: actions/github-script@v7
with:
script: |
const findings = `${{ steps.find_empty_strings.outputs.results }}`.split('%0A');
for (const finding of findings) {
const [path, line] = finding.split(':');
const body = "Empty string detected!";
const prNumber = context.payload.pull_request.number;
const owner = context.repo.owner;
const repo = context.repo.repo;

await github.rest.pulls.createReviewComment({
owner,
repo,
pull_number: prNumber,
body,
commit_id: context.payload.pull_request.head.sha,
path,
line: parseInt(line, 10),
});
}
3 changes: 3 additions & 0 deletions static/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ export async function mainModule() {
console.log(`Hello from mainModule`);
}

const testEmptyString = ""
const testEmptyStrings = ''

mainModule()
.then(() => {
console.log("mainModule loaded");
Expand Down
51 changes: 51 additions & 0 deletions tests/workflows/empty-strings/empty-strings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
export interface CheckEmptyStringsOptions {
readDir: (path: string) => Promise<string[]>;
readFile: (path: string, encoding: string) => Promise<string>;
ignorePatterns?: string[];
}

export async function checkForEmptyStringsInFiles(
dir: string,
options: CheckEmptyStringsOptions
): Promise<string[]> {
const { readDir, readFile, ignorePatterns = [] } = options;
const filesWithEmptyStrings: string[] = [];
const ignoreList: string[] = ["^\\.\\/\\.git", "^\\.\\/\\..*", "^\\.\\/node_modules", "^\\.\\/dist", "^\\.\\/out", ".*\\.test\\.ts$", ...ignorePatterns];

// Add .gitignore patterns
try {
const gitignoreContent = await readFile(".gitignore", "utf8");
gitignoreContent.split("\n").forEach((line) => {
if (line.trim() && !line.startsWith("#")) { // Ignore comments and empty lines
ignoreList.push(`^\\.\\/${line.replace(/\./g, "\\.").replace(/\//g, "\\/")}`);
}
});
} catch (error) {
console.error("Error reading .gitignore file:", error);
}

try {
const files = await readDir(dir);
for (const fileName of files) {
let shouldIgnore = false;
for (const pattern of ignoreList) {
if (new RegExp(pattern).test(fileName)) {
shouldIgnore = true;
break;
}
}
if (shouldIgnore || !fileName.endsWith(".ts")) continue;

const fileContent = await readFile(`${dir}/${fileName}`, "utf8");
// Refined to match only standalone empty strings
if (fileContent.match(/(?<!\w)""|''/)) {
filesWithEmptyStrings.push(fileName);
}
}
} catch (error) {
console.error("Error reading directory or file contents:", error);
}

return filesWithEmptyStrings;
}

Loading