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 resolution notes service #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Client struct {
OnCallShifts *OnCallShiftService
Teams *TeamService
Webhooks *WebhookService
ResolutionNotes *ResolutionNoteService
}

func New(base_url, token string) (*Client, error) {
Expand Down Expand Up @@ -112,6 +113,7 @@ func newClient(url string) (*Client, error) {
c.OnCallShifts = NewOnCallShiftService(c)
c.Teams = NewTeamService(c)
c.Webhooks = NewWebhookService(c)
c.ResolutionNotes = NewResolutionNoteService(c)

return c, nil
}
Expand Down
64 changes: 64 additions & 0 deletions resolution_note.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package aapi

import (
"fmt"
"net/http"
)

// ResolutionNoteService handles requests to the on-call resolution_notes endpoint.
//
// https://grafana.com/docs/oncall/latest/oncall-api-reference/resolution_notes/
type ResolutionNoteService struct {
client *Client
url string
}

// NewResolutionNoteService creates an ResolutionNoteService with the defined URL.
func NewResolutionNoteService(client *Client) *ResolutionNoteService {
resolutionNoteService := ResolutionNoteService{}
resolutionNoteService.client = client
resolutionNoteService.url = "resolution_notes"
return &resolutionNoteService
}

// PaginatedResolutionNotesResponse represents a paginated response from the on-call resolution note API.
type PaginatedResolutionNotesResponse struct {
PaginatedResponse
ResolutionNotes []*ResolutionNote `json:"results"`
}

// ResolutionNote represents an on-call resolution note.
type ResolutionNote struct {
ID string `json:"id"`
AlertGroupID string `json:"alert_group_id"`
Author string `json:"author"`
Source string `json:"source"`
CreatedAt string `json:"created_at"`
Text string `json:"text"`
}

// ListResolutionNoteOptions represent filter options supported by the on-call resolution note API.
type ListResolutionNoteOptions struct {
ListOptions
AlertGroupID string `url:"alert_group_id,omitempty" json:"alert_group_id,omitempty"`
}

// ListResolutionNotes fetches all on-call resolution notes associated to an alert group for authorized organization.
//
// https://grafana.com/docs/oncall/latest/oncall-api-reference/alertgroups/
func (service *ResolutionNoteService) ListResolutionNotes(opt *ListResolutionNoteOptions) (*PaginatedResolutionNotesResponse, *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 resolutionNotes *PaginatedResolutionNotesResponse
resp, err := service.client.Do(req, &resolutionNotes)
if err != nil {
return nil, resp, err
}

return resolutionNotes, resp, err
}
64 changes: 64 additions & 0 deletions resolution_note_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package aapi

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

var testResolutionNote = &ResolutionNote{
ID: "M4BTQUS3PRHYQ",
AlertGroupID: "I68T24C13IFW1",
Author: "U4DNY931HHJS5",
Source: "web",
CreatedAt: "2020-06-19T12:40:01.429805Z",
Text: "Demo resolution note",
}

var testResolutionNoteBody = `{
"id": "M4BTQUS3PRHYQ",
"alert_group_id": "I68T24C13IFW1",
"author": "U4DNY931HHJS5",
"source": "web",
"created_at": "2020-06-19T12:40:01.429805Z",
"text": "Demo resolution note"
}`



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

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

options := &ListResolutionNoteOptions{
AlertGroupID: "I68T24C13IFW1",
}

alerts, _, err := client.ResolutionNotes.ListResolutionNotes(options)
if err != nil {
t.Fatal(err)
}

want := &PaginatedResolutionNotesResponse{
PaginatedResponse: PaginatedResponse{
Count: 1,
Next: nil,
Previous: nil,
},
ResolutionNotes: []*ResolutionNote{
testResolutionNote,
},
}

if !reflect.DeepEqual(want, alerts) {
fmt.Println(alerts.ResolutionNotes[0])
fmt.Println(want.ResolutionNotes[0])
t.Errorf(" returned\n %+v, \nwant\n %+v", alerts, want)
}
}