Skip to content

Commit 377e9ee

Browse files
committed
Merge branch 'fatal10110-feature/queue_build'
2 parents 7f662b7 + cbdb9a6 commit 377e9ee

File tree

5 files changed

+104
-6
lines changed

5 files changed

+104
-6
lines changed

azuredevops/azuredevops.go

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package azuredevops
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
67
"io"
@@ -78,6 +79,15 @@ func (c *Client) NewRequest(method, URL string, body interface{}) (*http.Request
7879
// and simply uses the base https://%s.visualstudio.com base URL
7980
func (c *Client) NewBaseRequest(method, URL string, body interface{}) (*http.Request, error) {
8081
var buf io.ReadWriter
82+
if body != nil {
83+
buf = new(bytes.Buffer)
84+
enc := json.NewEncoder(buf)
85+
enc.SetEscapeHTML(false)
86+
err := enc.Encode(body)
87+
if err != nil {
88+
return nil, err
89+
}
90+
}
8191

8292
request, err := http.NewRequest(method, c.BaseURL+URL, buf)
8393

azuredevops/azuredevops_test.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package azuredevops_test
22

33
import (
44
"fmt"
5+
"io/ioutil"
56
"net/http"
67
"net/http/httptest"
78
"net/url"
@@ -42,7 +43,7 @@ func setup() (client *azuredevops.Client, mux *http.ServeMux, serverURL string,
4243
// The client being tested and is configured to use test server.
4344
client = azuredevops.NewClient("AZURE_DEVOPS_Account", "AZURE_DEVOPS_Project", "AZURE_DEVOPS_TOKEN")
4445

45-
url, _ := url.Parse(server.URL + baseURLPath + "/")
46+
url, _ := url.Parse(server.URL + baseURLPath)
4647
client.BaseURL = url.String()
4748
return client, mux, server.URL, server.Close
4849
}
@@ -53,6 +54,16 @@ func testMethod(t *testing.T, r *http.Request, want string) {
5354
}
5455
}
5556

57+
func testBody(t *testing.T, r *http.Request, want string) {
58+
b, err := ioutil.ReadAll(r.Body)
59+
if err != nil {
60+
t.Errorf("Error reading request body: %v", err)
61+
}
62+
if got := string(b); got != want {
63+
t.Errorf("request Body is %s, want %s", got, want)
64+
}
65+
}
66+
5667
func testURL(t *testing.T, r *http.Request, want string) {
5768
if got := r.URL; got.String() != want {
5869
t.Errorf("request URL is %s, want %s", got, want)

azuredevops/builds.go

+27
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,30 @@ func (s *BuildsService) List(opts *BuildsListOptions) ([]Build, error) {
147147

148148
return response.Builds, err
149149
}
150+
151+
// QueueBuildOptions describes what the request to the API should look like
152+
type QueueBuildOptions struct {
153+
IgnoreWarnings bool `url:"ignoreWarnings,omitempty"`
154+
CheckInTicket string `url:"checkInTicket,omitempty"`
155+
}
156+
157+
// Queue inserts new build creation to queue
158+
// utilising https://docs.microsoft.com/en-us/rest/api/vsts/build/builds/queue?view=vsts-rest-4.1
159+
func (s *BuildsService) Queue(build *Build, opts *QueueBuildOptions) error {
160+
URL := "_apis/build/builds?api-version=4.1"
161+
URL, err := addOptions(URL, opts)
162+
163+
if err != nil {
164+
return err
165+
}
166+
167+
request, err := s.client.NewRequest("POST", URL, build)
168+
169+
if err != nil {
170+
return err
171+
}
172+
173+
_, err = s.client.Execute(request, &build)
174+
175+
return err
176+
}

azuredevops/builds_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package azuredevops_test
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"net/http"
67
"testing"
@@ -10,6 +11,7 @@ import (
1011

1112
const (
1213
buildListURL = "/AZURE_DEVOPS_Project/_apis/build/builds"
14+
queueBuildURL = "/AZURE_DEVOPS_Project/_apis/build/builds"
1315
buildListResponse = `{
1416
"value": [
1517
{
@@ -82,3 +84,54 @@ func TestBuildsService_List(t *testing.T) {
8284
})
8385
}
8486
}
87+
88+
func TestBuildsService_Queue(t *testing.T) {
89+
t.Run("Updates build struct with returned data", func(t *testing.T) {
90+
c, mux, _, teardown := setup()
91+
defer teardown()
92+
93+
requestBuild := &azuredevops.Build{
94+
Status: "completed",
95+
Definition: azuredevops.BuildDefinition{
96+
Name: "build-one",
97+
},
98+
}
99+
100+
responseBuild := requestBuild
101+
responseBuild.Result = "succeeded"
102+
103+
mux.HandleFunc(queueBuildURL, func(w http.ResponseWriter, r *http.Request) {
104+
b, err := json.Marshal(responseBuild)
105+
queueBuildResponse := string(b)
106+
107+
if err != nil {
108+
t.Fatalf("returned error: %v", err)
109+
}
110+
111+
testMethod(t, r, "POST")
112+
testBody(t, r, queueBuildResponse+"\n")
113+
114+
fmt.Fprint(w, queueBuildResponse)
115+
})
116+
117+
options := &azuredevops.QueueBuildOptions{}
118+
119+
err := c.Builds.Queue(requestBuild, options)
120+
121+
if err != nil {
122+
t.Fatalf("returned error: %v", err)
123+
}
124+
125+
if requestBuild.Result != responseBuild.Result {
126+
t.Fatalf("expected result %s, got %s", responseBuild.Result, requestBuild.Result)
127+
}
128+
129+
if requestBuild.Status != responseBuild.Status {
130+
t.Fatalf("expected status %s, got %s", responseBuild.Status, requestBuild.Status)
131+
}
132+
133+
if requestBuild.Definition.Name != responseBuild.Definition.Name {
134+
t.Fatalf("expected definition name %s, got %s", responseBuild.Definition.Name, requestBuild.Definition.Name)
135+
}
136+
})
137+
}

go.mod

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
module github.com/benmatselby/go-vsts
1+
module github.com/benmatselby/go-azuredevops
22

3-
require (
4-
github.com/benmatselby/go-azuredevops v0.0.0-20190112144040-c2a0219308ca
5-
github.com/google/go-querystring v1.0.0
6-
)
3+
require github.com/google/go-querystring v1.0.0

0 commit comments

Comments
 (0)