Skip to content

Commit

Permalink
Merge pull request #9 from incident-io/rob/add-attachments-on-incidents
Browse files Browse the repository at this point in the history
Add attachments, and incident updates onto incidents stream
  • Loading branch information
rliddler authored Oct 19, 2023
2 parents 6c6686d + fc1f5e2 commit 2a88a45
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 2 deletions.
31 changes: 31 additions & 0 deletions model/external_resource_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package model

import "github.com/incident-io/singer-tap/client"

type externalResourceV1 struct{}

var ExternalResourceV1 externalResourceV1

func (externalResourceV1) Schema() Property {
return Property{
Types: []string{"object"},
Properties: map[string]Property{
"external_id": {
Types: []string{"string"},
},
"permalink": {
Types: []string{"string"},
},
"resource_type": {
Types: []string{"string"},
},
"title": {
Types: []string{"string"},
},
},
}
}

func (externalResourceV1) Serialize(input client.ExternalResourceV1) map[string]any {
return DumpToMap(input)
}
30 changes: 30 additions & 0 deletions model/incident_attachment_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package model

import "github.com/incident-io/singer-tap/client"

type incidentAttachmentV1 struct{}

var IncidentAttachmentV1 incidentAttachmentV1

func (incidentAttachmentV1) Schema() Property {
return Property{
Types: []string{"object"},
Properties: map[string]Property{
"id": {
Types: []string{"string"},
},
"incident_id": {
Types: []string{"string"},
},
"resource": ExternalResourceV1.Schema(),
},
}
}

func (incidentAttachmentV1) Serialize(input client.IncidentAttachmentV1) map[string]any {
return map[string]any{
"id": input.Id,
"incident_id": input.IncidentId,
"resource": ExternalResourceV1.Serialize(input.Resource),
}
}
24 changes: 23 additions & 1 deletion model/incident_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func (incidentV2) Schema() Property {
"creator": ActorV2.Schema(),
"custom_field_entries": ArrayOf(CustomFieldEntryV1.Schema()),
"external_issue_reference": Optional(ExternalIssueReferenceV2.Schema()),
"attachments": Optional(ArrayOf(IncidentAttachmentV1.Schema())),
"updates": Optional(ArrayOf(IncidentUpdateV2.Schema())),
"incident_role_assignments": ArrayOf(IncidentRoleAssignmentV1.Schema()),
"incident_status": IncidentStatusV1.Schema(),
"incident_timestamp_values": Optional(ArrayOf(IncidentTimestampWithValueV2.Schema())),
Expand Down Expand Up @@ -75,7 +77,25 @@ func (incidentV2) Schema() Property {
}
}

func (incidentV2) Serialize(input client.IncidentV2) map[string]any {
func (incidentV2) Serialize(
input client.IncidentV2,
incidentAttachments []client.IncidentAttachmentV1,
incidentUpdates []client.IncidentUpdateV2,
) map[string]any {
var attachments []map[string]any
if len(incidentAttachments) > 0 {
attachments = lo.Map(incidentAttachments, func(attachment client.IncidentAttachmentV1, _ int) map[string]any {
return IncidentAttachmentV1.Serialize(attachment)
})
}

var updates []map[string]any
if len(incidentUpdates) > 0 {
updates = lo.Map(incidentUpdates, func(update client.IncidentUpdateV2, _ int) map[string]any {
return IncidentUpdateV2.Serialize(update)
})
}

return map[string]any{
"id": input.Id,
"name": input.Name,
Expand All @@ -85,6 +105,8 @@ func (incidentV2) Serialize(input client.IncidentV2) map[string]any {
return CustomFieldEntryV1.Serialize(entry)
}),
"external_issue_reference": ExternalIssueReferenceV2.Serialize(input.ExternalIssueReference),
"attachments": attachments,
"updates": updates,
"incident_role_assignments": lo.Map(input.IncidentRoleAssignments, func(assignment client.IncidentRoleAssignmentV1, _ int) map[string]any {
return IncidentRoleAssignmentV1.Serialize(assignment)
}),
Expand Down
56 changes: 55 additions & 1 deletion tap/stream_incidents.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,17 @@ func (s *StreamIncidents) GetRecords(ctx context.Context, logger kitlog.Logger,
}

for _, element := range page.JSON200.Incidents {
results = append(results, model.IncidentV2.Serialize(element))
attachments, err := s.GetAttachments(ctx, logger, cl, element.Id)
if err != nil {
return nil, errors.Wrap(err, "listing incident attachments")
}

updates, err := s.GetIncidentUpdates(ctx, logger, cl, element.Id)
if err != nil {
return nil, errors.Wrap(err, "listing incident attachments")
}

results = append(results, model.IncidentV2.Serialize(element, attachments, updates))
}
if count := len(page.JSON200.Incidents); count == 0 {
return results, nil // end pagination
Expand All @@ -58,3 +68,47 @@ func (s *StreamIncidents) GetRecords(ctx context.Context, logger kitlog.Logger,
}
}
}

func (s *StreamIncidents) GetAttachments(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses, incidentId string) ([]client.IncidentAttachmentV1, error) {
var (
results = []client.IncidentAttachmentV1{}
)

response, err := cl.IncidentAttachmentsV1ListWithResponse(ctx, &client.IncidentAttachmentsV1ListParams{
IncidentId: &incidentId,
})
if err != nil {
return nil, errors.Wrap(err, "listing attachments for incidents stream")
}

results = append(results, response.JSON200.IncidentAttachments...)
return results, nil
}

func (s *StreamIncidents) GetIncidentUpdates(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses, incidentId string) ([]client.IncidentUpdateV2, error) {
var (
after *string
pageSize = 250
results = []client.IncidentUpdateV2{}
)

for {
logger.Log("msg", "loading incident updates page", "page_size", pageSize, "after", after)
page, err := cl.IncidentUpdatesV2ListWithResponse(ctx, &client.IncidentUpdatesV2ListParams{
IncidentId: &incidentId,
PageSize: &pageSize,
After: after,
})
if err != nil {
return nil, errors.Wrap(err, "listing incident updates")
}

results = append(results, page.JSON200.IncidentUpdates...)

if count := len(page.JSON200.IncidentUpdates); count == 0 {
return results, nil // end pagination
} else {
after = lo.ToPtr(page.JSON200.IncidentUpdates[count-1].Id)
}
}
}

0 comments on commit 2a88a45

Please sign in to comment.