Skip to content

Commit

Permalink
feat: add support for webhook secrets (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachsmith1 committed Nov 8, 2023
1 parent 6b1a577 commit 6eae88d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
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

0 comments on commit 6eae88d

Please sign in to comment.