1// Copyright 2013 The go-github AUTHORS. All rights reserved.
2//
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file.
5
6package github
7
8import (
9	"context"
10	"fmt"
11	"time"
12)
13
14// RepoStatus represents the status of a repository at a particular reference.
15type RepoStatus struct {
16	ID     *int64  `json:"id,omitempty"`
17	NodeID *string `json:"node_id,omitempty"`
18	URL    *string `json:"url,omitempty"`
19
20	// State is the current state of the repository. Possible values are:
21	// pending, success, error, or failure.
22	State *string `json:"state,omitempty"`
23
24	// TargetURL is the URL of the page representing this status. It will be
25	// linked from the GitHub UI to allow users to see the source of the status.
26	TargetURL *string `json:"target_url,omitempty"`
27
28	// Description is a short high level summary of the status.
29	Description *string `json:"description,omitempty"`
30
31	// A string label to differentiate this status from the statuses of other systems.
32	Context *string `json:"context,omitempty"`
33
34	Creator   *User      `json:"creator,omitempty"`
35	CreatedAt *time.Time `json:"created_at,omitempty"`
36	UpdatedAt *time.Time `json:"updated_at,omitempty"`
37}
38
39func (r RepoStatus) String() string {
40	return Stringify(r)
41}
42
43// ListStatuses lists the statuses of a repository at the specified
44// reference. ref can be a SHA, a branch name, or a tag name.
45//
46// GitHub API docs: https://developer.github.com/v3/repos/statuses/#list-commit-statuses-for-a-reference
47func (s *RepositoriesService) ListStatuses(ctx context.Context, owner, repo, ref string, opts *ListOptions) ([]*RepoStatus, *Response, error) {
48	u := fmt.Sprintf("repos/%v/%v/commits/%v/statuses", owner, repo, refURLEscape(ref))
49	u, err := addOptions(u, opts)
50	if err != nil {
51		return nil, nil, err
52	}
53
54	req, err := s.client.NewRequest("GET", u, nil)
55	if err != nil {
56		return nil, nil, err
57	}
58
59	var statuses []*RepoStatus
60	resp, err := s.client.Do(ctx, req, &statuses)
61	if err != nil {
62		return nil, resp, err
63	}
64
65	return statuses, resp, nil
66}
67
68// CreateStatus creates a new status for a repository at the specified
69// reference. Ref can be a SHA, a branch name, or a tag name.
70//
71// GitHub API docs: https://developer.github.com/v3/repos/statuses/#create-a-commit-status
72func (s *RepositoriesService) CreateStatus(ctx context.Context, owner, repo, ref string, status *RepoStatus) (*RepoStatus, *Response, error) {
73	u := fmt.Sprintf("repos/%v/%v/statuses/%v", owner, repo, refURLEscape(ref))
74	req, err := s.client.NewRequest("POST", u, status)
75	if err != nil {
76		return nil, nil, err
77	}
78
79	repoStatus := new(RepoStatus)
80	resp, err := s.client.Do(ctx, req, repoStatus)
81	if err != nil {
82		return nil, resp, err
83	}
84
85	return repoStatus, resp, nil
86}
87
88// CombinedStatus represents the combined status of a repository at a particular reference.
89type CombinedStatus struct {
90	// State is the combined state of the repository. Possible values are:
91	// failure, pending, or success.
92	State *string `json:"state,omitempty"`
93
94	Name       *string       `json:"name,omitempty"`
95	SHA        *string       `json:"sha,omitempty"`
96	TotalCount *int          `json:"total_count,omitempty"`
97	Statuses   []*RepoStatus `json:"statuses,omitempty"`
98
99	CommitURL     *string `json:"commit_url,omitempty"`
100	RepositoryURL *string `json:"repository_url,omitempty"`
101}
102
103func (s CombinedStatus) String() string {
104	return Stringify(s)
105}
106
107// GetCombinedStatus returns the combined status of a repository at the specified
108// reference. ref can be a SHA, a branch name, or a tag name.
109//
110// GitHub API docs: https://developer.github.com/v3/repos/statuses/#get-the-combined-status-for-a-specific-reference
111func (s *RepositoriesService) GetCombinedStatus(ctx context.Context, owner, repo, ref string, opts *ListOptions) (*CombinedStatus, *Response, error) {
112	u := fmt.Sprintf("repos/%v/%v/commits/%v/status", owner, repo, refURLEscape(ref))
113	u, err := addOptions(u, opts)
114	if err != nil {
115		return nil, nil, err
116	}
117
118	req, err := s.client.NewRequest("GET", u, nil)
119	if err != nil {
120		return nil, nil, err
121	}
122
123	status := new(CombinedStatus)
124	resp, err := s.client.Do(ctx, req, status)
125	if err != nil {
126		return nil, resp, err
127	}
128
129	return status, resp, nil
130}
131