Skip to content

Commit

Permalink
Add new webhooks API to client (#13)
Browse files Browse the repository at this point in the history
* Add new webhooks API to client

* remove custom actions

* restore custom actions

* IntegrationFilter *[]string

---------

Co-authored-by: Vadim Stepanov <[email protected]>
  • Loading branch information
mderynck and vstpme authored Nov 10, 2023
1 parent 66fc22f commit a4046f1
Show file tree
Hide file tree
Showing 4 changed files with 317 additions and 2 deletions.
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Client struct {
CustomActions *CustomActionService
OnCallShifts *OnCallShiftService
Teams *TeamService
Webhooks *WebhookService
}

func New(base_url, token string) (*Client, error) {
Expand Down Expand Up @@ -110,6 +111,7 @@ func newClient(url string) (*Client, error) {
c.CustomActions = NewCustomActionService(c)
c.OnCallShifts = NewOnCallShiftService(c)
c.Teams = NewTeamService(c)
c.Webhooks = NewWebhookService(c)

return c, nil
}
Expand Down
4 changes: 2 additions & 2 deletions schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ type CreateScheduleOptions struct {
TeamId string `json:"team_id"`
Name string `json:"name"`
Type string `json:"type"`
ICalUrlPrimary *string `json:"ical_url_primary"`
ICalUrlOverrides *string `json:"ical_url_overrides"`
ICalUrlPrimary *string `json:"ical_url_primary"`
ICalUrlOverrides *string `json:"ical_url_overrides"`
EnableWebOverrides bool `json:"enable_web_overrides"`
TimeZone string `json:"time_zone,omitempty"`
Slack *SlackSchedule `json:"slack,omitempty"`
Expand Down
188 changes: 188 additions & 0 deletions webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package aapi

import (
"fmt"
"net/http"
)

// WebhookService handles requests to outgoing webhook endpoint
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/outgoing_webhooks/
type WebhookService struct {
client *Client
url string
}

// NewWebhookService creates WebhookService with defined url
func NewWebhookService(client *Client) *WebhookService {
WebhookService := WebhookService{}
WebhookService.client = client
WebhookService.url = "webhooks"
return &WebhookService
}

type PaginatedWebhooksResponse struct {
PaginatedResponse
Webhooks []*Webhook `json:"results"`
}

type Webhook struct {
ID string `json:"id"`
Name string `json:"name"`
Team string `json:"team"`
Url string `json:"url"`
TriggerType string `json:"trigger_type"`
HttpMethod string `json:"http_method"`
Data *string `json:"data"`
Username *string `json:"username"`
Password *string `json:"password"`
AuthorizationHeader *string `json:"authorization_header"`
TriggerTemplate *string `json:"trigger_template"`
Headers *string `json:"headers"`
ForwardAll bool `json:"forward_all"`
IntegrationFilter *[]string `json:"integration_filter"`
IsWebhookEnabled bool `json:"is_webhook_enabled"`
}

type ListWebhookOptions struct {
ListOptions
Name string `url:"name,omitempty" json:"name,omitempty"`
}

// ListWebhooks fetches all Webhooks for authorized organization
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/outgoing_webhooks/#list-actions
func (service *WebhookService) ListWebhooks(opt *ListWebhookOptions) (*PaginatedWebhooksResponse, *http.Response, error) {
u := fmt.Sprintf("%s", service.url)

req, err := service.client.NewRequest("GET", u, opt)
if err != nil {
return nil, nil, err
}

var Webhooks *PaginatedWebhooksResponse
resp, err := service.client.Do(req, &Webhooks)
if err != nil {
return nil, resp, err
}

return Webhooks, resp, err
}

type GetWebhookOptions struct {
}

// GetWebhook fetches webhook by given id.
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/outgoing_webhooks/
func (service *WebhookService) GetWebhook(id string, opt *GetWebhookOptions) (*Webhook, *http.Response, error) {
u := fmt.Sprintf("%s/%s/", service.url, id)

req, err := service.client.NewRequest("GET", u, opt)
if err != nil {
return nil, nil, err
}

Webhook := new(Webhook)
resp, err := service.client.Do(req, Webhook)
if err != nil {
return nil, resp, err
}

return Webhook, resp, err
}

type CreateWebhookOptions struct {
Name string `json:"name"`
Team string `json:"team"`
Url string `json:"url"`
TriggerType string `json:"trigger_type"`
HttpMethod string `json:"http_method"`
Data *string `json:"data"`
Username *string `json:"username"`
Password *string `json:"password"`
AuthorizationHeader *string `json:"authorization_header"`
TriggerTemplate *string `json:"trigger_template"`
Headers *string `json:"headers"`
ForwardAll bool `json:"forward_all"`
IntegrationFilter *[]string `json:"integration_filter"`
IsWebhookEnabled bool `json:"is_webhook_enabled"`
}

// CreateWebhook creates webhook
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/outgoing_webhooks/
func (service *WebhookService) CreateWebhook(opt *CreateWebhookOptions) (*Webhook, *http.Response, error) {
u := fmt.Sprintf("%s/", service.url)

req, err := service.client.NewRequest("POST", u, opt)
if err != nil {
return nil, nil, err
}

Webhook := new(Webhook)

resp, err := service.client.Do(req, Webhook)

if err != nil {
return nil, resp, err
}

return Webhook, resp, err
}

type UpdateWebhookOptions struct {
Name string `json:"name"`
Team string `json:"team"`
Url string `json:"url"`
TriggerType string `json:"trigger_type"`
HttpMethod string `json:"http_method"`
Data *string `json:"data"`
Username *string `json:"username"`
Password *string `json:"password"`
AuthorizationHeader *string `json:"authorization_header"`
TriggerTemplate *string `json:"trigger_template"`
Headers *string `json:"headers"`
ForwardAll bool `json:"forward_all"`
IntegrationFilter *[]string `json:"integration_filter"`
IsWebhookEnabled bool `json:"is_webhook_enabled"`
}

// UpdateWebhook updates webhook
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/outgoing_webhooks/
func (service *WebhookService) UpdateWebhook(id string, opt *UpdateWebhookOptions) (*Webhook, *http.Response, error) {
u := fmt.Sprintf("%s/%s/", service.url, id)

req, err := service.client.NewRequest("PUT", u, opt)
if err != nil {
return nil, nil, err
}

Webhook := new(Webhook)
resp, err := service.client.Do(req, Webhook)
if err != nil {
return nil, resp, err
}

return Webhook, resp, err
}

type DeleteWebhookOptions struct {
}

// DeleteWebhook deletes webhook.
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/outgoing_webhooks/
func (service *WebhookService) DeleteWebhook(id string, opt *DeleteWebhookOptions) (*http.Response, error) {

u := fmt.Sprintf("%s/%s/", service.url, id)

req, err := service.client.NewRequest("DELETE", u, opt)
if err != nil {
return nil, err
}

resp, err := service.client.Do(req, nil)
return resp, err
}
125 changes: 125 additions & 0 deletions webhook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package aapi

import (
"fmt"
"net/http"
"reflect"
"testing"
)

var testWebhook = &Webhook{
ID: "KGEFG74LU1D8L",
Name: "Test action",
Team: "T3HRAP3K3IKOP",
HttpMethod: "POST",
TriggerType: "escalation step",
Url: "http://test.com",
}

var testWebhookBody = `{
"id": "KGEFG74LU1D8L",
"name": "Test action",
"team": "T3HRAP3K3IKOP",
"http_method": "POST",
"trigger_type": "escalation step",
"url":"http://test.com"
}`

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

mux.HandleFunc("/api/v1/webhooks/", func(w http.ResponseWriter, r *http.Request) {
testRequestMethod(t, r, "GET")
fmt.Fprint(w, fmt.Sprintf(`{"count": 1, "next": null, "previous": null, "results": [%s]}`, testWebhookBody))
})

options := &ListWebhookOptions{
Name: "Test action",
}

Webhooks, _, err := client.Webhooks.ListWebhooks(options)
if err != nil {
t.Fatal(err)
}

want := &PaginatedWebhooksResponse{
PaginatedResponse: PaginatedResponse{
Count: 1,
Next: nil,
Previous: nil,
},
Webhooks: []*Webhook{
testWebhook,
},
}
if !reflect.DeepEqual(want, Webhooks) {
t.Errorf("returned\n %+v, \nwant\n %+v", Webhooks, want)
}
}

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

mux.HandleFunc("/api/v1/webhooks/", func(w http.ResponseWriter, r *http.Request) {
testRequestMethod(t, r, "POST")
fmt.Fprint(w, testWebhookBody)
})

createOptions := &CreateWebhookOptions{
Name: "Test Webhook",
Url: "https://example.com",
}
Webhook, _, err := client.Webhooks.CreateWebhook(createOptions)

if err != nil {
t.Fatal(err)
}

want := testWebhook

if !reflect.DeepEqual(want, Webhook) {
t.Errorf("returned\n %+v\n want\n %+v\n", Webhook, want)
}
}

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

mux.HandleFunc("/api/v1/webhooks/KGEFG74LU1D8L/", func(w http.ResponseWriter, r *http.Request) {
testRequestMethod(t, r, "DELETE")
})

options := &DeleteWebhookOptions{}

_, err := client.Webhooks.DeleteWebhook("KGEFG74LU1D8L", options)
if err != nil {
t.Fatal(err)
}
}

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

mux.HandleFunc("/api/v1/webhooks/KGEFG74LU1D8L/", func(w http.ResponseWriter, r *http.Request) {
testRequestMethod(t, r, "GET")
fmt.Fprint(w, testWebhookBody)
})

options := &GetWebhookOptions{}

Webhook, _, err := client.Webhooks.GetWebhook("KGEFG74LU1D8L", options)

if err != nil {
t.Fatal(err)
}

want := testWebhook

if !reflect.DeepEqual(want, Webhook) {
t.Errorf("returned\n %+v\n want\n %+v\n", Webhook, want)
}
}

0 comments on commit a4046f1

Please sign in to comment.