...

Source file src/github.com/google/go-github/v33/github/repos_prereceive_hooks.go

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

     1  // Copyright 2018 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  // PreReceiveHook represents a GitHub pre-receive hook for a repository.
    14  type PreReceiveHook struct {
    15  	ID          *int64  `json:"id,omitempty"`
    16  	Name        *string `json:"name,omitempty"`
    17  	Enforcement *string `json:"enforcement,omitempty"`
    18  	ConfigURL   *string `json:"configuration_url,omitempty"`
    19  }
    20  
    21  func (p PreReceiveHook) String() string {
    22  	return Stringify(p)
    23  }
    24  
    25  // ListPreReceiveHooks lists all pre-receive hooks for the specified repository.
    26  //
    27  // GitHub API docs: https://developer.github.com/enterprise/2.13/v3/repos/pre_receive_hooks/#list-pre-receive-hooks
    28  func (s *RepositoriesService) ListPreReceiveHooks(ctx context.Context, owner, repo string, opts *ListOptions) ([]*PreReceiveHook, *Response, error) {
    29  	u := fmt.Sprintf("repos/%v/%v/pre-receive-hooks", owner, repo)
    30  	u, err := addOptions(u, opts)
    31  	if err != nil {
    32  		return nil, nil, err
    33  	}
    34  
    35  	req, err := s.client.NewRequest("GET", u, nil)
    36  	if err != nil {
    37  		return nil, nil, err
    38  	}
    39  
    40  	// TODO: remove custom Accept header when this API fully launches.
    41  	req.Header.Set("Accept", mediaTypePreReceiveHooksPreview)
    42  
    43  	var hooks []*PreReceiveHook
    44  	resp, err := s.client.Do(ctx, req, &hooks)
    45  	if err != nil {
    46  		return nil, resp, err
    47  	}
    48  
    49  	return hooks, resp, nil
    50  }
    51  
    52  // GetPreReceiveHook returns a single specified pre-receive hook.
    53  //
    54  // GitHub API docs: https://developer.github.com/enterprise/2.13/v3/repos/pre_receive_hooks/#get-a-single-pre-receive-hook
    55  func (s *RepositoriesService) GetPreReceiveHook(ctx context.Context, owner, repo string, id int64) (*PreReceiveHook, *Response, error) {
    56  	u := fmt.Sprintf("repos/%v/%v/pre-receive-hooks/%d", owner, repo, id)
    57  	req, err := s.client.NewRequest("GET", u, nil)
    58  	if err != nil {
    59  		return nil, nil, err
    60  	}
    61  
    62  	// TODO: remove custom Accept header when this API fully launches.
    63  	req.Header.Set("Accept", mediaTypePreReceiveHooksPreview)
    64  
    65  	h := new(PreReceiveHook)
    66  	resp, err := s.client.Do(ctx, req, h)
    67  	if err != nil {
    68  		return nil, resp, err
    69  	}
    70  
    71  	return h, resp, nil
    72  }
    73  
    74  // UpdatePreReceiveHook updates a specified pre-receive hook.
    75  //
    76  // GitHub API docs: https://developer.github.com/enterprise/2.13/v3/repos/pre_receive_hooks/#update-pre-receive-hook-enforcement
    77  func (s *RepositoriesService) UpdatePreReceiveHook(ctx context.Context, owner, repo string, id int64, hook *PreReceiveHook) (*PreReceiveHook, *Response, error) {
    78  	u := fmt.Sprintf("repos/%v/%v/pre-receive-hooks/%d", owner, repo, id)
    79  	req, err := s.client.NewRequest("PATCH", u, hook)
    80  	if err != nil {
    81  		return nil, nil, err
    82  	}
    83  
    84  	// TODO: remove custom Accept header when this API fully launches.
    85  	req.Header.Set("Accept", mediaTypePreReceiveHooksPreview)
    86  
    87  	h := new(PreReceiveHook)
    88  	resp, err := s.client.Do(ctx, req, h)
    89  	if err != nil {
    90  		return nil, resp, err
    91  	}
    92  
    93  	return h, resp, nil
    94  }
    95  
    96  // DeletePreReceiveHook deletes a specified pre-receive hook.
    97  //
    98  // GitHub API docs: https://developer.github.com/enterprise/2.13/v3/repos/pre_receive_hooks/#remove-enforcement-overrides-for-a-pre-receive-hook
    99  func (s *RepositoriesService) DeletePreReceiveHook(ctx context.Context, owner, repo string, id int64) (*Response, error) {
   100  	u := fmt.Sprintf("repos/%v/%v/pre-receive-hooks/%d", owner, repo, id)
   101  	req, err := s.client.NewRequest("DELETE", u, nil)
   102  	if err != nil {
   103  		return nil, err
   104  	}
   105  
   106  	// TODO: remove custom Accept header when this API fully launches.
   107  	req.Header.Set("Accept", mediaTypePreReceiveHooksPreview)
   108  
   109  	return s.client.Do(ctx, req, nil)
   110  }
   111  

View as plain text