Skip to content

Anirud/add license header and workflows #114

Anirud/add license header and workflows

Anirud/add license header and workflows #114

name: Validate SPDX License year and company mention in currently changed Files
on:
pull_request:
branches: [main]
types:
- opened
- reopened
- synchronize
- assigned
- review_requested
push:
branches: [main]
jobs:
check-spdx-license:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Ensure full history for accurate diff
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v44
with:
files: |
model_demos/**.py
- name: Identify currently changed files and check licenses
id: check-licenses
run: |
current_year=$(date +"%Y") # Dynamically set current year for checking
echo "current_year=$current_year" >> $GITHUB_ENV # Export current year to environment variables
missing_license_files=""
check_file() {
local file=$1
if [[ "$file" == *.py ]]; then
# Check if file contains the correct SPDX license identifier and the exact copyright statement with dynamic current year
if ! grep -q "SPDX-License-Identifier: Apache-2.0" "$file" || ! grep -q "SPDX-FileCopyrightText: © $current_year Tenstorrent AI ULC" "$file"; then
missing_license_files="$missing_license_files$file "
fi
fi
}
# Get the list of changed files from the GitHub Action
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
check_file "$file"
done
echo "missing_license_files=$missing_license_files" >> $GITHUB_ENV
echo "Checked files for correct SPDX-License compliance."
- name: Comment on PR about incorrect SPDX licenses
if: env.missing_license_files != ''
uses: actions/github-script@v6
with:
script: |
const issueNumber = context.issue.number;
const owner = context.repo.owner;
const repo = context.repo.repo;
const missingFiles = process.env.missing_license_files.trim().replace(/\s+/g, '\n');
const currentYear = process.env.current_year;
const commentBody = `Tester: The following files are not compliant with the required licensing standards for ${currentYear} or do not contain the correct copyright text 'Tenstorrent AI ULC' within their license header:\n\`\`\`\n${missingFiles}\n\`\`\`\nPlease update the files accordingly. Your attention and cooperation in this matter are greatly appreciated.\nThank you.`;
github.rest.issues.createComment({
issue_number: issueNumber,
owner: owner,
repo: repo,
body: commentBody
});
- name: Fail the workflow if license issues are found
if: env.missing_license_files != ''
run: |
echo "Failing the workflow due to non-compliance with SPDX license requirements."
exit 1