@@ -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
3655func 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 }
0 commit comments