From fe6e29b68595815676874fe5db0b240c215f7d48 Mon Sep 17 00:00:00 2001 From: Marco Ferrari Date: Thu, 8 Feb 2024 09:41:07 +0100 Subject: [PATCH] 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. --- .github/workflows/ci.yml | 2 -- README.md | 5 ++--- lib/functions/githubEvent.sh | 12 ++++++++++++ lib/linter.sh | 19 ++++++++++++++++++- test/lib/githubEventTest.sh | 19 +++++++++++++++++++ 5 files changed, 51 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fc1ead84..0b5fa3a7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -109,7 +109,6 @@ jobs: CREATE_LOG_FILE: true VALIDATE_ALL_CODEBASE: false GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - DEFAULT_BRANCH: main GITLEAKS_CONFIG_FILE: .gitleaks-ignore-tests.toml FILTER_REGEX_EXCLUDE: ".*(/test/linters/|CHANGELOG.md).*" RENOVATE_SHAREABLE_CONFIG_PRESET_FILE_NAMES: "default.json,hoge.json" @@ -125,7 +124,6 @@ jobs: env: VALIDATE_ALL_CODEBASE: false GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - DEFAULT_BRANCH: main GITLEAKS_CONFIG_FILE: .gitleaks-ignore-tests.toml FILTER_REGEX_EXCLUDE: ".*(/test/linters/|CHANGELOG.md).*" TYPESCRIPT_STANDARD_TSCONFIG_FILE: ".github/linters/tsconfig.json" diff --git a/README.md b/README.md index 92d7d07c..b977fa34 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,6 @@ To run super-linter as a GitHub Action, you do the following: - name: Super-linter uses: super-linter/super-linter@v6.0.0 # x-release-please-version env: - DEFAULT_BRANCH: main # To report GitHub Actions status checks 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. | | **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`) | -| **DEFAULT_BRANCH** | `master` | The name of the repository default branch. | -| **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_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 on GitHub Actions. | | **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`) | | **EDITORCONFIG_FILE_NAME** | `.ecrc` | Filename for [editorconfig-checker configuration](https://github.com/editorconfig-checker/editorconfig-checker) | diff --git a/lib/functions/githubEvent.sh b/lib/functions/githubEvent.sh index 1e900251..aea09357 100755 --- a/lib/functions/githubEvent.sh +++ b/lib/functions/githubEvent.sh @@ -16,3 +16,15 @@ function GetGithubPushEventCommitCount() { fatal "GITHUB_PUSH_COMMIT_COUNT is not an integer: ${GITHUB_PUSH_COMMIT_COUNT}" 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}" +} diff --git a/lib/linter.sh b/lib/linter.sh index ff6cdd31..64263d8e 100755 --- a/lib/linter.sh +++ b/lib/linter.sh @@ -136,7 +136,6 @@ ValidateBooleanConfigurationVariables ########### # GLOBALS # ########### -DEFAULT_BRANCH="${DEFAULT_BRANCH:-master}" DEFAULT_RULES_LOCATION='/action/lib/.automation' # Default rules files location 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 @@ -385,6 +384,9 @@ GetGitHubVars() { info "--------------------------------------------" info "Gathering GitHub information..." + local GITHUB_REPOSITORY_DEFAULT_BRANCH + GITHUB_REPOSITORY_DEFAULT_BRANCH="master" + if [[ ${RUN_LOCAL} != "false" ]]; then info "RUN_LOCAL has been set to: ${RUN_LOCAL}. Bypassing GitHub Actions variables..." @@ -487,8 +489,23 @@ GetGitHubVars() { else info "Successfully found GITHUB_REPO: ${GITHUB_REPO}" fi + + GITHUB_REPOSITORY_DEFAULT_BRANCH=$(GetGithubRepositoryDefaultBranch "${GITHUB_EVENT_PATH}") 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 [[ ${RUN_LOCAL} == "true" ]]; then diff --git a/test/lib/githubEventTest.sh b/test/lib/githubEventTest.sh index f6059a1b..d6273707 100755 --- a/test/lib/githubEventTest.sh +++ b/test/lib/githubEventTest.sh @@ -43,3 +43,22 @@ function 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