-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from incident-io/rob/add-custom-fields-stream
Add custom fields and custom field options streams
- Loading branch information
Showing
3 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |