Skip to content

Commit

Permalink
Cli get repo from git remote (#3830)
Browse files Browse the repository at this point in the history
  • Loading branch information
anbraten authored Jun 24, 2024
1 parent d6aa870 commit e9e512b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
46 changes: 46 additions & 0 deletions cli/internal/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit e9e512b

Please sign in to comment.