Skip to content
Merged
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
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
Loading