Skip to content

Commit

Permalink
implement dynamic branch comparison technique
Browse files Browse the repository at this point in the history
  • Loading branch information
Hweinstock committed Oct 25, 2024
1 parent e8d62f1 commit ca3c3ec
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/jscpd.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"pattern": "packages/**/*.ts",
"ignore": ["**node_modules**", "**dist**"],
"gitignore": true,
"threshold": 3.0,
"minLines": 5,
"output": "./",
"reporters": ["json"]
}
78 changes: 78 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,84 @@ jobs:
- run: npm run testCompile
- run: npm run lint

jscpd:
needs: lint-commits
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x]
env:
NODE_OPTIONS: '--max-old-space-size=8192'

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: Fetch fork upstream
run: |
git remote add forkUpstream https://github.com/${{ github.event.pull_request.head.repo.full_name }} # URL of the fork
git fetch forkUpstream # Fetch fork
- name: Determine base and target branches for comparison.
run: |
echo "CURRENT_BRANCH=${{ github.head_ref }}" >> $GITHUB_ENV
echo "TARGET_BRANCH=${{ github.event.pull_request.base.ref }}" >> $GITHUB_ENV
- run: git diff --name-only origin/$TARGET_BRANCH forkUpstream/$CURRENT_BRANCH > diff_output.txt
- run: |
npm install -g jscpd diff-so-fancy
- run: jscpd --config "$GITHUB_WORKSPACE/.github/workflows/jscpd.json"

- if: always()
uses: actions/upload-artifact@v4
with:
name: unfiltered-jscpd-report
path: ./jscpd-report.json

- name: Filter jscpd report for changed files
run: |
if [ ! -f ./jscpd-report.json ]; then
echo "jscpd-report.json not found"
exit 1
fi
echo "Filtering jscpd report for changed files..."
CHANGED_FILES=$(jq -R -s -c 'split("\n")[:-1]' diff_output.txt)
echo "Changed files: $CHANGED_FILES"
jq --argjson changed_files "$CHANGED_FILES" '
.duplicates | map(select(
(.firstFile?.name as $fname | $changed_files | any(. == $fname)) or
(.secondFile?.name as $sname | $changed_files | any(. == $sname))
))
' ./jscpd-report.json > filtered-jscpd-report.json
cat filtered-jscpd-report.json
- name: Check for duplicates
run: |
if [ $(wc -l < ./filtered-jscpd-report.json) -gt 1 ]; then
echo "filtered_report_exists=true" >> $GITHUB_ENV
else
echo "filtered_report_exists=false" >> $GITHUB_ENV
fi
- name: upload filtered report (if applicable)
if: env.filtered_report_exists == 'true'
uses: actions/upload-artifact@v4
with:
name: filtered-jscpd-report
path: ./filtered-jscpd-report.json

- name: Fail and log found duplicates.
if: env.filtered_report_exists == 'true'
run: |
cat ./filtered-jscpd-report.json
echo "Duplications found, failing the check."
exit 1
macos:
needs: lint-commits
name: test macOS
Expand Down

0 comments on commit ca3c3ec

Please sign in to comment.