Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the CustomWebhookTemplate #1892

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions group_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type GroupHook struct {
EnableSSLVerification bool `json:"enable_ssl_verification"`
AlertStatus string `json:"alert_status"`
CreatedAt *time.Time `json:"created_at"`
CustomWebhookTemplate string `json:"custom_webhook_template"`
}

// ListGroupHooksOptions represents the available ListGroupHooks() options.
Expand Down Expand Up @@ -122,6 +123,7 @@ type AddGroupHookOptions struct {
SubGroupEvents *bool `url:"subgroup_events,omitempty" json:"subgroup_events,omitempty"`
EnableSSLVerification *bool `url:"enable_ssl_verification,omitempty" json:"enable_ssl_verification,omitempty"`
Token *string `url:"token,omitempty" json:"token,omitempty"`
CustomWebhookTemplate *string `url:"custom_webhook_template,omitempty" json:"custom_webhook_template,omitempty"`
}

// AddGroupHook create a new group scoped webhook.
Expand Down Expand Up @@ -170,6 +172,7 @@ type EditGroupHookOptions struct {
SubGroupEvents *bool `url:"subgroup_events,omitempty" json:"subgroup_events,omitempty"`
EnableSSLVerification *bool `url:"enable_ssl_verification,omitempty" json:"enable_ssl_verification,omitempty"`
Token *string `url:"token,omitempty" json:"token,omitempty"`
CustomWebhookTemplate *string `url:"custom_webhook_template,omitempty" json:"custom_webhook_template,omitempty"`
}

// EditGroupHook edits a hook for a specified group.
Expand Down
8 changes: 6 additions & 2 deletions group_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ func TestAddGroupHook(t *testing.T) {
"releases_events": true,
"subgroup_events": true,
"enable_ssl_verification": true,
"created_at": "2012-10-12T17:04:47Z"
"created_at": "2012-10-12T17:04:47Z",
"custom_webhook_template": "addTestValue"
}`)
})

Expand Down Expand Up @@ -209,6 +210,7 @@ func TestAddGroupHook(t *testing.T) {
SubGroupEvents: true,
EnableSSLVerification: true,
CreatedAt: &datePointer,
CustomWebhookTemplate: "addTestValue",
}

if !reflect.DeepEqual(groupHooks, want) {
Expand Down Expand Up @@ -240,7 +242,8 @@ func TestEditGroupHook(t *testing.T) {
"releases_events": true,
"subgroup_events": true,
"enable_ssl_verification": true,
"created_at": "2012-10-12T17:04:47Z"
"created_at": "2012-10-12T17:04:47Z",
"custom_webhook_template": "testValue"
}`)
})

Expand Down Expand Up @@ -275,6 +278,7 @@ func TestEditGroupHook(t *testing.T) {
SubGroupEvents: true,
EnableSSLVerification: true,
CreatedAt: &datePointer,
CustomWebhookTemplate: "testValue",
}

if !reflect.DeepEqual(groupHooks, want) {
Expand Down
3 changes: 3 additions & 0 deletions projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,7 @@ type ProjectHook struct {
ReleasesEvents bool `json:"releases_events"`
EnableSSLVerification bool `json:"enable_ssl_verification"`
CreatedAt *time.Time `json:"created_at"`
CustomWebhookTemplate string `json:"custom_webhook_template"`
}

// ListProjectHooksOptions represents the available ListProjectHooks() options.
Expand Down Expand Up @@ -1295,6 +1296,7 @@ type AddProjectHookOptions struct {
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"`
CustomWebhookTemplate *string `url:"custom_webhook_template,omitempty" json:"custom_webhook_template,omitempty"`
}

// AddProjectHook adds a hook to a specified project.
Expand Down Expand Up @@ -1343,6 +1345,7 @@ type EditProjectHookOptions struct {
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"`
CustomWebhookTemplate *string `url:"custom_webhook_template,omitempty" json:"custom_webhook_template,omitempty"`
}

// EditProjectHook edits a hook for a specified project.
Expand Down
61 changes: 61 additions & 0 deletions projects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1468,3 +1468,64 @@ func TestProjectModelsOptionalMergeAttribute(t *testing.T) {
}
assert.False(t, strings.Contains(string(jsonString), "only_allow_merge_if_all_status_checks_passed"))
}

// Test that the "CustomWebhookTemplate" serializes properly
func TestProjectAddWebhook_CustomTemplate(t *testing.T) {
mux, client := setup(t)
customWebhookSet := false

mux.HandleFunc("/api/v4/projects/1/hooks",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
w.WriteHeader(http.StatusCreated)

body, err := io.ReadAll(r.Body)
if err != nil {
t.Fatalf("Unable to read body properly. Error: %v", err)
}
customWebhookSet = strings.Contains(string(body), "custom_webhook_template")

fmt.Fprint(w, `{
"custom_webhook_template": "testValue"
}`)
},
)

hook, resp, err := client.Projects.AddProjectHook(1, &AddProjectHookOptions{
CustomWebhookTemplate: Ptr(`{"example":"{{object_kind}}"}`),
})

assert.NoError(t, err)
assert.Equal(t, http.StatusCreated, resp.StatusCode)
assert.Equal(t, true, customWebhookSet)
assert.Equal(t, "testValue", hook.CustomWebhookTemplate)
}

// Test that the "CustomWebhookTemplate" serializes properly when editing
func TestProjectEditWebhook_CustomTemplate(t *testing.T) {
mux, client := setup(t)
customWebhookSet := false

mux.HandleFunc("/api/v4/projects/1/hooks/1",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPut)
w.WriteHeader(http.StatusOK)

body, err := io.ReadAll(r.Body)
if err != nil {
t.Fatalf("Unable to read body properly. Error: %v", err)
}
customWebhookSet = strings.Contains(string(body), "custom_webhook_template")

fmt.Fprint(w, "{}")
},
)

_, resp, err := client.Projects.EditProjectHook(1, 1, &EditProjectHookOptions{
CustomWebhookTemplate: Ptr(`{"example":"{{object_kind}}"}`),
})

assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, true, customWebhookSet)
}