forked from rande/gitlab-ci-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
159 lines (119 loc) · 3.21 KB
/
helper.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
159
// Copyright © 2016 Thomas Rabaix <[email protected]>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package gitlab_ci_helper
import (
"bytes"
"errors"
"fmt"
gitlab "github.com/plouc/go-gitlab-client"
"io"
"net/http"
"net/http/httptest"
"os"
"regexp"
"strconv"
"strings"
"testing"
)
type Paths []string
func (p *Paths) String() string {
return fmt.Sprintf("%v", *p)
}
func (p *Paths) Set(value string) error {
*p = append(*p, value)
return nil
}
var SemVersion = regexp.MustCompile("(v|)[0-9]{1,}\\.[0-9]{1,}\\.[0-9]{1,}(-[A-Za-z]*|)")
func GetProject(p string, client *gitlab.Gitlab) (*gitlab.Project, error) {
pId, err := strconv.ParseInt(p, 10, 32)
if err != nil {
// invalid build id, search from a project list
paths := strings.Split(p, "/")
if len(paths) != 2 {
return nil, errors.New("Error: Invalid project format, must be namespace/project-name")
}
projects, _ := client.Projects()
try := ""
for _, p := range projects {
if p.Name == paths[1] {
try = fmt.Sprintf("%s/%s", p.Namespace.Name, p.Name)
}
if p.Name == paths[1] && p.Namespace.Name == paths[0] {
pId = int64(p.Id)
break
}
}
if pId == 0 {
extra := ""
if len(try) > 0 {
extra = fmt.Sprintf("\nDid you mean: %s ?", try)
}
return nil, fmt.Errorf("Unable to find the project: %s/%s.%s", paths[0], paths[1], extra)
}
}
project, err := client.Project(strconv.FormatInt(pId, 10))
if err != nil {
return nil, errors.New("Error: " + err.Error())
}
return project, err
}
func GetBuild(project *gitlab.Project, buildId string, client *gitlab.Gitlab) (*gitlab.Build, error) {
build, err := client.ProjectBuild(strconv.FormatInt(int64(project.Id), 10), buildId)
if err != nil {
return nil, fmt.Errorf("Error: %s.\nUnable to find the build (projectId:%d, buildId:%s)", err.Error(), project.Id, buildId)
}
return build, err
}
func GetEnv(name, deflt string) string {
if len(os.Getenv(name)) == 0 {
return deflt
}
return os.Getenv(name)
}
type FakeRequest struct {
Host string
Path string
Method string
Called int
Response *http.Response
}
func WrapperTestCommand(reqs []*FakeRequest, envs map[string]string, t *testing.T, fn func(ts *httptest.Server)) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Handling a request: %s\n", r.URL.Path)
// dummy matcher
for _, req := range reqs {
if req.Path == r.URL.Path && req.Method == r.Method {
fmt.Printf("Found request: %s\n", req.Path)
req.Called++
for name, values := range req.Response.Header {
for _, v := range values {
w.Header().Add(name, v)
}
}
buf := bytes.NewBuffer([]byte(""))
io.Copy(buf, req.Response.Body)
//bytes.NewBuffer(buf.Bytes()).WriteTo(os.Stdout)
bytes.NewBuffer(buf.Bytes()).WriteTo(w)
req.Response.Body.Close()
return
}
}
t.Error("Unable to find a response to handle the request")
}))
envs["GITLAB_HOST"] = ts.URL
defer func() {
ts.Close()
for name := range envs {
os.Unsetenv(name)
}
}()
for name, value := range envs {
err := os.Setenv(name, value)
if err != nil {
panic(err)
}
}
fn(ts)
}