-
Notifications
You must be signed in to change notification settings - Fork 1
95 lines (82 loc) · 3.13 KB
/
empty-string-warning.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
name: Warn for Empty Strings
on:
pull_request:
paths:
- "**/*.ts"
- "**/*.tsx"
permissions:
issues: write
pull-requests: write
contents: write
jobs:
check-empty-strings:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 2
- name: List files in the pull
id: list_files
run: |
files=$(git diff --name-only HEAD~ HEAD | grep -E "\.(ts|tsx)$")
files="${files//'%'/'%25'}"
files="${files//$'\n'/'%0A'}"
files="${files//$'\r'/'%0D'}"
echo "::set-output name=files::$files"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Find empty strings in TypeScript files
if: steps.list_files.outputs.files != ''
id: find_empty_strings
run: |
files="${{ steps.list_files.outputs.files }}"
output=$(echo "$files" | xargs grep -Hn --include=\*.{ts,tsx} -E "=''\|\"\"|>{\s*\"\"\s*}<|\{''\}" --exclude-dir={node_modules,dist,out} || true)
if [ -z "$output" ]; then
echo "::set-output name=results::No empty strings found."
exit 0
else
encoded_output=$(echo "$output" | base64)
echo "::set-output name=results::$encoded_output"
fi
- name: Findings
if: steps.find_empty_strings.outputs.results != 'No empty strings found.'
run: |
findings=$(echo "${{ steps.find_empty_strings.outputs.results }}" | base64 --decode)
if [[ -n "$findings" ]]; then
echo "::warning::Empty strings found in the following files:"
echo "$findings"
else
echo "No empty strings found. No action required."
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 findingsRaw = Buffer.from(`${{ steps.find_empty_strings.outputs.results }}`, 'base64').toString('utf-8');
const findings = findingsRaw.split('\n').filter(line => line.trim().length > 0);
const body = "Empty string detected!";
const owner = context.repo.owner;
const repo = context.repo.repo;
const commit_sha = context.payload.pull_request.head.sha;
for (const finding of findings) {
const parts = finding.split(':');
const path = parts[0];
const line = parseInt(parts[1], 10);
try {
await github.rest.repos.createCommitComment({
owner,
repo,
commit_sha,
path,
body,
position: line,
});
console.log(`Comment posted to ${path} at position ${line}`);
} catch (error) {
console.error(`Error posting comment to ${path}:${line}`, error);
}
}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}