...

Source file src/github.com/google/go-github/v45/github/orgs_hooks.go

Documentation: github.com/google/go-github/v45/github

     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/rest/orgs/webhooks#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/rest/orgs/webhooks#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  
    47  	hook := new(Hook)
    48  	resp, err := s.client.Do(ctx, req, hook)
    49  	if err != nil {
    50  		return nil, resp, err
    51  	}
    52  
    53  	return hook, resp, nil
    54  }
    55  
    56  // CreateHook creates a Hook for the specified org.
    57  // Config is a required field.
    58  //
    59  // Note that only a subset of the hook fields are used and hook must
    60  // not be nil.
    61  //
    62  // GitHub API docs: https://docs.github.com/en/rest/orgs/webhooks#create-an-organization-webhook
    63  func (s *OrganizationsService) CreateHook(ctx context.Context, org string, hook *Hook) (*Hook, *Response, error) {
    64  	u := fmt.Sprintf("orgs/%v/hooks", org)
    65  
    66  	hookReq := &createHookRequest{
    67  		Name:   "web",
    68  		Events: hook.Events,
    69  		Active: hook.Active,
    70  		Config: hook.Config,
    71  	}
    72  
    73  	req, err := s.client.NewRequest("POST", u, hookReq)
    74  	if err != nil {
    75  		return nil, nil, err
    76  	}
    77  
    78  	h := new(Hook)
    79  	resp, err := s.client.Do(ctx, req, h)
    80  	if err != nil {
    81  		return nil, resp, err
    82  	}
    83  
    84  	return h, resp, nil
    85  }
    86  
    87  // EditHook updates a specified Hook.
    88  //
    89  // GitHub API docs: https://docs.github.com/en/rest/orgs/webhooks#update-an-organization-webhook
    90  func (s *OrganizationsService) EditHook(ctx context.Context, org string, id int64, hook *Hook) (*Hook, *Response, error) {
    91  	u := fmt.Sprintf("orgs/%v/hooks/%d", org, id)
    92  	req, err := s.client.NewRequest("PATCH", 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  // PingHook triggers a 'ping' event to be sent to the Hook.
   107  //
   108  // GitHub API docs: https://docs.github.com/en/rest/orgs/webhooks#ping-an-organization-webhook
   109  func (s *OrganizationsService) PingHook(ctx context.Context, org string, id int64) (*Response, error) {
   110  	u := fmt.Sprintf("orgs/%v/hooks/%d/pings", org, id)
   111  	req, err := s.client.NewRequest("POST", u, nil)
   112  	if err != nil {
   113  		return nil, err
   114  	}
   115  
   116  	return s.client.Do(ctx, req, nil)
   117  }
   118  
   119  // DeleteHook deletes a specified Hook.
   120  //
   121  // GitHub API docs: https://docs.github.com/en/rest/orgs/webhooks#delete-an-organization-webhook
   122  func (s *OrganizationsService) DeleteHook(ctx context.Context, org string, id int64) (*Response, error) {
   123  	u := fmt.Sprintf("orgs/%v/hooks/%d", org, id)
   124  	req, err := s.client.NewRequest("DELETE", u, nil)
   125  	if err != nil {
   126  		return nil, err
   127  	}
   128  
   129  	return s.client.Do(ctx, req, nil)
   130  }
   131  

View as plain text