@@ -3,6 +3,7 @@ package bitbucket
3
3
import (
4
4
"encoding/json"
5
5
"errors"
6
+ "fmt"
6
7
"io/ioutil"
7
8
"net/url"
8
9
"os"
@@ -141,6 +142,29 @@ type BranchModel struct {
141
142
Use_Mainbranch bool
142
143
}
143
144
145
+ type Environments struct {
146
+ Page int
147
+ Pagelen int
148
+ MaxDepth int
149
+ Size int
150
+ Next string
151
+ Environments []Environment
152
+ }
153
+
154
+ type EnvironmentType struct {
155
+ Name string
156
+ Rank int
157
+ Type string
158
+ }
159
+
160
+ type Environment struct {
161
+ Uuid string
162
+ Name string
163
+ EnvironmentType EnvironmentType
164
+ Rank int
165
+ Type string
166
+ }
167
+
144
168
func (r * Repository ) Create (ro * RepositoryOptions ) (* Repository , error ) {
145
169
data := r .buildRepositoryBody (ro )
146
170
urlStr := r .c .requestUrl ("/repositories/%s/%s" , ro .Owner , ro .RepoSlug )
@@ -426,15 +450,46 @@ func (r *Repository) BranchingModel(rbmo *RepositoryBranchingModelOptions) (*Bra
426
450
return decodeBranchingModel (response )
427
451
}
428
452
429
- func (r * Repository ) buildJsonBody (body map [string ]interface {}) string {
453
+ func (r * Repository ) ListEnvironments (opt * RepositoryEnvironmentsOptions ) (* Environments , error ) {
454
+ urlStr := r .c .requestUrl ("/repositories/%s/%s/environments/" , opt .Owner , opt .RepoSlug )
455
+ res , err := r .c .executeRaw ("GET" , urlStr , "" )
456
+ if err != nil {
457
+ return nil , err
458
+ }
430
459
431
- data , err := json . Marshal ( body )
460
+ bodyBytes , err := ioutil . ReadAll ( res )
432
461
if err != nil {
433
- pp .Println (err )
434
- os .Exit (9 )
462
+ return nil , err
435
463
}
436
464
437
- return string (data )
465
+ bodyString := string (bodyBytes )
466
+ return decodeEnvironments (bodyString )
467
+ }
468
+
469
+ func (r * Repository ) AddEnvironment (opt * RepositoryEnvironmentOptions ) (* Environment , error ) {
470
+ body := r .buildEnvironmentBody (opt )
471
+ urlStr := r .c .requestUrl ("/repositories/%s/%s/environments/" , opt .Owner , opt .RepoSlug )
472
+ res , err := r .c .execute ("POST" , urlStr , body )
473
+ if err != nil {
474
+ return nil , err
475
+ }
476
+
477
+ return decodeEnvironment (res )
478
+ }
479
+
480
+ func (r * Repository ) DeleteEnvironment (opt * RepositoryEnvironmentDeleteOptions ) (interface {}, error ) {
481
+ urlStr := r .c .requestUrl ("/repositories/%s/%s/environments/%s" , opt .Owner , opt .RepoSlug , opt .Uuid )
482
+ return r .c .execute ("DELETE" , urlStr , "" )
483
+ }
484
+
485
+ func (r * Repository ) GetEnvironment (opt * RepositoryEnvironmentOptions ) (* Environment , error ) {
486
+ urlStr := r .c .requestUrl ("/repositories/%s/%s/environments/%s" , opt .Owner , opt .RepoSlug , opt .Uuid )
487
+ res , err := r .c .execute ("GET" , urlStr , "" )
488
+ if err != nil {
489
+ return nil , err
490
+ }
491
+
492
+ return decodeEnvironment (res )
438
493
}
439
494
440
495
func (r * Repository ) buildRepositoryBody (ro * RepositoryOptions ) string {
@@ -570,6 +625,34 @@ func (r *Repository) buildTagBody(rbo *RepositoryTagCreationOptions) string {
570
625
return r .buildJsonBody (body )
571
626
}
572
627
628
+ func (r * Repository ) buildEnvironmentBody (opt * RepositoryEnvironmentOptions ) string {
629
+ body := map [string ]interface {}{}
630
+
631
+ body ["environment_type" ] = map [string ]interface {}{
632
+ "name" : opt .EnvironmentType .String (),
633
+ "rank" : opt .Rank ,
634
+ "type" : "deployment_environment_type" ,
635
+ }
636
+ if opt .Uuid != "" {
637
+ body ["uuid" ] = opt .Uuid
638
+ }
639
+ body ["name" ] = opt .Name
640
+ body ["rank" ] = opt .Rank
641
+
642
+ return r .buildJsonBody (body )
643
+ }
644
+
645
+ func (r * Repository ) buildJsonBody (body map [string ]interface {}) string {
646
+
647
+ data , err := json .Marshal (body )
648
+ if err != nil {
649
+ pp .Println (err )
650
+ os .Exit (9 )
651
+ }
652
+
653
+ return string (data )
654
+ }
655
+
573
656
func decodeRepository (repoResponse interface {}) (* Repository , error ) {
574
657
repoMap := repoResponse .(map [string ]interface {})
575
658
@@ -862,6 +945,83 @@ func decodeBranchingModel(branchingModelResponse interface{}) (*BranchingModel,
862
945
return branchingModel , nil
863
946
}
864
947
948
+ func decodeEnvironments (response string ) (* Environments , error ) {
949
+ var responseMap map [string ]interface {}
950
+ err := json .Unmarshal ([]byte (response ), & responseMap )
951
+ if err != nil {
952
+ return nil , err
953
+ }
954
+
955
+ values := responseMap ["values" ].([]interface {})
956
+ var environmentsArray []Environment
957
+ var errs error = nil
958
+ for idx , value := range values {
959
+ var environment Environment
960
+ err = mapstructure .Decode (value , & environment )
961
+ if err != nil {
962
+ if errs == nil {
963
+ errs = err
964
+ } else {
965
+ errs = fmt .Errorf ("%w; environment %d: %w" , errs , idx , err )
966
+ }
967
+ } else {
968
+ environmentsArray = append (environmentsArray , environment )
969
+ }
970
+ }
971
+
972
+ page , ok := responseMap ["page" ].(float64 )
973
+ if ! ok {
974
+ page = 0
975
+ }
976
+
977
+ pagelen , ok := responseMap ["pagelen" ].(float64 )
978
+ if ! ok {
979
+ pagelen = 0
980
+ }
981
+
982
+ max_depth , ok := responseMap ["max_depth" ].(float64 )
983
+ if ! ok {
984
+ max_depth = 0
985
+ }
986
+
987
+ size , ok := responseMap ["size" ].(float64 )
988
+ if ! ok {
989
+ size = 0
990
+ }
991
+
992
+ next , ok := responseMap ["next" ].(string )
993
+ if ! ok {
994
+ next = ""
995
+ }
996
+
997
+ environments := Environments {
998
+ Page : int (page ),
999
+ Pagelen : int (pagelen ),
1000
+ MaxDepth : int (max_depth ),
1001
+ Size : int (size ),
1002
+ Next : next ,
1003
+ Environments : environmentsArray ,
1004
+ }
1005
+
1006
+ return & environments , nil
1007
+ }
1008
+
1009
+ func decodeEnvironment (response interface {}) (* Environment , error ) {
1010
+ responseMap := response .(map [string ]interface {})
1011
+
1012
+ if responseMap ["type" ] == "error" {
1013
+ return nil , DecodeError (responseMap )
1014
+ }
1015
+
1016
+ var environment = new (Environment )
1017
+ err := mapstructure .Decode (responseMap , & environment )
1018
+ if err != nil {
1019
+ return nil , err
1020
+ }
1021
+
1022
+ return environment , nil
1023
+ }
1024
+
865
1025
func (rf RepositoryFile ) String () string {
866
1026
return rf .Path
867
1027
}
0 commit comments