Don't forcefully validate Git repos if not needed (#4953)

* Fix find when linting non-git repos

* Remove uses

* Move safe.directory config after we set GITHUB_WORKSPACE

* Fix Git validation check

* Move 'find' test runner to makefile

* Validate vars before validating Git repo

* Validate Git repo even when VALIDATE_ALL_CODEBASE=false

* Initialize GITHUB_SHA when running locally

* Initialize safe git dirs

* Check git safe dirs error code

* Fix log level color marker

* Fix linting errors

* Fix change dir command

* Fix linting errors

* Set default branch
This commit is contained in:
Marco Ferrari 2023-12-07 20:07:22 +01:00 committed by GitHub
parent 9869638131
commit 7a21f934b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 16 deletions

View file

@ -100,6 +100,7 @@ jobs:
-e TEST_CASE_RUN=true \
-e ANSIBLE_DIRECTORY=.automation/test/ansible \
-e ACTIONS_RUNNER_DEBUG=true \
-e DEFAULT_BRANCH=main \
-e RENOVATE_SHAREABLE_CONFIG_PRESET_FILE_NAMES="default.json,hoge.json" \
-e ERROR_ON_MISSING_EXEC_BIT=true \
-e TYPESCRIPT_STANDARD_TSCONFIG_FILE=".github/linters/tsconfig.json" \
@ -112,6 +113,7 @@ jobs:
-e RUN_LOCAL=true \
-e OUTPUT_DETAILS=detailed \
-e ACTIONS_RUNNER_DEBUG=true \
-e DEFAULT_BRANCH=main \
-e RENOVATE_SHAREABLE_CONFIG_PRESET_FILE_NAMES="default.json,hoge.json" \
-e ERROR_ON_MISSING_EXEC_BIT=true \
-v "${GITHUB_WORKSPACE}:/tmp/lint" \

View file

@ -121,6 +121,7 @@ jobs:
-e TEST_CASE_RUN=true \
-e ANSIBLE_DIRECTORY=.automation/test/ansible \
-e ACTIONS_RUNNER_DEBUG=true \
-e DEFAULT_BRANCH=main \
-e RENOVATE_SHAREABLE_CONFIG_PRESET_FILE_NAMES="default.json,hoge.json" \
-e ERROR_ON_MISSING_EXEC_BIT=true \
-e TYPESCRIPT_STANDARD_TSCONFIG_FILE=".github/linters/tsconfig.json" \
@ -132,6 +133,7 @@ jobs:
docker run \
-e RUN_LOCAL=true \
-e ACTIONS_RUNNER_DEBUG=true \
-e DEFAULT_BRANCH=main \
-e RENOVATE_SHAREABLE_CONFIG_PRESET_FILE_NAMES="default.json,hoge.json" \
-e ERROR_ON_MISSING_EXEC_BIT=true \
-v "${GITHUB_WORKSPACE}:/tmp/lint" \

View file

@ -4,7 +4,7 @@
all: info docker test ## Run all targets.
.PHONY: test
test: info validate-container-image-labels inspec ## Run tests
test: info validate-container-image-labels inspec test-find ## Run tests
# if this session isn't interactive, then we don't want to allocate a
# TTY, which would fail, but if it is interactive, we do want to attach
@ -104,3 +104,14 @@ validate-container-image-labels: ## Validate container image labels
$(BUILD_DATE) \
$(BUILD_REVISION) \
$(BUILD_VERSION)
.phony: test-find
test-find: ## Run super-linter on a subdirectory with USE_FIND_ALGORITHM=true
docker run \
-e RUN_LOCAL=true \
-e ACTIONS_RUNNER_DEBUG=true \
-e ERROR_ON_MISSING_EXEC_BIT=true \
-e DEFAULT_BRANCH=main \
-e USE_FIND_ALGORITHM=true \
-v "$(CURDIR)/.github":/tmp/lint \
$(SUPER_LINTER_TEST_CONTAINER_URL)

View file

