Skip to content

feat: add support for webhook secrets #261

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

Merged
merged 1 commit into from
Nov 8, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ type WebhooksOptions struct {
Owner string `json:"owner"`
RepoSlug string `json:"repo_slug"`
Uuid string `json:"uuid"`
Secret string `json:"secret"`
Description string `json:"description"`
Url string `json:"url"`
Active bool `json:"active"`
Expand Down
12 changes: 12 additions & 0 deletions tests/webhooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestWebhook(t *testing.T) {
opt := &bitbucket.WebhooksOptions{
Owner: owner,
RepoSlug: repo,
Secret: "unsecureSecret",
Description: "go-bb-test",
Url: "https://example.com",
Active: false,
Expand Down Expand Up @@ -63,6 +64,9 @@ func TestWebhook(t *testing.T) {
if len(webhook.Events) != 2 {
t.Error("The webhook `events` attribute does not match the expected value.")
}
if webhook.Secret != "unsecureSecret" {
t.Error("The webhook `secret` attribute does not match the expected value.")
}

webhookResourceUuid = webhook.Uuid
})
Expand Down Expand Up @@ -94,13 +98,17 @@ func TestWebhook(t *testing.T) {
if len(webhook.Events) != 2 {
t.Error("The webhook `events` attribute does not match the expected value.")
}
if webhook.Secret != "unsecureSecret" {
t.Error("The webhook `secret` attribute does not match the expected value.")
}
})

t.Run("update", func(t *testing.T) {
opt := &bitbucket.WebhooksOptions{
Owner: owner,
RepoSlug: repo,
Uuid: webhookResourceUuid,
Secret: "newUnsecureSecret",
Description: "go-bb-test-new",
Url: "https://new-example.com",
Events: []string{bitbucket.RepoPushEvent, bitbucket.IssueCreatedEvent, bitbucket.RepoForkEvent},
Expand All @@ -126,6 +134,9 @@ func TestWebhook(t *testing.T) {
if len(webhook.Events) != 3 {
t.Error("The webhook `events` attribute does not match the expected value.")
}
if webhook.Secret != "newUnsecureSecret" {
t.Error("The webhook `secret` attribute does not match the expected value.")
}
})

t.Run("delete", func(t *testing.T) {
Expand Down Expand Up @@ -161,6 +172,7 @@ func TestWebhook(t *testing.T) {
opt := &bitbucket.WebhooksOptions{
Owner: owner,
RepoSlug: repo,
Secret: "unsecureSecret",
Description: fmt.Sprintf("go-bb-test-%d", i),
Url: fmt.Sprintf("https://example.com/%d", i),
Active: false,
Expand Down
4 changes: 4 additions & 0 deletions webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Webhook struct {
Owner string `json:"owner"`
RepoSlug string `json:"repo_slug"`
Uuid string `json:"uuid"`
Secret string `json:"secret"`
Description string `json:"description"`
Url string `json:"url"`
Active bool `json:"active"`
Expand Down Expand Up @@ -61,6 +62,9 @@ func (r *Webhooks) buildWebhooksBody(ro *WebhooksOptions) (string, error) {
if ro.Active == true || ro.Active == false {
body["active"] = ro.Active
}
if ro.Secret != "" {
body["secret"] = ro.Secret
}

body["events"] = ro.Events

Expand Down