Skip to content

Commit e9190a9

Browse files
committed
Add custom fields and custom field options
This PR adds the custom fields stream and the custom field options streams. This data appears on the incidents tables in a nested fashion - these streams allow you to specifically query and join this data in a more relational fashion
1 parent a39fc89 commit e9190a9

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
lines changed

model/custom_field_v2.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package model
2+
3+
import "github.com/incident-io/singer-tap/client"
4+
5+
type customFieldV2 struct{}
6+
7+
var CustomFieldV2 customFieldV2
8+
9+
func (customFieldV2) Schema() Property {
10+
return Property{
11+
Types: []string{"object"},
12+
Properties: map[string]Property{
13+
"id": {
14+
Types: []string{"string"},
15+
},
16+
"name": {
17+
Types: []string{"string"},
18+
},
19+
"field_type": {
20+
Types: []string{"string"},
21+
},
22+
"description": {
23+
Types: []string{"string"},
24+
},
25+
"created_at": DateTime.Schema(),
26+
"updated_at": DateTime.Schema(),
27+
},
28+
}
29+
30+
}
31+
32+
func (customFieldV2) Serialize(input client.CustomFieldV2) map[string]any {
33+
return map[string]any{
34+
"id": input.Id,
35+
"name": input.Name,
36+
"field_type": input.FieldType,
37+
"description": input.Description,
38+
"created_at": input.CreatedAt,
39+
"updated_at": input.UpdatedAt,
40+
}
41+
}

tap/stream_custom_field_options.go

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package tap
2+
3+
import (
4+
"context"
5+
6+
kitlog "github.com/go-kit/log"
7+
"github.com/incident-io/singer-tap/client"
8+
"github.com/incident-io/singer-tap/model"
9+
"github.com/pkg/errors"
10+
"github.com/samber/lo"
11+
)
12+
13+
func init() {
14+
register(&StreamCustomFieldOptions{})
15+
}
16+
17+
type StreamCustomFieldOptions struct {
18+
}
19+
20+
func (s *StreamCustomFieldOptions) Output() *Output {
21+
return &Output{
22+
Type: OutputTypeSchema,
23+
Stream: "custom_field_options",
24+
Schema: &model.Schema{
25+
HasAdditionalProperties: false,
26+
Type: []string{"object"},
27+
Properties: model.CustomFieldOptionV1.Schema().Properties,
28+
},
29+
KeyProperties: []string{"id"},
30+
BookmarkProperties: []string{},
31+
}
32+
}
33+
34+
func (s *StreamCustomFieldOptions) GetRecords(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses) ([]map[string]any, error) {
35+
var (
36+
results = []map[string]any{}
37+
)
38+
39+
// We need to go over all custom fields to build the options
40+
response, err := cl.CustomFieldsV2ListWithResponse(ctx)
41+
if err != nil {
42+
return nil, errors.Wrap(err, "listing incidents")
43+
}
44+
45+
for _, element := range response.JSON200.CustomFields {
46+
if err != nil {
47+
return nil, errors.Wrap(err, "listing custom field options")
48+
}
49+
50+
options, err := s.GetOptions(ctx, logger, cl, element.Id)
51+
if err != nil {
52+
return nil, errors.Wrap(err, "listing custom field options")
53+
}
54+
55+
for _, option := range options {
56+
results = append(results, model.CustomFieldOptionV1.Serialize(&option))
57+
}
58+
}
59+
60+
return results, nil
61+
}
62+
63+
func (s *StreamCustomFieldOptions) GetOptions(
64+
ctx context.Context,
65+
logger kitlog.Logger,
66+
cl *client.ClientWithResponses,
67+
customFieldId string,
68+
) ([]client.CustomFieldOptionV1, error) {
69+
var (
70+
after *string
71+
pageSize = int64(250)
72+
results = []client.CustomFieldOptionV1{}
73+
)
74+
75+
for {
76+
page, err := cl.CustomFieldOptionsV1ListWithResponse(ctx, &client.CustomFieldOptionsV1ListParams{
77+
CustomFieldId: customFieldId,
78+
PageSize: &pageSize,
79+
After: after,
80+
})
81+
if err != nil {
82+
return nil, errors.Wrap(err, "listing custom field options")
83+
}
84+
85+
results = append(results, page.JSON200.CustomFieldOptions...)
86+
87+
if count := len(page.JSON200.CustomFieldOptions); count == 0 {
88+
return results, nil // end pagination
89+
} else {
90+
after = lo.ToPtr(page.JSON200.CustomFieldOptions[count-1].Id)
91+
}
92+
}
93+
}

tap/stream_custom_fields.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package tap
2+
3+
import (
4+
"context"
5+
6+
kitlog "github.com/go-kit/log"
7+
"github.com/incident-io/singer-tap/client"
8+
"github.com/incident-io/singer-tap/model"
9+
"github.com/pkg/errors"
10+
)
11+
12+
func init() {
13+
register(&StreamCustomFields{})
14+
}
15+
16+
type StreamCustomFields struct {
17+
}
18+
19+
func (s *StreamCustomFields) Output() *Output {
20+
return &Output{
21+
Type: OutputTypeSchema,
22+
Stream: "custom_fields",
23+
Schema: &model.Schema{
24+
HasAdditionalProperties: false,
25+
Type: []string{"object"},
26+
Properties: model.CustomFieldV2.Schema().Properties,
27+
},
28+
KeyProperties: []string{"id"},
29+
BookmarkProperties: []string{},
30+
}
31+
}
32+
33+
func (s *StreamCustomFields) GetRecords(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses) ([]map[string]any, error) {
34+
var (
35+
results = []map[string]any{}
36+
)
37+
38+
response, err := cl.CustomFieldsV2ListWithResponse(ctx)
39+
if err != nil {
40+
return nil, errors.Wrap(err, "listing custom fields")
41+
}
42+
43+
for _, element := range response.JSON200.CustomFields {
44+
results = append(results, model.CustomFieldV2.Serialize(element))
45+
}
46+
47+
return results, nil
48+
}

0 commit comments

Comments
 (0)