github.com/google/go-github/v33@v33.0.0/github/orgs_hooks.go (about) 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 6 package github 7 8 import ( 9 "context" 10 "fmt" 11 ) 12 13 // ListHooks lists all Hooks for the specified organization. 14 // 15 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#list-organization-webhooks 16 func (s *OrganizationsService) ListHooks(ctx context.Context, org string, opts *ListOptions) ([]*Hook, *Response, error) { 17 u := fmt.Sprintf("orgs/%v/hooks", org) 18 u, err := addOptions(u, opts) 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://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#get-an-organization-webhook 40 func (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 // Config is a required field. 53 // 54 // Note that only a subset of the hook fields are used and hook must 55 // not be nil. 56 // 57 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#create-an-organization-webhook 58 func (s *OrganizationsService) CreateHook(ctx context.Context, org string, hook *Hook) (*Hook, *Response, error) { 59 u := fmt.Sprintf("orgs/%v/hooks", org) 60 61 hookReq := &createHookRequest{ 62 Name: "web", 63 Events: hook.Events, 64 Active: hook.Active, 65 Config: hook.Config, 66 } 67 68 req, err := s.client.NewRequest("POST", u, hookReq) 69 if err != nil { 70 return nil, nil, err 71 } 72 73 h := new(Hook) 74 resp, err := s.client.Do(ctx, req, h) 75 if err != nil { 76 return nil, resp, err 77 } 78 79 return h, resp, nil 80 } 81 82 // EditHook updates a specified Hook. 83 // 84 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#update-an-organization-webhook 85 func (s *OrganizationsService) EditHook(ctx context.Context, org string, id int64, hook *Hook) (*Hook, *Response, error) { 86 u := fmt.Sprintf("orgs/%v/hooks/%d", org, id) 87 req, err := s.client.NewRequest("PATCH", u, hook) 88 if err != nil { 89 return nil, nil, err 90 } 91 h := new(Hook) 92 resp, err := s.client.Do(ctx, req, h) 93 return h, resp, err 94 } 95 96 // PingHook triggers a 'ping' event to be sent to the Hook. 97 // 98 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#ping-an-organization-webhook 99 func (s *OrganizationsService) PingHook(ctx context.Context, org string, id int64) (*Response, error) { 100 u := fmt.Sprintf("orgs/%v/hooks/%d/pings", org, id) 101 req, err := s.client.NewRequest("POST", u, nil) 102 if err != nil { 103 return nil, err 104 } 105 return s.client.Do(ctx, req, nil) 106 } 107 108 // DeleteHook deletes a specified Hook. 109 // 110 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#delete-an-organization-webhook 111 func (s *OrganizationsService) DeleteHook(ctx context.Context, org string, id int64) (*Response, error) { 112 u := fmt.Sprintf("orgs/%v/hooks/%d", org, id) 113 req, err := s.client.NewRequest("DELETE", u, nil) 114 if err != nil { 115 return nil, err 116 } 117 return s.client.Do(ctx, req, nil) 118 }