Commit 538808ff authored by Martin Sefcik's avatar Martin Sefcik

Added new Status API calls

parent da0408da
......@@ -164,15 +164,18 @@ type CommitComment struct {
Path string `json:"path"`
Line int `json:"line"`
LineType string `json:"line_type"`
Author struct {
ID int `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
Name string `json:"name"`
State string `json:"state"`
Blocked bool `json:"blocked"`
CreatedAt time.Time `json:"created_at"`
} `json:"author"`
Author Author `json:"author"`
}
type Author struct {
ID *int `json:"id"`
Username *string `json:"username"`
Email *string `json:"email"`
Name *string `json:"name"`
State *string `json:"state"`
Blocked *bool `json:"blocked"`
CreatedAt *time.Time `json:"created_at"`
}
func (c CommitComment) String() string {
......@@ -247,3 +250,103 @@ func (s *CommitsService) PostCommitComment(
return c, resp, err
}
// GetCommitStatusesOptions represents the available GetCommitStatuses() options.
//
// GitLab API docs: http://doc.gitlab.com/ce/api/commits.html#get-the-status-of-a-commit
type GetCommitStatusesOptions struct {
Ref string `url:"ref,omitempty"`
Stage string `url:"stage,omitempty"`
Name string `url:"name,omitempty"`
All bool `url:"all,omitempty"`
}
// CommitStatus represents a GitLab commit status.
//
// GitLab API docs: http://doc.gitlab.com/ce/api/commits.html#get-the-status-of-a-commit
type CommitStatus struct {
ID *int `json:"id"`
SHA *string `json:"sha"`
Ref *string `json:"ref"`
Status *string `json:"status"`
Name *string `json:"name"`
TargetUrl *string `json:"target_url"`
Description *string `json:"description"`
CreatedAt *time.Time `json:"created_at"`
StartedAt *time.Time `json:"started_at"`
FinishedAt *time.Time `json:"finished_at"`
Author *Author `json:"author"`
}
// GetCommitStatuses gets the statuses of a commit in a project.
//
// GitLab API docs: http://doc.gitlab.com/ce/api/commits.html#get-the-status-of-a-commit
func (s *CommitsService) GetCommitStatuses(
pid interface{}, sha string, opt *GetCommitStatusesOptions) ([]*CommitStatus, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/repository/commits/%s/statuses", url.QueryEscape(project), sha)
req, err := s.client.NewRequest("GET", u, opt)
if err != nil {
return nil, nil, err
}
var cs []*CommitStatus
resp, err := s.client.Do(req, &cs)
if err != nil {
return nil, resp, err
}
return cs, resp, err
}
// SetCommitStatusOptions represents the available SetCommitStatus() options.
//
// GitLab API docs: http://doc.gitlab.com/ce/api/commits.html#post-the-status-to-commit
type SetCommitStatusOptions struct {
State BuildState `url:"state"`
Ref string `url:"ref,omitempty"`
Name string `url:"name,omitempty"`
Context string `url:"context,omitempty"`
TargetUrl string `url:"target_url,omitempty"`
Description string `url:"description,omitempty"`
}
type BuildState string
const (
Pending BuildState = "pending"
Running BuildState = "running"
Success BuildState = "success"
Failed BuildState = "failed"
Canceled BuildState = "canceled"
)
// SetCommitStatus sets the status of a commit in a project.
//
// GitLab API docs: http://doc.gitlab.com/ce/api/commits.html#post-the-status-to-commit
func (s *CommitsService) SetCommitStatus(
pid interface{},
sha string, opt *SetCommitStatusOptions) (*CommitStatus, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/statuses/%s", url.QueryEscape(project), sha)
req, err := s.client.NewRequest("POST", u, opt)
if err != nil {
return nil, nil, err
}
var cs *CommitStatus
resp, err := s.client.Do(req, &cs)
if err != nil {
return nil, resp, err
}
return cs, resp, err
}
package gitlab
import (
"fmt"
"net/http"
"reflect"
"testing"
)
func TestGetCommitStatuses(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/projects/1/repository/commits/b0b3a907f41409829b307a28b82fdbd552ee5a27/statuses", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{
"ref": "master",
"stage": "test",
"name": "ci/jenkins",
"all": "true",
})
fmt.Fprint(w, `[{"id":1}]`)
})
opt := &GetCommitStatusesOptions{"master", "test", "ci/jenkins", true}
statuses, _, err := client.Commits.GetCommitStatuses("1", "b0b3a907f41409829b307a28b82fdbd552ee5a27", opt)
if err != nil {
t.Errorf("Commits.GetCommitStatuses returned error: %v", err)
}
want := []*CommitStatus{{ID: Int(1)}}
if !reflect.DeepEqual(want, statuses) {
t.Errorf("Commits.GetCommitStatuses returned %+v, want %+v", statuses, want)
}
}
func TestSetCommitStatus(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/projects/1/statuses/b0b3a907f41409829b307a28b82fdbd552ee5a27", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
testFormValues(t, r, values{
"state": "running",
"ref": "master",
"name": "ci/jenkins",
"target_url": "http://abc",
"description": "build",
})
fmt.Fprint(w, `{"id":1}`)
})
opt := &SetCommitStatusOptions{Running, "master", "ci/jenkins", "", "http://abc", "build"}
status, _, err := client.Commits.SetCommitStatus("1", "b0b3a907f41409829b307a28b82fdbd552ee5a27", opt)
if err != nil {
t.Errorf("Commits.SetCommitStatus returned error: %v", err)
}
want := &CommitStatus{ID: Int(1)}
if !reflect.DeepEqual(want, status) {
t.Errorf("Commits.SetCommitStatus returned %+v, want %+v", status, want)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment