Verified Commit 1ac46b50 authored by Olivier Robardet's avatar Olivier Robardet
Browse files

Makes url option have precedence over detection

parent c43b6035
Pipeline #151651372 passed with stages
in 5 minutes and 6 seconds
......@@ -2,6 +2,7 @@
- Add a `--personal-access-token` option specify a personal access token (e.g. when 2FA is enabled). Thanks @fhitche1 (!13)
- Fixes short command line options that where not working since upgrade to urfave/cli v2 (#12)
- Option `--gitlab-url` now has precedence over detecting URL from the origin remote(#13)
- Code refactoring, CI and build tooling improvement (!15)
# v2.1.0
......
......@@ -134,15 +134,15 @@ Usage:
gitlab-ci-linter [global options] [command [command options]] [PATH]
Global options:
--gitlab-url URL, -u URL root URL of the Gitlab instance to use API (default: "https://gitlab.com") [$GCL_GITLAB_URL]
--ci-file FILE, -f FILE FILE is the relative or absolute path to the gitlab-ci file [$GCL_GITLAB_CI_FILE]
--directory DIR, -d DIR DIR is the directory from where to search for gitlab-ci file and git repository (default: ".") [$GCL_DIRECTORY]
--personal-access-token TOK -p TOK personal access token TOK for accessing repositories when you have 2FA enabled [$GCL_PERSONAL_ACCESS_TOKEN]
--timeout value, -t value timeout in second after which http request to Gitlab API will timeout (and the program will fails) (default: 5) [$GCL_TIMEOUT]
--no-color, -n don't color output. By defaults the output is colorized if a compatible terminal is detected. [$GCL_NOCOLOR]
--verbose, -v verbose mode [$GCL_VERBOSE]
--help, -h show help
--version, -V print the version information
--gitlab-url URL, -u URL root URL of the Gitlab instance to use API (default: auto-detect from remote origin, else "https://gitlab.com") [$GCL_GITLAB_URL]
--ci-file FILE, -f FILE FILE is the relative or absolute path to the gitlab-ci file [$GCL_GITLAB_CI_FILE]
--directory DIR, -d DIR DIR is the directory from where to search for gitlab-ci file and git repository (default: ".") [$GCL_DIRECTORY]
--personal-access-token TOK, -p TOK personal access token TOK for accessing repositories when you have 2FA enabled [$GCL_PERSONAL_ACCESS_TOKEN]
--timeout value, -t value timeout in second after which http request to Gitlab API will timeout (and the program will fails) (default: 5) [$GCL_TIMEOUT]
--no-color, -n don't color output. By defaults the output is colorized if a compatible terminal is detected. (default: false) [$GCL_NOCOLOR]
--verbose, -v verbose mode (default: false) [$GCL_VERBOSE]
--help, -h show help (default: false)
--version print the version information (default: false)
Arguments:
If PATH if given, it will depending of its type on filesystem:
......@@ -202,7 +202,8 @@ Install a pre-commit hook in another git repository:
gitlab-ci-lint -d /path/to/another/git install
```
You don't want https://gitlab.com to be the default Gitlab URL to use?
You don't want https://gitlab.com to be the default Gitlab URL to use? There is no origin remote configured in your repository?
Or you don't want to use this gitlab?
```shell
gitlab-ci-lint --gitlab-url https://gitlab.my.org check
......
......@@ -30,6 +30,44 @@ import (
"strings"
)
func getGitlabRootURL(gitRepoPath string) (string, error) {
// If a gitlab URL was given as parameter, just use it
if gitlabRootURL != "" {
return gitlabRootURL, nil
}
// Else, let's try to guess it, it there is a git repository
if gitRepoPath == "" {
// Warn user that we're defaulting because no git repo was found
yellow := color.New(color.FgYellow).SprintFunc()
fmt.Fprintf(color.Output, yellow("No GIT repository found, using default Gitlab API '%s'\n"), defaultGitlabRootURL)
return defaultGitlabRootURL, nil
}
// Extract origin remote url from repository config
remoteURL, err := getGitOriginRemoteURL(gitRepoPath)
if err != nil {
return defaultGitlabRootURL, fmt.Errorf("Failed to find origin remote url in repository: %s", err)
}
// Check if we can use the origin remote url
if remoteURL != "" {
// Guess gitlab url based on remote url
localGitlabRootURL, err := guessGitlabAPIFromGitRemoteURL(remoteURL)
if err != nil {
return defaultGitlabRootURL, fmt.Errorf("No valid and responding Gitlab API URL found from repository's origin remote: %s", err)
}
return localGitlabRootURL, nil
}
// Warn user that we're defaulting because no origin remote was found
yellow := color.New(color.FgYellow).SprintFunc()
fmt.Fprintf(color.Output, yellow("No origin remote found in repository, using default Gitlab API '%s'\n"), gitlabRootURL)
return defaultGitlabRootURL, nil
}
// 'check' command of the program, which is the main action
// It aims to validate the syntax of a .gitlab-ci.yml file, using the CI Lint API of a Gitlab instance
// First it search for a gitlab-ci file if no one is given
......@@ -68,29 +106,9 @@ func commandCheck(c *cli.Context) error {
gitRepoPath, _ = findGitRepo(directoryRoot)
}
if gitRepoPath == "" {
// Warn user that we're defaulting because no git repo was found
yellow := color.New(color.FgYellow).SprintFunc()
fmt.Fprintf(color.Output, yellow("No GIT repository found, using default Gitlab API '%s'\n"), gitlabRootURL)
} else {
// Extract origin remote url from repository config
remoteURL, err := getGitOriginRemoteURL(gitRepoPath)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Failed to find origin remote url in repository: %s", err), 5)
}
// Check if we can use the origin remote url
if remoteURL != "" {
// Guess gitlab url based on remote url
gitlabRootURL, err = guessGitlabAPIFromGitRemoteURL(remoteURL)
if err != nil {
return cli.NewExitError(fmt.Sprintf("No valid and responding Gitlab API URL found from repository's origin remote: %s", err), 5)
}
} else {
// Warn user that we're defaulting because no origin remote was found
yellow := color.New(color.FgYellow).SprintFunc()
fmt.Fprintf(color.Output, yellow("No origin remote found in repository, using default Gitlab API '%s'\n"), gitlabRootURL)
}
localGitlabRootURL, err := getGitlabRootURL(gitRepoPath)
if err != nil {
return cli.NewExitError(err, 5)
}
fmt.Printf("Validating %s... ", relativeGitlabCiFilePath)
......@@ -105,7 +123,7 @@ func commandCheck(c *cli.Context) error {
return cli.NewExitError(fmt.Sprintf("Error while reading '%s' file content: %s", relativeGitlabCiFilePath, err), 5)
}
status, errorMessages, err := lintGitlabCIUsingAPI(gitlabRootURL, string(ciFileContent))
status, errorMessages, err := lintGitlabCIUsingAPI(localGitlabRootURL, string(ciFileContent))
if err != nil {
return cli.NewExitError(fmt.Sprintf("Error querying Gitlab API '%s' for CI lint: %s", gitlabRootURL, err), 5)
}
......
......@@ -25,6 +25,7 @@ import (
"github.com/fatih/color"
"github.com/urfave/cli/v2"
"math"
"net/url"
"os"
"path/filepath"
)
......@@ -129,8 +130,8 @@ Usage:
&cli.StringFlag{
Name: "gitlab-url",
Aliases: []string{"u"},
Value: defaultGitlabRootURL,
Usage: "root `URL` of the Gitlab instance to use API",
Value: "",
Usage: fmt.Sprintf("root `URL` of the Gitlab instance to use API (default: auto-detect from remote origin, else \"%s\")", defaultGitlabRootURL),
EnvVars: []string{"GCL_GITLAB_URL"},
Destination: &gitlabRootURL,
},
......@@ -250,6 +251,17 @@ Usage:
}
}
if gitlabRootURL != "" {
u, err := url.Parse(gitlabRootURL)
if err != nil {
cli.NewExitError(fmt.Sprintf("Unable to parse gitlab root URL '%s': %s", gitlabRootURL, err), 1)
}
if u.Scheme == "" {
u.Scheme = "http"
}
gitlabRootURL = u.String()
}
return nil
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment