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 Get Webhook endpoint and handle io/ioutil deprecation #28

Merged
merged 2 commits into from
Dec 20, 2022
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
18 changes: 14 additions & 4 deletions circleci_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package circleci

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/google/go-cmp/cmp"
)

func setup() (client *Client, mux *http.ServeMux, serverURL string, teardown func()) {
Expand Down Expand Up @@ -45,12 +47,20 @@ func testHeader(t *testing.T, r *http.Request, header string, want string) {

func testBody(t *testing.T, r *http.Request, want string) {
t.Helper()
b, err := ioutil.ReadAll(r.Body)
var mgot map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&mgot)
if err != nil {
t.Errorf("Error reading request body: %v", err)
}
if got := string(b); got != want {
t.Errorf("request Body is %s, want %s", got, want)

var mwant map[string]interface{}
err = json.Unmarshal([]byte(want), &mwant)
if err != nil {
t.Errorf("Error Unmarshalling want string: %v", err)
}

if !cmp.Equal(mgot, mwant) {
t.Errorf("request Body is %s, want %s", mgot, mwant)
}
}

Expand Down
1 change: 1 addition & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
ErrRequiredJobName = errors.New("job name is required")
ErrRequiredWebhookEvents = errors.New("webhook events is required")
ErrRequiredWebhookName = errors.New("webhook name is required")
ErrRequiredWebhookID = errors.New("webhook ID is required")
ErrRequiredWebhookURL = errors.New("webhook URL is required")
ErrRequiredWebhookVerifyTLS = errors.New("webhook verifyTLS is required")
ErrRequiredWebhookSigningSecret = errors.New("webhook signingSecret is required")
Expand Down
22 changes: 22 additions & 0 deletions webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package circleci

import (
"context"
"fmt"
)

type Webhooks interface {
Get(ctx context.Context, id string) (*Webhook, error)
List(ctx context.Context, options WebhookListOptions) (*WebhookList, error)
Create(ctx context.Context, options WebhookCreateOptions) (*Webhook, error)
}
Expand Down Expand Up @@ -33,6 +35,26 @@ type Scope struct {
Type string `json:"type"`
}

func (s *webhooks) Get(ctx context.Context, id string) (*Webhook, error) {
if !validString(&id) {
return nil, ErrRequiredWebhookID
}

u := fmt.Sprintf("webhook/%s", id)
req, err := s.client.newRequest("GET", u, nil)
if err != nil {
return nil, err
}

w := &Webhook{}
err = s.client.do(ctx, req, w)
if err != nil {
return nil, err
}

return w, nil
}

type WebhookListOptions struct {
ScopeID *string `url:"scope-id,omitempty"`
ScopeType *string `url:"scope-type,omitempty"`
Expand Down
25 changes: 25 additions & 0 deletions webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@ import (
"github.com/google/go-cmp/cmp"
)

func Test_webhooks_Get(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

webhookID := "webhook1"
mux.HandleFunc(fmt.Sprintf("/webhook/%s", webhookID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", "application/json")
testHeader(t, r, "Circle-Token", client.token)
fmt.Fprint(w, `{"id": "1"}`)
})

ctx := context.Background()
w, err := client.Webhooks.Get(ctx, webhookID)
if err != nil {
t.Errorf("Webhooks.Get got error: %v", err)
}

want := &Webhook{ID: "1"}

if !cmp.Equal(w, want) {
t.Errorf("Webhooks.Get got %+v, want %+v", w, want)
}
}

func Test_webhooks_List(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
Expand Down