Skip to content

Commit

Permalink
ci(jscpd): improve output format #6373
Browse files Browse the repository at this point in the history
## Problem
The output format of clones found in `jscpd` can be difficult to parse.
As an example, see
https://github.com/aws/aws-toolkit-vscode/actions/runs/12778633640/job/35621822359?pr=6370.
Additionally, it mentions file names and line numbers, but doesn't
provide a way to get to those files.

## Solution
- reduce friction by linking directly to the duplicates
  • Loading branch information
Hweinstock authored Jan 15, 2025
1 parent 2b047ac commit efedd96
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
21 changes: 18 additions & 3 deletions .github/workflows/filterDuplicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* the program exits with an error and logs the filtered report to console.
*
* Usage:
* node filterDuplicates.js run [path_to_git_diff] [path_to_jscpd_report]
* node filterDuplicates.js run [path_to_git_diff] [path_to_jscpd_report] [commit_hash] [repo_name]
*
* Tests:
* node filterDuplicates.js test
Expand Down Expand Up @@ -84,25 +84,40 @@ function filterDuplicates(report, changes) {
return duplicates
}

function formatDuplicates(duplicates, commitHash, repoName) {
const baseUrl = `https://github.com/${repoName}`
return duplicates.map((dupe) => {
return {
first: formUrl(dupe.firstFile, commitHash),
second: formUrl(dupe.secondFile, commitHash),
numberOfLines: dupe.lines,
}
})
function formUrl(file, commitHash) {
return `${baseUrl}/blob/${commitHash}/${file.name}#L${file.start}-L${file.end}`
}
}

async function run() {
const rawDiffPath = process.argv[3]
const jscpdReportPath = process.argv[4]
const commitHash = process.argv[5]
const repoName = process.argv[6]
const changes = await parseDiff(rawDiffPath)
const jscpdReport = JSON.parse(await fs.readFile(jscpdReportPath, 'utf8'))
const filteredDuplicates = filterDuplicates(jscpdReport, changes)

console.log('%s files changes', changes.size)
console.log('%s duplicates found', filteredDuplicates.length)
if (filteredDuplicates.length > 0) {
console.log(filteredDuplicates)
console.log(formatDuplicates(filteredDuplicates, commitHash, repoName))
process.exit(1)
}
}

/**
* Mini-test Suite
*/
console.log(__dirname)
const testDiffFile = path.resolve(__dirname, 'test/test_diff.txt')
let testCounter = 0
function assertEqual(actual, expected) {
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ jobs:
path: ./jscpd-report.json

- name: Check for Duplicates
run: node "$GITHUB_WORKSPACE/.github/workflows/filterDuplicates.js" run diff_output.txt jscpd-report.json
env:
COMMIT_HASH: ${{ github.sha}}
REPO_NAME: ${{ github.repository }}
run: node "$GITHUB_WORKSPACE/.github/workflows/filterDuplicates.js" run diff_output.txt jscpd-report.json $COMMIT_HASH $REPO_NAME

macos:
needs: lint-commits
Expand Down

0 comments on commit efedd96

Please sign in to comment.