From 0fef8a002cf3fa2315a3d4dccdb33e7ba4b84e70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20=22BKC=22=20Carlb=C3=A4cker?= Date: Sun, 19 Mar 2017 18:32:40 +0100 Subject: [PATCH 1/3] Initial support for Status-API --- gitea/status.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 gitea/status.go diff --git a/gitea/status.go b/gitea/status.go new file mode 100644 index 0000000..cb1cf02 --- /dev/null +++ b/gitea/status.go @@ -0,0 +1,74 @@ +package gitea + +import ( + "bytes" + "encoding/json" + "fmt" + "time" +) + +type StatusState string + +const ( + StatusPending StatusState = "pending" + StatusSuccess StatusState = "success" + StatusError StatusState = "error" + StatusFailure StatusState = "failure" + StatusWarning StatusState = "warning" +) + +type Status struct { + ID int64 `json:"id"` + State StatusState `json:"status"` + TargetURL string `json:"target_url"` + Description string `json:"description"` + URL string `json:"url"` + Context string `json:"context"` + Creator *User `json:"creator"` + Created time.Time `json:"created_at"` + Updated time.Time `json:"updated_at"` +} + +type CombinedStatus struct { + State StatusState `json:"state"` + SHA string `json:"sha"` + TotalCount int `json:"total_count"` + Statuses []*Status `json:"statuses"` + Repository *Repository `json:"repository"` + CommitURL string `json:"commit_url"` + URL string `json:"url"` +} + +type CreateStatusOption struct { + State StatusState `json:"state"` + TargetURL string `json:"target_url"` + Description string `json:"description"` + Context string `json:"context"` +} + +type ListStatusesOption struct { + Page int +} + +// POST /repos/:owner/:repo/statuses/:sha +func (c *Client) CreateStatus(owner, repo, sha string, opts CreateStatusOption) (*Status, error) { + body, err := json.Marshal(&opts) + if err != nil { + return nil, err + } + status := &Status{} + return status, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/statuses/%s", owner, repo, sha), + jsonHeader, bytes.NewReader(body), status) +} + +// GET /repos/:owner/:repo/commits/:ref/statuses +func (c *Client) ListStatuses(owner, repo, sha string, opts ListStatusesOption) ([]*Status, error) { + statuses := make([]*Status, 0, 10) + return statuses, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/statuses?page=%d", owner, repo, sha, opts.Page), nil, nil, &statuses) +} + +// GET /repos/:owner/:repo/commits/:ref/status +func (c *Client) GetCombinedStatus(owner, repo, sha string) (*CombinedStatus, error) { + status := &CombinedStatus{} + return status, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/status", owner, repo, sha), nil, nil, status) +} From 01cf3f12e2865284b865a4e44ab0523a876c651f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20=22BKC=22=20Carlb=C3=A4cker?= Date: Wed, 22 Mar 2017 01:11:27 +0100 Subject: [PATCH 2/3] Linting and Copyright Notice --- gitea/status.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/gitea/status.go b/gitea/status.go index cb1cf02..d5cdcd5 100644 --- a/gitea/status.go +++ b/gitea/status.go @@ -1,3 +1,7 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + package gitea import ( @@ -7,16 +11,24 @@ import ( "time" ) +// StatusState holds the state of a Status +// It can be "pending", "success", "error", "failure", and "warning" type StatusState string const ( + // StatusPending is for when the Status is Pending StatusPending StatusState = "pending" + // StatusSuccess is for when the Status is Success StatusSuccess StatusState = "success" - StatusError StatusState = "error" + // StatusError is for when the Status is Error + StatusError StatusState = "error" + // StatusFailure is for when the Status is Failure StatusFailure StatusState = "failure" + // StatusWarning is for when the Status is Warning StatusWarning StatusState = "warning" ) +// Status holds a single Status of a single Commit type Status struct { ID int64 `json:"id"` State StatusState `json:"status"` @@ -29,6 +41,7 @@ type Status struct { Updated time.Time `json:"updated_at"` } +// CombinedStatus holds the combined state of several statuses for a single commit type CombinedStatus struct { State StatusState `json:"state"` SHA string `json:"sha"` @@ -39,6 +52,7 @@ type CombinedStatus struct { URL string `json:"url"` } +// CreateStatusOption holds the information needed to create a new Status for a Commit type CreateStatusOption struct { State StatusState `json:"state"` TargetURL string `json:"target_url"` @@ -46,10 +60,13 @@ type CreateStatusOption struct { Context string `json:"context"` } +// ListStatusesOption holds pagination information type ListStatusesOption struct { Page int } +// CreateStatus creates a new Status for a given Commit +// // POST /repos/:owner/:repo/statuses/:sha func (c *Client) CreateStatus(owner, repo, sha string, opts CreateStatusOption) (*Status, error) { body, err := json.Marshal(&opts) @@ -61,12 +78,16 @@ func (c *Client) CreateStatus(owner, repo, sha string, opts CreateStatusOption) jsonHeader, bytes.NewReader(body), status) } +// ListStatuses returns all statuses for a given Commit +// // GET /repos/:owner/:repo/commits/:ref/statuses func (c *Client) ListStatuses(owner, repo, sha string, opts ListStatusesOption) ([]*Status, error) { statuses := make([]*Status, 0, 10) return statuses, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/statuses?page=%d", owner, repo, sha, opts.Page), nil, nil, &statuses) } +// GetCombinedStatus returns the CombinedStatus for a given Commit +// // GET /repos/:owner/:repo/commits/:ref/status func (c *Client) GetCombinedStatus(owner, repo, sha string) (*CombinedStatus, error) { status := &CombinedStatus{} From 5e42897083c8070ae117f8edeace810ad7457047 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20=22BKC=22=20Carlb=C3=A4cker?= Date: Wed, 22 Mar 2017 01:13:52 +0100 Subject: [PATCH 3/3] gofmt by hand Look maa'! No hands! --- gitea/status.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitea/status.go b/gitea/status.go index d5cdcd5..e694add 100644 --- a/gitea/status.go +++ b/gitea/status.go @@ -21,7 +21,7 @@ const ( // StatusSuccess is for when the Status is Success StatusSuccess StatusState = "success" // StatusError is for when the Status is Error - StatusError StatusState = "error" + StatusError StatusState = "error" // StatusFailure is for when the Status is Failure StatusFailure StatusState = "failure" // StatusWarning is for when the Status is Warning