Skip to content

Commit c1f6531

Browse files
author
Raphaël Benitte
committed
Merge pull request #26 from rande/add_support_for_builds
Improve api support
2 parents e311008 + 17da653 commit c1f6531

File tree

17 files changed

+588
-26
lines changed

17 files changed

+588
-26
lines changed

.travis.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
language: go
2+
3+
sudo: false
4+
25
go:
36
- 1.1
47
- 1.2
58
- 1.3
69
- 1.4
7-
install:
8-
- go get github.com/stretchr/testify/assert
9-
script:
10-
- go test -v
10+
- 1.5
11+
- 1.6
12+
13+
install: make install
14+
15+
script: make test

Makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.PHONY: test run update format install
2+
3+
install:
4+
go get github.com/stretchr/testify/assert
5+
go list -f '{{range .Imports}}{{.}} {{end}}' ./... | xargs go get -v
6+
go list -f '{{range .TestImports}}{{.}} {{end}}' ./... | xargs go get -v
7+
8+
update:
9+
go get -u all
10+
11+
format:
12+
gofmt -l -w -s .
13+
go fix ./...
14+
15+
test:
16+
go test -v ./...
17+
go vet ./...
18+
exit `gofmt -l -s -e . | wc -l`

README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ go-gitlab-client is a simple client written in golang to consume gitlab API.
99
##features
1010

1111
*
12-
###Projects [gitlab api doc](http://api.gitlab.org/projects.html)
12+
### Projects [gitlab api doc](http://doc.gitlab.com/ce/api/projects.html)
1313
* list projects
1414
* get single project
1515
* remove project
1616

1717
*
18-
###Repositories [gitlab api doc](http://api.gitlab.org/repositories.html)
18+
### Repositories [gitlab api doc](http://doc.gitlab.com/ce/api/repositories.html)
1919
* list repository branches
2020
* get single repository branch
2121
* list project repository tags
@@ -24,17 +24,24 @@ go-gitlab-client is a simple client written in golang to consume gitlab API.
2424
* add/get/edit/rm project hook
2525

2626
*
27-
###Users [gitlab api doc](http://api.gitlab.org/users.html)
27+
### Users [gitlab api doc](http://api.gitlab.org/users.html)
2828
* get single user
2929
* manage user keys
3030

3131
*
32-
###Deploy Keys [gitlab api doc](http://api.gitlab.org/deploy_keys.html)
32+
### Deploy Keys [gitlab api doc](http://doc.gitlab.com/ce/api/deploy_keys.html)
3333
* list project deploy keys
3434
* add/get/rm project deploy key
3535

36-
37-
36+
*
37+
### Builds [gitlab api doc](http://doc.gitlab.com/ce/api/builds.html)
38+
* List project builds
39+
* Get a single build
40+
* List commit builds
41+
* Get build artifacts
42+
* Cancel a build
43+
* Retry a build
44+
* Erase a build
3845

3946
##Installation
4047

