Skip to content

Commit 2a88a45

Browse files
authored
Merge pull request #9 from incident-io/rob/add-attachments-on-incidents
Add attachments, and incident updates onto incidents stream
2 parents 6c6686d + fc1f5e2 commit 2a88a45

4 files changed

+139
-2
lines changed

model/external_resource_v1.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package model
2+
3+
import "github.com/incident-io/singer-tap/client"
4+
5+
type externalResourceV1 struct{}
6+
7+
var ExternalResourceV1 externalResourceV1
8+
9+
func (externalResourceV1) Schema() Property {
10+
return Property{
11+
Types: []string{"object"},
12+
Properties: map[string]Property{
13+
"external_id": {
14+
Types: []string{"string"},
15+
},
16+
"permalink": {
17+
Types: []string{"string"},
18+
},
19+
"resource_type": {
20+
Types: []string{"string"},
21+
},
22+
"title": {
23+
Types: []string{"string"},
24+
},
25+
},
26+
}
27+
}
28+
29+
func (externalResourceV1) Serialize(input client.ExternalResourceV1) map[string]any {
30+
return DumpToMap(input)
31+
}

model/incident_attachment_v1.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package model
2+
3+
import "github.com/incident-io/singer-tap/client"
4+
5+
type incidentAttachmentV1 struct{}
6+
7+
var IncidentAttachmentV1 incidentAttachmentV1
8+
9+
func (incidentAttachmentV1) Schema() Property {
10+
return Property{
11+
Types: []string{"object"},
12+
Properties: map[string]Property{
13+
"id": {
14+
Types: []string{"string"},
15+
},
16+
"incident_id": {
17+
Types: []string{"string"},
18+
},
19+
"resource": ExternalResourceV1.Schema(),
20+
},
21+
}
22+
}
23+
24+
func (incidentAttachmentV1) Serialize(input client.IncidentAttachmentV1) map[string]any {
25+
return map[string]any{
26+
"id": input.Id,
27+
"incident_id": input.IncidentId,
28+
"resource": ExternalResourceV1.Serialize(input.Resource),
29+
}
30+
}

model/incident_v2.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ func (incidentV2) Schema() Property {
2525
"creator": ActorV2.Schema(),
2626
"custom_field_entries": ArrayOf(CustomFieldEntryV1.Schema()),
2727
"external_issue_reference": Optional(ExternalIssueReferenceV2.Schema()),
28+
"attachments": Optional(ArrayOf(IncidentAttachmentV1.Schema())),
29+
"updates": Optional(ArrayOf(IncidentUpdateV2.Schema())),
2830
"incident_role_assignments": ArrayOf(IncidentRoleAssignmentV1.Schema()),
2931
"incident_status": IncidentStatusV1.Schema(),
3032
"incident_timestamp_values": Optional(ArrayOf(IncidentTimestampWithValueV2.Schema())),
@@ -75,7 +77,25 @@ func (incidentV2) Schema() Property {
7577
}
7678
}
7779

78-
func (incidentV2) Serialize(input client.IncidentV2) map[string]any {
80+
func (incidentV2) Serialize(
81+
input client.IncidentV2,
82+
incidentAttachments []client.IncidentAttachmentV1,
83+
incidentUpdates []client.IncidentUpdateV2,
84+
) map[string]any {
85+
var attachments []map[string]any
86+
if len(incidentAttachments) > 0 {
87+
attachments = lo.Map(incidentAttachments, func(attachment client.IncidentAttachmentV1, _ int) map[string]any {
88+
return IncidentAttachmentV1.Serialize(attachment)
89+
})
90+
}
91+
92+
var updates []map[string]any
93+
if len(incidentUpdates) > 0 {
94+
updates = lo.Map(incidentUpdates, func(update client.IncidentUpdateV2, _ int) map[string]any {
95+
return IncidentUpdateV2.Serialize(update)
96+
})
97+
}
98+
7999
return map[string]any{
80100
"id": input.Id,
81101
"name": input.Name,
@@ -85,6 +105,8 @@ func (incidentV2) Serialize(input client.IncidentV2) map[string]any {
85105
return CustomFieldEntryV1.Serialize(entry)
86106
}),
87107
"external_issue_reference": ExternalIssueReferenceV2.Serialize(input.ExternalIssueReference),
108+
"attachments": attachments,
109+
"updates": updates,
88110
"incident_role_assignments": lo.Map(input.IncidentRoleAssignments, func(assignment client.IncidentRoleAssignmentV1, _ int) map[string]any {
89111
return IncidentRoleAssignmentV1.Serialize(assignment)
90112
}),

tap/stream_incidents.go

+55-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,17 @@ func (s *StreamIncidents) GetRecords(ctx context.Context, logger kitlog.Logger,
4949
}
5050

5151
for _, element := range page.JSON200.Incidents {
52-
results = append(results, model.IncidentV2.Serialize(element))
52+
attachments, err := s.GetAttachments(ctx, logger, cl, element.Id)
53+
if err != nil {
54+
return nil, errors.Wrap(err, "listing incident attachments")
55+
}
56+
57+
updates, err := s.GetIncidentUpdates(ctx, logger, cl, element.Id)
58+
if err != nil {
59+
return nil, errors.Wrap(err, "listing incident attachments")
60+
}
61+
62+
results = append(results, model.IncidentV2.Serialize(element, attachments, updates))
5363
}
5464
if count := len(page.JSON200.Incidents); count == 0 {
5565
return results, nil // end pagination
@@ -58,3 +68,47 @@ func (s *StreamIncidents) GetRecords(ctx context.Context, logger kitlog.Logger,
5868
}
5969
}
6070
}
71+
72+
func (s *StreamIncidents) GetAttachments(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses, incidentId string) ([]client.IncidentAttachmentV1, error) {
73+
var (
74+
results = []client.IncidentAttachmentV1{}
75+
)
76+
77+
response, err := cl.IncidentAttachmentsV1ListWithResponse(ctx, &client.IncidentAttachmentsV1ListParams{
78+
IncidentId: &incidentId,
79+
})
80+
if err != nil {
81+
return nil, errors.Wrap(err, "listing attachments for incidents stream")
82+
}
83+
84+
results = append(results, response.JSON200.IncidentAttachments...)
85+
return results, nil
86+
}
87+
88+
func (s *StreamIncidents) GetIncidentUpdates(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses, incidentId string) ([]client.IncidentUpdateV2, error) {
89+
var (
90+
after *string
91+
pageSize = 250
92+
results = []client.IncidentUpdateV2{}
93+
)
94+
95+
for {
96+
logger.Log("msg", "loading incident updates page", "page_size", pageSize, "after", after)
97+
page, err := cl.IncidentUpdatesV2ListWithResponse(ctx, &client.IncidentUpdatesV2ListParams{
98+
IncidentId: &incidentId,
99+
PageSize: &pageSize,
100+
After: after,
101+
})
102+
if err != nil {
103+
return nil, errors.Wrap(err, "listing incident updates")
104+
}
105+
106+
results = append(results, page.JSON200.IncidentUpdates...)
107+
108+
if count := len(page.JSON200.IncidentUpdates); count == 0 {
109+
return results, nil // end pagination
110+
} else {
111+
after = lo.ToPtr(page.JSON200.IncidentUpdates[count-1].Id)
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)