...

Source file src/github.com/xanzy/go-gitlab/group_hooks.go

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, Eric Stevens
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  package gitlab
    18  
    19  import (
    20  	"fmt"
    21  	"net/http"
    22  	"time"
    23  )
    24  
    25  // GroupHook represents a GitLab group hook.
    26  //
    27  // GitLab API docs: https://docs.gitlab.com/ee/api/groups.html#list-group-hooks
    28  type GroupHook struct {
    29  	ID                       int        `json:"id"`
    30  	URL                      string     `json:"url"`
    31  	GroupID                  int        `json:"group_id"`
    32  	PushEvents               bool       `json:"push_events"`
    33  	PushEventsBranchFilter   string     `json:"push_events_branch_filter"`
    34  	IssuesEvents             bool       `json:"issues_events"`
    35  	ConfidentialIssuesEvents bool       `json:"confidential_issues_events"`
    36  	ConfidentialNoteEvents   bool       `json:"confidential_note_events"`
    37  	MergeRequestsEvents      bool       `json:"merge_requests_events"`
    38  	TagPushEvents            bool       `json:"tag_push_events"`
    39  	NoteEvents               bool       `json:"note_events"`
    40  	JobEvents                bool       `json:"job_events"`
    41  	PipelineEvents           bool       `json:"pipeline_events"`
    42  	WikiPageEvents           bool       `json:"wiki_page_events"`
    43  	DeploymentEvents         bool       `json:"deployment_events"`
    44  	ReleasesEvents           bool       `json:"releases_events"`
    45  	SubGroupEvents           bool       `json:"subgroup_events"`
    46  	EnableSSLVerification    bool       `json:"enable_ssl_verification"`
    47  	AlertStatus              string     `json:"alert_status"`
    48  	CreatedAt                *time.Time `json:"created_at"`
    49  	CustomWebhookTemplate    string     `json:"custom_webhook_template"`
    50  }
    51  
    52  // ListGroupHooksOptions represents the available ListGroupHooks() options.
    53  //
    54  // GitLab API docs: https://docs.gitlab.com/ee/api/groups.html#list-group-hooks
    55  type ListGroupHooksOptions ListOptions
    56  
    57  // ListGroupHooks gets a list of group hooks.
    58  //
    59  // GitLab API docs: https://docs.gitlab.com/ee/api/groups.html#list-group-hooks
    60  func (s *GroupsService) ListGroupHooks(gid interface{}, opt *ListGroupHooksOptions, options ...RequestOptionFunc) ([]*GroupHook, *Response, error) {
    61  	group, err := parseID(gid)
    62  	if err != nil {
    63  		return nil, nil, err
    64  	}
    65  	u := fmt.Sprintf("groups/%s/hooks", PathEscape(group))
    66  
    67  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
    68  	if err != nil {
    69  		return nil, nil, err
    70  	}
    71  	var gh []*GroupHook
    72  	resp, err := s.client.Do(req, &gh)
    73  	if err != nil {
    74  		return nil, resp, err
    75  	}
    76  
    77  	return gh, resp, nil
    78  }
    79  
    80  // GetGroupHook gets a specific hook for a group.
    81  //
    82  // GitLab API docs:
    83  // https://docs.gitlab.com/ee/api/groups.html#get-group-hook
    84  func (s *GroupsService) GetGroupHook(pid interface{}, hook int, options ...RequestOptionFunc) (*GroupHook, *Response, error) {
    85  	group, err := parseID(pid)
    86  	if err != nil {
    87  		return nil, nil, err
    88  	}
    89  	u := fmt.Sprintf("groups/%s/hooks/%d", PathEscape(group), hook)
    90  
    91  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
    92  	if err != nil {
    93  		return nil, nil, err
    94  	}
    95  
    96  	gh := new(GroupHook)
    97  	resp, err := s.client.Do(req, gh)
    98  	if err != nil {
    99  		return nil, resp, err
   100  	}
   101  
   102  	return gh, resp, nil
   103  }
   104  
   105  // AddGroupHookOptions represents the available AddGroupHook() options.
   106  //
   107  // GitLab API docs: https://docs.gitlab.com/ee/api/groups.html#add-group-hook
   108  type AddGroupHookOptions struct {
   109  	URL                      *string `url:"url,omitempty" json:"url,omitempty"`
   110  	PushEvents               *bool   `url:"push_events,omitempty"  json:"push_events,omitempty"`
   111  	PushEventsBranchFilter   *string `url:"push_events_branch_filter,omitempty"  json:"push_events_branch_filter,omitempty"`
   112  	IssuesEvents             *bool   `url:"issues_events,omitempty"  json:"issues_events,omitempty"`
   113  	ConfidentialIssuesEvents *bool   `url:"confidential_issues_events,omitempty"  json:"confidential_issues_events,omitempty"`
   114  	ConfidentialNoteEvents   *bool   `url:"confidential_note_events,omitempty"  json:"confidential_note_events,omitempty"`
   115  	MergeRequestsEvents      *bool   `url:"merge_requests_events,omitempty"  json:"merge_requests_events,omitempty"`
   116  	TagPushEvents            *bool   `url:"tag_push_events,omitempty"  json:"tag_push_events,omitempty"`
   117  	NoteEvents               *bool   `url:"note_events,omitempty"  json:"note_events,omitempty"`
   118  	JobEvents                *bool   `url:"job_events,omitempty"  json:"job_events,omitempty"`
   119  	PipelineEvents           *bool   `url:"pipeline_events,omitempty"  json:"pipeline_events,omitempty"`
   120  	WikiPageEvents           *bool   `url:"wiki_page_events,omitempty"  json:"wiki_page_events,omitempty"`
   121  	DeploymentEvents         *bool   `url:"deployment_events,omitempty" json:"deployment_events,omitempty"`
   122  	ReleasesEvents           *bool   `url:"releases_events,omitempty" json:"releases_events,omitempty"`
   123  	SubGroupEvents           *bool   `url:"subgroup_events,omitempty" json:"subgroup_events,omitempty"`
   124  	EnableSSLVerification    *bool   `url:"enable_ssl_verification,omitempty"  json:"enable_ssl_verification,omitempty"`
   125  	Token                    *string `url:"token,omitempty" json:"token,omitempty"`
   126  	CustomWebhookTemplate    *string `url:"custom_webhook_template,omitempty" json:"custom_webhook_template,omitempty"`
   127  }
   128  
   129  // AddGroupHook create a new group scoped webhook.
   130  //
   131  // GitLab API docs: https://docs.gitlab.com/ee/api/groups.html#add-group-hook
   132  func (s *GroupsService) AddGroupHook(gid interface{}, opt *AddGroupHookOptions, options ...RequestOptionFunc) (*GroupHook, *Response, error) {
   133  	group, err := parseID(gid)
   134  	if err != nil {
   135  		return nil, nil, err
   136  	}
   137  	u := fmt.Sprintf("groups/%s/hooks", PathEscape(group))
   138  
   139  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   140  	if err != nil {
   141  		return nil, nil, err
   142  	}
   143  
   144  	gh := new(GroupHook)
   145  	resp, err := s.client.Do(req, gh)
   146  	if err != nil {
   147  		return nil, resp, err
   148  	}
   149  
   150  	return gh, resp, nil
   151  }
   152  
   153  // EditGroupHookOptions represents the available EditGroupHook() options.
   154  //
   155  // GitLab API docs:
   156  // https://docs.gitlab.com/ee/api/groups.html#edit-group-hook
   157  type EditGroupHookOptions struct {
   158  	URL                      *string `url:"url,omitempty" json:"url,omitempty"`
   159  	PushEvents               *bool   `url:"push_events,omitempty" json:"push_events,omitempty"`
   160  	PushEventsBranchFilter   *string `url:"push_events_branch_filter,omitempty"  json:"push_events_branch_filter,omitempty"`
   161  	IssuesEvents             *bool   `url:"issues_events,omitempty" json:"issues_events,omitempty"`
   162  	ConfidentialIssuesEvents *bool   `url:"confidential_issues_events,omitempty" json:"confidential_issues_events,omitempty"`
   163  	ConfidentialNoteEvents   *bool   `url:"confidential_note_events,omitempty" json:"confidential_note_events,omitempty"`
   164  	MergeRequestsEvents      *bool   `url:"merge_requests_events,omitempty" json:"merge_requests_events,omitempty"`
   165  	TagPushEvents            *bool   `url:"tag_push_events,omitempty" json:"tag_push_events,omitempty"`
   166  	NoteEvents               *bool   `url:"note_events,omitempty" json:"note_events,omitempty"`
   167  	JobEvents                *bool   `url:"job_events,omitempty" json:"job_events,omitempty"`
   168  	PipelineEvents           *bool   `url:"pipeline_events,omitempty" json:"pipeline_events,omitempty"`
   169  	WikiPageEvents           *bool   `url:"wiki_page_events,omitempty" json:"wiki_page_events,omitempty"`
   170  	DeploymentEvents         *bool   `url:"deployment_events,omitempty" json:"deployment_events,omitempty"`
   171  	ReleasesEvents           *bool   `url:"releases_events,omitempty" json:"releases_events,omitempty"`
   172  	SubGroupEvents           *bool   `url:"subgroup_events,omitempty" json:"subgroup_events,omitempty"`
   173  	EnableSSLVerification    *bool   `url:"enable_ssl_verification,omitempty" json:"enable_ssl_verification,omitempty"`
   174  	Token                    *string `url:"token,omitempty" json:"token,omitempty"`
   175  	CustomWebhookTemplate    *string `url:"custom_webhook_template,omitempty" json:"custom_webhook_template,omitempty"`
   176  }
   177  
   178  // EditGroupHook edits a hook for a specified group.
   179  //
   180  // Gitlab API docs:
   181  // https://docs.gitlab.com/ee/api/groups.html#edit-group-hook
   182  func (s *GroupsService) EditGroupHook(pid interface{}, hook int, opt *EditGroupHookOptions, options ...RequestOptionFunc) (*GroupHook, *Response, error) {
   183  	group, err := parseID(pid)
   184  	if err != nil {
   185  		return nil, nil, err
   186  	}
   187  	u := fmt.Sprintf("groups/%s/hooks/%d", PathEscape(group), hook)
   188  
   189  	req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
   190  	if err != nil {
   191  		return nil, nil, err
   192  	}
   193  
   194  	gh := new(GroupHook)
   195  	resp, err := s.client.Do(req, gh)
   196  	if err != nil {
   197  		return nil, resp, err
   198  	}
   199  
   200  	return gh, resp, nil
   201  }
   202  
   203  // DeleteGroupHook removes a hook from a group. This is an idempotent
   204  // method and can be called multiple times.
   205  //
   206  // GitLab API docs:
   207  // https://docs.gitlab.com/ee/api/groups.html#delete-group-hook
   208  func (s *GroupsService) DeleteGroupHook(pid interface{}, hook int, options ...RequestOptionFunc) (*Response, error) {
   209  	group, err := parseID(pid)
   210  	if err != nil {
   211  		return nil, err
   212  	}
   213  	u := fmt.Sprintf("groups/%s/hooks/%d", PathEscape(group), hook)
   214  
   215  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   216  	if err != nil {
   217  		return nil, err
   218  	}
   219  
   220  	return s.client.Do(req, nil)
   221  }
   222  

View as plain text