Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit 048e97a

Browse files
fix unit tests and repository branches
1 parent 1649c83 commit 048e97a

10 files changed

+150
-59
lines changed

bridgecrew/data_source_integrations_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ func TestAccDataSourceIntegrations(t *testing.T) {
1313
Steps: []resource.TestStep{
1414
{
1515
Config: testAccDataSourceIntegrations(),
16+
Check: resource.ComposeAggregateTestCheckFunc(
17+
resource.TestCheckResourceAttrSet("data.bridgecrew_integrations.test", "integrations.0.id"),
18+
resource.TestCheckResourceAttrSet("data.bridgecrew_integrations.test", "integrations.0.enable"),
19+
resource.TestCheckResourceAttrSet("data.bridgecrew_integrations.test", "integrations.0.integration_details"),
20+
resource.TestCheckResourceAttrSet("data.bridgecrew_integrations.test", "integrations.0.params"),
21+
resource.TestCheckResourceAttrSet("data.bridgecrew_integrations.test", "integrations.0.type"),
22+
resource.TestCheckResourceAttrSet("data.bridgecrew_integrations.test", "integrations.0.sf_execution_name"),
23+
resource.TestCheckResourceAttrSet("data.bridgecrew_integrations.test", "integrations.0.status"),
24+
),
1625
},
1726
},
1827
})

bridgecrew/data_source_policies_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ func TestAccDataSourcePolicies(t *testing.T) {
1313
Steps: []resource.TestStep{
1414
{
1515
Config: testAccDataSourcePolicies(),
16+
Check: resource.ComposeAggregateTestCheckFunc(
17+
resource.TestCheckResourceAttrSet("data.bridgecrew_policies.test", "id"),
18+
resource.TestCheckResourceAttrSet("data.bridgecrew_policies.test", "policies.0.cloud_provider"),
19+
resource.TestCheckResourceAttrSet("data.bridgecrew_policies.test", "policies.0.title"),
20+
resource.TestCheckResourceAttrSet("data.bridgecrew_policies.test", "policies.0.severity"),
21+
resource.TestCheckResourceAttrSet("data.bridgecrew_policies.test", "policies.0.category"),
22+
resource.TestCheckResourceAttrSet("data.bridgecrew_policies.test", "policies.0.resource_types.0"),
23+
resource.TestCheckResourceAttrSet("data.bridgecrew_policies.test", "policies.0.guideline"),
24+
resource.TestCheckResourceAttrSet("data.bridgecrew_policies.test", "policies.0.createdby"),
25+
resource.TestCheckResourceAttrSet("data.bridgecrew_policies.test", "policies.0.frameworks.0"),
26+
),
1627
},
1728
},
1829
})

bridgecrew/data_source_repositories_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ func TestAccDataSourceRepositories(t *testing.T) {
1313
Steps: []resource.TestStep{
1414
{
1515
Config: testAccDataSourceRepositories(),
16+
Check: resource.ComposeAggregateTestCheckFunc(
17+
resource.TestCheckResourceAttrSet("data.bridgecrew_repositories.test", "id"),
18+
resource.TestCheckResourceAttrSet("data.bridgecrew_repositories.test", "repositories.0.repository"),
19+
resource.TestCheckResourceAttrSet("data.bridgecrew_repositories.test", "repositories.0.source"),
20+
resource.TestCheckResourceAttrSet("data.bridgecrew_repositories.test", "repositories.0.owner"),
21+
resource.TestCheckResourceAttrSet("data.bridgecrew_repositories.test", "repositories.0.defaultbranch"),
22+
resource.TestCheckResourceAttrSet("data.bridgecrew_repositories.test", "repositories.0.ispublic"),
23+
resource.TestCheckResourceAttrSet("data.bridgecrew_repositories.test", "repositories.0.runs"),
24+
resource.TestCheckResourceAttrSet("data.bridgecrew_repositories.test", "repositories.0.creationdate"),
25+
resource.TestCheckResourceAttrSet("data.bridgecrew_repositories.test", "repositories.0.lastscandate"),
26+
),
1627
},
1728
},
1829
})

