Skip to content

Commit

Permalink
created teamset struct
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerMizuyabu committed Aug 7, 2024
1 parent fc15813 commit eed770f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 25 deletions.
15 changes: 15 additions & 0 deletions internal/pkg/types/github_foundations/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package githubfoundations

import "github.com/zclconf/go-cty/cty"

func toCtyValueSlice(values []string) cty.Value {
if len(values) == 0 {
return cty.ListValEmpty(cty.String)
}

ctyValues := make([]cty.Value, len(values))
for _, v := range values {
ctyValues = append(ctyValues, cty.StringVal(v))
}
return cty.ListVal(ctyValues)
}
32 changes: 7 additions & 25 deletions internal/pkg/types/github_foundations/repository_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,7 @@ func (r *RepositoryInput) GetCtyValue() cty.Value {
mapVal["default_branch"] = cty.StringVal(r.DefaultBranch)
mapVal["advance_security"] = cty.BoolVal(r.AdvanceSecurity)
mapVal["has_vulnerability_alerts"] = cty.BoolVal(r.HasVulnerabilityAlerts)
topics := []cty.Value{}
for _, topic := range r.Topics {
topics = append(topics, cty.StringVal(topic))
}
if len(topics) > 0 {
mapVal["topics"] = cty.ListVal(topics)
} else {
mapVal["topics"] = cty.ListValEmpty(cty.String)
}
mapVal["topics"] = toCtyValueSlice(r.Topics)
mapVal["homepage"] = cty.StringVal(r.Homepage)
mapVal["delete_head_on_merge"] = cty.BoolVal(r.DeleteHeadBranchOnMerge)
mapVal["requires_web_commit_signing"] = cty.BoolVal(r.RequiresWebCommitSignOff)
Expand All @@ -88,27 +80,17 @@ func (r *RepositoryInput) GetCtyValue() cty.Value {

// Optional fields
if len(r.OrganizationActionSecrets) > 0 {
orgActionSecrets := []cty.Value{}
for _, secret := range r.OrganizationActionSecrets {
orgActionSecrets = append(orgActionSecrets, cty.StringVal(secret))
}
mapVal["organization_action_secrets"] = cty.ListVal(orgActionSecrets)

mapVal["organization_action_secrets"] = toCtyValueSlice(r.OrganizationActionSecrets)
}

if len(r.OrganizationCodespaceSecrets) > 0 {
orgCodespaceSecrets := []cty.Value{}
for _, secret := range r.OrganizationCodespaceSecrets {
orgCodespaceSecrets = append(orgCodespaceSecrets, cty.StringVal(secret))
}
mapVal["organization_codespace_secrets"] = cty.ListVal(orgCodespaceSecrets)
mapVal["organization_codespace_secrets"] = toCtyValueSlice(r.OrganizationCodespaceSecrets)
}

if len(r.OrganizationDependabotSecrets) > 0 {
orgDependaBotSecrets := []cty.Value{}
for _, secret := range r.OrganizationDependabotSecrets {
orgDependaBotSecrets = append(orgDependaBotSecrets, cty.StringVal(secret))
}
mapVal["organization_dependabot_secrets"] = cty.ListVal(orgDependaBotSecrets)
mapVal["organization_dependabot_secrets"] = toCtyValueSlice(r.OrganizationDependabotSecrets)
}

if len(r.ActionSecrets) > 0 {
actionSecretsMap := make(map[string]cty.Value)
for key, val := range r.ActionSecrets {
Expand Down
45 changes: 45 additions & 0 deletions internal/pkg/types/github_foundations/team_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package githubfoundations

import (
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/zclconf/go-cty/cty"
)

// Module Root Inputs

type TeamSetInput struct {
Teams []*TeamInput
}

func (r *TeamSetInput) WriteHCL(file *hclwrite.File) {
rootBody := file.Body()
rootBodyMap := make(map[string]cty.Value)

teams := make(map[string]cty.Value)
for _, team := range r.Teams {
teams[team.Name] = team.GetCtyValue()
}

rootBodyMap["teams"] = cty.ObjectVal(teams)
rootBody.SetAttributeValue("inputs", cty.ObjectVal(rootBodyMap))
}

type TeamInput struct {
Name string
Description string
Privacy string
Maintainers []string
Members []string
ParentId string
}

func (t *TeamInput) GetCtyValue() cty.Value {
mapVal := make(map[string]cty.Value)
mapVal["description"] = cty.StringVal(t.Description)
mapVal["privacy"] = cty.StringVal(t.Privacy)
mapVal["parent_id"] = cty.StringVal(t.ParentId)
mapVal["maintainers"] = toCtyValueSlice(t.Maintainers)
mapVal["members"] = toCtyValueSlice(t.Members)

return cty.ObjectVal(mapVal)
}

0 comments on commit eed770f

Please sign in to comment.