woodpecker/cli/internal/util.go
Anbraten ff01a9ff1d
Access repos by their ids (#1691)
closes #1295 
closes #648

# TODO
- [x] add new routes with `:repoID`
- [x] load repo in middleware using `:repoID` if present
- [x] update UI routes `:owner/:name` to `:repoID`
- [x] load repos using id in UI
- [x] add lookup endpoint `:owner/:name` to `:repoID`
- [x] redirect `:owner/:name` to `:repoID` in UI
- [x] use badge with `:repoID` route in UI
- [x] update `woodpecker-go`
- [x] check cli
- [x] add migrations / deprecation notes
- [x] check if #648 got solved directly
- [x] Test
  - [x] create repo
  - [x] repo pages
  - [x] ui redirects
  - [x] forge status links
2023-06-13 01:07:52 +02:00

103 lines
2.3 KiB
Go

package internal
import (
"crypto/tls"
"crypto/x509"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"golang.org/x/net/proxy"
"golang.org/x/oauth2"
"github.com/woodpecker-ci/woodpecker/woodpecker-go/woodpecker"
)
// NewClient returns a new client from the CLI context.
func NewClient(c *cli.Context) (woodpecker.Client, error) {
var (
skip = c.Bool("skip-verify")
socks = c.String("socks-proxy")
socksoff = c.Bool("socks-proxy-off")
token = c.String("token")
server = c.String("server")
)
server = strings.TrimRight(server, "/")
// if no server url is provided we can default
// to the hosted Woodpecker service.
if len(server) == 0 {
return nil, fmt.Errorf("Error: you must provide the Woodpecker server address")
}
if len(token) == 0 {
return nil, fmt.Errorf("Error: you must provide your Woodpecker access token")
}
// attempt to find system CA certs
certs, err := x509.SystemCertPool()
if err != nil {
log.Error().Msgf("failed to find system CA certs: %v", err)
}
tlsConfig := &tls.Config{
RootCAs: certs,
InsecureSkipVerify: skip,
}
config := new(oauth2.Config)
client := config.Client(
c.Context,
&oauth2.Token{
AccessToken: token,
},
)
trans, _ := client.Transport.(*oauth2.Transport)
if len(socks) != 0 && !socksoff {
dialer, err := proxy.SOCKS5("tcp", socks, nil, proxy.Direct)
if err != nil {
return nil, err
}
trans.Base = &http.Transport{
TLSClientConfig: tlsConfig,
Proxy: http.ProxyFromEnvironment,
Dial: dialer.Dial,
}
} else {
trans.Base = &http.Transport{
TLSClientConfig: tlsConfig,
Proxy: http.ProxyFromEnvironment,
}
}
return woodpecker.NewClient(server, client), nil
}
// ParseRepo parses the repository owner and name from a string.
func ParseRepo(client woodpecker.Client, str string) (repoID int64, err error) {
if strings.Contains(str, "/") {
repo, err := client.RepoLookup(str)
if err != nil {
return 0, err
}
return repo.ID, nil
}
return strconv.ParseInt(str, 10, 64)
}
// ParseKeyPair parses a key=value pair.
func ParseKeyPair(p []string) map[string]string {
params := map[string]string{}
for _, i := range p {
parts := strings.SplitN(i, "=", 2)
if len(parts) != 2 {
continue
}
params[parts[0]] = parts[1]
}
return params
}