Skip to content

Commit

Permalink
Add support for custom webhook headers
Browse files Browse the repository at this point in the history
  • Loading branch information
RicePatrick committed Jun 5, 2024
1 parent fc07191 commit a5171a1
Show file tree
Hide file tree
Showing 2 changed files with 259 additions and 61 deletions.
173 changes: 117 additions & 56 deletions projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -1197,31 +1197,43 @@ type ProjectMember struct {
AvatarURL string `json:"avatar_url"`
}

// ProjectHookCustomHeader represents a project hook custom header
// Note: "Key" is returned from the Get operation, but "Value" is not
// The List operation doesn't return any headers at all.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/projects.html#list-project-hooks
type ProjectHookCustomHeader struct {
Key string `json:"key"`
Value string `json:"value"`
}

// ProjectHook represents a project hook.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/projects.html#list-project-hooks
type ProjectHook struct {
ID int `json:"id"`
URL string `json:"url"`
ConfidentialNoteEvents bool `json:"confidential_note_events"`
ProjectID int `json:"project_id"`
PushEvents bool `json:"push_events"`
PushEventsBranchFilter string `json:"push_events_branch_filter"`
IssuesEvents bool `json:"issues_events"`
ConfidentialIssuesEvents bool `json:"confidential_issues_events"`
MergeRequestsEvents bool `json:"merge_requests_events"`
TagPushEvents bool `json:"tag_push_events"`
NoteEvents bool `json:"note_events"`
JobEvents bool `json:"job_events"`
PipelineEvents bool `json:"pipeline_events"`
WikiPageEvents bool `json:"wiki_page_events"`
DeploymentEvents bool `json:"deployment_events"`
ReleasesEvents bool `json:"releases_events"`
EnableSSLVerification bool `json:"enable_ssl_verification"`
CreatedAt *time.Time `json:"created_at"`
ResourceAccessTokenEvents bool `json:"resource_access_token_events"`
CustomWebhookTemplate string `json:"custom_webhook_template"`
ID int `json:"id"`
URL string `json:"url"`
ConfidentialNoteEvents bool `json:"confidential_note_events"`
ProjectID int `json:"project_id"`
PushEvents bool `json:"push_events"`
PushEventsBranchFilter string `json:"push_events_branch_filter"`
IssuesEvents bool `json:"issues_events"`
ConfidentialIssuesEvents bool `json:"confidential_issues_events"`
MergeRequestsEvents bool `json:"merge_requests_events"`
TagPushEvents bool `json:"tag_push_events"`
NoteEvents bool `json:"note_events"`
JobEvents bool `json:"job_events"`
PipelineEvents bool `json:"pipeline_events"`
WikiPageEvents bool `json:"wiki_page_events"`
DeploymentEvents bool `json:"deployment_events"`
ReleasesEvents bool `json:"releases_events"`
EnableSSLVerification bool `json:"enable_ssl_verification"`
CreatedAt *time.Time `json:"created_at"`
ResourceAccessTokenEvents bool `json:"resource_access_token_events"`
CustomWebhookTemplate string `json:"custom_webhook_template"`
CustomHeaders []ProjectHookCustomHeader `json:"custom_headers"`
}

