Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
language: go

sudo: false

go:
- 1.1
- 1.2
- 1.3
- 1.4
install:
- go get github.com/stretchr/testify/assert
script:
- go test -v
- 1.5
- 1.6

install: make install

script: make test
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.PHONY: test run update format install

install:
go get github.com/stretchr/testify/assert
go list -f '{{range .Imports}}{{.}} {{end}}' ./... | xargs go get -v
go list -f '{{range .TestImports}}{{.}} {{end}}' ./... | xargs go get -v

update:
go get -u all

format:
gofmt -l -w -s .
go fix ./...

test:
go test -v ./...
go vet ./...
exit `gofmt -l -s -e . | wc -l`
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ go-gitlab-client is a simple client written in golang to consume gitlab API.
##features

*
###Projects [gitlab api doc](http://api.gitlab.org/projects.html)
### Projects [gitlab api doc](http://doc.gitlab.com/ce/api/projects.html)
* list projects
* get single project
* remove project

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

*
###Users [gitlab api doc](http://api.gitlab.org/users.html)
### Users [gitlab api doc](http://api.gitlab.org/users.html)
* get single user
* manage user keys

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



*
### Builds [gitlab api doc](http://doc.gitlab.com/ce/api/builds.html)
* List project builds
* Get a single build
* List commit builds
* Get build artifacts
* Cancel a build
* Retry a build
* Erase a build

##Installation

Expand Down
159 changes: 159 additions & 0 deletions builds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package gogitlab

import (
"encoding/json"
"io"
)

const (
project_builds = "/projects/:id/builds" // List project builds
project_build = "/projects/:id/builds/:build_id" // Get a single build
project_commit_builds = "/projects/:id/repository/commits/:sha/builds" // List commit builds
project_build_artifacts = "/projects/:id/builds/:build_id/artifacts" // Get build artifacts
project_build_cancel = "/projects/:id/builds/:build_id/cancel" // Cancel a build
project_build_retry = "/projects/:id/builds/:build_id/retry" // Retry a build
project_build_erase = "/projects/:id/builds/:build_id/erase" // Erase a build
)

type ArtifactsFile struct {
Filename string `json:"filename"`
Size int `json:"size"`
}

type Build struct {
Id int `json:"id"`
ArtifactsFile ArtifactsFile `json:"artifacts_file"`
Commit Commit `json:"commit"`
CreatedAt string `json:"created_at"`
DownloadURL string `json:"download_url"`
FinishedAt string `json:"finished_at"`
Name string `json:"name"`
Ref string `json:"ref"`
Stage string `json:"stage"`
StartedAt string `json:"started_at"`
Status string `json:"status"`
Tag bool `json:"tag"`
User User `json:"user"`
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runner and coverage are really interface{} ? i only see null in tests. what types can they be in real world 😃 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know ;) the documentation is not explicit about their values ... I can remove those fields in the mean time. So the lib will be BC when those types will be defined.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


func (g *Gitlab) ProjectBuilds(id string) ([]*Build, error) {
url, opaque := g.ResourceUrlRaw(project_builds, map[string]string{
":id": id,
})

builds := make([]*Build, 0)

contents, err := g.buildAndExecRequestRaw("GET", url, opaque, nil)
if err != nil {
return builds, err
}

err = json.Unmarshal(contents, &builds)

return builds, err
}

func (g *Gitlab) ProjectCommitBuilds(id, sha1 string) ([]*Build, error) {
url, opaque := g.ResourceUrlRaw(project_commit_builds, map[string]string{
":id": id,
":sha": sha1,
})

builds := make([]*Build, 0)

contents, err := g.buildAndExecRequestRaw("GET", url, opaque, nil)
if err != nil {
return builds, err
}

err = json.Unmarshal(contents, &builds)

return builds, err
}

func (g *Gitlab) ProjectBuild(id, buildId string) (*Build, error) {
url, opaque := g.ResourceUrlRaw(project_build, map[string]string{
":id": id,
":build_id": buildId,
})

build := &Build{}

contents, err := g.buildAndExecRequestRaw("GET", url, opaque, nil)
if err != nil {
return nil, err
}

err = json.Unmarshal(contents, &build)

return build, err
}

func (g *Gitlab) ProjectBuildArtifacts(id, buildId string) (io.ReadCloser, error) {
url, _ := g.ResourceUrlRaw(project_build_artifacts, map[string]string{
":id": id,
":build_id": buildId,
})

resp, err := g.execRequest("GET", url, nil)

if err != nil {
return nil, err
}

return resp.Body, nil
}

