From 0191c3e585bcdefc5263b920a4ab772d24a69758 Mon Sep 17 00:00:00 2001 From: Zbynek Roubalik Date: Mon, 20 Mar 2023 10:29:32 +0100 Subject: [PATCH] feat: PAC - try to read git info from local .git config Signed-off-by: Zbynek Roubalik --- cmd/config_git_set.go | 13 ++- go.mod | 2 +- go.sum | 2 + .../pipelines-as-code/pkg/git/git.go | 79 +++++++++++++++++++ vendor/modules.txt | 3 +- 5 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 vendor/github.com/openshift-pipelines/pipelines-as-code/pkg/git/git.go diff --git a/cmd/config_git_set.go b/cmd/config_git_set.go index f95275d0b..666c8b4ea 100644 --- a/cmd/config_git_set.go +++ b/cmd/config_git_set.go @@ -7,6 +7,8 @@ import ( "github.com/ory/viper" "github.com/spf13/cobra" + "github.com/openshift-pipelines/pipelines-as-code/pkg/git" + "knative.dev/func/pkg/config" fn "knative.dev/func/pkg/functions" "knative.dev/func/pkg/pipelines" @@ -142,10 +144,15 @@ func (c configGitSetConfig) Prompt(f fn.Function) (configGitSetConfig, error) { return c, err } + // try to read git url from the local .git settings + gitInfo := git.GetGitInfo(c.Path) + // prompt if git URL hasn't been set previously if c.GitURL == "" { - // TODO we can try to read git url from the local .git settings url := f.Build.Git.URL + if gitInfo.URL != "" { + url = gitInfo.URL + } if err := survey.AskOne(&survey.Input{ Message: "The URL to Git repository with the function source code:", Default: url, @@ -157,8 +164,10 @@ func (c configGitSetConfig) Prompt(f fn.Function) (configGitSetConfig, error) { // prompt if git revision hasn't been set previously if c.GitRevision == "" { - // TODO we can try to read git url from the local .git settings revision := f.Build.Git.Revision + if gitInfo.Branch != "" { + revision = gitInfo.Branch + } if err := survey.AskOne(&survey.Input{ Message: "The Git branch or tag we are targeting:", Help: "ie: main, refs/tags/*", diff --git a/go.mod b/go.mod index 914eb97fa..770490a68 100644 --- a/go.mod +++ b/go.mod @@ -26,7 +26,7 @@ require ( github.com/heroku/color v0.0.6 github.com/hinshun/vt10x v0.0.0-20220228203356-1ab2cad5fd82 github.com/opencontainers/image-spec v1.1.0-rc2 - github.com/openshift-pipelines/pipelines-as-code v0.17.0 + github.com/openshift-pipelines/pipelines-as-code v0.17.1 github.com/openshift/source-to-image v1.3.1 github.com/ory/viper v1.7.5 github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index 98d57b59f..7d6e34cb8 100644 --- a/go.sum +++ b/go.sum @@ -1334,6 +1334,8 @@ github.com/opencontainers/selinux v1.10.2 h1:NFy2xCsjn7+WspbfZkUd5zyVeisV7VFbPSP github.com/opencontainers/selinux v1.10.2/go.mod h1:cARutUbaUrlRClyvxOICCgKixCs6L05aUsohzA3EkHQ= github.com/openshift-pipelines/pipelines-as-code v0.17.0 h1:RtUZp7sX46lP72UntAoDfhU1iZBiKDueTgLjGzmmUrA= github.com/openshift-pipelines/pipelines-as-code v0.17.0/go.mod h1:5gCkO4y2PEFZ842tbF8376rD386DkoSyyQI3vjdqwq4= +github.com/openshift-pipelines/pipelines-as-code v0.17.1 h1:9+vnu3FMrfMy6R+jhaV6jvmVhDrXRYuGvgHHrCfTdrU= +github.com/openshift-pipelines/pipelines-as-code v0.17.1/go.mod h1:5gCkO4y2PEFZ842tbF8376rD386DkoSyyQI3vjdqwq4= github.com/opentracing-contrib/go-stdlib v1.0.0/go.mod h1:qtI1ogk+2JhVPIXVc6q+NHziSmy2W5GbdQZFUHADCBU= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= diff --git a/vendor/github.com/openshift-pipelines/pipelines-as-code/pkg/git/git.go b/vendor/github.com/openshift-pipelines/pipelines-as-code/pkg/git/git.go new file mode 100644 index 000000000..f42ecaa7a --- /dev/null +++ b/vendor/github.com/openshift-pipelines/pipelines-as-code/pkg/git/git.go @@ -0,0 +1,79 @@ +package git + +import ( + "bytes" + "fmt" + "os/exec" + "strings" +) + +type Info struct { + URL string + TopLevelPath string + SHA string + Branch string +} + +func RunGit(dir string, args ...string) (string, error) { + gitPath, err := exec.LookPath("git") + if err != nil { + //nolint: nilerr + return "", nil + } + c := exec.Command(gitPath, args...) + var output bytes.Buffer + c.Stderr = &output + c.Stdout = &output + // This is the optional working directory. If not set, it defaults to the current + // working directory of the process. + if dir != "" { + c.Dir = dir + } + if err := c.Run(); err != nil { + return "", fmt.Errorf("error running, %s, output: %s error: %w", args, output.String(), err) + } + return output.String(), nil +} + +// GetGitInfo try to detect the current remote for this URL return the origin url transformed and the topdir +func GetGitInfo(dir string) *Info { + gitURL, err := RunGit(dir, "remote", "get-url", "origin") + if err != nil { + gitURL, err = RunGit(dir, "remote", "get-url", "upstream") + if err != nil { + return &Info{} + } + } + gitURL = strings.TrimSpace(gitURL) + gitURL = strings.TrimSuffix(gitURL, ".git") + + // convert github and probably others ssh access format into https + // i think it only fails with bitbucket server + if strings.HasPrefix(gitURL, "git@") { + sp := strings.Split(gitURL, ":") + prefix := strings.ReplaceAll(sp[0], "git@", "https://") + gitURL = fmt.Sprintf("%s/%s", prefix, strings.Join(sp[1:], ":")) + } + + brootdir, err := RunGit(dir, "rev-parse", "--show-toplevel") + if err != nil { + return &Info{} + } + + sha, err := RunGit(dir, "rev-parse", "HEAD") + if err != nil { + return &Info{} + } + + headbranch, err := RunGit(dir, "rev-parse", "--abbrev-ref", "HEAD") + if err != nil { + return &Info{} + } + + return &Info{ + URL: gitURL, + TopLevelPath: strings.TrimSpace(brootdir), + SHA: strings.TrimSpace(sha), + Branch: strings.TrimSpace(headbranch), + } +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 024a4640b..e950efd82 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -880,7 +880,7 @@ github.com/opencontainers/selinux/go-selinux github.com/opencontainers/selinux/go-selinux/label github.com/opencontainers/selinux/pkg/pwalk github.com/opencontainers/selinux/pkg/pwalkdir -# github.com/openshift-pipelines/pipelines-as-code v0.17.0 +# github.com/openshift-pipelines/pipelines-as-code v0.17.1 ## explicit; go 1.18 github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1 @@ -888,6 +888,7 @@ github.com/openshift-pipelines/pipelines-as-code/pkg/cli github.com/openshift-pipelines/pipelines-as-code/pkg/formatting github.com/openshift-pipelines/pipelines-as-code/pkg/generated/clientset/versioned/scheme github.com/openshift-pipelines/pipelines-as-code/pkg/generated/clientset/versioned/typed/pipelinesascode/v1alpha1 +github.com/openshift-pipelines/pipelines-as-code/pkg/git # github.com/openshift/source-to-image v1.3.1 => github.com/boson-project/source-to-image v1.3.2 ## explicit; go 1.13 github.com/openshift/source-to-image/pkg/api