@ -54,7 +54,7 @@ export LOG_TEMP
log() {
local TOTERM=${1:-}
local MESSAGE=${2:-}
local LOG_LEVEL_LABEL="[${3}]"
local LOG_LEVEL_LABEL="${3}"
local LOG_MESSAGE_DATE
LOG_MESSAGE_DATE="$(date +"%F %T")"
@ -69,6 +69,8 @@ log() {
COLOR_MARKER="${F[R]}"
fi
LOG_LEVEL_LABEL="[${LOG_LEVEL_LABEL}]"
local COLORED_MESSAGE
COLORED_MESSAGE="${NC}${LOG_MESSAGE_DATE} ${COLOR_MARKER}${LOG_LEVEL_LABEL}${NC} ${MESSAGE}${NC}"
local MESSAGE_FOR_LOG_FILE

View file

@ -235,6 +235,8 @@ function ValidateLocalGitRepository() {
}
function ValidateGitShaReference() {
debug "Git HEAD: $(git -C "${GITHUB_WORKSPACE}" show HEAD --stat)"
debug "Validate that the GITHUB_SHA reference (${GITHUB_SHA}) exists in this Git repository."
if ! git -C "${GITHUB_WORKSPACE}" cat-file -e "${GITHUB_SHA}"; then
fatal "The GITHUB_SHA reference (${GITHUB_SHA}) doesn't exist in this Git repository"

View file

@ -375,7 +375,8 @@ debug "IGNORE_GENERATED_FILES: ${IGNORE_GENERATED_FILES}"
# Default Vars #
################
DEFAULT_VALIDATE_ALL_CODEBASE='true' # Default value for validate all files
DEFAULT_WORKSPACE="${DEFAULT_WORKSPACE:-/tmp/lint}" # Default workspace if running locally
DEFAULT_SUPER_LINTER_WORKSPACE="/tmp/lint" # Fall-back value for the workspace
DEFAULT_WORKSPACE="${DEFAULT_WORKSPACE:-${DEFAULT_SUPER_LINTER_WORKSPACE}}" # Default workspace if running locally
DEFAULT_RUN_LOCAL='false' # Default value for debugging locally
DEFAULT_TEST_CASE_RUN='false' # Flag to tell code to run only test cases
@ -443,6 +444,18 @@ Header() {
info " - https://github.com/super-linter/super-linter"
info "---------------------------------------------"
}
ConfigureGitSafeDirectories() {
debug "Configuring Git safe directories"
declare -a git_safe_directories=("${GITHUB_WORKSPACE}" "${DEFAULT_SUPER_LINTER_WORKSPACE}" "${DEFAULT_WORKSPACE}")
for safe_directory in "${git_safe_directories[@]}"; do
debug "Set ${safe_directory} as a Git safe directory"
if ! git config --global --add safe.directory "${safe_directory}"; then
fatal "Cannot configure ${safe_directory} as a Git safe directory."
fi
done
}
################################################################################
#### Function GetGitHubVars ####################################################
GetGitHubVars() {
@ -453,7 +466,7 @@ GetGitHubVars() {
info "Gathering GitHub information..."
if [[ ${RUN_LOCAL} != "false" ]]; then
info "RUN_LOCAL has been set to:[${RUN_LOCAL}]. Bypassing GitHub Actions variables..."
info "RUN_LOCAL has been set to: ${RUN_LOCAL}. Bypassing GitHub Actions variables..."
if [ -z "${GITHUB_WORKSPACE}" ]; then
GITHUB_WORKSPACE="${DEFAULT_WORKSPACE}"
@ -463,11 +476,24 @@ GetGitHubVars() {
fatal "Provided volume is not a directory!"
fi
info "Linting all files in mapped directory:[${GITHUB_WORKSPACE}]"
pushd "${GITHUB_WORKSPACE}" >/dev/null || exit 1
VALIDATE_ALL_CODEBASE="${DEFAULT_VALIDATE_ALL_CODEBASE}"
info "Linting all files in mapped directory: ${GITHUB_WORKSPACE}. Setting VALIDATE_ALL_CODEBASE to: ${VALIDATE_ALL_CODEBASE}"
if [[ "${USE_FIND_ALGORITHM}" == "false" ]]; then
ConfigureGitSafeDirectories
debug "Initializing GITHUB_SHA considering ${GITHUB_WORKSPACE}"
GITHUB_SHA=$(git -C "${GITHUB_WORKSPACE}" rev-parse HEAD)
ERROR_CODE=$?
debug "GITHUB_SHA initalization return code: ${ERROR_CODE}"
if [ ${ERROR_CODE} -ne 0 ]; then
fatal "Failed to initialize GITHUB_SHA. Output: ${GITHUB_SHA}"
fi
debug "GITHUB_SHA: ${GITHUB_SHA}"
else
debug "Skip the initalization of GITHUB_SHA because we don't need it"
fi
else
if [ -z "${GITHUB_WORKSPACE}" ]; then
error "Failed to get [GITHUB_WORKSPACE]!"
@ -817,10 +843,6 @@ UpdateLoopsForImage
##################################
GetLinterVersions
debug "Allow Git to work on ${GITHUB_WORKSPACE}"
git config --global --add safe.directory "${GITHUB_WORKSPACE}" 2>&1
git config --global --add safe.directory "/tmp/lint" 2>&1
#######################
# Get GitHub Env Vars #
#######################
@ -828,6 +850,10 @@ git config --global --add safe.directory "/tmp/lint" 2>&1
# needed to connect back and update checks
GetGitHubVars
# Ensure that Git safe directories are configured because we don't do this in
# all cases when initializing variables
ConfigureGitSafeDirectories
########################################################
# Initialize variables that depend on GitHub variables #
########################################################
@ -842,7 +868,9 @@ debug "TYPESCRIPT_STANDARD_TSCONFIG_FILE: ${TYPESCRIPT_STANDARD_TSCONFIG_FILE}"
############################
# Validate the environment #
############################
if [[ "${USE_FIND_ALGORITHM}" != "false" ]] || [[ "${VALIDATE_ALL_CODEBASE}" != "true" ]]; then
GetValidationInfo
if [[ "${USE_FIND_ALGORITHM}" == "false" ]]; then
debug "Validate the local Git environment"
ValidateLocalGitRepository
ValidateGitShaReference
@ -851,8 +879,6 @@ else
debug "Skipped the validation of the local Git environment because we don't depend on it."
fi
GetValidationInfo
#################################
# Get the linter rules location #
#################################