@@ -26,61 +26,47 @@ jobs:
2626 with :
2727 node-version : ${{ matrix.node-version }}
2828
29+ - name : Determine if local
30+ run : echo "IS_LOCAL=false" >> $GITHUB_ENV
31+
2932 - name : Fetch fork upstream
33+ if : ${{ env.IS_LOCAL == 'false' }}
3034 run : |
3135 git remote add forkUpstream https://github.com/${{ github.event.pull_request.head.repo.full_name }} # URL of the fork
3236 git fetch forkUpstream # Fetch fork
3337
3438 - name : Determine base and target branches for comparison.
3539 run : |
36- echo "CURRENT_BRANCH=${{ github.head_ref }}" >> $GITHUB_ENV
37- echo "TARGET_BRANCH=${{ github.event.pull_request.base.ref }}" >> $GITHUB_ENV
38- - run : git diff --name-only origin/$TARGET_BRANCH forkUpstream/$CURRENT_BRANCH > diff_output.txt
39- - run : |
40- npm install -g jscpd
40+ if [[ $IS_LOCAL == 'false' ]]; then
41+ echo "CURRENT_BRANCH=${{ github.head_ref }}" >> $GITHUB_ENV
42+ echo "TARGET_BRANCH=${{ github.event.pull_request.base.ref }}" >> $GITHUB_ENV
43+ else
44+ echo "CURRENT_BRANCH=${{ github.ref_name }}" >> $GITHUB_ENV
45+ echo "TARGET_BRANCH=master" >> $GITHUB_ENV
46+ fi
47+
48+ - name : Print base and target branches for comparison.
49+ run : |
50+ echo "CURRENT_BRANCH=$CURRENT_BRANCH"
51+ echo "TARGET_BRANCH=$TARGET_BRANCH"
52+
53+ - name : Compare target and current branches.
54+ run : |
55+ if [[ $IS_LOCAL == 'false' ]]; then
56+ git diff origin/$TARGET_BRANCH forkUpstream/$CURRENT_BRANCH > diff_output.txt
57+ else
58+ git diff origin/$TARGET_BRANCH $CURRENT_BRANCH > diff_output.txt
59+ fi
60+
61+ - run : npm install -g jscpd
4162
4263 - run : jscpd --config "$GITHUB_WORKSPACE/.github/workflows/jscpd.json"
4364
44- - if : always()
65+ - if : ${{ env.IS_LOCAL == 'false' }}
4566 uses : actions/upload-artifact@v4
4667 with :
4768 name : unfiltered-jscpd-report
4869 path : ./jscpd-report.json
4970
50- - name : Filter jscpd report for changed files
51- run : |
52- if [ ! -f ./jscpd-report.json ]; then
53- echo "jscpd-report.json not found"
54- exit 1
55- fi
56- echo "Filtering jscpd report for changed files..."
57- CHANGED_FILES=$(jq -R -s -c 'split("\n")[:-1]' diff_output.txt)
58- echo "Changed files: $CHANGED_FILES"
59- jq --argjson changed_files "$CHANGED_FILES" '
60- .duplicates | map(select(
61- (.firstFile?.name as $fname | $changed_files | any(. == $fname)) or
62- (.secondFile?.name as $sname | $changed_files | any(. == $sname))
63- ))
64- ' ./jscpd-report.json > filtered-jscpd-report.json
65- cat filtered-jscpd-report.json
66-
6771 - name : Check for duplicates
68- run : |
69- if [ $(wc -l < ./filtered-jscpd-report.json) -gt 1 ]; then
70- echo "filtered_report_exists=true" >> $GITHUB_ENV
71- else
72- echo "filtered_report_exists=false" >> $GITHUB_ENV
73- fi
74- - name : upload filtered report (if applicable)
75- if : env.filtered_report_exists == 'true'
76- uses : actions/upload-artifact@v4
77- with :
78- name : filtered-jscpd-report
79- path : ./filtered-jscpd-report.json
80-
81- - name : Fail and log found duplicates.
82- if : env.filtered_report_exists == 'true'
83- run : |
84- cat ./filtered-jscpd-report.json
85- echo "Duplications found, failing the check."
86- exit 1
72+ run : node "$GITHUB_WORKSPACE/.github/workflows/filterDuplicates.js" diff_output.txt jscpd-report.json
0 commit comments