builds.go

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package gogitlab
2+
3+
import (
4+
"encoding/json"
5+
"io"
6+
)
7+
8+
const (
9+
project_builds = "/projects/:id/builds" // List project builds
10+
project_build = "/projects/:id/builds/:build_id" // Get a single build
11+
project_commit_builds = "/projects/:id/repository/commits/:sha/builds" // List commit builds
12+
project_build_artifacts = "/projects/:id/builds/:build_id/artifacts" // Get build artifacts
13+
project_build_cancel = "/projects/:id/builds/:build_id/cancel" // Cancel a build
14+
project_build_retry = "/projects/:id/builds/:build_id/retry" // Retry a build
15+
project_build_erase = "/projects/:id/builds/:build_id/erase" // Erase a build
16+
)
17+
18+
type ArtifactsFile struct {
19+
Filename string `json:"filename"`
20+
Size int `json:"size"`
21+
}
22+
23+
type Build struct {
24+
Id int `json:"id"`
25+
ArtifactsFile ArtifactsFile `json:"artifacts_file"`
26+
Commit Commit `json:"commit"`
27+
CreatedAt string `json:"created_at"`
28+
DownloadURL string `json:"download_url"`
29+
FinishedAt string `json:"finished_at"`
30+
Name string `json:"name"`
31+
Ref string `json:"ref"`
32+
Stage string `json:"stage"`
33+
StartedAt string `json:"started_at"`
34+
Status string `json:"status"`
35+
Tag bool `json:"tag"`
36+
User User `json:"user"`
37+
}
38+
39+
func (g *Gitlab) ProjectBuilds(id string) ([]*Build, error) {
40+
url, opaque := g.ResourceUrlRaw(project_builds, map[string]string{
41+
":id": id,
42+
})
43+
44+
builds := make([]*Build, 0)
45+
46+
contents, err := g.buildAndExecRequestRaw("GET", url, opaque, nil)
47+
if err != nil {
48+
return builds, err
49+
}
50+
51+
err = json.Unmarshal(contents, &builds)
52+
53+
return builds, err
54+
}
55+
56+
func (g *Gitlab) ProjectCommitBuilds(id, sha1 string) ([]*Build, error) {
57+
url, opaque := g.ResourceUrlRaw(project_commit_builds, map[string]string{
58+
":id": id,
59+
":sha": sha1,
60+
})
61+
62+
builds := make([]*Build, 0)
63+
64+
contents, err := g.buildAndExecRequestRaw("GET", url, opaque, nil)
65+
if err != nil {
66+
return builds, err
67+
}
68+
69+
err = json.Unmarshal(contents, &builds)
70+
71+
return builds, err
72+
}
73+
74+
func (g *Gitlab) ProjectBuild(id, buildId string) (*Build, error) {
75+
url, opaque := g.ResourceUrlRaw(project_build, map[string]string{
76+
":id": id,
77+
":build_id": buildId,
78+
})
79+
80+
build := &Build{}
81+
82+
contents, err := g.buildAndExecRequestRaw("GET", url, opaque, nil)
83+
if err != nil {
84+
return nil, err
85+
}
86+
87+
err = json.Unmarshal(contents, &build)
88+
89+
return build, err
90+
}
91+
92+
func (g *Gitlab) ProjectBuildArtifacts(id, buildId string) (io.ReadCloser, error) {
93+
url, _ := g.ResourceUrlRaw(project_build_artifacts, map[string]string{
94+
":id": id,
95+
":build_id": buildId,
96+
})
97+
98+
resp, err := g.execRequest("GET", url, nil)
99+
100+
if err != nil {
101+
return nil, err
102+
}
103+
104+
return resp.Body, nil
105+
}
106+
107+
func (g *Gitlab) ProjectCancelBuild(id, buildId string) (*Build, error) {
108+
url, opaque := g.ResourceUrlRaw(project_build_cancel, map[string]string{
109+
":id": id,
110+
":build_id": buildId,
111+
})
112+
113+
build := &Build{}
114+
115+
contents, err := g.buildAndExecRequestRaw("POST", url, opaque, nil)
116+
if err != nil {
117+
return nil, err
118+
}
119+
120+
err = json.Unmarshal(contents, &build)
121+
122+
return build, err
123+
}
124+
125+
func (g *Gitlab) ProjectRetryBuild(id, buildId string) (*Build, error) {
126+
url, opaque := g.ResourceUrlRaw(project_build_retry, map[string]string{
127+
":id": id,
128+
":build_id": buildId,
129+
})
130+
131+
build := &Build{}
132+
133+
contents, err := g.buildAndExecRequestRaw("POST", url, opaque, nil)
134+
if err != nil {
135+
return nil, err
136+
}
137+
138+
err = json.Unmarshal(contents, &build)
139+
140+
return build, err
141+
}
142+
143+
func (g *Gitlab) ProjectEraseBuild(id, buildId string) (*Build, error) {
144+
url, opaque := g.ResourceUrlRaw(project_build_erase, map[string]string{
145+
":id": id,
146+
":build_id": buildId,
147+
})
148+
149+
build := &Build{}
150+
151+
contents, err := g.buildAndExecRequestRaw("POST", url, opaque, nil)
152+
if err != nil {
153+
return nil, err
154+
}
155+
156+
err = json.Unmarshal(contents, &build)
157+
158+
return build, err
159+
}

