Skip to content

Commit

Permalink
github-actions-for-automated-license-header-addition (#71)
Browse files Browse the repository at this point in the history
* port all changes into new branch

* add --fix flag

* 🚨✨ AUTOMATED COMMIT | Applied Code Formatting, Cleanup and added SPDX license headers

* fix es lint errors

* test without lc header

* 🚨✨AUTOMATED COMMIT | Final SPDX license header additions

---------

Co-authored-by: anirudTT <anirudTT@users.noreply.github.com>
  • Loading branch information
anirudTT and anirudTT authored Oct 28, 2024
1 parent 51d5549 commit 6866785
Show file tree
Hide file tree
Showing 12 changed files with 426 additions and 75 deletions.
144 changes: 144 additions & 0 deletions .github/workflows/backend-license-checker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
name: Backend-Models SPDX Licenses Checker

on:
workflow_dispatch:
workflow_call:
pull_request:
branches:
- "main"
- "staging"
types:
- opened
- reopened
- synchronize
- assigned
- review_requested

jobs:
check-spdx-licenses:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v4

- uses: actions/setup-python@v5.0.0
with:
cache: "pip"
python-version: "3.8"

# Install the copyright checking tool
- name: Install copyright check tool
run: pip install git+https://github.com/espressif/check-copyright.git@master

# Check SPDX licenses
- name: Check SPDX licenses
id: check_spdx_licenses
run: |
set +e
output=$(python -m check_copyright --verbose --dry-run --config ./check_copyright_config.yaml . 2>&1)
exit_code=$?
clean_output=$(echo "$output" | sed 's/\x1b\[[0-9;]*m//g')
echo "CLEAN_OUTPUT<<EOF" >> $GITHUB_ENV
echo "$clean_output" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
echo "EXIT_CODE=$exit_code" >> $GITHUB_ENV
exit 0
- name: Debug Extracted Files
run: |
echo "Extracted Files:"
echo "$CLEAN_OUTPUT"
# Extract files needing SPDX header addition
- name: Extract Files
id: extract_files
run: |
set +e
files=$(echo "$CLEAN_OUTPUT" | awk '
/Files which failed the copyright check:/, /Additional information about this hook and copyright headers may be found here:/ {
if ($0 !~ /Files which failed the copyright check:/ && $0 !~ /Additional information about this hook and copyright headers may be found here:/) print
}
/Some files are without a copyright note and a license header needs to be added:/, /Additional information about this hook and copyright headers may be found here:/ {
if ($0 !~ /Some files are without a copyright note and a license header needs to be added:/ && $0 !~ /Additional information about this hook and copyright headers may be found here:/) print
}' | sed 's/^ *//' | sed '/^$/d' | grep '^.')
echo "FILES<<EOF" >> $GITHUB_ENV
echo "$files" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
echo "Extracted Files: $files"
exit 0
- name: Debug Extracted Files
run: |
echo "Extracted Files:"
echo "$FILES"
# Run the `add_spdx_header.py` script to fix missing SPDX headers
- name: Run SPDX Header Script
if: env.FILES != ''
run: |
set +e
python add_spdx_header.py # Run the script to add SPDX headers to the missing files
exit 0
# Commit the changes made by `add_spdx_header.py`
- name: Commit changes
if: env.FILES != ''
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_user_name: SPDX-Bot
commit_user_email: bot@example.com
commit_message: '🚨✨AUTOMATED COMMIT | Added missing SPDX license headers automatically'
branch: ${{ github.head_ref }}

# Extract and clean file paths
- name: Extract File Paths
if: env.FILES != ''
run: |
files_clean=$(echo "$FILES" | awk '/^Modified files:/ {ignore = 1} /^Above is a list of files/ {ignore = 0} !ignore && /^\.\// {print}' | sort | uniq)
echo "FILES_CLEAN<<EOF" >> $GITHUB_ENV
echo "$files_clean" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
echo "Extracted Paths: $files_clean"
- name: Debug Extracted Paths
run: |
echo "Extracted Paths:"
echo "$FILES_CLEAN"
echo "---------------------------------"
# Post a comment on the PR or Issue if there are SPDX issues
- name: Comment on PR or Issue
if: env.FILES_CLEAN != ''
uses: actions/github-script@v7
with:
script: |
const formattedOutput = process.env.FILES_CLEAN;
const issueNumber = context.payload.pull_request.number;
const owner = context.repo.owner;
const repo = context.repo.repo;
if (formattedOutput) {
const commentBody = `Our automated SPDX license verification process has discovered that the following files are missing a license header:\n\`\`\`\n${formattedOutput}\n\`\`\`\nPlease ensure each indicated file includes a valid SPDX license identifier. This is essential for maintaining licensing compliance. Thank you.`;
await github.rest.issues.createComment({
issue_number: issueNumber,
owner: owner,
repo: repo,
body: commentBody
});
} else {
console.log("No SPDX license issues found.");
}
- name: Run SPDX Header Script Again
run: |
set +e
echo "Running SPDX header script again on all files in the repository"
python ./add_spdx_header.py
exit 0
# Commit the changes made by the second run of `add_spdx_header.py`
- name: Commit changes from second run
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_user_name: SPDX-Bot
commit_user_email: bot@example.com
commit_message: '🚨✨AUTOMATED COMMIT | Final SPDX license header additions'
branch: ${{ github.head_ref }}
176 changes: 176 additions & 0 deletions .github/workflows/frontend-lint-license-checker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
name: Front-End Linter SPDX Licenses Checker

on:
push:
branches:
- main
pull_request:
branches:
- "main"
- "staging"
types:
- opened
- reopened
- synchronize
- assigned
- review_requested

jobs:
lint:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}

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

- name: Install dependencies
working-directory: app/frontend
run: npm install

- name: Run ESLint with Auto-fix
working-directory: app/frontend
run: |
set +e
npm run lint || true # Run lint and allow the workflow to continue even if there are warnings/errors
# Check if GPG key exists
- name: Check if GPG key exists
run: |
if [ -n "${{ secrets.GPG_PRIVATE_KEY }}" ]; then
echo "GPG_KEY_EXISTS=true" >> $GITHUB_ENV
else
echo "GPG_KEY_EXISTS=false" >> $GITHUB_ENV
fi
# Import GPG key if it exists
- name: Import GPG key if it exists
if: env.GPG_KEY_EXISTS == 'true'
uses: crazy-max/ghaction-import-gpg@v6
with:
gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
passphrase: ${{ secrets.GPG_PASSPHRASE }}
git_user_signingkey: true
git_commit_gpgsign: true

# Auto-commit changes using stefanzweifel/git-auto-commit-action
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_user_name: CleanBot
commit_user_email: your-email@example.com
commit_message: '🚨✨ AUTOMATED COMMIT | Applied Code Formatting, Cleanup and added SPDX license headers'
commit_options: ${{ env.GPG_KEY_EXISTS == 'true' && '-S' || '' }}
branch: ${{ github.head_ref }}

# Run ESLint and Capture Output
- name: Run ESLint and Capture Output
working-directory: app/frontend
id: run_eslint
run: |
set +e
output=$(npm run lint --silent 2>&1)
exit_code=$?
echo "$output"
clean_output=$(echo "$output" | sed 's/\x1b\[[0-9;]*m//g') # Remove ANSI escape codes
echo "CLEAN_OUTPUT<<EOF" >> $GITHUB_ENV
echo "$clean_output" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
grouped_errors=""
current_file=""
errors_for_file=""
has_errors=false
missing_lc_headers=false
lc_flagged_files=""
while IFS= read -r line; do
if echo "$line" | grep -q '^\s*\/'; then
if [ "$has_errors" = true ]; then
grouped_errors+="$current_file\n$errors_for_file\n------------------------------------------------------------------------------------------------------------------------------------------\n"
errors_for_file=""
has_errors=false
fi
current_file=$(echo "$line" | sed 's/\n//g')
elif echo "$line" | grep -q 'error'; then
errors_for_file+=" $line\n"
has_errors=true
if echo "$line" | grep -q 'missing header'; then
missing_lc_headers=true
errors_for_file+="!Flagged: LC header missing\n"
lc_flagged_files+="${current_file}\n"
fi
fi
done <<< "$clean_output"
if [ "$has_errors" = true ]; then
grouped_errors+="$current_file\n$errors_for_file\n"
fi
if [ -n "$grouped_errors" ]; then
echo "Errors found."
echo "GROUPED_ERRORS<<EOF" >> $GITHUB_ENV
echo -e "$grouped_errors" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
echo "HAS_ERRORS=true" >> $GITHUB_ENV
else
echo "No relevant errors found."
echo "HAS_ERRORS=false" >> $GITHUB_ENV
fi
if [ "$missing_lc_headers" = true ]; then
echo "LC headers missing in one or more files."
echo "MISSING_LC_HEADERS=true" >> $GITHUB_ENV
echo "LC_FLAGGED_FILES<<EOF" >> $GITHUB_ENV
echo -e "$(echo -e "$lc_flagged_files" | sed '/^\s*$/d')" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
else
echo "MISSING_LC_HEADERS=false" >> $GITHUB_ENV
fi
exit 0
# Comment on PR with ESLint Errors and LC Headers
- name: Comment on PR with ESLint Errors and LC Headers
if: env.HAS_ERRORS == 'true'
uses: actions/github-script@v7
with:
script: |
const lintErrors = process.env.GROUPED_ERRORS;
const issueNumber = context.payload.pull_request.number;
const owner = context.repo.owner;
const repo = context.repo.repo;
let commentBody = "";
if (process.env.MISSING_LC_HEADERS === 'true') {
const flaggedFiles = process.env.LC_FLAGGED_FILES.trim();
commentBody += `## 🚨 SPDX-License Header Errors\n\nThe following files are missing the required license headers:\n\n\`\`\`\n${flaggedFiles}\n\`\`\`\nPlease ensure each of these files includes a valid SPDX license identifier. This is essential for maintaining licensing compliance. Thank you!`;
}
if (lintErrors) {
commentBody += `## Frontend Project: ESLint Errors\n\`\`\`\n${lintErrors}\n\`\`\`\nPlease review and resolve the ESLint errors.`;
}
if (commentBody.trim() && issueNumber) {
await github.rest.issues.createComment({
issue_number: issueNumber,
owner: owner,
repo: repo,
body: commentBody
});
} else {
console.log("No relevant errors to report.");
}
- name: Fail the Workflow if LC Headers Are Missing
if: env.MISSING_LC_HEADERS == 'true'
run: |
echo "Failing the workflow because LC headers are missing."
exit 1
28 changes: 14 additions & 14 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
Copyright (c) 2024 Tenstorrent AI ULC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
Expand Down Expand Up @@ -189,8 +175,22 @@

END OF TERMS AND CONDITIONS

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-------------------------------------------------------------------------------
Copyright (c) 2024 Tenstorrent AI ULC

-------------------------------------------------------------------------------
Third-Party Dependencies:

The following dependencies are utilized by this project but are not explicitly
Expand Down
Loading

0 comments on commit 6866785

Please sign in to comment.