2023-08-10 09:06:00 +00:00
|
|
|
// Copyright 2023 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-04-06 19:32:14 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2021-10-28 07:14:16 +00:00
|
|
|
"crypto/x509"
|
2019-04-06 19:32:14 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2023-06-12 23:07:52 +00:00
|
|
|
"strconv"
|
2019-04-06 19:32:14 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-10-28 07:14:16 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-10-27 19:03:14 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2019-04-06 19:32:14 +00:00
|
|
|
"golang.org/x/net/proxy"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
2021-10-02 22:27:43 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/woodpecker-go/woodpecker"
|
2019-04-06 19:32:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewClient returns a new client from the CLI context.
|
2021-10-02 22:27:43 +00:00
|
|
|
func NewClient(c *cli.Context) (woodpecker.Client, error) {
|
2019-04-06 19:32:14 +00:00
|
|
|
var (
|
2021-10-27 19:03:14 +00:00
|
|
|
skip = c.Bool("skip-verify")
|
|
|
|
socks = c.String("socks-proxy")
|
|
|
|
socksoff = c.Bool("socks-proxy-off")
|
|
|
|
token = c.String("token")
|
|
|
|
server = c.String("server")
|
2019-04-06 19:32:14 +00:00
|
|
|
)
|
|
|
|
server = strings.TrimRight(server, "/")
|
|
|
|
|
|
|
|
// if no server url is provided we can default
|
2021-10-02 22:27:43 +00:00
|
|
|
// to the hosted Woodpecker service.
|
2019-04-06 19:32:14 +00:00
|
|
|
if len(server) == 0 {
|
2021-12-01 13:22:06 +00:00
|
|
|
return nil, fmt.Errorf("Error: you must provide the Woodpecker server address")
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
if len(token) == 0 {
|
2021-12-01 13:22:06 +00:00
|
|
|
return nil, fmt.Errorf("Error: you must provide your Woodpecker access token")
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// attempt to find system CA certs
|
2021-10-28 07:14:16 +00:00
|
|
|
certs, err := x509.SystemCertPool()
|
2022-04-05 12:37:02 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Msgf("failed to find system CA certs: %v", err)
|
|
|
|
}
|
2019-04-06 19:32:14 +00:00
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
RootCAs: certs,
|
|
|
|
InsecureSkipVerify: skip,
|
|
|
|
}
|
|
|
|
|
|
|
|
config := new(oauth2.Config)
|
2021-10-08 16:35:56 +00:00
|
|
|
client := config.Client(
|
2021-10-28 07:14:16 +00:00
|
|
|
c.Context,
|
2019-04-06 19:32:14 +00:00
|
|
|
&oauth2.Token{
|
|
|
|
AccessToken: token,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-10-08 16:35:56 +00:00
|
|
|
trans, _ := client.Transport.(*oauth2.Transport)
|
2019-04-06 19:32:14 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 16:35:56 +00:00
|
|
|
return woodpecker.NewClient(server, client), nil
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseRepo parses the repository owner and name from a string.
|
2023-06-12 23:07:52 +00:00
|
|
|
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
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
2023-06-12 23:07:52 +00:00
|
|
|
|
|
|
|
return strconv.ParseInt(str, 10, 64)
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|