bridgecrew/data_source_repository_branches.go

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,48 @@ func dataSourceRepositoryBranches() *schema.Resource {
1616
return &schema.Resource{
1717
ReadContext: dataSourceRepositoryBranchRead,
1818
Schema: map[string]*schema.Schema{
19-
"repositoriesbranches": {
19+
"branches": {
2020
Type: schema.TypeList,
2121
Computed: true,
22-
Elem: &schema.Resource{},
22+
Elem: &schema.Resource{
23+
Schema: map[string]*schema.Schema{
24+
"name": {
25+
Type: schema.TypeString,
26+
Computed: true,
27+
},
28+
"creationdate": {
29+
Type: schema.TypeString,
30+
Computed: true,
31+
},
32+
"defaultbranch": {
33+
Type: schema.TypeBool,
34+
Computed: true,
35+
},
36+
},
37+
},
2338
},
24-
"target": {
39+
"repoowner": {
2540
Type: schema.TypeString,
2641
Required: true,
2742
},
28-
"sourcetype": {
43+
"reponame": {
2944
Type: schema.TypeString,
3045
Required: true,
3146
},
47+
"source": {
48+
Type: schema.TypeString,
49+
Computed: true,
50+
},
3251
},
3352
}
3453
}
3554

3655
func dataSourceRepositoryBranchRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
37-
target := d.Get("target")
38-
params := RequestParams{"%s/repositories/branches?repoOwner=" + "/" + target.(string), "v1", "GET"}
56+
owner := d.Get("repoowner").(string)
57+
reponame := d.Get("reponame").(string)
3958

40-
//todo endpoint doesnt work like this
59+
request := "%s/repositories/branches?repoOwner=" + owner + "&repoName=" + reponame
60+
params := RequestParams{request, "v1", "GET"}
4161

4262
configure := m.(ProviderConfig)
4363
client, req, diagnostics, done := authClient(params, configure, nil)
@@ -52,26 +72,24 @@ func dataSourceRepositoryBranchRead(ctx context.Context, d *schema.ResourceData,
5272
log.Print("Failed at client.Do")
5373
return diag.FromErr(err)
5474
}
55-
//goland:noinspection GoUnhandledErrorResult
75+
76+
// goland:noinspection GoUnhandledErrorResult
5677
defer r.Body.Close()
5778

58-
log.Print("All data obtained")
59-
repositoriesbranches := make([]map[string]interface{}, 0)
79+
repositoriesbranches := make(map[string]interface{})
6080
err = json.NewDecoder(r.Body).Decode(&repositoriesbranches)
6181

62-
//todo this actually needs the target repository
63-
64-
log.Print("Decoded data")
65-
log.Print(r.Body)
66-
6782
if err != nil {
6883
log.Fatal("Failed to parse data")
6984
}
7085

71-
log.Print(repositoriesbranches)
7286
flatBranch := flattenBranchData(&repositoriesbranches)
7387

74-
if err := d.Set("repositories", flatBranch); err != nil {
88+
if err := d.Set("branches", flatBranch); err != nil {
89+
log.Fatal(reflect.TypeOf(repositoriesbranches))
90+
}
91+
92+
if err := d.Set("source", repositoriesbranches["source"].(string)); err != nil {
7593
log.Fatal(reflect.TypeOf(repositoriesbranches))
7694
}
7795

@@ -81,18 +99,20 @@ func dataSourceRepositoryBranchRead(ctx context.Context, d *schema.ResourceData,
8199
return diagnostics
82100
}
83101

84-
func flattenBranchData(Repositories *[]map[string]interface{}) []interface{} {
85-
if Repositories != nil {
86-
ois := make([]interface{}, len(*Repositories))
87-
88-
//for i, Repository := range *Repositories {
89-
// oi := make(map[string]interface{})
90-
//oi["name"] = Repository["name"]
91-
//oi["creationdate"] = Repository["creationDate"]
92-
//oi["defaultbranch"] = Repository["defaultBranch"]
93-
94-
// ois[i] = oi
95-
//}
102+
func flattenBranchData(repos *map[string]interface{}) []interface{} {
103+
if repos != nil {
104+
ois := make([]interface{}, len(*repos))
105+
temp := *repos
106+
branches := temp["branches"].([]interface{})
107+
for i, Repository := range branches {
108+
oi := make(map[string]interface{})
109+
scratch := Repository.(map[string]interface{})
110+
oi["name"] = scratch["name"].(string)
111+
oi["creationdate"] = scratch["creationDate"].(string)
112+
oi["defaultbranch"] = scratch["defaultBranch"].(bool)
113+
114+
ois[i] = oi
115+
}
96116

97117
return ois
98118
}
Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
11
package bridgecrew
22

3-
//func TestAccDataSourceRepositoryBranches(t *testing.T) {
4-
// resource.ParallelTest(t, resource.TestCase{
5-
// PreCheck: func() { testAccPreCheck(t) },
6-
// ProviderFactories: testAccProviders,
7-
// Steps: []resource.TestStep{
8-
// {
9-
// Config: testAccDataSourceRepositoryBranches(),
10-
// },
11-
// },
12-
// })
13-
//}
3+
import (
4+
"testing"
145

15-
//func testAccDataSourceRepositoryBranches() string {
16-
// return `
17-
// data "bridgecrew_repository_branches" "test" {
18-
// target="cfngoat"
19-
// }`
20-
//}
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccDataSourceRepositoryBranches(t *testing.T) {
10+
resource.ParallelTest(t, resource.TestCase{
11+
PreCheck: func() { testAccPreCheck(t) },
12+
ProviderFactories: testAccProviders,
13+
Steps: []resource.TestStep{
14+
{
15+
Config: testAccDataSourceRepositoryBranches(),
16+
Check: resource.ComposeAggregateTestCheckFunc(
17+
resource.TestCheckResourceAttrSet("data.bridgecrew_repository_branches.test", "id"),
18+
resource.TestCheckResourceAttrSet("data.bridgecrew_repository_branches.test", "branches.0.creationdate"),
19+
resource.TestCheckResourceAttrSet("data.bridgecrew_repository_branches.test", "branches.0.defaultbranch"),
20+
resource.TestCheckResourceAttrSet("data.bridgecrew_repository_branches.test", "branches.0.name"),
21+
resource.TestCheckResourceAttrSet("data.bridgecrew_repository_branches.test", "branches.0.defaultbranch"),
22+
resource.TestCheckResourceAttrSet("data.bridgecrew_repository_branches.test", "reponame"),
23+
resource.TestCheckResourceAttrSet("data.bridgecrew_repository_branches.test", "repoowner"),
24+
resource.TestCheckResourceAttrSet("data.bridgecrew_repository_branches.test", "source"),
25+
),
26+
},
27+
},
28+
})
29+
}
30+
31+
func testAccDataSourceRepositoryBranches() string {
32+
return `
33+
data "bridgecrew_repository_branches" "test" {
34+
reponame="terraform-aws-cassandra"
35+
repoowner="JamesWoolfenden"
36+
}`
37+
}

bridgecrew/models.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,16 @@ type User struct {
113113
LastModified int64 `json:"last_modified"`
114114
CustomerName string `json:"customer_name"`
115115
}
116+
117+
// Branch contains a branches used in CICD
118+
type Branch struct {
119+
Name string `json:"name"`
120+
CreationDate string `json:"creationdate"`
121+
DefaultBranch bool `json:"defaultbranch"`
122+
}
123+
124+
// Repositories For CICD
125+
type Repositories struct {
126+
Source string `json:"source"`
127+
Branches []Branch `json:"branches"`
128+
}

docs/data-sources/repository_branches.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,33 @@ Use this datasource to get the details of your managed repositories branches fro
1818

1919
## Example Usage
2020
```hcl
21-
data "bridgecrew_repository_branches" "branches" {
21+
data "bridgecrew_repository_branches" "test" {
22+
reponame="terraform-aws-cassandra"
23+
repoowner="JamesWoolfenden"
2224
}
2325
```
2426
<!-- schema generated by tfplugindocs -->
2527
## Schema
2628

2729
### Required
2830

29-
- **sourcetype** (String)
30-
- **target** (String)
31+
- **reponame** (String)
32+
- **repoowner** (String)
3133

3234
### Optional
3335

3436
- **id** (String) The ID of this resource.
3537

3638
### Read-Only
3739

38-
- **repositoriesbranches** (List of Object) (see [below for nested schema](#nestedatt--repositoriesbranches))
40+
- **branches** (List of Object) (see [below for nested schema](#nestedatt--branches))
41+
- **source** (String)
3942

40-
<a id="nestedatt--repositoriesbranches"></a>
41-
### Nested Schema for `repositoriesbranches`
43+
<a id="nestedatt--branches"></a>
44+
### Nested Schema for `branches`
4245

4346
Read-Only:
47+
48+
- **creationdate** (String)
49+
- **defaultbranch** (Boolean)
50+
- **name** (String)

templates/data-sources/repository_branches.md.tmpl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ Use this datasource to get the details of your managed repositories branches fro
1818

1919
## Example Usage
2020
```hcl
21-
data "bridgecrew_repository_branches" "branches" {
21+
data "bridgecrew_repository_branches" "test" {
22+
reponame="terraform-aws-cassandra"
23+
repoowner="JamesWoolfenden"
2224
}
2325
```
2426
{{end}}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
# data "bridgecrew_repository_branches" "all" {
2-
# target="terraform-aws-s3"
3-
# }
1+
data "bridgecrew_repository_branches" "all" {
2+
reponame = "terraform-aws-cassandra"
3+
repoowner = "JamesWoolfenden"
4+
}

terraform/outputs.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ output "users" {
1818
value = data.bridgecrew_users.all
1919
}
2020

21-
#output "branches" {
22-
# value = data.bridgecrew_repository_branches.all
23-
#}
21+
output "branches" {
22+
value = data.bridgecrew_repository_branches.all
23+
}
2424

2525

2626
output "authors" {

0 commit comments

Comments
 (0)