Skip to content
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
2 changes: 1 addition & 1 deletion bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ type WebhooksOptions struct {
Description string `json:"description"`
Url string `json:"url"`
Active bool `json:"active"`
Events []string `json:"events"` // EX) {'repo:push','issue:created',..} REF) https://goo.gl/VTj93b
Events []string `json:"events"` // EX: {'repo:push','issue:created',..} REF: https://bit.ly/3FjRHHu
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ktrysmt The previous goo.gl shortlink referenced an old & now broken page, so I created a new shortlink to the new page.

}

type RepositoryPipelineOptions struct {
Expand Down
140 changes: 140 additions & 0 deletions tests/webhooks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package tests

import (
"os"
"testing"

"github.com/ktrysmt/go-bitbucket"
)

func TestWebhook(t *testing.T) {
user := os.Getenv("BITBUCKET_TEST_USERNAME")
pass := os.Getenv("BITBUCKET_TEST_PASSWORD")
owner := os.Getenv("BITBUCKET_TEST_OWNER")
repo := os.Getenv("BITBUCKET_TEST_REPOSLUG")

if user == "" {
t.Error("BITBUCKET_TEST_USERNAME is empty.")
}
if pass == "" {
t.Error("BITBUCKET_TEST_PASSWORD is empty.")
}
if owner == "" {
t.Error("BITBUCKET_TEST_OWNER is empty.")
}
if repo == "" {
t.Error("BITBUCKET_TEST_REPOSLUG is empty.")
}

c := bitbucket.NewBasicAuth(user, pass)

var webhookResourceUuid string

t.Run("create", func(t *testing.T) {
opt := &bitbucket.WebhooksOptions{
Owner: owner,
RepoSlug: repo,
Description: "go-bb-test",
Url: "https://example.com",
Active: false,
Events: []string{"repo:push", "issue:created"},
}

webhook, err := c.Repositories.Webhooks.Create(opt)
if err != nil {
t.Error(err)
}

if webhook == nil {
t.Error("The webhook could not be created.")
}

if webhook.Description != "go-bb-test" {
t.Error("The webhook `description` attribute does not match the expected value.")
}
if webhook.Url != "https://example.com" {
t.Error("The webhook `url` attribute does not match the expected value.")
}
if webhook.Active != false {
t.Error("The webhook `active` attribute does not match the expected value.")
}
if len(webhook.Events) != 2 {
t.Error("The webhook `events` attribute does not match the expected value.")
}

webhookResourceUuid = webhook.Uuid
})

t.Run("get", func(t *testing.T) {
opt := &bitbucket.WebhooksOptions{
Owner: owner,
RepoSlug: repo,
Uuid: webhookResourceUuid,
}
webhook, err := c.Repositories.Webhooks.Get(opt)
if err != nil {
t.Error(err)
}

if webhook == nil {
t.Error("The webhook could not be retrieved.")
}

if webhook.Description != "go-bb-test" {
t.Error("The webhook `description` attribute does not match the expected value.")
}
if webhook.Url != "https://example.com" {
t.Error("The webhook `url` attribute does not match the expected value.")
}
if webhook.Active != false {
t.Error("The webhook `active` attribute does not match the expected value.")
}
if len(webhook.Events) != 2 {
t.Error("The webhook `events` attribute does not match the expected value.")
}
})

t.Run("update", func(t *testing.T) {
opt := &bitbucket.WebhooksOptions{
Owner: owner,
RepoSlug: repo,
Uuid: webhookResourceUuid,
Description: "go-bb-test-new",
Url: "https://new-example.com",
Events: []string{"repo:push", "issue:created", "repo:fork"},
}
webhook, err := c.Repositories.Webhooks.Update(opt)
if err != nil {
t.Error(err)
}

if webhook == nil {
t.Error("The webhook could not be retrieved.")
}

if webhook.Description != "go-bb-test-new" {
t.Error("The webhook `description` attribute does not match the expected value.")
}
if webhook.Url != "https://new-example.com" {
t.Error("The webhook `url` attribute does not match the expected value.")
}
if webhook.Active != false {
t.Error("The webhook `active` attribute does not match the expected value.")
}
if len(webhook.Events) != 3 {
t.Error("The webhook `events` attribute does not match the expected value.")
}
})

t.Run("delete", func(t *testing.T) {
opt := &bitbucket.WebhooksOptions{
Owner: owner,
RepoSlug: repo,
Uuid: webhookResourceUuid,
}
_, err := c.Repositories.Webhooks.Delete(opt)
if err != nil {
t.Error(err)
}
})
}
56 changes: 48 additions & 8 deletions webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,39 @@ import (
"os"

"github.com/k0kubun/pp"
"github.com/mitchellh/mapstructure"
)

type Webhooks struct {
c *Client
}

type Webhook struct {
Owner string `json:"owner"`
RepoSlug string `json:"repo_slug"`
Uuid string `json:"uuid"`
Description string `json:"description"`
Url string `json:"url"`
Active bool `json:"active"`
Events []string `json:"events"` // EX: {'repo:push','issue:created',..} REF: https://bit.ly/3FjRHHu
}

func decodeWebhook(response interface{}) (*Webhook, error) {
respMap := response.(map[string]interface{})

if respMap["type"] == "error" {
return nil, DecodeError(respMap)
}

var webhook = new(Webhook)
err := mapstructure.Decode(respMap, webhook)
if err != nil {
return nil, err
}

return webhook, nil
}

func (r *Webhooks) buildWebhooksBody(ro *WebhooksOptions) string {

body := map[string]interface{}{}
Expand Down Expand Up @@ -41,26 +68,39 @@ func (r *Webhooks) Gets(ro *WebhooksOptions) (interface{}, error) {
return r.c.execute("GET", urlStr, "")
}

func (r *Webhooks) Create(ro *WebhooksOptions) (interface{}, error) {
func (r *Webhooks) Create(ro *WebhooksOptions) (*Webhook, error) {
data := r.buildWebhooksBody(ro)
urlStr := r.c.requestUrl("/repositories/%s/%s/hooks", ro.Owner, ro.RepoSlug)
return r.c.execute("POST", urlStr, data)
response, err := r.c.execute("POST", urlStr, data)
if err != nil {
return nil, err
}

return decodeWebhook(response)
}

func (r *Webhooks) Get(ro *WebhooksOptions) (interface{}, error) {
func (r *Webhooks) Get(ro *WebhooksOptions) (*Webhook, error) {
urlStr := r.c.requestUrl("/repositories/%s/%s/hooks/%s", ro.Owner, ro.RepoSlug, ro.Uuid)
return r.c.execute("GET", urlStr, "")
response, err := r.c.execute("GET", urlStr, "")
if err != nil {
return nil, err
}

return decodeWebhook(response)
}

func (r *Webhooks) Update(ro *WebhooksOptions) (interface{}, error) {
func (r *Webhooks) Update(ro *WebhooksOptions) (*Webhook, error) {
data := r.buildWebhooksBody(ro)
urlStr := r.c.requestUrl("/repositories/%s/%s/hooks/%s", ro.Owner, ro.RepoSlug, ro.Uuid)
return r.c.execute("PUT", urlStr, data)
response, err := r.c.execute("PUT", urlStr, data)
if err != nil {
return nil, err
}

return decodeWebhook(response)
}

func (r *Webhooks) Delete(ro *WebhooksOptions) (interface{}, error) {
urlStr := r.c.requestUrl("/repositories/%s/%s/hooks/%s", ro.Owner, ro.RepoSlug, ro.Uuid)
return r.c.execute("DELETE", urlStr, "")
}

//