Skip to content

Commit 281beab

Browse files
committed
change to run on pull request event only
1 parent e447610 commit 281beab

File tree

2 files changed

+99
-78
lines changed

2 files changed

+99
-78
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# github actions: https://docs.github.com/en/actions/use-cases-and-examples/building-and-testing/building-and-testing-nodejs
2+
# setup-node: https://github.com/actions/setup-node
3+
4+
name: Copy-Paste Detection
5+
6+
on:
7+
pull_request:
8+
branches: [master, feature/*, staging]
9+
# Default = opened + synchronize + reopened.
10+
# We also want "edited" so that lint-commits runs when PR title is updated.
11+
# https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#pull_request
12+
types:
13+
- edited
14+
- opened
15+
- reopened
16+
- synchronize
17+
18+
# Cancel old jobs when a pull request is updated.
19+
concurrency:
20+
group: ${{ github.head_ref || github.run_id }}
21+
cancel-in-progress: true
22+
23+
jobs:
24+
jscpd:
25+
runs-on: ubuntu-latest
26+
strategy:
27+
matrix:
28+
node-version: [18.x]
29+
env:
30+
NODE_OPTIONS: '--max-old-space-size=8192'
31+
32+
steps:
33+
- uses: actions/checkout@v4
34+
with:
35+
fetch-depth: 0
36+
37+
- name: Use Node.js ${{ matrix.node-version }}
38+
uses: actions/setup-node@v4
39+
with:
40+
node-version: ${{ matrix.node-version }}
41+
42+
- name: Fetch fork upstream
43+
run: |
44+
git remote add forkUpstream https://github.com/${{ github.event.pull_request.head.repo.full_name }} # URL of the fork
45+
git fetch forkUpstream # Fetch fork
46+
47+
- name: Determine base and target branches for comparison.
48+
run: |
49+
echo "CURRENT_BRANCH=${{ github.head_ref }}" >> $GITHUB_ENV
50+
echo "TARGET_BRANCH=${{ github.event.pull_request.base.ref }}" >> $GITHUB_ENV
51+
- run: git diff --name-only origin/$TARGET_BRANCH forkUpstream/$CURRENT_BRANCH > diff_output.txt
52+
- run: |
53+
npm install -g jscpd
54+
55+
- run: jscpd --config "$GITHUB_WORKSPACE/.github/workflows/jscpd.json"
56+
57+
- if: always()
58+
uses: actions/upload-artifact@v4
59+
with:
60+
name: unfiltered-jscpd-report
61+
path: ./jscpd-report.json
62+
63+
- name: Filter jscpd report for changed files
64+
run: |
65+
if [ ! -f ./jscpd-report.json ]; then
66+
echo "jscpd-report.json not found"
67+
exit 1
68+
fi
69+
echo "Filtering jscpd report for changed files..."
70+
CHANGED_FILES=$(jq -R -s -c 'split("\n")[:-1]' diff_output.txt)
71+
echo "Changed files: $CHANGED_FILES"
72+
jq --argjson changed_files "$CHANGED_FILES" '
73+
.duplicates | map(select(
74+
(.firstFile?.name as $fname | $changed_files | any(. == $fname)) or
75+
(.secondFile?.name as $sname | $changed_files | any(. == $sname))
76+
))
77+
' ./jscpd-report.json > filtered-jscpd-report.json
78+
cat filtered-jscpd-report.json
79+
80+
- name: Check for duplicates
81+
run: |
82+
if [ $(wc -l < ./filtered-jscpd-report.json) -gt 1 ]; then
83+
echo "filtered_report_exists=true" >> $GITHUB_ENV
84+
else
85+
echo "filtered_report_exists=false" >> $GITHUB_ENV
86+
fi
87+
- name: upload filtered report (if applicable)
88+
if: env.filtered_report_exists == 'true'
89+
uses: actions/upload-artifact@v4
90+
with:
91+
name: filtered-jscpd-report
92+
path: ./filtered-jscpd-report.json
93+
94+
- name: Fail and log found duplicates.
95+
if: env.filtered_report_exists == 'true'
96+
run: |
97+
cat ./filtered-jscpd-report.json
98+
echo "Duplications found, failing the check."
99+
exit 1

.github/workflows/node.js.yml

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -55,84 +55,6 @@ jobs:
5555
- run: npm run testCompile
5656
- run: npm run lint
5757

58-
jscpd:
59-
needs: lint-commits
60-
runs-on: ubuntu-latest
61-
strategy:
62-
matrix:
63-
node-version: [18.x]
64-
env:
65-
NODE_OPTIONS: '--max-old-space-size=8192'
66-
67-
steps:
68-
- uses: actions/checkout@v4
69-
with:
70-
fetch-depth: 0
71-
72-
- name: Use Node.js ${{ matrix.node-version }}
73-
uses: actions/setup-node@v4
74-
with:
75-
node-version: ${{ matrix.node-version }}
76-
77-
- name: Fetch fork upstream
78-
run: |
79-
git remote add forkUpstream https://github.com/${{ github.event.pull_request.head.repo.full_name }} # URL of the fork
80-
git fetch forkUpstream # Fetch fork
81-
82-
- name: Determine base and target branches for comparison.
83-
run: |
84-
echo "CURRENT_BRANCH=${{ github.head_ref }}" >> $GITHUB_ENV
85-
echo "TARGET_BRANCH=${{ github.event.pull_request.base.ref }}" >> $GITHUB_ENV
86-
- run: git diff --name-only origin/$TARGET_BRANCH forkUpstream/$CURRENT_BRANCH > diff_output.txt
87-
- run: |
88-
npm install -g jscpd
89-
90-
- run: jscpd --config "$GITHUB_WORKSPACE/.github/workflows/jscpd.json"
91-
92-
- if: always()
93-
uses: actions/upload-artifact@v4
94-
with:
95-
name: unfiltered-jscpd-report
96-
path: ./jscpd-report.json
97-
98-
- name: Filter jscpd report for changed files
99-
run: |
100-
if [ ! -f ./jscpd-report.json ]; then
101-
echo "jscpd-report.json not found"
102-
exit 1
103-
fi
104-
echo "Filtering jscpd report for changed files..."
105-
CHANGED_FILES=$(jq -R -s -c 'split("\n")[:-1]' diff_output.txt)
106-
echo "Changed files: $CHANGED_FILES"
107-
jq --argjson changed_files "$CHANGED_FILES" '
108-
.duplicates | map(select(
109-
(.firstFile?.name as $fname | $changed_files | any(. == $fname)) or
110-
(.secondFile?.name as $sname | $changed_files | any(. == $sname))
111-
))
112-
' ./jscpd-report.json > filtered-jscpd-report.json
113-
cat filtered-jscpd-report.json
114-
115-
- name: Check for duplicates
116-
run: |
117-
if [ $(wc -l < ./filtered-jscpd-report.json) -gt 1 ]; then
118-
echo "filtered_report_exists=true" >> $GITHUB_ENV
119-
else
120-
echo "filtered_report_exists=false" >> $GITHUB_ENV
121-
fi
122-
- name: upload filtered report (if applicable)
123-
if: env.filtered_report_exists == 'true'
124-
uses: actions/upload-artifact@v4
125-
with:
126-
name: filtered-jscpd-report
127-
path: ./filtered-jscpd-report.json
128-
129-
- name: Fail and log found duplicates.
130-
if: env.filtered_report_exists == 'true'
131-
run: |
132-
cat ./filtered-jscpd-report.json
133-
echo "Duplications found, failing the check."
134-
exit 1
135-
13658
macos:
13759
needs: lint-commits
13860
name: test macOS

0 commit comments

Comments
 (0)