Skip to content

Commit

Permalink
Merge pull request #7 from incident-io/rob/add-custom-fields-stream
Browse files Browse the repository at this point in the history
Add custom fields and custom field options streams
  • Loading branch information
rliddler authored Oct 19, 2023
2 parents 5aa9594 + e9190a9 commit 48eaa2f
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 0 deletions.
41 changes: 41 additions & 0 deletions model/custom_field_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package model

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

type customFieldV2 struct{}

var CustomFieldV2 customFieldV2

func (customFieldV2) Schema() Property {
return Property{
Types: []string{"object"},
Properties: map[string]Property{
"id": {
Types: []string{"string"},
},
"name": {
Types: []string{"string"},
},
"field_type": {
Types: []string{"string"},
},
"description": {
Types: []string{"string"},
},
"created_at": DateTime.Schema(),
"updated_at": DateTime.Schema(),
},
}

}

func (customFieldV2) Serialize(input client.CustomFieldV2) map[string]any {
return map[string]any{
"id": input.Id,
"name": input.Name,
"field_type": input.FieldType,
"description": input.Description,
"created_at": input.CreatedAt,
"updated_at": input.UpdatedAt,
}
}
93 changes: 93 additions & 0 deletions tap/stream_custom_field_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package tap

import (
"context"

kitlog "github.com/go-kit/log"
"github.com/incident-io/singer-tap/client"
"github.com/incident-io/singer-tap/model"
"github.com/pkg/errors"
"github.com/samber/lo"
)

func init() {
register(&StreamCustomFieldOptions{})
}

type StreamCustomFieldOptions struct {
}

func (s *StreamCustomFieldOptions) Output() *Output {
return &Output{
Type: OutputTypeSchema,
Stream: "custom_field_options",
Schema: &model.Schema{
HasAdditionalProperties: false,
Type: []string{"object"},
Properties: model.CustomFieldOptionV1.Schema().Properties,
},
KeyProperties: []string{"id"},
BookmarkProperties: []string{},
}
}

func (s *StreamCustomFieldOptions) GetRecords(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses) ([]map[string]any, error) {
var (
results = []map[string]any{}
)

// We need to go over all custom fields to build the options
response, err := cl.CustomFieldsV2ListWithResponse(ctx)
if err != nil {
return nil, errors.Wrap(err, "listing incidents")
}

for _, element := range response.JSON200.CustomFields {
if err != nil {
return nil, errors.Wrap(err, "listing custom field options")
}

options, err := s.GetOptions(ctx, logger, cl, element.Id)
if err != nil {
return nil, errors.Wrap(err, "listing custom field options")
}

for _, option := range options {
results = append(results, model.CustomFieldOptionV1.Serialize(&option))
}
}

return results, nil
}

func (s *StreamCustomFieldOptions) GetOptions(
ctx context.Context,
logger kitlog.Logger,
cl *client.ClientWithResponses,
customFieldId string,
) ([]client.CustomFieldOptionV1, error) {
var (
after *string
pageSize = int64(250)
results = []client.CustomFieldOptionV1{}
)

for {
page, err := cl.CustomFieldOptionsV1ListWithResponse(ctx, &client.CustomFieldOptionsV1ListParams{
CustomFieldId: customFieldId,
PageSize: &pageSize,
After: after,
})
if err != nil {
return nil, errors.Wrap(err, "listing custom field options")
}

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

if count := len(page.JSON200.CustomFieldOptions); count == 0 {
return results, nil // end pagination
} else {
after = lo.ToPtr(page.JSON200.CustomFieldOptions[count-1].Id)
}
}
}
48 changes: 48 additions & 0 deletions tap/stream_custom_fields.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package tap

import (
"context"

kitlog "github.com/go-kit/log"
"github.com/incident-io/singer-tap/client"
"github.com/incident-io/singer-tap/model"
"github.com/pkg/errors"
)

func init() {
register(&StreamCustomFields{})
}

type StreamCustomFields struct {
}

func (s *StreamCustomFields) Output() *Output {
return &Output{
Type: OutputTypeSchema,
Stream: "custom_fields",
Schema: &model.Schema{
HasAdditionalProperties: false,
Type: []string{"object"},
Properties: model.CustomFieldV2.Schema().Properties,
},
KeyProperties: []string{"id"},
BookmarkProperties: []string{},
}
}

func (s *StreamCustomFields) GetRecords(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses) ([]map[string]any, error) {
var (
results = []map[string]any{}
)

response, err := cl.CustomFieldsV2ListWithResponse(ctx)
if err != nil {
return nil, errors.Wrap(err, "listing custom fields")
}

for _, element := range response.JSON200.CustomFields {
results = append(results, model.CustomFieldV2.Serialize(element))
}

return results, nil
}

0 comments on commit 48eaa2f

Please sign in to comment.