Skip to content

Commit

Permalink
ci(asf): change the check-asf-header script to docker command via haw…
Browse files Browse the repository at this point in the history
…keye-native
  • Loading branch information
LinkinStars committed Nov 17, 2023
1 parent 274eaf2 commit 84ef7d0
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 121 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ install-ui-packages:
ui:
@cd ui && pnpm pre-install && pnpm build && cd -

lint: generate
@bash ./script/check-asf-header.sh
@gofmt -w -l .

all: clean build
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ require (
github.com/golang/geo v0.0.0-20190812012225-f41920e961ce // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/subcommands v1.0.1 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbu
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/google/subcommands v1.0.1 h1:/eqq+otEXm5vhfBrbREPCSVQbvofip6kIz+mX5TUH7k=
github.com/google/subcommands v1.0.1/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down
216 changes: 216 additions & 0 deletions internal/service/mock/siteinfo_repo_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

121 changes: 2 additions & 119 deletions script/check-asf-header.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,122 +16,5 @@
# specific language governing permissions and limitations
# under the License.

# List of patterns to ignore using regular expressions
IGNORED_PATTERNS=("Makefile" "NOTICE" "README.md" "README_CN.md" "SECURITY.md" "go.mod" "go.sum" "DISCLAIMER" "LICENSE" "*.json" "*.svg" "*.png" "*.jpg" "*.ico" "plugin_list")

# List of directories to ignore
IGNORED_DIRECTORIES=("answer-data" "release" "node_modules")

# Recursive function to traverse directories and add ASF header
traverse_directory() {
local dir="$1"

# Check if the current directory needs to be ignored
if is_directory_ignored "$dir"; then
return
fi

# Iterate over all files and directories in the current directory
for file in "$dir"/*; do
if [ -d "$file" ]; then
# If it's a directory, recursively process the subdirectory
traverse_directory "$file"
elif [ -f "$file" ]; then
# If it's a file, check if ASF header needs to be added
process_file "$file"
fi
done
}

process_file() {
local file="$1"
local filename=$(basename "$file")

# Check if the file needs to be ignored
if is_file_ignored "$filename"; then
return
fi

# Check if the file already contains ASF header
if has_asf_header "$file"; then
echo "File $file already contains ASF header. Skipping."
return
fi

# Prompt the user to add ASF header
echo "ASF header needs to be added to file: $file"
read -p "Do you want to add ASF header? (y/n): " choice

if [[ $choice == "y" || $choice == "Y" ]]; then
# Write ASF header to a temporary file
local temp_file=$(mktemp)
cat << EOF > "$temp_file"
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
EOF

# Append the original file content to the temporary file
cat "$file" >> "$temp_file"

# Overwrite the original file with the content of the temporary file
mv "$temp_file" "$file"

echo "ASF header added to file: $file"
else
echo "Skipping file: $file"
fi
}

# Check if the file needs to be ignored
is_file_ignored() {
local filename="$1"
for pattern in "${IGNORED_PATTERNS[@]}"; do
if [[ "$filename" == $pattern ]]; then
return 0
fi
done
return 1
}

# Check if the directory needs to be ignored
is_directory_ignored() {
local directory="$1"
local dirname=$(basename "$directory")
for ignored_dir in "${IGNORED_DIRECTORIES[@]}"; do
if [ "$dirname" = "$ignored_dir" ]; then
return 0
fi
done
return 1
}

# Check if the file already contains ASF header
has_asf_header() {
local file="$1"
local header=$(head -n 15 "$file") # Assuming ASF header is within the first 15 lines
if [[ $header == *"Licensed to the Apache Software Foundation (ASF)"* ]]; then
return 0
else
return 1
fi
}

# Execute the script, starting from the current directory
traverse_directory "../"
docker run -it --rm -v $(pwd):/github/workspace ghcr.io/korandoru/hawkeye-native format
gofmt -w -l .

0 comments on commit 84ef7d0

Please sign in to comment.