@@ -165,6 +165,23 @@ type Environment struct {
165165 Type string
166166}
167167
168+ type DeploymentVariables struct {
169+ Page int
170+ Pagelen int
171+ MaxDepth int
172+ Size int
173+ Next string
174+ Variables []DeploymentVariable
175+ }
176+
177+ type DeploymentVariable struct {
178+ Type string
179+ Uuid string
180+ Key string
181+ Value string
182+ Secured bool
183+ }
184+
168185func (r * Repository ) Create (ro * RepositoryOptions ) (* Repository , error ) {
169186 data := r .buildRepositoryBody (ro )
170187 urlStr := r .c .requestUrl ("/repositories/%s/%s" , ro .Owner , ro .RepoSlug )
@@ -492,6 +509,70 @@ func (r *Repository) GetEnvironment(opt *RepositoryEnvironmentOptions) (*Environ
492509 return decodeEnvironment (res )
493510}
494511
512+ func (r * Repository ) ListDeploymentVariables (opt * RepositoryDeploymentVariablesOptions ) (* DeploymentVariables , error ) {
513+ params := url.Values {}
514+ if opt .Query != "" {
515+ params .Add ("q" , opt .Query )
516+ }
517+
518+ if opt .Sort != "" {
519+ params .Add ("sort" , opt .Sort )
520+ }
521+
522+ if opt .PageNum > 0 {
523+ params .Add ("page" , strconv .Itoa (opt .PageNum ))
524+ }
525+
526+ if opt .Pagelen > 0 {
527+ params .Add ("pagelen" , strconv .Itoa (opt .Pagelen ))
528+ }
529+
530+ if opt .MaxDepth > 0 {
531+ params .Add ("max_depth" , strconv .Itoa (opt .MaxDepth ))
532+ }
533+
534+ urlStr := r .c .requestUrl ("/repositories/%s/%s/deployments_config/environments/%s/variables?%s" , opt .Owner , opt .RepoSlug , opt .Environment .Uuid , params .Encode ())
535+ response , err := r .c .executeRaw ("GET" , urlStr , "" )
536+ if err != nil {
537+ return nil , err
538+ }
539+ bodyBytes , err := ioutil .ReadAll (response )
540+ if err != nil {
541+ return nil , err
542+ }
543+ bodyString := string (bodyBytes )
544+ return decodeDeploymentVariables (bodyString )
545+ }
546+
547+ func (r * Repository ) AddDeploymentVariable (opt * RepositoryDeploymentVariableOptions ) (* DeploymentVariable , error ) {
548+ body := r .buildDeploymentVariableBody (opt )
549+ urlStr := r .c .requestUrl ("/repositories/%s/%s/deployments_config/environments/%s/variables" , opt .Owner , opt .RepoSlug , opt .Environment .Uuid )
550+
551+ response , err := r .c .execute ("POST" , urlStr , body )
552+ if err != nil {
553+ return nil , err
554+ }
555+
556+ return decodeDeploymentVariable (response )
557+ }
558+
559+ func (r * Repository ) DeleteDeploymentVariable (opt * RepositoryDeploymentVariableDeleteOptions ) (interface {}, error ) {
560+ urlStr := r .c .requestUrl ("/repositories/%s/%s/deployments_config/environments/%s/variables/%s" , opt .Owner , opt .RepoSlug , opt .Environment .Uuid , opt .Uuid )
561+ return r .c .execute ("DELETE" , urlStr , "" )
562+ }
563+
564+ func (r * Repository ) UpdateDeploymentVariable (opt * RepositoryDeploymentVariableOptions ) (* DeploymentVariable , error ) {
565+ body := r .buildDeploymentVariableBody (opt )
566+ urlStr := r .c .requestUrl ("/repositories/%s/%s/deployments_config/environments/%s/variables/%s" , opt .Owner , opt .RepoSlug , opt .Environment .Uuid , opt .Uuid )
567+
568+ response , err := r .c .execute ("PUT" , urlStr , body )
569+ if err != nil {
570+ return nil , err
571+ }
572+
573+ return decodeDeploymentVariable (response )
574+ }
575+
495576func (r * Repository ) buildRepositoryBody (ro * RepositoryOptions ) string {
496577
497578 body := map [string ]interface {}{}
@@ -642,6 +723,19 @@ func (r *Repository) buildEnvironmentBody(opt *RepositoryEnvironmentOptions) str
642723 return r .buildJsonBody (body )
643724}
644725
726+ func (r * Repository ) buildDeploymentVariableBody (opt * RepositoryDeploymentVariableOptions ) string {
727+ body := map [string ]interface {}{}
728+
729+ if opt .Uuid != "" {
730+ body ["uuid" ] = opt .Uuid
731+ }
732+ body ["key" ] = opt .Key
733+ body ["value" ] = opt .Value
734+ body ["secured" ] = opt .Secured
735+
736+ return r .buildJsonBody (body )
737+ }
738+
645739func (r * Repository ) buildJsonBody (body map [string ]interface {}) string {
646740
647741 data , err := json .Marshal (body )
@@ -1022,6 +1116,83 @@ func decodeEnvironment(response interface{}) (*Environment, error) {
10221116 return environment , nil
10231117}
10241118
1119+ func decodeDeploymentVariables (response string ) (* DeploymentVariables , error ) {
1120+ var responseMap map [string ]interface {}
1121+ err := json .Unmarshal ([]byte (response ), & responseMap )
1122+ if err != nil {
1123+ return nil , err
1124+ }
1125+
1126+ values := responseMap ["values" ].([]interface {})
1127+ var variablesArray []DeploymentVariable
1128+ var errs error = nil
1129+ for idx , value := range values {
1130+ var variable DeploymentVariable
1131+ err = mapstructure .Decode (value , & variable )
1132+ if err != nil {
1133+ if errs == nil {
1134+ errs = err
1135+ } else {
1136+ errs = fmt .Errorf ("%w; deployment variable %d: %w" , errs , idx , err )
1137+ }
1138+ } else {
1139+ variablesArray = append (variablesArray , variable )
1140+ }
1141+ }
1142+
1143+ page , ok := responseMap ["page" ].(float64 )
1144+ if ! ok {
1145+ page = 0
1146+ }
1147+
1148+ pagelen , ok := responseMap ["pagelen" ].(float64 )
1149+ if ! ok {
1150+ pagelen = 0
1151+ }
1152+
1153+ max_depth , ok := responseMap ["max_depth" ].(float64 )
1154+ if ! ok {
1155+ max_depth = 0
1156+ }
1157+
1158+ size , ok := responseMap ["size" ].(float64 )
1159+ if ! ok {
1160+ size = 0
1161+ }
1162+
1163+ next , ok := responseMap ["next" ].(string )
1164+ if ! ok {
1165+ next = ""
1166+ }
1167+
1168+ deploymentVariables := DeploymentVariables {
1169+ Page : int (page ),
1170+ Pagelen : int (pagelen ),
1171+ MaxDepth : int (max_depth ),
1172+ Size : int (size ),
1173+ Next : next ,
1174+ Variables : variablesArray ,
1175+ }
1176+
1177+ return & deploymentVariables , nil
1178+ }
1179+
1180+ func decodeDeploymentVariable (response interface {}) (* DeploymentVariable , error ) {
1181+ responseMap := response .(map [string ]interface {})
1182+
1183+ if responseMap ["type" ] == "error" {
1184+ return nil , DecodeError (responseMap )
1185+ }
1186+
1187+ var variable = new (DeploymentVariable )
1188+ err := mapstructure .Decode (responseMap , & variable )
1189+ if err != nil {
1190+ return nil , err
1191+ }
1192+
1193+ return variable , nil
1194+ }
1195+
10251196func (rf RepositoryFile ) String () string {
10261197 return rf .Path
10271198}
0 commit comments