Skip to content

Commit e1e1a83

Browse files
CopilotfunctionstackxCopilot
authored
Add line diff tracking to PR line counter workflow (#165)
* Add line number diff calculation to PR line counter workflow Co-authored-by: functionstackx <47992694+functionstackx@users.noreply.github.com> * Use mktemp for temporary file to avoid conflicts in concurrent runs Co-authored-by: functionstackx <47992694+functionstackx@users.noreply.github.com> * Fix line diff calculation for new files and add explicit new file handling Co-authored-by: functionstackx <47992694+functionstackx@users.noreply.github.com> * Make temporary file cleanup consistent with -f flag Co-authored-by: functionstackx <47992694+functionstackx@users.noreply.github.com> * Update .github/workflows/pr-line-counter.yml Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Add trap to ensure temporary file cleanup on exit Co-authored-by: functionstackx <47992694+functionstackx@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: functionstackx <47992694+functionstackx@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 2cf106d commit e1e1a83

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

.github/workflows/pr-line-counter.yml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ jobs:
1616
steps:
1717
- name: Checkout code
1818
uses: actions/checkout@v4
19+
with:
20+
fetch-depth: 0
1921

2022
- name: Count lines in generate_sweep_configs.py
2123
id: line-count
2224
run: |
25+
# Count lines in PR version
2326
if [ -f "$TARGET_FILE" ]; then
2427
LINE_COUNT=$(wc -l < "$TARGET_FILE")
2528
# If the last character is not a newline, increment the count
@@ -31,6 +34,36 @@ jobs:
3134
else
3235
echo "file_exists=false" >> $GITHUB_OUTPUT
3336
echo "line_count=0" >> $GITHUB_OUTPUT
37+
LINE_COUNT=0
38+
fi
39+
40+
# Count lines in base branch version
41+
BASE_REF="${{ github.event.pull_request.base.ref }}"
42+
# Fetch the base branch to ensure we have it
43+
if ! git fetch origin "$BASE_REF" 2>/dev/null; then
44+
echo "ERROR: Failed to fetch base branch '$BASE_REF' from origin." >&2
45+
exit 1
46+
fi
47+
BASE_FILE_TMP=$(mktemp)
48+
# Ensure cleanup on exit
49+
trap "rm -f '$BASE_FILE_TMP'" EXIT
50+
if git show "origin/$BASE_REF:$TARGET_FILE" > "$BASE_FILE_TMP" 2>/dev/null; then
51+
BASE_LINE_COUNT=$(wc -l < "$BASE_FILE_TMP")
52+
# If the last character is not a newline, increment the count
53+
if [ -n "$(tail -c 1 "$BASE_FILE_TMP" | tr -d '\n')" ]; then
54+
BASE_LINE_COUNT=$((BASE_LINE_COUNT + 1))
55+
fi
56+
echo "base_line_count=$BASE_LINE_COUNT" >> $GITHUB_OUTPUT
57+
echo "base_file_exists=true" >> $GITHUB_OUTPUT
58+
59+
# Calculate line difference
60+
LINE_DIFF=$((LINE_COUNT - BASE_LINE_COUNT))
61+
echo "line_diff=$LINE_DIFF" >> $GITHUB_OUTPUT
62+
else
63+
echo "base_file_exists=false" >> $GITHUB_OUTPUT
64+
echo "base_line_count=0" >> $GITHUB_OUTPUT
65+
# If base file doesn't exist, the diff is the full line count (new file)
66+
echo "line_diff=$LINE_COUNT" >> $GITHUB_OUTPUT
3467
fi
3568
3669
- name: Generate summary
@@ -39,11 +72,34 @@ jobs:
3972
echo "" >> $GITHUB_STEP_SUMMARY
4073
4174
FILE_EXISTS="${{ steps.line-count.outputs.file_exists }}"
75+
BASE_FILE_EXISTS="${{ steps.line-count.outputs.base_file_exists }}"
76+
4277
if [ "$FILE_EXISTS" == "true" ]; then
4378
LINE_COUNT="${{ steps.line-count.outputs.line_count }}"
79+
BASE_LINE_COUNT="${{ steps.line-count.outputs.base_line_count }}"
80+
LINE_DIFF="${{ steps.line-count.outputs.line_diff }}"
81+
4482
echo "**File:** \`$TARGET_FILE\`" >> $GITHUB_STEP_SUMMARY
4583
echo "" >> $GITHUB_STEP_SUMMARY
4684
echo "**Total Lines:** $LINE_COUNT" >> $GITHUB_STEP_SUMMARY
85+
86+
if [ "$BASE_FILE_EXISTS" == "true" ]; then
87+
echo "" >> $GITHUB_STEP_SUMMARY
88+
echo "**Base Lines:** $BASE_LINE_COUNT" >> $GITHUB_STEP_SUMMARY
89+
echo "" >> $GITHUB_STEP_SUMMARY
90+
if [ "$LINE_DIFF" -gt 0 ]; then
91+
echo "**Change:** +$LINE_DIFF lines 📈" >> $GITHUB_STEP_SUMMARY
92+
elif [ "$LINE_DIFF" -lt 0 ]; then
93+
echo "**Change:** $LINE_DIFF lines 📉" >> $GITHUB_STEP_SUMMARY
94+
else
95+
echo "**Change:** No change ➡️" >> $GITHUB_STEP_SUMMARY
96+
fi
97+
else
98+
echo "" >> $GITHUB_STEP_SUMMARY
99+
echo "**Base Lines:** 0 (new file)" >> $GITHUB_STEP_SUMMARY
100+
echo "" >> $GITHUB_STEP_SUMMARY
101+
echo "**Change:** +$LINE_DIFF lines 📈" >> $GITHUB_STEP_SUMMARY
102+
fi
47103
else
48104
echo "⚠️ **File not found:** \`$TARGET_FILE\`" >> $GITHUB_STEP_SUMMARY
49105
fi
@@ -53,12 +109,31 @@ jobs:
53109
with:
54110
script: |
55111
const fileExists = '${{ steps.line-count.outputs.file_exists }}';
112+
const baseFileExists = '${{ steps.line-count.outputs.base_file_exists }}';
56113
const lineCount = '${{ steps.line-count.outputs.line_count }}';
114+
const baseLineCount = '${{ steps.line-count.outputs.base_line_count }}';
115+
const lineDiff = '${{ steps.line-count.outputs.line_diff }}';
57116
const targetFile = process.env.TARGET_FILE;
58117
59118
let commentBody;
60119
if (fileExists === 'true') {
61120
commentBody = `## 📊 Line Count Report\n\n**File:** \`${targetFile}\`\n\n**Total Lines:** ${lineCount}`;
121+
122+
if (baseFileExists === 'true') {
123+
commentBody += `\n\n**Base Lines:** ${baseLineCount}`;
124+
125+
const diff = parseInt(lineDiff);
126+
if (diff > 0) {
127+
commentBody += `\n\n**Change:** +${diff} lines 📈`;
128+
} else if (diff < 0) {
129+
commentBody += `\n\n**Change:** ${diff} lines 📉`;
130+
} else {
131+
commentBody += `\n\n**Change:** No change ➡️`;
132+
}
133+
} else {
134+
commentBody += `\n\n**Base Lines:** 0 (new file)`;
135+
commentBody += `\n\n**Change:** +${lineDiff} lines 📈`;
136+
}
62137
} else {
63138
commentBody = `## 📊 Line Count Report\n\n⚠️ **File not found:** \`${targetFile}\``;
64139
}

0 commit comments

Comments
 (0)