github.com/google/go-github/v42@v42.0.0/github/repos_hooks.go (about)

     1  // Copyright 2013 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  	"time"
    12  )
    13  
    14  // WebHookPayload represents the data that is received from GitHub when a push
    15  // event hook is triggered. The format of these payloads pre-date most of the
    16  // GitHub v3 API, so there are lots of minor incompatibilities with the types
    17  // defined in the rest of the API. Therefore, several types are duplicated
    18  // here to account for these differences.
    19  //
    20  // GitHub API docs: https://help.github.com/articles/post-receive-hooks
    21  type WebHookPayload struct {
    22  	Action       *string          `json:"action,omitempty"`
    23  	After        *string          `json:"after,omitempty"`
    24  	Before       *string          `json:"before,omitempty"`
    25  	Commits      []*WebHookCommit `json:"commits,omitempty"`
    26  	Compare      *string          `json:"compare,omitempty"`
    27  	Created      *bool            `json:"created,omitempty"`
    28  	Deleted      *bool            `json:"deleted,omitempty"`
    29  	Forced       *bool            `json:"forced,omitempty"`
    30  	HeadCommit   *WebHookCommit   `json:"head_commit,omitempty"`
    31  	Installation *Installation    `json:"installation,omitempty"`
    32  	Organization *Organization    `json:"organization,omitempty"`
    33  	Pusher       *User            `json:"pusher,omitempty"`
    34  	Ref          *string          `json:"ref,omitempty"`
    35  	Repo         *Repository      `json:"repository,omitempty"`
    36  	Sender       *User            `json:"sender,omitempty"`
    37  }
    38  
    39  func (w WebHookPayload) String() string {
    40  	return Stringify(w)
    41  }
    42  
    43  // WebHookCommit represents the commit variant we receive from GitHub in a
    44  // WebHookPayload.
    45  type WebHookCommit struct {
    46  	Added     []string       `json:"added,omitempty"`
    47  	Author    *WebHookAuthor `json:"author,omitempty"`
    48  	Committer *WebHookAuthor `json:"committer,omitempty"`
    49  	Distinct  *bool          `json:"distinct,omitempty"`
    50  	ID        *string        `json:"id,omitempty"`
    51  	Message   *string        `json:"message,omitempty"`
    52  	Modified  []string       `json:"modified,omitempty"`
    53  	Removed   []string       `json:"removed,omitempty"`
    54  	Timestamp *time.Time     `json:"timestamp,omitempty"`
    55  }
    56  
    57  func (w WebHookCommit) String() string {
    58  	return Stringify(w)
    59  }
    60  
    61  // WebHookAuthor represents the author or committer of a commit, as specified
    62  // in a WebHookCommit. The commit author may not correspond to a GitHub User.
    63  type WebHookAuthor struct {
    64  	Email    *string `json:"email,omitempty"`
    65  	Name     *string `json:"name,omitempty"`
    66  	Username *string `json:"username,omitempty"`
    67  }
    68  
    69  func (w WebHookAuthor) String() string {
    70  	return Stringify(w)
    71  }
    72  
    73  // Hook represents a GitHub (web and service) hook for a repository.
    74  type Hook struct {
    75  	CreatedAt    *time.Time             `json:"created_at,omitempty"`
    76  	UpdatedAt    *time.Time             `json:"updated_at,omitempty"`
    77  	URL          *string                `json:"url,omitempty"`
    78  	ID           *int64                 `json:"id,omitempty"`
    79  	Type         *string                `json:"type,omitempty"`
    80  	Name         *string                `json:"name,omitempty"`
    81  	TestURL      *string                `json:"test_url,omitempty"`
    82  	PingURL      *string                `json:"ping_url,omitempty"`
    83  	LastResponse map[string]interface{} `json:"last_response,omitempty"`
    84  
    85  	// Only the following fields are used when creating a hook.
    86  	// Config is required.
    87  	Config map[string]interface{} `json:"config,omitempty"`
    88  	Events []string               `json:"events,omitempty"`
    89  	Active *bool                  `json:"active,omitempty"`
    90  }
    91  
    92  func (h Hook) String() string {
    93  	return Stringify(h)
    94  }
    95  
    96  // createHookRequest is a subset of Hook and is used internally
    97  // by CreateHook to pass only the known fields for the endpoint.
    98  //
    99  // See https://github.com/google/go-github/issues/1015 for more
   100  // information.
   101  type createHookRequest struct {
   102  	// Config is required.
   103  	Name   string                 `json:"name"`
   104  	Config map[string]interface{} `json:"config,omitempty"`
   105  	Events []string               `json:"events,omitempty"`
   106  	Active *bool                  `json:"active,omitempty"`
   107  }
   108  
   109  // CreateHook creates a Hook for the specified repository.
   110  // Config is a required field.
   111  //
   112  // Note that only a subset of the hook fields are used and hook must
   113  // not be nil.
   114  //
   115  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#create-a-repository-webhook
   116  func (s *RepositoriesService) CreateHook(ctx context.Context, owner, repo string, hook *Hook) (*Hook, *Response, error) {
   117  	u := fmt.Sprintf("repos/%v/%v/hooks", owner, repo)
   118  
   119  	hookReq := &createHookRequest{
   120  		Name:   "web",
   121  		Events: hook.Events,
   122  		Active: hook.Active,
   123  		Config: hook.Config,
   124  	}
   125  
   126  	req, err := s.client.NewRequest("POST", u, hookReq)
   127  	if err != nil {
   128  		return nil, nil, err
   129  	}
   130  
   131  	h := new(Hook)
   132  	resp, err := s.client.Do(ctx, req, h)
   133  	if err != nil {
   134  		return nil, resp, err
   135  	}
   136  
   137  	return h, resp, nil
   138  }
   139  
   140  // ListHooks lists all Hooks for the specified repository.
   141  //
   142  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#list-repository-webhooks
   143  func (s *RepositoriesService) ListHooks(ctx context.Context, owner, repo string, opts *ListOptions) ([]*Hook, *Response, error) {
   144  	u := fmt.Sprintf("repos/%v/%v/hooks", owner, repo)
   145  	u, err := addOptions(u, opts)
   146  	if err != nil {
   147  		return nil, nil, err
   148  	}
   149  
   150  	req, err := s.client.NewRequest("GET", u, nil)
   151  	if err != nil {
   152  		return nil, nil, err
   153  	}
   154  
   155  	var hooks []*Hook
   156  	resp, err := s.client.Do(ctx, req, &hooks)
   157  	if err != nil {
   158  		return nil, resp, err
   159  	}
   160  
   161  	return hooks, resp, nil
   162  }
   163  
   164  // GetHook returns a single specified Hook.
   165  //
   166  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#get-a-repository-webhook
   167  func (s *RepositoriesService) GetHook(ctx context.Context, owner, repo string, id int64) (*Hook, *Response, error) {
   168  	u := fmt.Sprintf("repos/%v/%v/hooks/%d", owner, repo, id)
   169  	req, err := s.client.NewRequest("GET", u, nil)
   170  	if err != nil {
   171  		return nil, nil, err
   172  	}
   173  	h := new(Hook)
   174  	resp, err := s.client.Do(ctx, req, h)
   175  	if err != nil {
   176  		return nil, resp, err
   177  	}
   178  
   179  	return h, resp, nil
   180  }
   181  
   182  // EditHook updates a specified Hook.
   183  //
   184  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#update-a-repository-webhook
   185  func (s *RepositoriesService) EditHook(ctx context.Context, owner, repo string, id int64, hook *Hook) (*Hook, *Response, error) {
   186  	u := fmt.Sprintf("repos/%v/%v/hooks/%d", owner, repo, id)
   187  	req, err := s.client.NewRequest("PATCH", u, hook)
   188  	if err != nil {
   189  		return nil, nil, err
   190  	}
   191  	h := new(Hook)
   192  	resp, err := s.client.Do(ctx, req, h)
   193  	if err != nil {
   194  		return nil, resp, err
   195  	}
   196  
   197  	return h, resp, nil
   198  }
   199  
   200  // DeleteHook deletes a specified Hook.
   201  //
   202  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#delete-a-repository-webhook
   203  func (s *RepositoriesService) DeleteHook(ctx context.Context, owner, repo string, id int64) (*Response, error) {
   204  	u := fmt.Sprintf("repos/%v/%v/hooks/%d", owner, repo, id)
   205  	req, err := s.client.NewRequest("DELETE", u, nil)
   206  	if err != nil {
   207  		return nil, err
   208  	}
   209  	return s.client.Do(ctx, req, nil)
   210  }
   211  
   212  // PingHook triggers a 'ping' event to be sent to the Hook.
   213  //
   214  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#ping-a-repository-webhook
   215  func (s *RepositoriesService) PingHook(ctx context.Context, owner, repo string, id int64) (*Response, error) {
   216  	u := fmt.Sprintf("repos/%v/%v/hooks/%d/pings", owner, repo, id)
   217  	req, err := s.client.NewRequest("POST", u, nil)
   218  	if err != nil {
   219  		return nil, err
   220  	}
   221  	return s.client.Do(ctx, req, nil)
   222  }
   223  
   224  // TestHook triggers a test Hook by github.
   225  //
   226  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#test-the-push-repository-webhook
   227  func (s *RepositoriesService) TestHook(ctx context.Context, owner, repo string, id int64) (*Response, error) {
   228  	u := fmt.Sprintf("repos/%v/%v/hooks/%d/tests", owner, repo, id)
   229  	req, err := s.client.NewRequest("POST", u, nil)
   230  	if err != nil {
   231  		return nil, err
   232  	}
   233  	return s.client.Do(ctx, req, nil)
   234  }