Skip to content

Commit

Permalink
Github action: Create jscpd.yml (#11738)
Browse files Browse the repository at this point in the history
* Create jscpd.yml

* Update jscpd.yml

* Update jscpd.yml

* Update jscpd.yml

* Update jscpd.yml

* Update jscpd.yml

* Update jscpd.yml
  • Loading branch information
patmmccann authored Jun 8, 2024
1 parent b3fbd02 commit 5985642
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions .github/workflows/jscpd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Check for Duplicated Code

on:
pull_request:
branches:
- master

jobs:
check-duplication:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0 # Fetch all history for all branches

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '20'

- name: Install dependencies
run: |
npm install -g jscpd diff-so-fancy
- name: Get the diff
run: git diff origin/master...HEAD --name-only > changed_files.txt

- name: Filter JavaScript files
run: |
grep -E '\.js$' changed_files.txt > js_files.txt || true
- name: Run jscpd on changed files
run: |
if [ -s js_files.txt ]; then
jscpd --files $(cat js_files.txt | tr '\n' ',') --threshold 1 --min-tokens 50 --reporters json --output jscpd-report.json
else
echo '{}' > jscpd-report.json
fi
- name: Upload jscpd report
if: always()
uses: actions/upload-artifact@v3
with:
name: jscpd-report
path: jscpd-report.json

- name: Parse jscpd report and post comment
id: post-comment
run: |
DUPLICATIONS=$(jq '.duplicates | length' jscpd-report.json)
if [ "$DUPLICATIONS" -gt 0 ]; then
COMMENT="Found $DUPLICATIONS duplications in the codebase:\n\n"
COMMENT+=$(jq -r '.duplicates[] | "- `\(.firstFile):\(.lines[0])-\(.lines[1])` duplicated in `\(.secondFile):\(.lines[0])-\(.lines[1])`"' jscpd-report.json)
echo "comment<<EOF" >> $GITHUB_ENV
echo "$COMMENT" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
exit 1
fi
- name: Post GitHub comment
if: failure()
uses: actions/github-script@v6
with:
script: |
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: process.env.comment
})

0 comments on commit 5985642

Please sign in to comment.