// ListProjectHooksOptions represents the available ListProjectHooks() options.
Expand Down Expand Up @@ -1284,24 +1296,25 @@ func (s *ProjectsService) GetProjectHook(pid interface{}, hook int, options ...R
// GitLab API docs:
// https://docs.gitlab.com/ee/api/projects.html#add-project-hook
type AddProjectHookOptions struct {
ConfidentialIssuesEvents *bool `url:"confidential_issues_events,omitempty" json:"confidential_issues_events,omitempty"`
ConfidentialNoteEvents *bool `url:"confidential_note_events,omitempty" json:"confidential_note_events,omitempty"`
DeploymentEvents *bool `url:"deployment_events,omitempty" json:"deployment_events,omitempty"`
EnableSSLVerification *bool `url:"enable_ssl_verification,omitempty" json:"enable_ssl_verification,omitempty"`
IssuesEvents *bool `url:"issues_events,omitempty" json:"issues_events,omitempty"`
JobEvents *bool `url:"job_events,omitempty" json:"job_events,omitempty"`
MergeRequestsEvents *bool `url:"merge_requests_events,omitempty" json:"merge_requests_events,omitempty"`
NoteEvents *bool `url:"note_events,omitempty" json:"note_events,omitempty"`
PipelineEvents *bool `url:"pipeline_events,omitempty" json:"pipeline_events,omitempty"`
PushEvents *bool `url:"push_events,omitempty" json:"push_events,omitempty"`
PushEventsBranchFilter *string `url:"push_events_branch_filter,omitempty" json:"push_events_branch_filter,omitempty"`
ReleasesEvents *bool `url:"releases_events,omitempty" json:"releases_events,omitempty"`
TagPushEvents *bool `url:"tag_push_events,omitempty" json:"tag_push_events,omitempty"`
Token *string `url:"token,omitempty" json:"token,omitempty"`
URL *string `url:"url,omitempty" json:"url,omitempty"`
WikiPageEvents *bool `url:"wiki_page_events,omitempty" json:"wiki_page_events,omitempty"`
ResourceAccessTokenEvents *bool `url:"resource_access_token_events,omitempty" json:"resource_access_token_events,omitempty"`
CustomWebhookTemplate *string `url:"custom_webhook_template,omitempty" json:"custom_webhook_template,omitempty"`
ConfidentialIssuesEvents *bool `url:"confidential_issues_events,omitempty" json:"confidential_issues_events,omitempty"`
ConfidentialNoteEvents *bool `url:"confidential_note_events,omitempty" json:"confidential_note_events,omitempty"`
DeploymentEvents *bool `url:"deployment_events,omitempty" json:"deployment_events,omitempty"`
EnableSSLVerification *bool `url:"enable_ssl_verification,omitempty" json:"enable_ssl_verification,omitempty"`
IssuesEvents *bool `url:"issues_events,omitempty" json:"issues_events,omitempty"`
JobEvents *bool `url:"job_events,omitempty" json:"job_events,omitempty"`
MergeRequestsEvents *bool `url:"merge_requests_events,omitempty" json:"merge_requests_events,omitempty"`
NoteEvents *bool `url:"note_events,omitempty" json:"note_events,omitempty"`
PipelineEvents *bool `url:"pipeline_events,omitempty" json:"pipeline_events,omitempty"`
PushEvents *bool `url:"push_events,omitempty" json:"push_events,omitempty"`
PushEventsBranchFilter *string `url:"push_events_branch_filter,omitempty" json:"push_events_branch_filter,omitempty"`
ReleasesEvents *bool `url:"releases_events,omitempty" json:"releases_events,omitempty"`
TagPushEvents *bool `url:"tag_push_events,omitempty" json:"tag_push_events,omitempty"`
Token *string `url:"token,omitempty" json:"token,omitempty"`
URL *string `url:"url,omitempty" json:"url,omitempty"`
WikiPageEvents *bool `url:"wiki_page_events,omitempty" json:"wiki_page_events,omitempty"`
ResourceAccessTokenEvents *bool `url:"resource_access_token_events,omitempty" json:"resource_access_token_events,omitempty"`
CustomWebhookTemplate *string `url:"custom_webhook_template,omitempty" json:"custom_webhook_template,omitempty"`
CustomHeaders []*ProjectHookCustomHeader `url:"custom_headers,omitempty" json:"custom_headers,omitempty"`
}

// AddProjectHook adds a hook to a specified project.
Expand Down Expand Up @@ -1334,24 +1347,25 @@ func (s *ProjectsService) AddProjectHook(pid interface{}, opt *AddProjectHookOpt
// GitLab API docs:
// https://docs.gitlab.com/ee/api/projects.html#edit-project-hook
type EditProjectHookOptions struct {
ConfidentialIssuesEvents *bool `url:"confidential_issues_events,omitempty" json:"confidential_issues_events,omitempty"`
ConfidentialNoteEvents *bool `url:"confidential_note_events,omitempty" json:"confidential_note_events,omitempty"`
DeploymentEvents *bool `url:"deployment_events,omitempty" json:"deployment_events,omitempty"`
EnableSSLVerification *bool `url:"enable_ssl_verification,omitempty" json:"enable_ssl_verification,omitempty"`
IssuesEvents *bool `url:"issues_events,omitempty" json:"issues_events,omitempty"`
JobEvents *bool `url:"job_events,omitempty" json:"job_events,omitempty"`
MergeRequestsEvents *bool `url:"merge_requests_events,omitempty" json:"merge_requests_events,omitempty"`
NoteEvents *bool `url:"note_events,omitempty" json:"note_events,omitempty"`
PipelineEvents *bool `url:"pipeline_events,omitempty" json:"pipeline_events,omitempty"`
PushEvents *bool `url:"push_events,omitempty" json:"push_events,omitempty"`
PushEventsBranchFilter *string `url:"push_events_branch_filter,omitempty" json:"push_events_branch_filter,omitempty"`
ReleasesEvents *bool `url:"releases_events,omitempty" json:"releases_events,omitempty"`
TagPushEvents *bool `url:"tag_push_events,omitempty" json:"tag_push_events,omitempty"`
Token *string `url:"token,omitempty" json:"token,omitempty"`
URL *string `url:"url,omitempty" json:"url,omitempty"`
WikiPageEvents *bool `url:"wiki_page_events,omitempty" json:"wiki_page_events,omitempty"`
ResourceAccessTokenEvents *bool `url:"resource_access_token_events,omitempty" json:"resource_access_token_events,omitempty"`
CustomWebhookTemplate *string `url:"custom_webhook_template,omitempty" json:"custom_webhook_template,omitempty"`
ConfidentialIssuesEvents *bool `url:"confidential_issues_events,omitempty" json:"confidential_issues_events,omitempty"`
ConfidentialNoteEvents *bool `url:"confidential_note_events,omitempty" json:"confidential_note_events,omitempty"`
DeploymentEvents *bool `url:"deployment_events,omitempty" json:"deployment_events,omitempty"`
EnableSSLVerification *bool `url:"enable_ssl_verification,omitempty" json:"enable_ssl_verification,omitempty"`
IssuesEvents *bool `url:"issues_events,omitempty" json:"issues_events,omitempty"`
JobEvents *bool `url:"job_events,omitempty" json:"job_events,omitempty"`
MergeRequestsEvents *bool `url:"merge_requests_events,omitempty" json:"merge_requests_events,omitempty"`
NoteEvents *bool `url:"note_events,omitempty" json:"note_events,omitempty"`
PipelineEvents *bool `url:"pipeline_events,omitempty" json:"pipeline_events,omitempty"`
PushEvents *bool `url:"push_events,omitempty" json:"push_events,omitempty"`
PushEventsBranchFilter *string `url:"push_events_branch_filter,omitempty" json:"push_events_branch_filter,omitempty"`
ReleasesEvents *bool `url:"releases_events,omitempty" json:"releases_events,omitempty"`
TagPushEvents *bool `url:"tag_push_events,omitempty" json:"tag_push_events,omitempty"`
Token *string `url:"token,omitempty" json:"token,omitempty"`
URL *string `url:"url,omitempty" json:"url,omitempty"`
WikiPageEvents *bool `url:"wiki_page_events,omitempty" json:"wiki_page_events,omitempty"`
ResourceAccessTokenEvents *bool `url:"resource_access_token_events,omitempty" json:"resource_access_token_events,omitempty"`
CustomWebhookTemplate *string `url:"custom_webhook_template,omitempty" json:"custom_webhook_template,omitempty"`
CustomHeaders []*ProjectHookCustomHeader `url:"custom_headers,omitempty" json:"custom_headers,omitempty"`
}

// EditProjectHook edits a hook for a specified project.
Expand Down Expand Up @@ -1399,6 +1413,53 @@ func (s *ProjectsService) DeleteProjectHook(pid interface{}, hook int, options .
return s.client.Do(req, nil)
}

// SetProjectHookCustomHeaderOptions represents a project hook custom header.
// If the header isn't present, it will be created.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/projects.html#set-a-custom-header
type SetProjectHookCustomHeaderOptions struct {
Value *string `json:"value,omitempty"`
}

// SetProjectCustomHeader creates or updates a project custom webhook header.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/projects.html#set-a-custom-header
func (s *ProjectsService) SetProjectCustomHeader(pid interface{}, hook int, key string, opt *SetProjectHookCustomHeaderOptions, options ...RequestOptionFunc) (*Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, err
}
u := fmt.Sprintf("projects/%s/hooks/%d/custom_headers/%s", PathEscape(project), hook, key)

req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
if err != nil {
return nil, err
}

return s.client.Do(req, nil)
}

// DeleteProjectCustomHeader deletes a project custom webhook header.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/projects.html#delete-a-custom-header
func (s *ProjectsService) DeleteProjectCustomHeader(pid interface{}, hook int, key string, options ...RequestOptionFunc) (*Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, err
}
u := fmt.Sprintf("projects/%s/hooks/%d/custom_headers/%s", PathEscape(project), hook, key)

req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
if err != nil {
return nil, err
}

return s.client.Do(req, nil)
}

// ProjectForkRelation represents a project fork relationship.
//
// GitLab API docs:
Expand Down
Loading

0 comments on commit a5171a1

Please sign in to comment.