func (g *Gitlab) ProjectCancelBuild(id, buildId string) (*Build, error) {
url, opaque := g.ResourceUrlRaw(project_build_cancel, map[string]string{
":id": id,
":build_id": buildId,
})

build := &Build{}

contents, err := g.buildAndExecRequestRaw("POST", url, opaque, nil)
if err != nil {
return nil, err
}

err = json.Unmarshal(contents, &build)

return build, err
}

func (g *Gitlab) ProjectRetryBuild(id, buildId string) (*Build, error) {
url, opaque := g.ResourceUrlRaw(project_build_retry, map[string]string{
":id": id,
":build_id": buildId,
})

build := &Build{}

contents, err := g.buildAndExecRequestRaw("POST", url, opaque, nil)
if err != nil {
return nil, err
}

err = json.Unmarshal(contents, &build)

return build, err
}

func (g *Gitlab) ProjectEraseBuild(id, buildId string) (*Build, error) {
url, opaque := g.ResourceUrlRaw(project_build_erase, map[string]string{
":id": id,
":build_id": buildId,
})

build := &Build{}

contents, err := g.buildAndExecRequestRaw("POST", url, opaque, nil)
if err != nil {
return nil, err
}

err = json.Unmarshal(contents, &build)

return build, err
}
84 changes: 84 additions & 0 deletions builds_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package gogitlab

import (
"github.com/stretchr/testify/assert"
"io/ioutil"
"testing"
)

func TestProjectBuilds(t *testing.T) {
ts, gitlab := Stub("stubs/builds/list.json")
defer ts.Close()

builds, err := gitlab.ProjectBuilds("3")

assert.Nil(t, err)
assert.Equal(t, len(builds), 2)
assert.Equal(t, builds[0].ArtifactsFile.Filename, "artifacts.zip")
}

func TestProjectCommitBuilds(t *testing.T) {
ts, gitlab := Stub("stubs/builds/commit_builds_list.json")
defer ts.Close()

builds, err := gitlab.ProjectBuilds("3")

assert.Nil(t, err)
assert.Equal(t, len(builds), 2)
assert.Equal(t, builds[1].User.AvatarUrl, "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon")
}

func TestProjectBuild(t *testing.T) {
ts, gitlab := Stub("stubs/builds/build.json")
defer ts.Close()

build, err := gitlab.ProjectBuild("3", "12")

assert.Nil(t, err)
assert.Equal(t, build.Commit.Id, "0ff3ae198f8601a285adcf5c0fff204ee6fba5fd")
}

func TestProjectCancelBuild(t *testing.T) {
ts, gitlab := Stub("stubs/builds/cancel.json")
defer ts.Close()

build, err := gitlab.ProjectBuild("3", "12")

assert.Nil(t, err)
assert.Equal(t, build.Status, "canceled")
}

func TestProjectRetryBuild(t *testing.T) {
ts, gitlab := Stub("stubs/builds/retry.json")
defer ts.Close()

build, err := gitlab.ProjectBuild("3", "12")

assert.Nil(t, err)
assert.Equal(t, build.Status, "pending")
}

func TestProjectEraseBuild(t *testing.T) {
ts, gitlab := Stub("stubs/builds/erase.json")
defer ts.Close()

build, err := gitlab.ProjectBuild("3", "12")

assert.Nil(t, err)
assert.Equal(t, build.Status, "failed")
}

func TestProjectArtifact(t *testing.T) {
ts, gitlab := Stub("stubs/builds/content.txt")
defer ts.Close()

r, err := gitlab.ProjectBuildArtifacts("3", "12")

assert.Nil(t, err)

defer r.Close()

contents, err := ioutil.ReadAll(r)

assert.Equal(t, string(contents), "a content")
}
8 changes: 4 additions & 4 deletions events.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ func (g *Gitlab) Activity() (ActivityFeed, error) {

contents, err := g.buildAndExecRequest("GET", url, nil)
if err != nil {
fmt.Println("%s", err)
fmt.Printf("%s\n", err.Error())
}

var activity ActivityFeed
err = xml.Unmarshal(contents, &activity)
if err != nil {
fmt.Println("%s", err)
fmt.Printf("%s\n", err.Error())
}

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

contents, err := g.buildAndExecRequest("GET", url, nil)
if err != nil {
fmt.Println("%s", err)
fmt.Printf("%s\n", err)
}

var activity ActivityFeed
err = xml.Unmarshal(contents, &activity)
if err != nil {
fmt.Println("%s", err)
fmt.Printf("%s\n", err)
}

return activity
Expand Down
Loading