Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
zeriyoshi committed Aug 22, 2024
1 parent 742294e commit bf8db65
Showing 1 changed file with 82 additions and 48 deletions.
130 changes: 82 additions & 48 deletions .github/workflows/coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,68 +75,102 @@ jobs:
uses: actions/download-artifact@v4
with:
name: coverage-unified
- name: Get changed files
uses: tj-actions/changed-files@v45
id: changed-files
- name: Install lcov-parse
run: npm install "lcov-parse"
- name: Report comment for PR
uses: actions/github-script@v6
with:
github-token: ${{secrets.GITHUB_TOKEN}}
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const lcovParse = require('lcov-parse');
const lcovFile = fs.readFileSync('./lcov.info', 'utf8');
function parseLcovInfo(filename) {
const content = fs.readFileSync(filename, 'utf8');
const lines = content.split('\n');
const uncoveredLines = {};
let currentFile = '';
lcovParse(lcovFile, async (err, data) => {
if (err) {
console.error('Error parsing lcov file:', err);
return;
for (const line of lines) {
if (line.startsWith('SF:')) {
currentFile = line.substring(3).trim();
} else if (line.startsWith('DA:')) {
const [lineNo, hits] = line.substring(3).trim().split(',').map(Number);
if (hits === 0) {
if (!uncoveredLines[currentFile]) {
uncoveredLines[currentFile] = [];
}
uncoveredLines[currentFile].push(lineNo);
}
}
}
const { data: diff } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
mediaType: {
format: 'diff'
return uncoveredLines;
}
function groupConsecutiveLines(lines) {
if (lines.length === 0) return [];
lines.sort((a, b) => a - b);
const groups = [];
let currentGroup = [lines[0]];
for (let i = 1; i < lines.length; i++) {
if (lines[i] === lines[i-1] + 1) {
currentGroup.push(lines[i]);
} else {
groups.push(currentGroup);
currentGroup = [lines[i]];
}
}
groups.push(currentGroup);
return groups;
}
async function createReviewForUncoveredLines(uncoveredLines) {
const { owner, repo } = context.repo;
const pull_number = context.issue.number;
const { data: files } = await github.rest.pulls.listFiles({
owner,
repo,
pull_number,
});
const lines = diff.split('\n');
let currentFile = '';
let lineNumber = 0;
const comments = [];
for (const line of lines) {
if (line.startsWith('+++')) {
currentFile = line.substring(6);
lineNumber = 0;
} else if (line.startsWith('@@')) {
const match = line.match(/@@ -\d+,\d+ \+(\d+),/);
if (match) {
lineNumber = parseInt(match[1]) - 1;
}
} else if (line.startsWith('+') && !line.startsWith('+++')) {
lineNumber++;
const fileData = data.find(f => f.file === currentFile);
if (fileData) {
const lineData = fileData.lines.details.find(l => l.line === lineNumber);
if (lineData && lineData.hit === 0) {
await github.rest.pulls.createReviewComment({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
body: 'This line is not covered by tests.',
commit_id: context.payload.pull_request.head.sha,
path: currentFile,
line: lineNumber
});
for (const file of files) {
const filename = file.filename;
if (uncoveredLines[filename]) {
const groups = groupConsecutiveLines(uncoveredLines[filename]);
for (const group of groups) {
const startLine = group[0];
const endLine = group[group.length - 1];
const commentBody = 'These lines are not covered by tests.';
const comment = {
path: filename,
body: commentBody,
line: startLine,
side: 'RIGHT',
};
if (startLine !== endLine) {
comment.start_line = startLine;
comment.line = endLine;
}
comments.push(comment);
}
} else if (!line.startsWith('-')) {
lineNumber++;
}
}
});
if (comments.length > 0) {
await github.rest.pulls.createReview({
owner,
repo,
pull_number,
event: 'COMMENT',
body: 'Review for uncovered lines',
comments,
});
}
}
const uncoveredLines = parseLcovInfo('coverage.lcov');
await createReviewForUncoveredLines(uncoveredLines);

0 comments on commit bf8db65

Please sign in to comment.