Skip to content

Commit

Permalink
fix: policy description was not updated (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliecharra authored Jun 20, 2024
1 parent 9964082 commit 45a2f25
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 74 deletions.
34 changes: 22 additions & 12 deletions internal/spacelift/repository/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type policyCreate struct {
AttachedStacks []attachedStack `graphql:"attachedStacks"`
}
type policyCreateMutation struct {
PolicyCreate policyCreate `graphql:"policyCreate(name: $name, body: $body, type: $type, labels: $labels, space: $space)"`
PolicyCreate policyCreate `graphql:"policyCreate(name: $name, body: $body, type: $type, labels: $labels, space: $space, description: $description)"`
}
type policyAttach struct {
Id string `graphql:"id"`
Expand All @@ -60,7 +60,7 @@ type policyUpdate struct {
AttachedStacks []attachedStack `graphql:"attachedStacks"`
}
type policyUpdateMutation struct {
PolicyUpdate policyUpdate `graphql:"policyUpdate(id: $id, name: $name, body: $body, labels: $labels, space: $space)"`
PolicyUpdate policyUpdate `graphql:"policyUpdate(id: $id, name: $name, body: $body, labels: $labels, space: $space, description: $description)"`
}

func NewPolicyRepository(client client.Client) *policyRepository {
Expand All @@ -75,11 +75,16 @@ func (r *policyRepository) Create(ctx context.Context, policy *v1beta1.Policy) (

var mutation policyCreateMutation
creationVars := map[string]any{
"name": graphql.String(policy.Name()),
"body": graphql.String(policy.Spec.Body),
"type": PolicyType(policy.Spec.Type),
"labels": structs.GetGraphQLStrings(&policy.Spec.Labels),
"space": (*graphql.ID)(nil),
"name": graphql.String(policy.Name()),
"body": graphql.String(policy.Spec.Body),
"description": graphql.String(""),
"type": PolicyType(policy.Spec.Type),
"labels": structs.GetGraphQLStrings(&policy.Spec.Labels),
"space": (*graphql.ID)(nil),
}

if policy.Spec.Description != nil && *policy.Spec.Description != "" {
creationVars["description"] = graphql.String(*policy.Spec.Description)
}

if policy.Spec.SpaceId != nil && *policy.Spec.SpaceId != "" {
Expand Down Expand Up @@ -119,11 +124,16 @@ func (r *policyRepository) Update(ctx context.Context, policy *v1beta1.Policy) (

var updateMutation policyUpdateMutation
updateVars := map[string]any{
"id": graphql.ID(policy.Status.Id),
"name": graphql.String(policy.Name()),
"body": graphql.String(policy.Spec.Body),
"labels": structs.GetGraphQLStrings(&policy.Spec.Labels),
"space": (*graphql.ID)(nil),
"id": graphql.ID(policy.Status.Id),
"name": graphql.String(policy.Name()),
"body": graphql.String(policy.Spec.Body),
"description": graphql.String(""),
"labels": structs.GetGraphQLStrings(&policy.Spec.Labels),
"space": (*graphql.ID)(nil),
}

if policy.Spec.Description != nil && *policy.Spec.Description != "" {
updateVars["description"] = graphql.String(*policy.Spec.Description)
}

if policy.Spec.SpaceId != nil && *policy.Spec.SpaceId != "" {
Expand Down
191 changes: 129 additions & 62 deletions internal/spacelift/repository/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,31 @@ func Test_policyRepository_Create(t *testing.T) {
}{
{
name: "basic policy",
policy: v1beta1.Policy{
ObjectMeta: v1.ObjectMeta{
Name: "name",
},
Spec: v1beta1.PolicySpec{
Body: "body",
Type: "PLAN",
Description: utils.AddressOf("description"),
Labels: []string{
"label1",
"label2",
},
},
},
expectedVars: map[string]any{
"name": graphql.String("name"),
"body": graphql.String("body"),
"description": graphql.String("description"),
"type": PolicyType("PLAN"),
"labels": structs.GetGraphQLStrings(&[]string{"label1", "label2"}),
"space": (*graphql.ID)(nil),
},
},
{
name: "basic policy without description",
policy: v1beta1.Policy{
ObjectMeta: v1.ObjectMeta{
Name: "name",
Expand All @@ -40,11 +65,12 @@ func Test_policyRepository_Create(t *testing.T) {
},
},
expectedVars: map[string]any{
"name": graphql.String("name"),
"body": graphql.String("body"),
"type": PolicyType("PLAN"),
"labels": structs.GetGraphQLStrings(&[]string{"label1", "label2"}),
"space": (*graphql.ID)(nil),
"name": graphql.String("name"),
"body": graphql.String("body"),
"type": PolicyType("PLAN"),
"description": graphql.String(""),
"labels": structs.GetGraphQLStrings(&[]string{"label1", "label2"}),
"space": (*graphql.ID)(nil),
},
},
{
Expand All @@ -54,21 +80,23 @@ func Test_policyRepository_Create(t *testing.T) {
Name: "name",
},
Spec: v1beta1.PolicySpec{
Name: utils.AddressOf("test name override"),
Body: "body",
Type: "PLAN",
Name: utils.AddressOf("test name override"),
Body: "body",
Description: utils.AddressOf("description"),
Type: "PLAN",
Labels: []string{
"label1",
"label2",
},
},
},
expectedVars: map[string]any{
"name": graphql.String("test name override"),
"body": graphql.String("body"),
"type": PolicyType("PLAN"),
"labels": structs.GetGraphQLStrings(&[]string{"label1", "label2"}),
"space": (*graphql.ID)(nil),
"name": graphql.String("test name override"),
"body": graphql.String("body"),
"description": graphql.String("description"),
"type": PolicyType("PLAN"),
"labels": structs.GetGraphQLStrings(&[]string{"label1", "label2"}),
"space": (*graphql.ID)(nil),
},
},
{
Expand All @@ -78,18 +106,20 @@ func Test_policyRepository_Create(t *testing.T) {
Name: "name",
},
Spec: v1beta1.PolicySpec{
Body: "body",
Type: "PLAN",
SpaceId: utils.AddressOf("space-1"),
Labels: []string{},
Body: "body",
Description: utils.AddressOf("description"),
Type: "PLAN",
SpaceId: utils.AddressOf("space-1"),
Labels: []string{},
},
},
expectedVars: map[string]any{
"name": graphql.String("name"),
"body": graphql.String("body"),
"type": PolicyType("PLAN"),
"labels": structs.GetGraphQLStrings(&[]string{}),
"space": graphql.ID("space-1"),
"name": graphql.String("name"),
"body": graphql.String("body"),
"description": graphql.String("description"),
"type": PolicyType("PLAN"),
"labels": structs.GetGraphQLStrings(&[]string{}),
"space": graphql.ID("space-1"),
},
},
{
Expand All @@ -99,18 +129,20 @@ func Test_policyRepository_Create(t *testing.T) {
Name: "name",
},
Spec: v1beta1.PolicySpec{
Body: "body",
Type: "PLAN",
SpaceId: utils.AddressOf("space-1"),
Labels: []string{},
Body: "body",
Description: utils.AddressOf("description"),
Type: "PLAN",
SpaceId: utils.AddressOf("space-1"),
Labels: []string{},
},
},
expectedVars: map[string]any{
"name": graphql.String("name"),
"body": graphql.String("body"),
"type": PolicyType("PLAN"),
"labels": structs.GetGraphQLStrings(&[]string{}),
"space": graphql.ID("space-1"),
"name": graphql.String("name"),
"body": graphql.String("body"),
"description": graphql.String("description"),
"type": PolicyType("PLAN"),
"labels": structs.GetGraphQLStrings(&[]string{}),
"space": graphql.ID("space-1"),
},
},
}
Expand Down Expand Up @@ -198,6 +230,34 @@ func Test_policyRepository_Update(t *testing.T) {
}{
{
name: "basic policy",
policy: v1beta1.Policy{
ObjectMeta: v1.ObjectMeta{
Name: "name",
},
Spec: v1beta1.PolicySpec{
Body: "body",
Type: "PLAN",
Description: utils.AddressOf("description"),
Labels: []string{
"label1",
"label2",
},
},
Status: v1beta1.PolicyStatus{
Id: "policy-id",
},
},
expectedVars: map[string]any{
"id": graphql.ID("policy-id"),
"name": graphql.String("name"),
"description": graphql.String("description"),
"body": graphql.String("body"),
"labels": structs.GetGraphQLStrings(&[]string{"label1", "label2"}),
"space": (*graphql.ID)(nil),
},
},
{
name: "basic policy without description",
policy: v1beta1.Policy{
ObjectMeta: v1.ObjectMeta{
Name: "name",
Expand All @@ -215,11 +275,12 @@ func Test_policyRepository_Update(t *testing.T) {
},
},
expectedVars: map[string]any{
"id": graphql.ID("policy-id"),
"name": graphql.String("name"),
"body": graphql.String("body"),
"labels": structs.GetGraphQLStrings(&[]string{"label1", "label2"}),
"space": (*graphql.ID)(nil),
"id": graphql.ID("policy-id"),
"name": graphql.String("name"),
"description": graphql.String(""),
"body": graphql.String("body"),
"labels": structs.GetGraphQLStrings(&[]string{"label1", "label2"}),
"space": (*graphql.ID)(nil),
},
},
{
Expand All @@ -229,9 +290,10 @@ func Test_policyRepository_Update(t *testing.T) {
Name: "name",
},
Spec: v1beta1.PolicySpec{
Name: utils.AddressOf("test name override"),
Body: "body",
Type: "PLAN",
Name: utils.AddressOf("test name override"),
Body: "body",
Description: utils.AddressOf("description"),
Type: "PLAN",
Labels: []string{
"label1",
"label2",
Expand All @@ -242,11 +304,12 @@ func Test_policyRepository_Update(t *testing.T) {
},
},
expectedVars: map[string]any{
"id": graphql.ID("policy-id"),
"name": graphql.String("test name override"),
"body": graphql.String("body"),
"labels": structs.GetGraphQLStrings(&[]string{"label1", "label2"}),
"space": (*graphql.ID)(nil),
"id": graphql.ID("policy-id"),
"name": graphql.String("test name override"),
"body": graphql.String("body"),
"description": graphql.String("description"),
"labels": structs.GetGraphQLStrings(&[]string{"label1", "label2"}),
"space": (*graphql.ID)(nil),
},
},
{
Expand All @@ -256,21 +319,23 @@ func Test_policyRepository_Update(t *testing.T) {
Name: "name",
},
Spec: v1beta1.PolicySpec{
Body: "body",
Type: "PLAN",
SpaceId: utils.AddressOf("space-1"),
Labels: []string{},
Body: "body",
Description: utils.AddressOf("description"),
Type: "PLAN",
SpaceId: utils.AddressOf("space-1"),
Labels: []string{},
},
Status: v1beta1.PolicyStatus{
Id: "policy-id",
},
},
expectedVars: map[string]any{
"id": graphql.ID("policy-id"),
"name": graphql.String("name"),
"body": graphql.String("body"),
"labels": structs.GetGraphQLStrings(&[]string{}),
"space": graphql.ID("space-1"),
"id": graphql.ID("policy-id"),
"name": graphql.String("name"),
"body": graphql.String("body"),
"description": graphql.String("description"),
"labels": structs.GetGraphQLStrings(&[]string{}),
"space": graphql.ID("space-1"),
},
},
{
Expand All @@ -280,21 +345,23 @@ func Test_policyRepository_Update(t *testing.T) {
Name: "name",
},
Spec: v1beta1.PolicySpec{
Body: "body",
Type: "PLAN",
SpaceId: utils.AddressOf("space-1"),
Labels: []string{},
Body: "body",
Description: utils.AddressOf("description"),
Type: "PLAN",
SpaceId: utils.AddressOf("space-1"),
Labels: []string{},
},
Status: v1beta1.PolicyStatus{
Id: "policy-id",
},
},
expectedVars: map[string]any{
"id": graphql.ID("policy-id"),
"name": graphql.String("name"),
"body": graphql.String("body"),
"labels": structs.GetGraphQLStrings(&[]string{}),
"space": graphql.ID("space-1"),
"id": graphql.ID("policy-id"),
"name": graphql.String("name"),
"body": graphql.String("body"),
"description": graphql.String("description"),
"labels": structs.GetGraphQLStrings(&[]string{}),
"space": graphql.ID("space-1"),
},
},
}
Expand Down

0 comments on commit 45a2f25

Please sign in to comment.