forked from tektoncd/pipeline
/
scm.go
143 lines (124 loc) · 3.8 KB
/
scm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Copyright 2019 The Tekton 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.
*/
package pullrequest
import (
"context"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"golang.org/x/oauth2"
"github.com/jenkins-x/go-scm/scm/driver/github"
"github.com/jenkins-x/go-scm/scm/driver/gitlab"
"go.uber.org/zap"
)
func NewSCMHandler(logger *zap.SugaredLogger, raw, provider, token string) (*Handler, error) {
u, err := url.Parse(raw)
if err != nil {
return nil, err
}
if provider == "" {
p, err := guessProvider(u)
if err != nil {
return nil, err
}
provider = p
}
logger = logger.With(zap.String("provider", provider))
var handler *Handler
switch provider {
case "github":
handler, err = githubHandlerFromURL(u, token, logger)
case "gitlab":
handler, err = gitlabHandlerFromURL(u, token, logger)
default:
return nil, fmt.Errorf("unsupported pr url: %s", raw)
}
return handler, err
}
func githubHandlerFromURL(u *url.URL, token string, logger *zap.SugaredLogger) (*Handler, error) {
split := strings.Split(u.Path, "/")
if len(split) < 5 {
return nil, fmt.Errorf("could not determine PR from URL: %v", u)
}
owner, repo, pr := split[1], split[2], split[4]
prNumber, err := strconv.Atoi(pr)
if err != nil {
return nil, fmt.Errorf("error parsing PR number: %s", pr)
}
logger = logger.With(
zap.String("owner", owner),
zap.String("repo", repo),
zap.String("pr", pr),
)
client := github.NewDefault()
ownerRepo := fmt.Sprintf("%s/%s", owner, repo)
h := NewHandler(logger, client, ownerRepo, prNumber)
if token != "" {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
h.client.Client = oauth2.NewClient(context.Background(), ts)
}
return h, nil
}
func gitlabHandlerFromURL(u *url.URL, token string, logger *zap.SugaredLogger) (*Handler, error) {
// The project name can be multiple /'s deep, so split on / and work from right to left.
split := strings.Split(u.Path, "/")
// The PR number should be the last element.
last := len(split) - 1
prNum := split[last]
prInt, err := strconv.Atoi(prNum)
if err != nil {
return nil, fmt.Errorf("unable to parse pr as number from %s", u)
}
// Next we sanity check that this is a correct url. The next to last element should be "merge_requests"
if split[last-1] != "merge_requests" {
return nil, fmt.Errorf("invalid gitlab url: %s", u)
}
// Next, we rejoin everything else into the project field.
project := strings.Join(split[1:last-1], "/")
logger = logger.With(
zap.String("project", project),
zap.String("pr", prNum),
)
client := gitlab.NewDefault()
if token != "" {
client.Client = &http.Client{
Transport: &gitlabClient{
token: token,
transport: http.DefaultTransport,
},
}
}
return NewHandler(logger, client, project, prInt), nil
}
// gitlab client wraps a normal http client, adding support for private-token auth.
type gitlabClient struct {
token string
transport http.RoundTripper
}
func (g *gitlabClient) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Add("Private-Token", g.token)
return g.transport.RoundTrip(r)
}
func guessProvider(u *url.URL) (string, error) {
switch {
case strings.Contains(u.Hostname(), "github"):
return "github", nil
case strings.Contains(u.Hostname(), "gitlab"):
return "gitlab", nil
}
return "", fmt.Errorf("unable to guess scm provider from url: %s", u)
}