feat: automatically set the default branch (#5242)

Get the default branch from the GitHub Actions event payload when
running on GitHub Actions. Default to 'master', as before, otherwise.
This commit is contained in:
Marco Ferrari 2024-02-08 09:41:07 +01:00 committed by GitHub
parent 38edbe557a
commit fe6e29b685
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 51 additions and 6 deletions

View file

@ -109,7 +109,6 @@ jobs:
CREATE_LOG_FILE: true CREATE_LOG_FILE: true
VALIDATE_ALL_CODEBASE: false VALIDATE_ALL_CODEBASE: false
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DEFAULT_BRANCH: main
GITLEAKS_CONFIG_FILE: .gitleaks-ignore-tests.toml GITLEAKS_CONFIG_FILE: .gitleaks-ignore-tests.toml
FILTER_REGEX_EXCLUDE: ".*(/test/linters/|CHANGELOG.md).*" FILTER_REGEX_EXCLUDE: ".*(/test/linters/|CHANGELOG.md).*"
RENOVATE_SHAREABLE_CONFIG_PRESET_FILE_NAMES: "default.json,hoge.json" RENOVATE_SHAREABLE_CONFIG_PRESET_FILE_NAMES: "default.json,hoge.json"
@ -125,7 +124,6 @@ jobs:
env: env:
VALIDATE_ALL_CODEBASE: false VALIDATE_ALL_CODEBASE: false
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DEFAULT_BRANCH: main
GITLEAKS_CONFIG_FILE: .gitleaks-ignore-tests.toml GITLEAKS_CONFIG_FILE: .gitleaks-ignore-tests.toml
FILTER_REGEX_EXCLUDE: ".*(/test/linters/|CHANGELOG.md).*" FILTER_REGEX_EXCLUDE: ".*(/test/linters/|CHANGELOG.md).*"
TYPESCRIPT_STANDARD_TSCONFIG_FILE: ".github/linters/tsconfig.json" TYPESCRIPT_STANDARD_TSCONFIG_FILE: ".github/linters/tsconfig.json"

View file

@ -113,7 +113,6 @@ To run super-linter as a GitHub Action, you do the following:
- name: Super-linter - name: Super-linter
uses: super-linter/super-linter@v6.0.0 # x-release-please-version uses: super-linter/super-linter@v6.0.0 # x-release-please-version
env: env:
DEFAULT_BRANCH: main
# To report GitHub Actions status checks # To report GitHub Actions status checks
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
... ...
@ -167,8 +166,8 @@ You can configure super-linter using the following environment variables:
| **CHECKOV_FILE_NAME** | `.checkov.yaml` | Configuration filename for Checkov. | | **CHECKOV_FILE_NAME** | `.checkov.yaml` | Configuration filename for Checkov. |
| **CREATE_LOG_FILE** | `false` | If set to `true`, it creates the log file. You can set the log filename using the `LOG_FILE` environment variable. This overrides any existing log files. | | **CREATE_LOG_FILE** | `false` | If set to `true`, it creates the log file. You can set the log filename using the `LOG_FILE` environment variable. This overrides any existing log files. |
| **CSS_FILE_NAME** | `.stylelintrc.json` | Filename for [Stylelint configuration](https://github.com/stylelint/stylelint) (ex: `.stylelintrc.yml`, `.stylelintrc.yaml`) | | **CSS_FILE_NAME** | `.stylelintrc.json` | Filename for [Stylelint configuration](https://github.com/stylelint/stylelint) (ex: `.stylelintrc.yml`, `.stylelintrc.yaml`) |
| **DEFAULT_BRANCH** | `master` | The name of the repository default branch. | | **DEFAULT_BRANCH** | Default repository branch when running on GitHub Actions, `master` otherwise | The name of the repository default branch. There's no need to configure this variable when running on GitHub Actions |
| **DEFAULT_WORKSPACE** | `/tmp/lint` | The location containing files to lint if you are running locally. Defaults to `GITHUB_WORKSPACE` when running in GitHub Actions. There's no need to configure this variable when running in GitHub Actions. | | **DEFAULT_WORKSPACE** | `/tmp/lint` | The location containing files to lint if you are running locally. Defaults to `GITHUB_WORKSPACE` when running in GitHub Actions. There's no need to configure this variable when running on GitHub Actions. |
| **DISABLE_ERRORS** | `false` | Flag to have the linter complete with exit code 0 even if errors were detected. | | **DISABLE_ERRORS** | `false` | Flag to have the linter complete with exit code 0 even if errors were detected. |
| **DOCKERFILE_HADOLINT_FILE_NAME** | `.hadolint.yaml` | Filename for [hadolint configuration](https://github.com/hadolint/hadolint) (ex: `.hadolintlintrc.yaml`) | | **DOCKERFILE_HADOLINT_FILE_NAME** | `.hadolint.yaml` | Filename for [hadolint configuration](https://github.com/hadolint/hadolint) (ex: `.hadolintlintrc.yaml`) |
| **EDITORCONFIG_FILE_NAME** | `.ecrc` | Filename for [editorconfig-checker configuration](https://github.com/editorconfig-checker/editorconfig-checker) | | **EDITORCONFIG_FILE_NAME** | `.ecrc` | Filename for [editorconfig-checker configuration](https://github.com/editorconfig-checker/editorconfig-checker) |

View file

@ -16,3 +16,15 @@ function GetGithubPushEventCommitCount() {
fatal "GITHUB_PUSH_COMMIT_COUNT is not an integer: ${GITHUB_PUSH_COMMIT_COUNT}" fatal "GITHUB_PUSH_COMMIT_COUNT is not an integer: ${GITHUB_PUSH_COMMIT_COUNT}"
fi fi
} }
function GetGithubRepositoryDefaultBranch() {
local GITHUB_EVENT_FILE_PATH
GITHUB_EVENT_FILE_PATH="${1}"
local GITHUB_REPOSITORY_DEFAULT_BRANCH
if ! GITHUB_REPOSITORY_DEFAULT_BRANCH=$(jq -r '.repository.default_branch' <"${GITHUB_EVENT_FILE_PATH}"); then
fatal "Failed to initialize GITHUB_REPOSITORY_DEFAULT_BRANCH. Output: ${GITHUB_REPOSITORY_DEFAULT_BRANCH}"
fi
echo "${GITHUB_REPOSITORY_DEFAULT_BRANCH}"
}

View file

@ -136,7 +136,6 @@ ValidateBooleanConfigurationVariables
########### ###########
# GLOBALS # # GLOBALS #
########### ###########
DEFAULT_BRANCH="${DEFAULT_BRANCH:-master}"
DEFAULT_RULES_LOCATION='/action/lib/.automation' # Default rules files location DEFAULT_RULES_LOCATION='/action/lib/.automation' # Default rules files location
DEFAULT_SUPER_LINTER_WORKSPACE="/tmp/lint" # Fall-back value for the workspace 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_WORKSPACE="${DEFAULT_WORKSPACE:-${DEFAULT_SUPER_LINTER_WORKSPACE}}" # Default workspace if running locally
@ -385,6 +384,9 @@ GetGitHubVars() {
info "--------------------------------------------" info "--------------------------------------------"
info "Gathering GitHub information..." info "Gathering GitHub information..."
local GITHUB_REPOSITORY_DEFAULT_BRANCH
GITHUB_REPOSITORY_DEFAULT_BRANCH="master"
if [[ ${RUN_LOCAL} != "false" ]]; then 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..."
@ -487,8 +489,23 @@ GetGitHubVars() {
else else
info "Successfully found GITHUB_REPO: ${GITHUB_REPO}" info "Successfully found GITHUB_REPO: ${GITHUB_REPO}"
fi fi
GITHUB_REPOSITORY_DEFAULT_BRANCH=$(GetGithubRepositoryDefaultBranch "${GITHUB_EVENT_PATH}")
fi fi
if [ -z "${GITHUB_REPOSITORY_DEFAULT_BRANCH}" ]; then
fatal "Failed to get GITHUB_REPOSITORY_DEFAULT_BRANCH"
else
debug "Successfully detected the default branch for this repository: ${GITHUB_REPOSITORY_DEFAULT_BRANCH}"
fi
DEFAULT_BRANCH="${DEFAULT_BRANCH:-${GITHUB_REPOSITORY_DEFAULT_BRANCH}}"
if [[ "${DEFAULT_BRANCH}" != "${GITHUB_REPOSITORY_DEFAULT_BRANCH}" ]]; then
debug "The default branch for this repository was set to ${GITHUB_REPOSITORY_DEFAULT_BRANCH}, but it was explicitly overridden using the DEFAULT_BRANCH variable, and set to: ${DEFAULT_BRANCH}"
fi
info "The default branch for this repository is set to: ${DEFAULT_BRANCH}"
if [ "${MULTI_STATUS}" == "true" ]; then if [ "${MULTI_STATUS}" == "true" ]; then
if [[ ${RUN_LOCAL} == "true" ]]; then if [[ ${RUN_LOCAL} == "true" ]]; then

View file

@ -43,3 +43,22 @@ function GetGithubPushEventCommitCountTest() {
} }
GetGithubPushEventCommitCountTest GetGithubPushEventCommitCountTest
function GetGithubRepositoryDefaultBranchTest() {
local GITHUB_REPOSITORY_DEFAULT_BRANCH
GITHUB_REPOSITORY_DEFAULT_BRANCH=$(GetGithubRepositoryDefaultBranch "test/data/github-event/github-event-push.json")
debug "GITHUB_REPOSITORY_DEFAULT_BRANCH: ${GITHUB_REPOSITORY_DEFAULT_BRANCH}"
local EXPECTED_GITHUB_REPOSITORY_DEFAULT_BRANCH
EXPECTED_GITHUB_REPOSITORY_DEFAULT_BRANCH="main"
if [ "${GITHUB_REPOSITORY_DEFAULT_BRANCH}" != "${EXPECTED_GITHUB_REPOSITORY_DEFAULT_BRANCH}" ]; then
fatal "GITHUB_REPOSITORY_DEFAULT_BRANCH (${GITHUB_REPOSITORY_DEFAULT_BRANCH}) is not equal to: ${EXPECTED_GITHUB_REPOSITORY_DEFAULT_BRANCH}"
fi
FUNCTION_NAME="${FUNCNAME[0]}"
notice "${FUNCTION_NAME} PASS"
}
GetGithubRepositoryDefaultBranchTest