From e9e512bcdd114f54d903b65ae06a97eea3988e52 Mon Sep 17 00:00:00 2001 From: Anbraten <6918444+anbraten@users.noreply.github.com> Date: Tue, 25 Jun 2024 00:39:18 +0200 Subject: [PATCH] Cli get repo from git remote (#3830) --- cli/internal/util.go | 46 ++++++++++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ 3 files changed, 49 insertions(+) diff --git a/cli/internal/util.go b/cli/internal/util.go index 5532c62e8..001752ed3 100644 --- a/cli/internal/util.go +++ b/cli/internal/util.go @@ -19,9 +19,11 @@ import ( "crypto/x509" "fmt" "net/http" + "os/exec" "strconv" "strings" + vsc_url "github.com/gitsight/go-vcsurl" "github.com/rs/zerolog/log" "github.com/urfave/cli/v2" "golang.org/x/net/proxy" @@ -90,8 +92,52 @@ func NewClient(c *cli.Context) (woodpecker.Client, error) { return woodpecker.NewClient(server, client), nil } +func getRepoFromGit(remoteName string) (string, error) { + cmd := exec.Command("git", "remote", "get-url", remoteName) + stdout, err := cmd.Output() + if err != nil { + return "", fmt.Errorf("could not get remote url: %w", err) + } + + gitRemote := strings.TrimSpace(string(stdout)) + + log.Debug().Str("git-remote", gitRemote).Msg("extracted remote url from git") + + if len(gitRemote) == 0 { + return "", fmt.Errorf("no repository provided") + } + + u, err := vsc_url.Parse(gitRemote) + if err != nil { + return "", fmt.Errorf("could not parse git remote url: %w", err) + } + + repoFullName := u.FullName + log.Debug().Str("repo", repoFullName).Msg("extracted repository from remote url") + + return repoFullName, nil +} + // ParseRepo parses the repository owner and name from a string. func ParseRepo(client woodpecker.Client, str string) (repoID int64, err error) { + if str == "" { + str, err = getRepoFromGit("upstream") + if err != nil { + log.Debug().Err(err).Msg("could not get repository from git upstream remote") + } + } + + if str == "" { + str, err = getRepoFromGit("origin") + if err != nil { + log.Debug().Err(err).Msg("could not get repository from git origin remote") + } + } + + if str == "" { + return 0, fmt.Errorf("no repository provided") + } + if strings.Contains(str, "/") { repo, err := client.RepoLookup(str) if err != nil { diff --git a/go.mod b/go.mod index c5fe5c976..2545cf271 100644 --- a/go.mod +++ b/go.mod @@ -27,6 +27,7 @@ require ( github.com/franela/goblin v0.0.0-20211003143422-0a4f594942bf github.com/fsnotify/fsnotify v1.7.0 github.com/gin-gonic/gin v1.10.0 + github.com/gitsight/go-vcsurl v1.0.1 github.com/go-ap/httpsig v0.0.0-20221203064646-3647b4d88fdf github.com/go-sql-driver/mysql v1.8.1 github.com/golang-jwt/jwt/v5 v5.2.1 diff --git a/go.sum b/go.sum index 98eb54196..03a965147 100644 --- a/go.sum +++ b/go.sum @@ -128,6 +128,8 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= +github.com/gitsight/go-vcsurl v1.0.1 h1:wkijKsbVg9R2IBP97U7wOANeIW9WJJKkBwS9XqllzWo= +github.com/gitsight/go-vcsurl v1.0.1/go.mod h1:qRFdKDa/0Lh9MT0xE+qQBYZ/01+mY1H40rZUHR24X9U= github.com/go-ap/httpsig v0.0.0-20221203064646-3647b4d88fdf h1:Ab5yBsD/dXhFmgf2hX7T/YYr+VK0Df7SrIxyNztT9YE= github.com/go-ap/httpsig v0.0.0-20221203064646-3647b4d88fdf/go.mod h1:+4SUDMvPlRMUPW5PlMTbxj3U5a4fWasBIbakUw7Kp6c= github.com/go-fed/httpsig v1.1.0 h1:9M+hb0jkEICD8/cAiNqEB66R87tTINszBRTjwjQzWcI=