github.com/google/go-github/v70@v70.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/rest/orgs/webhooks#list-organization-webhooks
    16  //
    17  //meta:operation GET /orgs/{org}/hooks
    18  func (s *OrganizationsService) ListHooks(ctx context.Context, org string, opts *ListOptions) ([]*Hook, *Response, error) {
    19  	u := fmt.Sprintf("orgs/%v/hooks", org)
    20  	u, err := addOptions(u, opts)
    21  	if err != nil {
    22  		return nil, nil, err
    23  	}
    24  
    25  	req, err := s.client.NewRequest("GET", u, nil)
    26  	if err != nil {
    27  		return nil, nil, err
    28  	}
    29  
    30  	var hooks []*Hook
    31  	resp, err := s.client.Do(ctx, req, &hooks)
    32  	if err != nil {
    33  		return nil, resp, err
    34  	}
    35  
    36  	return hooks, resp, nil
    37  }
    38  
    39  // GetHook returns a single specified Hook.
    40  //
    41  // GitHub API docs: https://docs.github.com/rest/orgs/webhooks#get-an-organization-webhook
    42  //
    43  //meta:operation GET /orgs/{org}/hooks/{hook_id}
    44  func (s *OrganizationsService) GetHook(ctx context.Context, org string, id int64) (*Hook, *Response, error) {
    45  	u := fmt.Sprintf("orgs/%v/hooks/%d", org, id)
    46  	req, err := s.client.NewRequest("GET", u, nil)
    47  	if err != nil {
    48  		return nil, nil, err
    49  	}
    50  
    51  	hook := new(Hook)
    52  	resp, err := s.client.Do(ctx, req, hook)
    53  	if err != nil {
    54  		return nil, resp, err
    55  	}
    56  
    57  	return hook, resp, nil
    58  }
    59  
    60  // CreateHook creates a Hook for the specified org.
    61  // Config is a required field.
    62  //
    63  // Note that only a subset of the hook fields are used and hook must
    64  // not be nil.
    65  //
    66  // GitHub API docs: https://docs.github.com/rest/orgs/webhooks#create-an-organization-webhook
    67  //
    68  //meta:operation POST /orgs/{org}/hooks
    69  func (s *OrganizationsService) CreateHook(ctx context.Context, org string, hook *Hook) (*Hook, *Response, error) {
    70  	u := fmt.Sprintf("orgs/%v/hooks", org)
    71  
    72  	hookReq := &createHookRequest{
    73  		Name:   "web",
    74  		Events: hook.Events,
    75  		Active: hook.Active,
    76  		Config: hook.Config,
    77  	}
    78  
    79  	req, err := s.client.NewRequest("POST", u, hookReq)
    80  	if err != nil {
    81  		return nil, nil, err
    82  	}
    83  
    84  	h := new(Hook)
    85  	resp, err := s.client.Do(ctx, req, h)
    86  	if err != nil {
    87  		return nil, resp, err
    88  	}
    89  
    90  	return h, resp, nil
    91  }
    92  
    93  // EditHook updates a specified Hook.
    94  //
    95  // GitHub API docs: https://docs.github.com/rest/orgs/webhooks#update-an-organization-webhook
    96  //
    97  //meta:operation PATCH /orgs/{org}/hooks/{hook_id}
    98  func (s *OrganizationsService) EditHook(ctx context.Context, org string, id int64, hook *Hook) (*Hook, *Response, error) {
    99  	u := fmt.Sprintf("orgs/%v/hooks/%d", org, id)
   100  	req, err := s.client.NewRequest("PATCH", u, hook)
   101  	if err != nil {
   102  		return nil, nil, err
   103  	}
   104  
   105  	h := new(Hook)
   106  	resp, err := s.client.Do(ctx, req, h)
   107  	if err != nil {
   108  		return nil, resp, err
   109  	}
   110  
   111  	return h, resp, nil
   112  }
   113  
   114  // PingHook triggers a 'ping' event to be sent to the Hook.
   115  //
   116  // GitHub API docs: https://docs.github.com/rest/orgs/webhooks#ping-an-organization-webhook
   117  //
   118  //meta:operation POST /orgs/{org}/hooks/{hook_id}/pings
   119  func (s *OrganizationsService) PingHook(ctx context.Context, org string, id int64) (*Response, error) {
   120  	u := fmt.Sprintf("orgs/%v/hooks/%d/pings", org, id)
   121  	req, err := s.client.NewRequest("POST", u, nil)
   122  	if err != nil {
   123  		return nil, err
   124  	}
   125  
   126  	return s.client.Do(ctx, req, nil)
   127  }
   128  
   129  // DeleteHook deletes a specified Hook.
   130  //
   131  // GitHub API docs: https://docs.github.com/rest/orgs/webhooks#delete-an-organization-webhook
   132  //
   133  //meta:operation DELETE /orgs/{org}/hooks/{hook_id}
   134  func (s *OrganizationsService) DeleteHook(ctx context.Context, org string, id int64) (*Response, error) {
   135  	u := fmt.Sprintf("orgs/%v/hooks/%d", org, id)
   136  	req, err := s.client.NewRequest("DELETE", u, nil)
   137  	if err != nil {
   138  		return nil, err
   139  	}
   140  
   141  	return s.client.Do(ctx, req, nil)
   142  }