1// Copyright 2015 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)
12
13// ListHooks lists all Hooks for the specified organization.
14//
15// GitHub API docs: https://developer.github.com/v3/orgs/hooks/#list-hooks
16func (s *OrganizationsService) ListHooks(ctx context.Context, org string, opt *ListOptions) ([]*Hook, *Response, error) {
17	u := fmt.Sprintf("orgs/%v/hooks", org)
18	u, err := addOptions(u, opt)
19	if err != nil {
20		return nil, nil, err
21	}
22
23	req, err := s.client.NewRequest("GET", u, nil)
24	if err != nil {
25		return nil, nil, err
26	}
27
28	var hooks []*Hook
29	resp, err := s.client.Do(ctx, req, &hooks)
30	if err != nil {
31		return nil, resp, err
32	}
33
34	return hooks, resp, nil
35}
36
37// GetHook returns a single specified Hook.
38//
39// GitHub API docs: https://developer.github.com/v3/orgs/hooks/#get-single-hook
40func (s *OrganizationsService) GetHook(ctx context.Context, org string, id int64) (*Hook, *Response, error) {
41	u := fmt.Sprintf("orgs/%v/hooks/%d", org, id)
42	req, err := s.client.NewRequest("GET", u, nil)
43	if err != nil {
44		return nil, nil, err
45	}
46	hook := new(Hook)
47	resp, err := s.client.Do(ctx, req, hook)
48	return hook, resp, err
49}
50
51// CreateHook creates a Hook for the specified org.
52// Name and Config are required fields.
53//
54// GitHub API docs: https://developer.github.com/v3/orgs/hooks/#create-a-hook
55func (s *OrganizationsService) CreateHook(ctx context.Context, org string, hook *Hook) (*Hook, *Response, error) {
56	u := fmt.Sprintf("orgs/%v/hooks", org)
57	req, err := s.client.NewRequest("POST", u, hook)
58	if err != nil {
59		return nil, nil, err
60	}
61
62	h := new(Hook)
63	resp, err := s.client.Do(ctx, req, h)
64	if err != nil {
65		return nil, resp, err
66	}
67
68	return h, resp, nil
69}
70
71// EditHook updates a specified Hook.
72//
73// GitHub API docs: https://developer.github.com/v3/orgs/hooks/#edit-a-hook
74func (s *OrganizationsService) EditHook(ctx context.Context, org string, id int64, hook *Hook) (*Hook, *Response, error) {
75	u := fmt.Sprintf("orgs/%v/hooks/%d", org, id)
76	req, err := s.client.NewRequest("PATCH", u, hook)
77	if err != nil {
78		return nil, nil, err
79	}
80	h := new(Hook)
81	resp, err := s.client.Do(ctx, req, h)
82	return h, resp, err
83}
84
85// PingHook triggers a 'ping' event to be sent to the Hook.
86//
87// GitHub API docs: https://developer.github.com/v3/orgs/hooks/#ping-a-hook
88func (s *OrganizationsService) PingHook(ctx context.Context, org string, id int64) (*Response, error) {
89	u := fmt.Sprintf("orgs/%v/hooks/%d/pings", org, id)
90	req, err := s.client.NewRequest("POST", u, nil)
91	if err != nil {
92		return nil, err
93	}
94	return s.client.Do(ctx, req, nil)
95}
96
97// DeleteHook deletes a specified Hook.
98//
99// GitHub API docs: https://developer.github.com/v3/orgs/hooks/#delete-a-hook
100func (s *OrganizationsService) DeleteHook(ctx context.Context, org string, id int64) (*Response, error) {
101	u := fmt.Sprintf("orgs/%v/hooks/%d", org, id)
102	req, err := s.client.NewRequest("DELETE", u, nil)
103	if err != nil {
104		return nil, err
105	}
106	return s.client.Do(ctx, req, nil)
107}
108