eclipse / kapua

Eclipse Public License 2.0
227 stars 160 forks source link

🐛 [checkstyle] Update checkstyle to dynamically handle copyright years #4135

Closed MDeLuise closed 1 week ago

MDeLuise commented 1 week ago

Description:

This PR modifies the checkstyle for the copyright header to no longer enforce the year 2022. The checkstyle has been updated to dynamically handle the copyright years by either appending the current year to a single year or replacing the second year in a two-year range.

Additionally, a script has been provided to automate the process of updating the copyright years across multiple files. The script:

Example:

  1. Before:

     * Copyright (c) 2016 Eurotech and/or its affiliates and others

    After (assuming the current year is 2024):

    * Copyright (c) 2016, 2024 Eurotech and/or its affiliates and others
  2. Before:

     * Copyright (c) 2019 Eurotech and/or its affiliates and others

    After (assuming the current year is 2024):

     * Copyright (c) 2019, 2024 Eurotech and/or its affiliates and others

Script

Below is the script used to apply these changes:

#!/bin/bash

# Check for the correct number of arguments
if [ "$#" -ne 1 ]; then
    echo "Usage: $0 <path_to_directory>"
    exit 1
fi

# Set variables from arguments
root="$1"
currentYear=$(date +%Y)  # Get the current year

# Initialize a counter for changed files
changedFiles=0

# Loop through all .java files in the specified directory recursively
for file in $(find "$root" -type f -name "*.java"); do
    fileChanged=false

    # Create a temporary file for modified content
    tempFile="${file}.tmp"

    # Capture the first line and the second line
    line1=$(head -n 1 "$file")  # First line
    line2=$(head -2 "$file" | tail -n 1)  # Second line

    {
        # Check for the second header pattern with two years (e.g., "2016, 2022")
        if [[ "$line2" =~ Copyright\ \(c\)\ ([0-9]{4}),\ ([0-9]{4})\ .+\ and\ others ]]; then
            # Replace the last year with the current year
            line2=$(echo "$line2" | sed -E 's/([0-9]{4}), ([0-9]{4})/\1, '"$currentYear"'/')
            fileChanged=true
        # Check for the first header pattern with a single year (e.g., "2016")
        elif [[ "$line2" =~ Copyright\ \(c\)\ ([0-9]{4})\ .+\ and\ others ]]; then
            # Append the current year to the existing year
            line2=$(echo "$line2" | sed -E "s/([0-9]{4})/\1, $currentYear/")
            fileChanged=true
        fi

        # Write the first line (unchanged) to the temporary file
        echo "$line1" > "$tempFile"
        # Write the modified second line
        echo "$line2" >> "$tempFile"

        # Write the rest of the file unchanged, starting from the third line
        tail -n +3 "$file" >> "$tempFile"
    }

    # If the file was changed, replace the original file
    if [ "$fileChanged" = true ]; then
        mv "$tempFile" "$file"
        echo "File changed: $file"
        ((changedFiles++))
    else
        rm "$tempFile"
    fi
done

# Print the total number of changed files
echo "Total files changed: $changedFiles"

This script ensures that the copyright header is updated with the current year, either by appending it or replacing the second year in a two-year range, based on the existing header format.

Note

I did not set a dynamic current-year variable in the checkstyle configuration, as checkstyle does not support runtime variables in its configuration files. Since the RegexpHeader module only allows static regular expressions, setting the current year dynamically within the configuration file is not possible. Consequently, the header pattern must be updated manually each year if set to a fixed value.

codecov[bot] commented 1 week ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 16.80%. Comparing base (840c13a) to head (c27fdb9). Report is 4 commits behind head on develop.

Additional details and impacted files [![Impacted file tree graph](https://app.codecov.io/gh/eclipse/kapua/pull/4135/graphs/tree.svg?width=650&height=150&src=pr&token=1P4N4CApH8&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=eclipse)](https://app.codecov.io/gh/eclipse/kapua/pull/4135?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=eclipse) ```diff @@ Coverage Diff @@ ## develop #4135 +/- ## ========================================== Coverage 16.80% 16.80% Complexity 22 22 ========================================== Files 2021 2021 Lines 52471 52471 Branches 4426 4426 ========================================== Hits 8820 8820 Misses 43253 43253 Partials 398 398 ```

🚨 Try these New Features: