1package gitlab
2
3import (
4	"fmt"
5	"net/http"
6	"time"
7)
8
9// ExternalStatusChecksService handles communication with the external
10// status check related methods of the GitLab API.
11//
12// GitLab API docs: https://docs.gitlab.com/ee/api/status_checks.html
13type ExternalStatusChecksService struct {
14	client *Client
15}
16
17type MergeStatusCheck struct {
18	ID          int    `json:"id"`
19	Name        string `json:"name"`
20	ExternalURL string `json:"external_url"`
21	Status      string `json:"status"`
22}
23
24type ProjectStatusCheck struct {
25	ID                int                          `json:"id"`
26	Name              string                       `json:"name"`
27	ProjectID         int                          `json:"project_id"`
28	ExternalURL       string                       `json:"external_url"`
29	ProtectedBranches []StatusCheckProtectedBranch `json:"protected_branches"`
30}
31
32type StatusCheckProtectedBranch struct {
33	ID                        int        `json:"id"`
34	ProjectID                 int        `json:"project_id"`
35	Name                      string     `json:"name"`
36	CreatedAt                 *time.Time `json:"created_at"`
37	UpdatedAt                 *time.Time `json:"updated_at"`
38	CodeOwnerApprovalRequired bool       `json:"code_owner_approval_required"`
39}
40
41// ListMergeStatusChecks lists the external status checks that apply to it
42// and their status for a single merge request.
43//
44// GitLab API docs:
45// https://docs.gitlab.com/ee/api/status_checks.html#list-status-checks-for-a-merge-request
46func (s *ExternalStatusChecksService) ListMergeStatusChecks(pid interface{}, mr int, opt *ListOptions, options ...RequestOptionFunc) ([]*MergeStatusCheck, *Response, error) {
47	project, err := parseID(pid)
48	if err != nil {
49		return nil, nil, err
50	}
51	u := fmt.Sprintf("projects/%s/merge_requests/%d/status_checks", PathEscape(project), mr)
52
53	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
54	if err != nil {
55		return nil, nil, err
56	}
57
58	var mscs []*MergeStatusCheck
59	resp, err := s.client.Do(req, &mscs)
60	if err != nil {
61		return nil, resp, err
62	}
63
64	return mscs, resp, err
65}
66
67// ListProjectStatusChecks lists the project external status checks.
68//
69// GitLab API docs:
70// https://docs.gitlab.com/ee/api/status_checks.html#get-project-external-status-checks
71func (s *ExternalStatusChecksService) ListProjectStatusChecks(pid interface{}, opt *ListOptions, options ...RequestOptionFunc) ([]*ProjectStatusCheck, *Response, error) {
72	project, err := parseID(pid)
73	if err != nil {
74		return nil, nil, err
75	}
76	u := fmt.Sprintf("projects/%s/external_status_checks", PathEscape(project))
77
78	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
79	if err != nil {
80		return nil, nil, err
81	}
82
83	var pscs []*ProjectStatusCheck
84	resp, err := s.client.Do(req, &pscs)
85	if err != nil {
86		return nil, resp, err
87	}
88
89	return pscs, resp, err
90}
91