-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
695 changed files
with
41,685 additions
and
47,014 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
name: Check for Duplicated Code | ||
|
||
on: | ||
pull_request_target: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
check-duplication: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 # Fetch all history for all branches | ||
ref: ${{ github.event.pull_request.head.sha }} | ||
|
||
- name: Set up Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: '20' | ||
|
||
- name: Install dependencies | ||
run: | | ||
npm install -g jscpd diff-so-fancy | ||
- name: Create jscpd config file | ||
run: | | ||
echo '{ | ||
"threshold": 20, | ||
"minTokens": 50, | ||
"reporters": [ | ||
"json" | ||
], | ||
"output": "./", | ||
"pattern": "**/*.js", | ||
"ignore": "**/*spec.js" | ||
}' > .jscpd.json | ||
- name: Run jscpd on entire codebase | ||
run: jscpd | ||
|
||
- name: Fetch base and target branches | ||
run: | | ||
git fetch origin +refs/heads/${{ github.event.pull_request.base.ref }}:refs/remotes/origin/${{ github.event.pull_request.base.ref }} | ||
git fetch origin +refs/pull/${{ github.event.pull_request.number }}/merge:refs/remotes/pull/${{ github.event.pull_request.number }}/merge | ||
- name: Get the diff | ||
run: git diff --name-only origin/${{ github.event.pull_request.base.ref }}...refs/remotes/pull/${{ github.event.pull_request.number }}/merge > changed_files.txt | ||
|
||
- name: List generated files (debug) | ||
run: ls -l | ||
|
||
- name: Upload unfiltered jscpd report | ||
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]' changed_files.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 if filtered jscpd report exists | ||
id: check_filtered_report | ||
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 jscpd report | ||
if: env.filtered_report_exists == 'true' | ||
uses: actions/upload-artifact@v4 | ||
with: | ||
name: filtered-jscpd-report | ||
path: ./filtered-jscpd-report.json | ||
|
||
- name: Post GitHub comment | ||
if: env.filtered_report_exists == 'true' | ||
uses: actions/github-script@v7 | ||
with: | ||
script: | | ||
const fs = require('fs'); | ||
const filteredReport = JSON.parse(fs.readFileSync('filtered-jscpd-report.json', 'utf8')); | ||
let comment = "Whoa there, partner! 🌵🤠 We wrangled some duplicated code in your PR:\n\n"; | ||
function link(dup) { | ||
return `https://github.com/${{ github.event.repository.full_name }}/blob/${{ github.event.pull_request.head.sha }}/${dup.name}#L${dup.start + 1}-L${dup.end - 1}` | ||
} | ||
filteredReport.forEach(duplication => { | ||
const firstFile = duplication.firstFile; | ||
const secondFile = duplication.secondFile; | ||
const lines = duplication.lines; | ||
comment += `- [\`${firstFile.name}\`](${link(firstFile)}) has ${lines} duplicated lines with [\`${secondFile.name}\`](${link(secondFile)})\n`; | ||
}); | ||
comment += "\nReducing code duplication by importing common functions from a library not only makes our code cleaner but also easier to maintain. Please move the common code from both files into a library and import it in each. Keep up the great work! 🚀"; | ||
github.rest.issues.createComment({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
issue_number: context.issue.number, | ||
body: comment | ||
}); | ||
- name: Fail if duplications are found | ||
if: env.filtered_report_exists == 'true' | ||
run: | | ||
echo "Duplications found, failing the check." | ||
exit 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
name: Check for linter warnings / exceptions | ||
|
||
on: | ||
pull_request_target: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
check-linter: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Set up Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: '20' | ||
|
||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
ref: ${{ github.event.pull_request.base.sha }} | ||
|
||
- name: Fetch base and target branches | ||
run: | | ||
git fetch origin +refs/heads/${{ github.event.pull_request.base.ref }}:refs/remotes/origin/${{ github.event.pull_request.base.ref }} | ||
git fetch origin +refs/pull/${{ github.event.pull_request.number }}/merge:refs/remotes/pull/${{ github.event.pull_request.number }}/merge | ||
- name: Install dependencies | ||
run: npm ci | ||
|
||
- name: Get the diff | ||
run: git diff --name-only origin/${{ github.event.pull_request.base.ref }}...refs/remotes/pull/${{ github.event.pull_request.number }}/merge > __changed_files.txt | ||
|
||
- name: Run linter on base branch | ||
run: npx eslint --no-inline-config --format json $(cat __changed_files.txt | xargs stat --printf '%n\n' 2> /dev/null) > __base.json || true | ||
|
||
- name: Check out PR | ||
run: git checkout ${{ github.event.pull_request.head.sha }} | ||
|
||
- name: Run linter on PR | ||
run: npx eslint --no-inline-config --format json $(cat __changed_files.txt | xargs stat --printf '%n\n' 2> /dev/null) > __pr.json || true | ||
|
||
- name: Compare them and post comment if necessary | ||
uses: actions/github-script@v7 | ||
with: | ||
script: | | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const process = require('process'); | ||
function parse(fn) { | ||
return JSON.parse(fs.readFileSync(fn)).reduce((memo, data) => { | ||
const file = path.relative(process.cwd(), data.filePath); | ||
if (!memo.hasOwnProperty(file)) { memo[file] = { errors: 0, warnings: 0} } | ||
data.messages.forEach(({severity}) => { | ||
memo[file][severity > 1 ? 'errors' : 'warnings']++; | ||
}); | ||
return memo; | ||
}, {}) | ||
} | ||
function mkDiff(old, new_) { | ||
const files = Object.fromEntries( | ||
Object.entries(new_) | ||
.map(([file, {errors, warnings}]) => { | ||
const {errors: oldErrors, warnings: oldWarnings} = old[file] || {}; | ||
return [file, {errors: Math.max(0, errors - (oldErrors ?? 0)), warnings: Math.max(0, warnings - (oldWarnings ?? 0))}] | ||
}) | ||
.filter(([_, {errors, warnings}]) => errors > 0 || warnings > 0) | ||
) | ||
return Object.values(files).reduce((memo, {warnings, errors}) => { | ||
memo.errors += errors; | ||
memo.warnings += warnings; | ||
return memo; | ||
}, {errors: 0, warnings: 0, files}) | ||
} | ||
function mkComment({errors, warnings, files}) { | ||
function pl(noun, number) { | ||
return noun + (number === 1 ? '' : 's') | ||
} | ||
if (errors === 0 && warnings === 0) return; | ||
const summary = []; | ||
if (errors) summary.push(`**${errors}** linter ${pl('error', errors)}`) | ||
if (warnings) summary.push(`**${warnings}** linter ${pl('warning', warnings)}`) | ||
let cm = `Tread carefully! This PR adds ${summary.join(' and ')} (possibly disabled through directives):\n\n`; | ||
Object.entries(files).forEach(([file, {errors, warnings}]) => { | ||
const summary = []; | ||
if (errors) summary.push(`+${errors} ${pl('error', errors)}`); | ||
if (warnings) summary.push(`+${warnings} ${pl('warning', warnings)}`) | ||
cm += ` * \`${file}\` (${summary.join(', ')})\n` | ||
}) | ||
return cm; | ||
} | ||
const [base, pr] = ['__base.json', '__pr.json'].map(parse); | ||
const comment = mkComment(mkDiff(base, pr)); | ||
if (comment) { | ||
github.rest.issues.createComment({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
issue_number: context.issue.number, | ||
body: comment | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
12.16.1 | ||
20.13.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.