/
svn.go
129 lines (105 loc) · 3.78 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
/*
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 {
//username, password, err := s.spilitToken(scm.Token)
//if err != nil {
// return false
//}
//fmt.Println(username)
//fmt.Println(password)
//
//url := scm.Server
//args := []string{"list", "--username", username, "--password", password,
// "--non-interactive", "--trust-server-cert-failures", "unknown-ca", "--no-auth-cache", url}
//_, err = executil.RunInDir("./", "svn", args...)
//if err != nil {
// log.Errorf("Error when list repos as : %v", err)
// return false
//}
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")
}