|
| 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 | +} |
0 commit comments