Skip to content

Commit

Permalink
generate input objects, start using Nullable (#498)
Browse files Browse the repository at this point in the history
* WIP: generate input objects, start using Nullable

* Nullable type wraps optional fields of API input objects

* add getFieldTypeForObject, used when generating input objects

* add print statement

* populate enumExamples on the fly

* skip generating Campaign input objects

* drop generated Campaign input objects, update struct tags

* walk back tests changes of empty string to JSON null

* drop getFieldTypeForObject from gen.go, added in other PR

* limit which input fields are Nullable

* update changie log

* update enum referencing

* revert NewNullableFrom back to RefOf

* enum fixes, PR feedback

* fix remaining Nullable discrepencies

* add changies, drop unused new_input.go file
  • Loading branch information
davidbloss authored Jan 3, 2025
1 parent 56b571c commit 8e0a557
Show file tree
Hide file tree
Showing 36 changed files with 1,262 additions and 1,100 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Refactor-20241227-103359.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Refactor
body: 'BREAKING CHANGE: "Nullable" type now wraps some optional struct fields of API input objects. This "Nullable" type enables fields to be set to the JSON "null" value.'
time: 2024-12-27T10:33:59.906375-06:00
3 changes: 3 additions & 0 deletions .changes/unreleased/Refactor-20250103-150158.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Refactor
body: "enums converted from consts to vars, e.g. opslevel.RefOf(opslevel.AlertSourceTypeEnumDatadog) should now be &opslevel.AlertSourceTypeEnumDatadog"
time: 2025-01-03T15:01:58.276367-06:00
3 changes: 3 additions & 0 deletions .changes/unreleased/Removed-20250103-151332.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Removed
body: removed NullableString func, no longer used
time: 2025-01-03T15:13:32.484099-06:00
4 changes: 2 additions & 2 deletions actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ func (client *Client) CreateTriggerDefinition(input CustomActionsTriggerDefiniti
} `graphql:"customActionsTriggerDefinitionCreate(input: $input)"`
}
if input.AccessControl == nil {
input.AccessControl = RefOf(CustomActionsTriggerDefinitionAccessControlEnumEveryone)
input.AccessControl = &CustomActionsTriggerDefinitionAccessControlEnumEveryone
}
if input.EntityType == nil {
input.EntityType = RefOf(CustomActionsEntityTypeEnumService)
input.EntityType = &CustomActionsEntityTypeEnumService
}
v := PayloadVariables{
"input": input,
Expand Down
32 changes: 18 additions & 14 deletions actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ func TestCreateWebhookAction(t *testing.T) {
client := BestTestClient(t, "custom_actions/create_action", testRequest)

// Act
jsonHeaders, err := ol.NewJSON(`{"Content-Type": "application/json"}`)
autopilot.Ok(t, err)
action, err := client.CreateWebhookAction(ol.CustomActionsWebhookActionCreateInput{
Name: "Deploy Rollback",
LiquidTemplate: ol.RefOf("{\"token\": \"XXX\", \"ref\":\"main\", \"action\": \"rollback\"}"),
Headers: &ol.JSON{"Content-Type": "application/json"},
Headers: jsonHeaders,
HttpMethod: ol.CustomActionsHttpMethodEnumPost,
WebhookUrl: "https://gitlab.com/api/v4/projects/1/trigger/pipeline",
})
Expand Down Expand Up @@ -71,7 +73,7 @@ func TestUpdateWebhookAction(t *testing.T) {
// Act
action, err := client.UpdateWebhookAction(ol.CustomActionsWebhookActionUpdateInput{
Id: *newID,
HttpMethod: ol.RefOf(ol.CustomActionsHttpMethodEnumPut),
HttpMethod: &ol.CustomActionsHttpMethodEnumPut,
})

// Assert
Expand All @@ -90,10 +92,12 @@ func TestUpdateWebhookAction2(t *testing.T) {
client := BestTestClient(t, "custom_actions/update_action2", testRequest)

// Act
jsonHeaders, err := ol.NewJSON(`{"Accept": "application/json"}`)
autopilot.Ok(t, err)
action, err := client.UpdateWebhookAction(ol.CustomActionsWebhookActionUpdateInput{
Id: *newID,
Description: ol.RefOf(""),
Headers: &ol.JSON{"Accept": "application/json"},
Headers: jsonHeaders,
})

// Assert
Expand Down Expand Up @@ -130,13 +134,13 @@ func TestCreateTriggerDefinition(t *testing.T) {
// Act
trigger, err := client.CreateTriggerDefinition(ol.CustomActionsTriggerDefinitionCreateInput{
Name: "Deploy Rollback",
AccessControl: ol.RefOf(ol.CustomActionsTriggerDefinitionAccessControlEnumEveryone),
AccessControl: &ol.CustomActionsTriggerDefinitionAccessControlEnumEveryone,
Description: ol.RefOf("Disables the Deploy Freeze"),
ResponseTemplate: ol.RefOf(""),
ManualInputsDefinition: ol.RefOf(""),
ActionId: newID,
ActionId: ol.RefOf(*newID),
OwnerId: *newID,
FilterId: ol.NewID("987654321"),
FilterId: ol.RefOf(ol.ID("987654321")),
})
// Assert
autopilot.Ok(t, err)
Expand All @@ -155,14 +159,14 @@ func TestCreateTriggerDefinitionWithGlobalEntityType(t *testing.T) {
// Act
trigger, err := client.CreateTriggerDefinition(ol.CustomActionsTriggerDefinitionCreateInput{
Name: "Deploy Rollback",
AccessControl: ol.RefOf(ol.CustomActionsTriggerDefinitionAccessControlEnumEveryone),
AccessControl: &ol.CustomActionsTriggerDefinitionAccessControlEnumEveryone,
Description: ol.RefOf("Disables the Deploy Freeze"),
ActionId: newID,
ActionId: ol.RefOf(*newID),
ManualInputsDefinition: ol.RefOf(""),
ResponseTemplate: ol.RefOf(""),
OwnerId: *newID,
FilterId: ol.NewID("987654321"),
EntityType: ol.RefOf(ol.CustomActionsEntityTypeEnumGlobal),
FilterId: ol.RefOf(ol.ID("987654321")),
EntityType: &ol.CustomActionsEntityTypeEnumGlobal,
ExtendedTeamAccess: &[]ol.IdentifierInput{
*ol.NewIdentifier("example_1"),
*ol.NewIdentifier("example_1"),
Expand All @@ -186,12 +190,12 @@ func TestCreateTriggerDefinitionWithNullExtendedTeams(t *testing.T) {
trigger, err := client.CreateTriggerDefinition(ol.CustomActionsTriggerDefinitionCreateInput{
Name: "Deploy Rollback",
Description: ol.RefOf("Disables the Deploy Freeze"),
AccessControl: ol.RefOf(ol.CustomActionsTriggerDefinitionAccessControlEnumEveryone),
AccessControl: &ol.CustomActionsTriggerDefinitionAccessControlEnumEveryone,
ManualInputsDefinition: ol.RefOf(""),
ResponseTemplate: ol.RefOf(""),
ActionId: newID,
ActionId: ol.RefOf(*newID),
OwnerId: *newID,
FilterId: ol.NewID("987654321"),
FilterId: ol.RefOf(ol.ID("987654321")),
ExtendedTeamAccess: &[]ol.IdentifierInput{},
})
// Assert
Expand Down Expand Up @@ -258,7 +262,7 @@ func TestUpdateTriggerDefinition(t *testing.T) {
// Act
trigger, err := client.UpdateTriggerDefinition(ol.CustomActionsTriggerDefinitionUpdateInput{
Id: *newID,
FilterId: ol.NewID(),
FilterId: ol.NewNullOf[ol.ID](),
})
// Assert
autopilot.Ok(t, err)
Expand Down
2 changes: 1 addition & 1 deletion aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (client *Client) CreateAliases(ownerId ID, aliases []string) ([]string, err
for _, alias := range aliases {
input := AliasCreateInput{
Alias: alias,
OwnerId: ownerId,
OwnerId: ID(ownerId),
}
result, err := client.CreateAlias(input)
allErrors = errors.Join(allErrors, err)
Expand Down
36 changes: 18 additions & 18 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ type CheckCreateInputProvider interface {
}

type CheckCreateInput struct {
Name string `json:"name" yaml:"name" mapstructure:"name"`
Enabled *bool `json:"enabled" yaml:"enabled" mapstructure:"enabled"`
EnableOn *iso8601.Time `json:"enableOn,omitempty" yaml:"enableOn,omitempty" mapstructure:"enabledOn,omitempty"`
Category ID `json:"categoryId" yaml:"categoryId" mapstructure:"categoryId"`
Level ID `json:"levelId" yaml:"levelId" mapstructure:"levelId"`
Owner *ID `json:"ownerId,omitempty" yaml:"ownerId,omitempty" mapstructure:"ownerId,omitempty"`
Filter *ID `json:"filterId,omitempty" yaml:"filterId,omitempty" mapstructure:"filterId,omitempty"`
Notes *string `json:"notes" yaml:"notes" default:"Notes on Example Check" mapstructure:"notes"`
Category ID `json:"categoryId" yaml:"categoryId" mapstructure:"categoryId"`
EnableOn *Nullable[iso8601.Time] `json:"enableOn,omitempty" yaml:"enableOn,omitempty" mapstructure:"enabledOn,omitempty"`
Enabled *Nullable[bool] `json:"enabled,omitempty" yaml:"enabled,omitempty" mapstructure:"enabled"`
Filter *Nullable[ID] `json:"filterId,omitempty" yaml:"filterId,omitempty" mapstructure:"filterId,omitempty"`
Level ID `json:"levelId" yaml:"levelId" mapstructure:"levelId"`
Name string `json:"name" yaml:"name" mapstructure:"name"`
Notes *Nullable[string] `json:"notes,omitempty" yaml:"notes,omitempty" mapstructure:"notes,omitempty"`
Owner *Nullable[ID] `json:"ownerId,omitempty" yaml:"ownerId,omitempty" mapstructure:"ownerId,omitempty"`
}

func NewCheckCreateInputTypeOf[T any](checkCreateInput CheckCreateInput) *T {
Expand All @@ -113,15 +113,15 @@ type CheckUpdateInputProvider interface {
}

type CheckUpdateInput struct {
Id ID `json:"id" mapstructure:"id"`
Name string `json:"name,omitempty" mapstructure:"name,omitempty"`
Enabled *bool `json:"enabled,omitempty" mapstructure:"enabled,omitempty"`
EnableOn *iso8601.Time `json:"enableOn,omitempty" mapstructure:"enabledOn,omitempty"`
Category ID `json:"categoryId,omitempty" mapstructure:"categoryId,omitempty"`
Level ID `json:"levelId,omitempty" mapstructure:"levelId,omitempty"`
Owner *ID `json:"ownerId,omitempty" yaml:"ownerId,omitempty" mapstructure:"ownerId,omitempty"`
Filter *ID `json:"filterId,omitempty" yaml:"filterId,omitempty" mapstructure:"filterId,omitempty"`
Notes *string `json:"notes,omitempty" mapstructure:"notes,omitempty"`
Category *Nullable[ID] `json:"categoryId,omitempty" mapstructure:"categoryId,omitempty"`
EnableOn *Nullable[iso8601.Time] `json:"enableOn,omitempty" mapstructure:"enabledOn,omitempty"`
Enabled *Nullable[bool] `json:"enabled,omitempty" mapstructure:"enabled,omitempty"`
Filter *Nullable[ID] `json:"filterId,omitempty" yaml:"filterId,omitempty" mapstructure:"filterId,omitempty"`
Id ID `json:"id" mapstructure:"id"`
Level *Nullable[ID] `json:"levelId,omitempty" mapstructure:"levelId,omitempty"`
Name *Nullable[string] `json:"name,omitempty" mapstructure:"name,omitempty"`
Notes *Nullable[string] `json:"notes,omitempty" mapstructure:"notes,omitempty"`
Owner *Nullable[ID] `json:"ownerId,omitempty" yaml:"ownerId,omitempty" mapstructure:"ownerId,omitempty"`
}

func NewCheckUpdateInputTypeOf[T any](checkUpdateInput CheckUpdateInput) *T {
Expand Down Expand Up @@ -275,7 +275,7 @@ func (client *Client) DeleteCheck(id ID) error {
} `graphql:"checkDelete(input: $input)"`
}
v := PayloadVariables{
"input": CheckDeleteInput{Id: &id},
"input": CheckDeleteInput{Id: RefOf(id)},
}
err := client.Mutate(&m, v, WithName("CheckDelete"))
return HandleErrors(err, m.Payload.Errors)
Expand Down
4 changes: 2 additions & 2 deletions check_manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func NewManualCheckFrequencyInput(startingDate string, timeScale FrequencyTimeSc
func NewManualCheckFrequencyUpdateInput(startingDate string, timeScale FrequencyTimeScale, value int) *ManualCheckFrequencyUpdateInput {
startingDateIso := NewISO8601Date(startingDate)
return &ManualCheckFrequencyUpdateInput{
StartingDate: &startingDateIso,
StartingDate: RefOf(startingDateIso),
FrequencyTimeScale: &timeScale,
FrequencyValue: &value,
FrequencyValue: RefOf(value),
}
}

Expand Down
Loading

0 comments on commit 8e0a557

Please sign in to comment.