From 654bf41c03e99791f2e2c654e3974cf80aadeb0c Mon Sep 17 00:00:00 2001 From: Franz Busch Date: Fri, 12 Jul 2024 12:07:06 +0200 Subject: [PATCH] [GHA] Add license header check (#2781) * [GHA] Add license header check # Motivation We need to make sure all code files have the appropriate license headers in place. This is currently part of the soundness check and we need to replace this with a GH action. # Modification Adds a new job to the reusable workflow the check all files for license headers. Since some files are ignored every repo can specify a `.licenseignore` file. # Result Last part of the soundness script migrated. * Review * Remove default excludes --- .github/workflows/pull_request.yml | 3 +- .github/workflows/reusable_pull_request.yml | 23 ++++++- .licenseignore | 47 +++++++++++++ Benchmarks/Package.swift | 13 ---- scripts/check-license-header.sh | 74 +++++++++++++++++++++ 5 files changed, 145 insertions(+), 15 deletions(-) create mode 100644 .licenseignore create mode 100755 scripts/check-license-header.sh diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index b2e62dbd2..6d19bb7b4 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -9,4 +9,5 @@ jobs: name: Checks uses: ./.github/workflows/reusable_pull_request.yml with: - benchmarks_linux_package_path: "Benchmarks" \ No newline at end of file + benchmarks_linux_package_path: "Benchmarks" + license_header_check_project_name: "SwiftNIO" diff --git a/.github/workflows/reusable_pull_request.yml b/.github/workflows/reusable_pull_request.yml index 5fe2d6747..b34e0c6c7 100644 --- a/.github/workflows/reusable_pull_request.yml +++ b/.github/workflows/reusable_pull_request.yml @@ -31,6 +31,14 @@ on: type: string description: "List of unacceptable words. Defaults to a sensible list of words." default: "blacklist whitelist slave master sane sanity insane insanity kill killed killing hang hung hanged hanging" #ignore-unacceptable-language + license_header_check_enabled: + type: boolean + description: "Boolean to enable the license header check job. Defaults to true." + default: true + license_header_check_project_name: + type: string + description: "Name of the project called out in the license header." + required: true ## We are cancelling previously triggered workflow runs concurrency: @@ -137,4 +145,17 @@ jobs: - name: Run unacceptable language check env: UNACCEPTABLE_WORD_LIST: ${{ inputs.unacceptable_language_check_word_list}} - run: curl -s https://raw.githubusercontent.com/apple/swift-nio/main/scripts/check-unacceptable-language.sh | bash \ No newline at end of file + run: curl -s https://raw.githubusercontent.com/apple/swift-nio/main/scripts/check-unacceptable-language.sh | bash + + license-header-check: + name: License headers check + if: ${{ inputs.license_header_check_enabled }} + runs-on: ubuntu-latest + timeout-minutes: 1 + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - name: Run license header check + env: + PROJECT_NAME: ${{ inputs.license_header_check_project_name }} + run: curl -s https://raw.githubusercontent.com/apple/swift-nio/main/scripts/check-license-header.sh | bash diff --git a/.licenseignore b/.licenseignore new file mode 100644 index 000000000..7e304d71f --- /dev/null +++ b/.licenseignore @@ -0,0 +1,47 @@ +.gitignore +**/.gitignore +.licenseignore +.gitattributes +.mailfilter +.mailmap +.spi.yml +.swift-format +.github/* +*.md +*.txt +*.yml +*.yaml +*.json +Package.swift +**/Package.swift +Package@-*.swift +**/Package@-*.swift +Package.resolved +**/Package.resolved +Makefile +*.modulemap +**/*.modulemap +**/*.docc/* +*.xcprivacy +**/*.xcprivacy +*.symlink +**/*.symlink +Dockerfile +**/Dockerfile +Snippets/* +Sources/CNIOAtomics/src/cpp_magic.h +Sources/CNIOLLHTTP/LICENSE-MIT +Sources/CNIOLLHTTP/c_nio_api.c +Sources/CNIOLLHTTP/c_nio_http.c +Sources/CNIOLLHTTP/c_nio_llhttp.c +Sources/CNIOLLHTTP/include/c_nio_llhttp.h +Sources/CNIOSHA1/c_nio_sha1.c +Sources/CNIOSHA1/include/CNIOSHA1.h +dev/alloc-limits-from-test-output +dev/boxed-existentials.d +dev/git.commit.template +dev/lldb-smoker +dev/make-single-file-spm +dev/malloc-aggregation.d +dev/update-alloc-limits-to-last-completed-ci-build +scripts/nio-diagnose diff --git a/Benchmarks/Package.swift b/Benchmarks/Package.swift index 507159a7b..6748b30a4 100644 --- a/Benchmarks/Package.swift +++ b/Benchmarks/Package.swift @@ -1,17 +1,4 @@ // swift-tools-version: 5.7 -//===----------------------------------------------------------------------===// -// -// This source file is part of the SwiftCertificates open source project -// -// Copyright (c) 2023 Apple Inc. and the SwiftCertificates project authors -// Licensed under Apache License v2.0 -// -// See LICENSE.txt for license information -// See CONTRIBUTORS.txt for the list of SwiftCertificates project authors -// -// SPDX-License-Identifier: Apache-2.0 -// -//===----------------------------------------------------------------------===// import PackageDescription diff --git a/scripts/check-license-header.sh b/scripts/check-license-header.sh new file mode 100755 index 000000000..1522fc8eb --- /dev/null +++ b/scripts/check-license-header.sh @@ -0,0 +1,74 @@ +#!/bin/bash +##===----------------------------------------------------------------------===## +## +## This source file is part of the SwiftNIO open source project +## +## Copyright (c) 2024 Apple Inc. and the SwiftNIO project authors +## Licensed under Apache License v2.0 +## +## See LICENSE.txt for license information +## See CONTRIBUTORS.txt for the list of SwiftNIO project authors +## +## SPDX-License-Identifier: Apache-2.0 +## +##===----------------------------------------------------------------------===## +set -euo pipefail + +log() { printf -- "** %s\n" "$*" >&2; } +error() { printf -- "** ERROR: %s\n" "$*" >&2; } +fatal() { error "$@"; exit 1; } + +test -n "${PROJECT_NAME:-}" || fatal "PROJECT_NAME unset" + +expected_file_header_template="@@===----------------------------------------------------------------------===@@ +@@ +@@ This source file is part of the ${PROJECT_NAME} open source project +@@ +@@ Copyright (c) YEARS Apple Inc. and the ${PROJECT_NAME} project authors +@@ Licensed under Apache License v2.0 +@@ +@@ See LICENSE.txt for license information +@@ See CONTRIBUTORS.txt for the list of ${PROJECT_NAME} project authors +@@ +@@ SPDX-License-Identifier: Apache-2.0 +@@ +@@===----------------------------------------------------------------------===@@" + +paths_with_missing_license=( ) + +file_paths=$(git ls-files $(cat .licenseignore | xargs -I% printf ":(exclude)% ")) + +while IFS= read -r file_path; do + file_basename=$(basename -- "${file_path}") + file_extension="${file_basename##*.}" + + case "${file_extension}" in + swift) expected_file_header=$(sed -e 's|@@|//|g' <<<"${expected_file_header_template}") ;; + h) expected_file_header=$(sed -e 's|@@|//|g' <<<"${expected_file_header_template}") ;; + c) expected_file_header=$(sed -e 's|@@|//|g' <<<"${expected_file_header_template}") ;; + sh) expected_file_header=$(cat <(echo '#!/bin/bash') <(sed -e 's|@@|##|g' <<<"${expected_file_header_template}")) ;; + py) expected_file_header=$(cat <(echo '#!/usr/bin/env python3') <(sed -e 's|@@|##|g' <<<"${expected_file_header_template}")) ;; + rb) expected_file_header=$(cat <(echo '#!/usr/bin/env ruby') <(sed -e 's|@@|##|g' <<<"${expected_file_header_template}")) ;; + *) fatal "Unsupported file extension for file (exclude or update this script): ${file_path}" ;; + esac + expected_file_header_linecount=$(wc -l <<<"${expected_file_header}") + + file_header=$(head -n "${expected_file_header_linecount}" "${file_path}") + normalized_file_header=$( + echo "${file_header}" \ + | sed -e 's/20[12][0123456789]-20[12][0123456789]/YEARS/' -e 's/20[12][0123456789]/YEARS/' \ + ) + + if ! diff -u \ + --label "Expected header" <(echo "${expected_file_header}") \ + --label "${file_path}" <(echo "${normalized_file_header}") + then + paths_with_missing_license+=("${file_path} ") + fi +done <<< "$file_paths" + +if [ "${#paths_with_missing_license[@]}" -gt 0 ]; then + fatal "❌ Found missing license header in files: ${paths_with_missing_license[*]}." +fi + +log "✅ Found no files with missing license header."