feat: PAC - try to read git info from local .git config (#1635)

Signed-off-by: Zbynek Roubalik <zroubalik@gmail.com>
This commit is contained in:
Zbynek Roubalik 2023-03-22 14:10:54 +01:00 committed by GitHub
parent 8eeff81580
commit 85d3d0206e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 95 additions and 6 deletions

View File

@ -7,6 +7,8 @@ import (
"github.com/ory/viper"
"github.com/spf13/cobra"
pacgit "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 := pacgit.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/*",

2
go.mod
View File

@ -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

4
go.sum
View File

@ -1332,8 +1332,8 @@ github.com/opencontainers/selinux v1.8.2/go.mod h1:MUIHuUEvKB1wtJjQdOyYRgOnLD2xA
github.com/opencontainers/selinux v1.10.0/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI=
github.com/opencontainers/selinux v1.10.2 h1:NFy2xCsjn7+WspbfZkUd5zyVeisV7VFbPSP96+8/ha4=
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=

View File

@ -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),
}
}

3
vendor/modules.txt vendored
View File

@ -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