mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-11-24 08:02:18 +02:00
Cli get repo from git remote (#3830)
This commit is contained in:
parent
d6aa870d08
commit
e9e512bcdd
@ -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 {
|
||||
|
1
go.mod
1
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
|
||||
|
2
go.sum
2
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=
|
||||
|
Loading…
Reference in New Issue
Block a user