Skip to content
This repository was archived by the owner on Jun 8, 2019. It is now read-only.

Commit 1bec42c

Browse files
bkcsoftlunny
authored andcommitted
Status-API (#49)
* Initial support for Status-API * Linting and Copyright Notice * gofmt by hand Look maa'! No hands!
1 parent 80778ed commit 1bec42c

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

gitea/status.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package gitea
6+
7+
import (
8+
"bytes"
9+
"encoding/json"
10+
"fmt"
11+
"time"
12+
)
13+
14+
// StatusState holds the state of a Status
15+
// It can be "pending", "success", "error", "failure", and "warning"
16+
type StatusState string
17+
18+
const (
19+
// StatusPending is for when the Status is Pending
20+
StatusPending StatusState = "pending"
21+
// StatusSuccess is for when the Status is Success
22+
StatusSuccess StatusState = "success"
23+
// StatusError is for when the Status is Error
24+
StatusError StatusState = "error"
25+
// StatusFailure is for when the Status is Failure
26+
StatusFailure StatusState = "failure"
27+
// StatusWarning is for when the Status is Warning
28+
StatusWarning StatusState = "warning"
29+
)
30+
31+
// Status holds a single Status of a single Commit
32+
type Status struct {
33+
ID int64 `json:"id"`
34+
State StatusState `json:"status"`
35+
TargetURL string `json:"target_url"`
36+
Description string `json:"description"`
37+
URL string `json:"url"`
38+
Context string `json:"context"`
39+
Creator *User `json:"creator"`
40+
Created time.Time `json:"created_at"`
41+
Updated time.Time `json:"updated_at"`
42+
}
43+
44+
// CombinedStatus holds the combined state of several statuses for a single commit
45+
type CombinedStatus struct {
46+
State StatusState `json:"state"`
47+
SHA string `json:"sha"`
48+
TotalCount int `json:"total_count"`
49+
Statuses []*Status `json:"statuses"`
50+
Repository *Repository `json:"repository"`
51+
CommitURL string `json:"commit_url"`
52+
URL string `json:"url"`
53+
}
54+
55+
// CreateStatusOption holds the information needed to create a new Status for a Commit
56+
type CreateStatusOption struct {
57+
State StatusState `json:"state"`
58+
TargetURL string `json:"target_url"`
59+
Description string `json:"description"`
60+
Context string `json:"context"`
61+
}
62+
63+
// ListStatusesOption holds pagination information
64+
type ListStatusesOption struct {
65+
Page int
66+
}
67+
68+
// CreateStatus creates a new Status for a given Commit
69+
//
70+
// POST /repos/:owner/:repo/statuses/:sha
71+
func (c *Client) CreateStatus(owner, repo, sha string, opts CreateStatusOption) (*Status, error) {
72+
body, err := json.Marshal(&opts)
73+
if err != nil {
74+
return nil, err
75+
}
76+
status := &Status{}
77+
return status, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/statuses/%s", owner, repo, sha),
78+
jsonHeader, bytes.NewReader(body), status)
79+
}
80+
81+
// ListStatuses returns all statuses for a given Commit
82+
//
83+
// GET /repos/:owner/:repo/commits/:ref/statuses
84+
func (c *Client) ListStatuses(owner, repo, sha string, opts ListStatusesOption) ([]*Status, error) {
85+
statuses := make([]*Status, 0, 10)
86+
return statuses, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/statuses?page=%d", owner, repo, sha, opts.Page), nil, nil, &statuses)
87+
}
88+
89+
// GetCombinedStatus returns the CombinedStatus for a given Commit
90+
//
91+
// GET /repos/:owner/:repo/commits/:ref/status
92+
func (c *Client) GetCombinedStatus(owner, repo, sha string) (*CombinedStatus, error) {
93+
status := &CombinedStatus{}
94+
return status, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/status", owner, repo, sha), nil, nil, status)
95+
}

0 commit comments

Comments
 (0)