-
Notifications
You must be signed in to change notification settings - Fork 0
/
caller.go
142 lines (105 loc) · 3.82 KB
/
caller.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
// Call Azure DevOps REST API with VSRM endpoint prefix
func callVsrm(endpoint string) string {
endp := fmt.Sprintf("https://vsrm.dev.azure.com/%s/%s/%s", configuration.Organization, configuration.Project, endpoint)
return base(endp)
}
// Call Azure DevOps REST API
func call(endpoint string) string {
endp := fmt.Sprintf("https://dev.azure.com/%s/%s", configuration.Organization, endpoint)
return base(endp)
}
// Base function to make REST API calls
func base(endpoint string) string {
client := &http.Client{}
req, err := http.NewRequest("GET", endpoint, nil)
req.SetBasicAuth(configuration.Username, configuration.Password)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
bodyText, err := ioutil.ReadAll(resp.Body)
s := string(bodyText)
return s
}
// Get project names of organization
func getProjectNames() []string {
projectsResult := call(fmt.Sprintf("_apis/projects?api-version=%s", configuration.ApiVersion))
var res ProjectsResponse
json.Unmarshal([]byte(projectsResult), &res)
var projectNames []string
for _, element := range res.Value {
projectNames = append(projectNames, element.Name)
}
return projectNames
}
// Get Definition struct by name
func getDefinitionByName(name string, definitions []GeneralStruct) GeneralStruct {
for _, element := range definitions {
if element.Name == name {
return element
}
}
return GeneralStruct{}
}
// Get an array of BuildChange by build ID
func getBuildChangesById(id string) []BuildChange {
endpoint := fmt.Sprintf("%s/_apis/build/builds/%s/changes?api-version=%s", configuration.Project, id, configuration.ApiVersion)
buildResult := call(endpoint)
var res BuildChangeResponse
json.Unmarshal([]byte(buildResult), &res)
return res.Value
}
// Get release definitions of a project
func getProjectReleaseDefinitions(project string) []string {
endpoint := fmt.Sprintf("_apis/release/definitions?api-version=%s", configuration.ApiVersion)
buildsResult := callVsrm(endpoint)
var definitionResponse DefinitionsResponse
json.Unmarshal([]byte(buildsResult), &definitionResponse)
var definitionsNames []string
releaseDefinitions = definitionResponse.Value
for _, element := range releaseDefinitions {
definitionsNames = append(definitionsNames, element.Name)
}
return definitionsNames
}
// Get build definitions of a project
func getBuildProjectDefinitions(project string) []string {
endpoint := fmt.Sprintf("%s/_apis/build/definitions?api-version=%s", project, configuration.ApiVersion)
buildsResult := call(endpoint)
var definitionResponse DefinitionsResponse
json.Unmarshal([]byte(buildsResult), &definitionResponse)
var definitionsNames []string
buildDefinitions = definitionResponse.Value
for _, element := range buildDefinitions {
definitionsNames = append(definitionsNames, element.Name)
}
return definitionsNames
}
func getLatestDeployments() []Deployment {
fmt.Println()
fmt.Println(fmt.Sprintf("Getting deployments of project %s and definition %d", configuration.Project, configuration.ReleaseDefinition))
fmt.Println()
endpoint := fmt.Sprintf("_apis/release/deployments?definitionId=%d&api-version=%s", configuration.ReleaseDefinition, configuration.ApiVersion)
result := callVsrm(endpoint)
var response ReleasesResponse
json.Unmarshal([]byte(result), &response)
return response.Value
}
func getLatestBuilds() []Build {
fmt.Println()
fmt.Println(fmt.Sprintf("Getting builds of project %s and definition %d", configuration.Project, configuration.BuildDefinition))
fmt.Println()
endpoint := fmt.Sprintf("%s/_apis/build/builds?definitions=%d&api-version=%s", configuration.Project, configuration.BuildDefinition, configuration.ApiVersion)
result := call(endpoint)
var response BuildsResponse
json.Unmarshal([]byte(result), &response)
return response.Value
}