Skip to content

Commit

Permalink
feat: automatically set the default branch (#5242)
Browse files Browse the repository at this point in the history
Get the default branch from the GitHub Actions event payload when
running on GitHub Actions. Default to 'master', as before, otherwise.
  • Loading branch information
ferrarimarco committed Feb 8, 2024
1 parent 38edbe5 commit fe6e29b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
...
Expand Down Expand Up @@ -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) |
Expand Down
12 changes: 12 additions & 0 deletions lib/functions/githubEvent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}
19 changes: 18 additions & 1 deletion lib/linter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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..."

Expand Down Expand Up @@ -487,7 +489,22 @@ 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

Expand Down
19 changes: 19 additions & 0 deletions test/lib/githubEventTest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fe6e29b

Please sign in to comment.