-
Notifications
You must be signed in to change notification settings - Fork 168
/
svn.go
158 lines (125 loc) · 4.9 KB
/
svn.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
Copyright 2017 caicloud authors. All rights reserved.
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 svn
import (
"fmt"
"strings"
log "github.com/golang/glog"
"github.com/caicloud/cyclone/pkg/api"
"github.com/caicloud/cyclone/pkg/scm"
executil "github.com/caicloud/cyclone/pkg/util/exec"
"github.com/caicloud/cyclone/pkg/util/http/errors"
)
func init() {
if err := scm.RegisterProvider(api.SVN, NewSVN); err != nil {
log.Errorln(err)
}
}
// SVN represents the SCM provider of SVN.
type SVN struct {
scmCfg *api.SCMConfig
}
func NewSVN(scmCfg *api.SCMConfig) (scm.SCMProvider, error) {
return &SVN{scmCfg}, nil
}
func (s *SVN) spilitToken(token string) (string, string, error) {
userPwd := strings.Split(token, api.SVNUsernPwdSep)
if len(userPwd) != 2 {
err := fmt.Errorf("split token fail as the length of userPwd equals %v", len(userPwd))
return "", "", err
}
return userPwd[0], userPwd[1], nil
}
func (s *SVN) GetToken() (string, error) {
return s.scmCfg.Username + api.SVNUsernPwdSep + s.scmCfg.Password, nil
}
func (s *SVN) ListRepos() ([]api.Repository, error) {
return nil, errors.ErrorNotImplemented.Error("list svn repos")
}
func (s *SVN) ListBranches(repo string) ([]string, error) {
return nil, errors.ErrorNotImplemented.Error("list svn branches")
}
func (s *SVN) ListTags(repo string) ([]string, error) {
return nil, errors.ErrorNotImplemented.Error("list svn tags")
}
func (s *SVN) CheckToken() bool {
return true
}
func (s *SVN) NewTagFromLatest(tagName, description, commitID, url string) error {
username, password, err := s.spilitToken(s.scmCfg.Token)
if err != nil {
return err
}
if !strings.Contains(url, "/trunk") {
return fmt.Errorf("not standard SVN dirs, cannot create tag")
}
tagURL := strings.Split(url, "/trunk")[0] + "/tags/" + tagName + "/"
log.Infof("trunk[%s] tag[%s]", url, tagURL)
args := []string{"copy", url, tagURL, "-m", "Cyclone auto tag " + tagName,
"--username", username, "--password", password,
"--non-interactive", "--trust-server-cert-failures", "unknown-ca", "--no-auth-cache"}
output, err := executil.RunInDir("./", "svn", args...)
log.Infof("Command output: %+v", string(output))
if err == nil {
log.Infof("Successfully svn create tag.")
}
return err
}
func (s *SVN) CreateWebHook(repoURL string, webHook *scm.WebHook) error {
return errors.ErrorNotImplemented.Error("create svn webhook")
}
func (s *SVN) DeleteWebHook(repoURL string, webHookUrl string) error {
return errors.ErrorNotImplemented.Error("delete svn webhook")
}
func (s *SVN) GetTemplateType(repo string) (string, error) {
return "", errors.ErrorNotImplemented.Error("get svn template type")
}
// CreateStatus generate a new status for repository.
func (g *SVN) CreateStatus(recordStatus api.Status, targetURL, repoURL, statusesURL string) error {
return errors.ErrorNotImplemented.Error("create svn status")
}
func (s *SVN) GetPullRequestSHA(repoURL string, number int) (string, error) {
return "", errors.ErrorNotImplemented.Error("get pull request sha")
}
// RetrieveRepoInfo retrive svn repository uuid/root-url by command:
//
// 'svn info --show-item repos-uuid(or repos-root-url) --username {user} --password {password}
// --non-interactive --trust-server-cert-failures unknown-ca,cn-mismatch,expired,not-yet-valid,other
// --no-auth-cache {remote-svn-address}'
func (s *SVN) RetrieveRepoInfo(url string) (*api.RepoInfo, error) {
repoInfo := &api.RepoInfo{}
username, password, err := s.spilitToken(s.scmCfg.Token)
if err != nil {
return repoInfo, errors.ErrorUnknownInternal.Error("get svn credential failed")
}
repoInfo.ID, err = retrieveSVNRepoInfo(url, username, password, "repos-uuid")
if err != nil {
return repoInfo, err
}
repoInfo.RootURL, err = retrieveSVNRepoInfo(url, username, password, "repos-root-url")
if err != nil {
return repoInfo, err
}
return repoInfo, nil
}
func retrieveSVNRepoInfo(url, username, password, item string) (string, error) {
args := []string{"info", "--show-item", item, "--username", username, "--password", password, "--non-interactive",
"--trust-server-cert-failures", "unknown-ca,cn-mismatch,expired,not-yet-valid,other", "--no-auth-cache", url}
output, err := executil.RunInDir("./", "svn", args...)
log.Infof("Command output: %+v", string(output))
if err != nil {
log.Errorf("Error when retrive repo %s as : %v", item, err)
return strings.TrimSpace(string(output)), errors.ErrorUnknownInternal.Error(err)
}
return strings.TrimSpace(string(output)), nil
}