Skip to content

Commit

Permalink
feat: configure github server url
Browse files Browse the repository at this point in the history
Allow the configuration of the GitHub server URL, and add some
validation rules that check for common misconfigurations.

Close #5572
  • Loading branch information
ferrarimarco committed Jun 19, 2024
1 parent c99ec77 commit f8124fe
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ You can configure super-linter using the following environment variables:
| **GITHUB_ACTIONS_CONFIG_FILE** | `actionlint.yml` | Filename for [Actionlint configuration](https://github.com/rhysd/actionlint/blob/main/docs/config.md) (ex: `actionlint.yml`) |
| **GITHUB_ACTIONS_COMMAND_ARGS** | `null` | Additional arguments passed to `actionlint` command. Useful to [ignore some errors](https://github.com/rhysd/actionlint/blob/main/docs/usage.md#ignore-some-errors) |
| **GITHUB_CUSTOM_API_URL** | `https://api.${GITHUB_DOMAIN}` | Specify a custom GitHub API URL in case GitHub Enterprise is used: e.g. `https://github.myenterprise.com/api/v3` |
| **GITHUB_DOMAIN** | `github.com` | Specify a custom GitHub domain in case GitHub Enterprise is used: e.g. `github.myenterprise.com` |
| **GITHUB_CUSTOM_SERVER_URL** | `https://${GITHUB_DOMAIN}"` | Specify a custom GitHub server URL. Useful for GitHub Enterprise instances. |
| **GITHUB_DOMAIN** | `github.com` | Specify a custom GitHub domain in case GitHub Enterprise is used: e.g. `github.myenterprise.com`. `GITHUB_DOMAIN` is a convenience configuration variable to automatically build `GITHUB_CUSTOM_API_URL` and `GITHUB_CUSTOM_SERVER_URL`. |
| **GITLEAKS_CONFIG_FILE** | `.gitleaks.toml` | Filename for [GitLeaks configuration](https://github.com/zricethezav/gitleaks#configuration) (ex: `.gitleaks.toml`) |
| **IGNORE_GENERATED_FILES** | `false` | If set to `true`, super-linter will ignore all the files with `@generated` marker but without `@not-generated` marker. |
| **IGNORE_GITIGNORED_FILES** | `false` | If set to `true`, super-linter will ignore all the files that are ignored by Git. |
Expand Down
16 changes: 16 additions & 0 deletions lib/functions/githubDomain.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

DEFAULT_GITHUB_DOMAIN="github.com"
GITHUB_DOMAIN="${GITHUB_DOMAIN:-${DEFAULT_GITHUB_DOMAIN}}"
GITHUB_DOMAIN="${GITHUB_DOMAIN%/}" # Remove trailing slash if present

# GitHub API root url
GITHUB_API_URL="${GITHUB_CUSTOM_API_URL:-"https://api.${GITHUB_DOMAIN}"}"
GITHUB_API_URL="${GITHUB_API_URL%/}" # Remove trailing slash if present

# shellcheck disable=SC2034 # Variable is referenced indirectly
GITHUB_SERVER_URL="${GITHUB_CUSTOM_SERVER_URL:-"https://${GITHUB_DOMAIN}"}"
GITHUB_SERVER_URL="${GITHUB_SERVER_URL%/}" # Remove trailing slash if present

# shellcheck disable=SC2034 # Variable is referenced indirectly
GITHUB_META_URL="${GITHUB_API_URL}/meta"
23 changes: 23 additions & 0 deletions lib/functions/validation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,29 @@ function CheckovConfigurationFileContainsDirectoryOption() {
}
export -f CheckovConfigurationFileContainsDirectoryOption

function ValidateGitHubUrls() {
if [[ -z "${DEFAULT_GITHUB_DOMAIN:-}" ]]; then
error "DEFAULT_GITHUB_DOMAIN is empty."
return 1
fi

if [[ -z "${GITHUB_DOMAIN:-}" ]]; then
error "GITHUB_DOMAIN is empty."
return 1
fi

if [[ "${GITHUB_DOMAIN}" != "${DEFAULT_GITHUB_DOMAIN}" ]]; then
debug "GITHUB_DOMAIN (${GITHUB_DOMAIN}) is not set to the default GitHub domain (${DEFAULT_GITHUB_DOMAIN})"

if [[ -n "${GITHUB_CUSTOM_API_URL:-}" || -n "${GITHUB_CUSTOM_SERVER_URL:-}" ]]; then
error "Cannot specify GITHUB_DOMAIN along with GITHUB_CUSTOM_API_URL or GITHUB_CUSTOM_SERVER_URL. GITHUB_DOMAIN is a convenience variable to automatically set GITHUB_CUSTOM_API_URL and GITHUB_CUSTOM_SERVER_URL. If you need to set GITHUB_CUSTOM_API_URL and GITHUB_CUSTOM_SERVER_URL, unset GITHUB_DOMAIN. Finally, set both GITHUB_CUSTOM_API_URL and GITHUB_CUSTOM_SERVER_URL"
return 1
fi
else
debug "GITHUB_DOMAIN (${GITHUB_DOMAIN}) is set to the default GitHub domain (${DEFAULT_GITHUB_DOMAIN})"
fi
}

function WarnIfVariableIsSet() {
local INPUT_VARIABLE="${1}"
shift
Expand Down
14 changes: 6 additions & 8 deletions lib/linter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ source /action/lib/functions/worker.sh # Source the function script(s)
source /action/lib/functions/setupSSH.sh # Source the function script(s)
# shellcheck source=/dev/null
source /action/lib/functions/githubEvent.sh
# shellcheck source=/dev/null
source /action/lib/functions/githubDomain.sh

if ! ValidateGitHubUrls; then
fatal "GitHub URLs failed validation"
fi

# We want a lowercase value
declare -l RUN_LOCAL
Expand Down Expand Up @@ -119,14 +125,6 @@ FILTER_REGEX_INCLUDE="${FILTER_REGEX_INCLUDE:-""}"
export FILTER_REGEX_INCLUDE
FILTER_REGEX_EXCLUDE="${FILTER_REGEX_EXCLUDE:-""}"
export FILTER_REGEX_EXCLUDE
GITHUB_DOMAIN="${GITHUB_DOMAIN:-"github.com"}"
GITHUB_DOMAIN="${GITHUB_DOMAIN%/}" # Remove trailing slash if present
# GitHub API root url
GITHUB_API_URL="${GITHUB_CUSTOM_API_URL:-"https://api.${GITHUB_DOMAIN}"}"
GITHUB_API_URL="${GITHUB_API_URL%/}" # Remove trailing slash if present
GITHUB_SERVER_URL="https://${GITHUB_DOMAIN}"
# shellcheck disable=SC2034 # Variable is referenced indirectly
GITHUB_META_URL="${GITHUB_API_URL}/meta"
LINTER_RULES_PATH="${LINTER_RULES_PATH:-.github/linters}" # Linter rules directory
# shellcheck disable=SC2034 # Variable is referenced in other scripts
RAW_FILE_ARRAY=() # Array of all files that were changed
Expand Down
53 changes: 53 additions & 0 deletions test/lib/validationTest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,59 @@ function ValidateDeprecatedVariablesTest() {
notice "${FUNCTION_NAME} PASS"
}

function ValidateGitHubUrlsTest() {
FUNCTION_NAME="${FUNCNAME[0]}"

# shellcheck disable=SC2034
DEFAULT_GITHUB_DOMAIN="github.com"

# shellcheck disable=SC2034
GITHUB_DOMAIN=
if ValidateGitHubUrls; then
fatal "Empty GITHUB_DOMAIN should have failed validation"
else
info "Empty GITHUB_DOMAIN passed validation"
fi

# shellcheck disable=SC2034
GITHUB_DOMAIN="github.example.com"
if ! ValidateGitHubUrls; then
fatal "${GITHUB_DOMAIN} should have passed validation"
else
info "${GITHUB_DOMAIN} passed validation"
fi

# shellcheck disable=SC2034
GITHUB_DOMAIN="${DEFAULT_GITHUB_DOMAIN}"
if ! ValidateGitHubUrls; then
fatal "${GITHUB_DOMAIN} should have passed validation"
else
info "${GITHUB_DOMAIN} passed validation"
fi

GITHUB_DOMAIN="github.example.com"
# shellcheck disable=SC2034
GITHUB_CUSTOM_API_URL="github.custom.api.url"
if ValidateGitHubUrls; then
fatal "${GITHUB_DOMAIN} and ${GITHUB_CUSTOM_API_URL} should have failed validation"
else
info "${GITHUB_DOMAIN} and ${GITHUB_CUSTOM_API_URL} failed validation as expected"
fi
unset GITHUB_CUSTOM_API_URL

# shellcheck disable=SC2034
GITHUB_CUSTOM_SERVER_URL="github.custom.server.url"
if ValidateGitHubUrls; then
fatal "${GITHUB_DOMAIN} and ${GITHUB_CUSTOM_SERVER_URL} should have failed validation"
else
info "${GITHUB_DOMAIN} and ${GITHUB_CUSTOM_SERVER_URL} failed validation as expected"
fi
unset GITHUB_CUSTOM_SERVER_URL

notice "${FUNCTION_NAME} PASS"
}

IsUnsignedIntegerSuccessTest
IsUnsignedIntegerFailureTest
ValidateDeprecatedVariablesTest
ValidateGitHubUrlsTest

0 comments on commit f8124fe

Please sign in to comment.