-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtfs.go
More file actions
126 lines (100 loc) · 3.2 KB
/
tfs.go
File metadata and controls
126 lines (100 loc) · 3.2 KB
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
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"crypto/tls"
ntlm "github.com/vadimi/go-http-ntlm"
)
// TFSServer serves as an interface to abstract away the differences between hosted and on-prem servers.
type TFSServer interface {
BuildsURL() string
}
// TFSBuildDefinition contains the build definition to monitor on the server.
type TFSBuildDefinition struct {
Collection string
Project string
BuildID string
}
// TFSCredentials contains the information required to authenticate to the server.
type TFSCredentials struct {
Username string
Password string
Domain string
}
// TFSOnPremServer contains the information required to access an on-premise server.
type TFSOnPremServer struct {
Instance string
Definition TFSBuildDefinition
}
// TFSHostedServer contains the information required to access a VSO instance.
type TFSHostedServer struct {
Account string
Definition TFSBuildDefinition
}
// TFSBuildResponse contains the results of the last n builds.
type TFSBuildResponse struct {
Count int
Value []TFSBuildStatus
}
// TFSBuildStatus contains the result for a single build.
type TFSBuildStatus struct {
Status string
Result string
}
// BuildsURL generates the URL for accessing an on-prem server.
func (srv TFSOnPremServer) BuildsURL() string {
baseURL := fmt.Sprintf("https://%s/%s/%s/_apis/build/builds", srv.Instance, url.PathEscape(srv.Definition.Collection), url.PathEscape(srv.Definition.Project))
query := url.Values{}
query.Add("api-version", "2.0")
query.Add("$top", "8")
query.Add("definitions", srv.Definition.BuildID)
query.Add("statusFilter", url.QueryEscape("inProgress,completed"))
return baseURL + "?" + query.Encode()
}
// BuildsURL generates the URL for accessing a VSO instance.
func (srv TFSHostedServer) BuildsURL() string {
baseURL := fmt.Sprintf("https://%s.visualstudio.com/DefaultCollection/%s/_apis/build/builds", srv.Account, url.PathEscape(srv.Definition.Project))
query := url.Values{}
query.Add("api-version", "2.0")
query.Add("$top", "8")
query.Add("definitions", srv.Definition.BuildID)
query.Add("statusFilter", url.QueryEscape("inProgress,completed"))
return baseURL + "?" + query.Encode()
}
// FetchBuild makes the http request to the specified server using the specified credentials.
func FetchBuild(srv TFSServer, cred TFSCredentials) ([]TFSBuildStatus, error) {
if srv == nil {
return make([]TFSBuildStatus, 0), nil
}
client := http.DefaultClient
switch srv.(type) {
case TFSOnPremServer:
client = &http.Client{
Transport: &ntlm.NtlmTransport{
Domain: cred.Domain,
User: cred.Username,
Password: cred.Password,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
break
case TFSHostedServer:
client = http.DefaultClient
break
}
fmt.Println("URL:", srv.BuildsURL())
req, _ := http.NewRequest("GET", srv.BuildsURL(), nil)
req.SetBasicAuth(cred.Username, cred.Password)
res, err := client.Do(req)
if err != nil {
return make([]TFSBuildStatus, 0), err
} else if res.StatusCode != http.StatusOK {
return make([]TFSBuildStatus, 0), nil
}
var response TFSBuildResponse
dec := json.NewDecoder(res.Body)
dec.Decode(&response)
return response.Value, nil
}