builds_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package gogitlab
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"io/ioutil"
6+
"testing"
7+
)
8+
9+
func TestProjectBuilds(t *testing.T) {
10+
ts, gitlab := Stub("stubs/builds/list.json")
11+
defer ts.Close()
12+
13+
builds, err := gitlab.ProjectBuilds("3")
14+
15+
assert.Nil(t, err)
16+
assert.Equal(t, len(builds), 2)
17+
assert.Equal(t, builds[0].ArtifactsFile.Filename, "artifacts.zip")
18+
}
19+
20+
func TestProjectCommitBuilds(t *testing.T) {
21+
ts, gitlab := Stub("stubs/builds/commit_builds_list.json")
22+
defer ts.Close()
23+
24+
builds, err := gitlab.ProjectBuilds("3")
25+
26+
assert.Nil(t, err)
27+
assert.Equal(t, len(builds), 2)
28+
assert.Equal(t, builds[1].User.AvatarUrl, "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon")
29+
}
30+
31+
func TestProjectBuild(t *testing.T) {
32+
ts, gitlab := Stub("stubs/builds/build.json")
33+
defer ts.Close()
34+
35+
build, err := gitlab.ProjectBuild("3", "12")
36+
37+
assert.Nil(t, err)
38+
assert.Equal(t, build.Commit.Id, "0ff3ae198f8601a285adcf5c0fff204ee6fba5fd")
39+
}
40+
41+
func TestProjectCancelBuild(t *testing.T) {
42+
ts, gitlab := Stub("stubs/builds/cancel.json")
43+
defer ts.Close()
44+
45+
build, err := gitlab.ProjectBuild("3", "12")
46+
47+
assert.Nil(t, err)
48+
assert.Equal(t, build.Status, "canceled")
49+
}
50+
51+
func TestProjectRetryBuild(t *testing.T) {
52+
ts, gitlab := Stub("stubs/builds/retry.json")
53+
defer ts.Close()
54+
55+
build, err := gitlab.ProjectBuild("3", "12")
56+
57+
assert.Nil(t, err)
58+
assert.Equal(t, build.Status, "pending")
59+
}
60+
61+
func TestProjectEraseBuild(t *testing.T) {
62+
ts, gitlab := Stub("stubs/builds/erase.json")
63+
defer ts.Close()
64+
65+
build, err := gitlab.ProjectBuild("3", "12")
66+
67+
assert.Nil(t, err)
68+
assert.Equal(t, build.Status, "failed")
69+
}
70+
71+
func TestProjectArtifact(t *testing.T) {
72+
ts, gitlab := Stub("stubs/builds/content.txt")
73+
defer ts.Close()
74+
75+
r, err := gitlab.ProjectBuildArtifacts("3", "12")
76+
77+
assert.Nil(t, err)
78+
79+
defer r.Close()
80+
81+
contents, err := ioutil.ReadAll(r)
82+
83+
assert.Equal(t, string(contents), "a content")
84+
}

events.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ func (g *Gitlab) Activity() (ActivityFeed, error) {
4141

4242
contents, err := g.buildAndExecRequest("GET", url, nil)
4343
if err != nil {
44-
fmt.Println("%s", err)
44+
fmt.Printf("%s\n", err.Error())
4545
}
4646

4747
var activity ActivityFeed
4848
err = xml.Unmarshal(contents, &activity)
4949
if err != nil {
50-
fmt.Println("%s", err)
50+
fmt.Printf("%s\n", err.Error())
5151
}
5252

5353
return activity, err
@@ -59,13 +59,13 @@ func (g *Gitlab) RepoActivityFeed(feedPath string) ActivityFeed {
5959

6060
contents, err := g.buildAndExecRequest("GET", url, nil)
6161
if err != nil {
62-
fmt.Println("%s", err)
62+
fmt.Printf("%s\n", err)
6363
}
6464

6565
var activity ActivityFeed
6666
err = xml.Unmarshal(contents, &activity)
6767
if err != nil {
68-
fmt.Println("%s", err)
68+
fmt.Printf("%s\n", err)
6969
}
7070

7171
return activity

0 commit comments

Comments
 (0)