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 (
|
2024-07-17 23:26:35 +00:00
|
|
|
"context"
|
2019-04-06 19:32:14 +00:00
|
|
|
"crypto/tls"
|
2021-10-28 07:14:16 +00:00
|
|
|
"crypto/x509"
|
2019-04-06 19:32:14 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2024-06-24 22:39:18 +00:00
|
|
|
"os/exec"
|
2023-06-12 23:07:52 +00:00
|
|
|
"strconv"
|
2019-04-06 19:32:14 +00:00
|
|
|
"strings"
|
|
|
|
|
2024-06-24 22:39:18 +00:00
|
|
|
vsc_url "github.com/gitsight/go-vcsurl"
|
2021-10-28 07:14:16 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2024-07-17 23:26:35 +00:00
|
|
|
"github.com/urfave/cli/v3"
|
2019-04-06 19:32:14 +00:00
|
|
|
"golang.org/x/net/proxy"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/woodpecker-go/woodpecker"
|
2019-04-06 19:32:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewClient returns a new client from the CLI context.
|
2024-07-17 23:26:35 +00:00
|
|
|
func NewClient(ctx context.Context, c *cli.Command) (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")
|
2024-05-24 20:35:04 +00:00
|
|
|
socksOff = c.Bool("socks-proxy-off")
|
2021-10-27 19:03:14 +00:00
|
|
|
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 {
|
2024-01-10 19:57:12 +00:00
|
|
|
return nil, fmt.Errorf("you must provide the Woodpecker server address")
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
if len(token) == 0 {
|
2024-01-10 19:57:12 +00:00
|
|
|
return nil, fmt.Errorf("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 {
|
2024-01-10 19:57:12 +00:00
|
|
|
log.Error().Err(err).Msg("failed to find system CA certs")
|
2022-04-05 12:37:02 +00:00
|
|
|
}
|
2019-04-06 19:32:14 +00:00
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
RootCAs: certs,
|
|
|
|
InsecureSkipVerify: skip,
|
|
|
|
}
|
|
|
|
|
|
|
|
config := new(oauth2.Config)
|
2024-07-17 23:26:35 +00:00
|
|
|
client := config.Client(ctx,
|
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
|
|
|
|
2024-05-24 20:35:04 +00:00
|
|
|
if len(socks) != 0 && !socksOff {
|
2019-04-06 19:32:14 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-06-24 22:39:18 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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) {
|
2024-06-24 22:39:18 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2023-06-12 23:07:52 +00:00
|
|
|
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 {
|
2024-03-15 17:00:25 +00:00
|
|
|
before, after, ok := strings.Cut(i, "=")
|
|
|
|
if !ok || before == "" {
|
2019-04-06 19:32:14 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-03-15 17:00:25 +00:00
|
|
|
params[before] = after
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
return params
|
|
|
|
}
|
2024-07-18 18:39:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
ParseStep parses the step id form a string which may either be the step PID (step number) or a step name.
|
|
|
|
These rules apply:
|
|
|
|
|
2024-07-19 07:29:22 +00:00
|
|
|
- Step PID take precedence over step name when searching for a match.
|
2024-07-18 18:39:18 +00:00
|
|
|
- First match is used, when there are multiple steps with the same name.
|
|
|
|
|
|
|
|
Strictly speaking, this is not parsing, but a lookup.
|
|
|
|
*/
|
|
|
|
func ParseStep(client woodpecker.Client, repoID, number int64, stepArg string) (stepID int64, err error) {
|
|
|
|
pipeline, err := client.Pipeline(repoID, number)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2024-07-19 07:29:22 +00:00
|
|
|
stepPID, err := strconv.ParseInt(stepArg, 10, 64)
|
2024-07-18 18:39:18 +00:00
|
|
|
if err == nil {
|
2024-07-19 07:29:22 +00:00
|
|
|
for _, wf := range pipeline.Workflows {
|
|
|
|
for _, step := range wf.Children {
|
|
|
|
if int64(step.PID) == stepPID {
|
|
|
|
return step.ID, nil
|
2024-07-18 18:39:18 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-19 07:29:22 +00:00
|
|
|
}
|
2024-07-18 18:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, wf := range pipeline.Workflows {
|
|
|
|
for _, step := range wf.Children {
|
|
|
|
if step.Name == stepArg {
|
|
|
|
return step.ID, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, fmt.Errorf("no step with number or name '%s' found", stepArg)
|
|
|
|
}
|