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// WebHookPayload represents the data that is received from GitHub when a push
15// event hook is triggered. The format of these payloads pre-date most of the
16// GitHub v3 API, so there are lots of minor incompatibilities with the types
17// defined in the rest of the API. Therefore, several types are duplicated
18// here to account for these differences.
19//
20// GitHub API docs: https://help.github.com/articles/post-receive-hooks
21type WebHookPayload struct {
22	After      *string         `json:"after,omitempty"`
23	Before     *string         `json:"before,omitempty"`
24	Commits    []WebHookCommit `json:"commits,omitempty"`
25	Compare    *string         `json:"compare,omitempty"`
26	Created    *bool           `json:"created,omitempty"`
27	Deleted    *bool           `json:"deleted,omitempty"`
28	Forced     *bool           `json:"forced,omitempty"`
29	HeadCommit *WebHookCommit  `json:"head_commit,omitempty"`
30	Pusher     *User           `json:"pusher,omitempty"`
31	Ref        *string         `json:"ref,omitempty"`
32	Repo       *Repository     `json:"repository,omitempty"`
33	Sender     *User           `json:"sender,omitempty"`
34}
35
36func (w WebHookPayload) String() string {
37	return Stringify(w)
38}
39
40// WebHookCommit represents the commit variant we receive from GitHub in a
41// WebHookPayload.
42type WebHookCommit struct {
43	Added     []string       `json:"added,omitempty"`
44	Author    *WebHookAuthor `json:"author,omitempty"`
45	Committer *WebHookAuthor `json:"committer,omitempty"`
46	Distinct  *bool          `json:"distinct,omitempty"`
47	ID        *string        `json:"id,omitempty"`
48	Message   *string        `json:"message,omitempty"`
49	Modified  []string       `json:"modified,omitempty"`
50	Removed   []string       `json:"removed,omitempty"`
51	Timestamp *time.Time     `json:"timestamp,omitempty"`
52}
53
54func (w WebHookCommit) String() string {
55	return Stringify(w)
56}
57
58// WebHookAuthor represents the author or committer of a commit, as specified
59// in a WebHookCommit. The commit author may not correspond to a GitHub User.
60type WebHookAuthor struct {
61	Email    *string `json:"email,omitempty"`
62	Name     *string `json:"name,omitempty"`
63	Username *string `json:"username,omitempty"`
64}
65
66func (w WebHookAuthor) String() string {
67	return Stringify(w)
68}
69
70// Hook represents a GitHub (web and service) hook for a repository.
71type Hook struct {
72	CreatedAt *time.Time             `json:"created_at,omitempty"`
73	UpdatedAt *time.Time             `json:"updated_at,omitempty"`
74	Name      *string                `json:"name,omitempty"`
75	URL       *string                `json:"url,omitempty"`
76	Events    []string               `json:"events,omitempty"`
77	Active    *bool                  `json:"active,omitempty"`
78	Config    map[string]interface{} `json:"config,omitempty"`
79	ID        *int64                 `json:"id,omitempty"`
80}
81
82func (h Hook) String() string {
83	return Stringify(h)
84}
85
86// CreateHook creates a Hook for the specified repository.
87// Name and Config are required fields.
88//
89// GitHub API docs: https://developer.github.com/v3/repos/hooks/#create-a-hook
90func (s *RepositoriesService) CreateHook(ctx context.Context, owner, repo string, hook *Hook) (*Hook, *Response, error) {
91	u := fmt.Sprintf("repos/%v/%v/hooks", owner, repo)
92	req, err := s.client.NewRequest("POST", u, hook)
93	if err != nil {
94		return nil, nil, err
95	}
96
97	h := new(Hook)
98	resp, err := s.client.Do(ctx, req, h)
99	if err != nil {
100		return nil, resp, err
101	}
102
103	return h, resp, nil
104}
105
106// ListHooks lists all Hooks for the specified repository.
107//
108// GitHub API docs: https://developer.github.com/v3/repos/hooks/#list
109func (s *RepositoriesService) ListHooks(ctx context.Context, owner, repo string, opt *ListOptions) ([]*Hook, *Response, error) {
110	u := fmt.Sprintf("repos/%v/%v/hooks", owner, repo)
111	u, err := addOptions(u, opt)
112	if err != nil {
113		return nil, nil, err
114	}
115
116	req, err := s.client.NewRequest("GET", u, nil)
117	if err != nil {
118		return nil, nil, err
119	}
120
121	var hooks []*Hook
122	resp, err := s.client.Do(ctx, req, &hooks)
123	if err != nil {
124		return nil, resp, err
125	}
126
127	return hooks, resp, nil
128}
129
130// GetHook returns a single specified Hook.
131//
132// GitHub API docs: https://developer.github.com/v3/repos/hooks/#get-single-hook
133func (s *RepositoriesService) GetHook(ctx context.Context, owner, repo string, id int64) (*Hook, *Response, error) {
134	u := fmt.Sprintf("repos/%v/%v/hooks/%d", owner, repo, id)
135	req, err := s.client.NewRequest("GET", u, nil)
136	if err != nil {
137		return nil, nil, err
138	}
139	h := new(Hook)
140	resp, err := s.client.Do(ctx, req, h)
141	if err != nil {
142		return nil, resp, err
143	}
144
145	return h, resp, nil
146}
147
148// EditHook updates a specified Hook.
149//
150// GitHub API docs: https://developer.github.com/v3/repos/hooks/#edit-a-hook
151func (s *RepositoriesService) EditHook(ctx context.Context, owner, repo string, id int64, hook *Hook) (*Hook, *Response, error) {
152	u := fmt.Sprintf("repos/%v/%v/hooks/%d", owner, repo, id)
153	req, err := s.client.NewRequest("PATCH", u, hook)
154	if err != nil {
155		return nil, nil, err
156	}
157	h := new(Hook)
158	resp, err := s.client.Do(ctx, req, h)
159	if err != nil {
160		return nil, resp, err
161	}
162
163	return h, resp, nil
164}
165
166// DeleteHook deletes a specified Hook.
167//
168// GitHub API docs: https://developer.github.com/v3/repos/hooks/#delete-a-hook
169func (s *RepositoriesService) DeleteHook(ctx context.Context, owner, repo string, id int64) (*Response, error) {
170	u := fmt.Sprintf("repos/%v/%v/hooks/%d", owner, repo, id)
171	req, err := s.client.NewRequest("DELETE", u, nil)
172	if err != nil {
173		return nil, err
174	}
175	return s.client.Do(ctx, req, nil)
176}
177
178// PingHook triggers a 'ping' event to be sent to the Hook.
179//
180// GitHub API docs: https://developer.github.com/v3/repos/hooks/#ping-a-hook
181func (s *RepositoriesService) PingHook(ctx context.Context, owner, repo string, id int64) (*Response, error) {
182	u := fmt.Sprintf("repos/%v/%v/hooks/%d/pings", owner, repo, id)
183	req, err := s.client.NewRequest("POST", u, nil)
184	if err != nil {
185		return nil, err
186	}
187	return s.client.Do(ctx, req, nil)
188}
189
190// TestHook triggers a test Hook by github.
191//
192// GitHub API docs: https://developer.github.com/v3/repos/hooks/#test-a-push-hook
193func (s *RepositoriesService) TestHook(ctx context.Context, owner, repo string, id int64) (*Response, error) {
194	u := fmt.Sprintf("repos/%v/%v/hooks/%d/tests", owner, repo, id)
195	req, err := s.client.NewRequest("POST", u, nil)
196	if err != nil {
197		return nil, err
198	}
199	return s.client.Do(ctx, req, nil)
200}
201