@@ -369,3 +369,97 @@ func (sd *SDAPI) getEvents(eventID int) (*eventResponse, error) {
369369
370370 return eventResponse , err
371371}
372+
373+ type Secret struct {
374+ ID int `json:"id"`
375+ PipelineID int `json:"pipelineId"`
376+ Name string `json:"name"`
377+ AllowInPR bool `json:"allowInPR"`
378+ }
379+
380+ func (sd * SDAPI ) SetSecret (pipelineID int , key , value string , allowInPR bool ) error {
381+
382+ secrets , err := sd .getPipelineSecrets (pipelineID )
383+ if err != nil {
384+ return err
385+ }
386+
387+ duplicatedKeyID , exist := sd .checkKey (secrets , key )
388+
389+ if ! exist {
390+ return sd .createSecret (pipelineID , key , value , allowInPR )
391+ }
392+
393+ return sd .updateSecret (duplicatedKeyID , value , allowInPR )
394+ }
395+
396+ func (sd * SDAPI ) getPipelineSecrets (pipelineID int ) ([]Secret , error ) {
397+ path := fmt .Sprintf ("/v4/pipelines/%d/secrets?token=%s" , pipelineID , sd .sdctx .SDJWT )
398+ res , err := sd .request (context .TODO (), http .MethodGet , path , nil )
399+ if err != nil {
400+ return nil , err
401+ }
402+ defer res .Body .Close ()
403+ if res .StatusCode != http .StatusOK {
404+ return nil , fmt .Errorf ("GET %s status code is not %d: %d" , path , http .StatusOK , res .StatusCode )
405+ }
406+ var secrets []Secret
407+ if err := json .NewDecoder (res .Body ).Decode (& secrets ); err != nil {
408+ return nil , err
409+ }
410+
411+ return secrets , nil
412+ }
413+
414+ func (sd * SDAPI ) checkKey (secrets []Secret , key string ) (int , bool ) {
415+ for _ , s := range secrets {
416+ if s .Name == key {
417+ return s .ID , true
418+ }
419+ }
420+ return 0 , false
421+ }
422+
423+ func (sd * SDAPI ) createSecret (pipelineID int , key , value string , allowInPR bool ) error {
424+ path := "/v4/secrets"
425+ body := make (map [string ]interface {})
426+ body ["pipelineId" ] = pipelineID
427+ body ["name" ] = key
428+ body ["value" ] = value
429+ body ["allowInPR" ] = allowInPR
430+ bodyJSON , err := json .Marshal (& body )
431+ if err != nil {
432+ return err
433+ }
434+ res , err := sd .request (context .TODO (), http .MethodPost , path , bytes .NewBuffer (bodyJSON ))
435+ if err != nil {
436+ return err
437+ }
438+ defer res .Body .Close ()
439+
440+ if res .StatusCode != http .StatusCreated {
441+ return fmt .Errorf ("POST %s status code is not %d: %d" , path , http .StatusCreated , res .StatusCode )
442+ }
443+ return nil
444+ }
445+
446+ func (sd * SDAPI ) updateSecret (secretID int , value string , allowInPR bool ) error {
447+ path := fmt .Sprintf ("/v4/secrets/%d" , secretID )
448+ body := make (map [string ]interface {})
449+ body ["value" ] = value
450+ body ["allowInPR" ] = allowInPR
451+ bodyJSON , err := json .Marshal (& body )
452+ if err != nil {
453+ return err
454+ }
455+ res , err := sd .request (context .TODO (), http .MethodPut , path , bytes .NewBuffer (bodyJSON ))
456+ if err != nil {
457+ return err
458+ }
459+ defer res .Body .Close ()
460+
461+ if res .StatusCode != http .StatusOK {
462+ return fmt .Errorf ("PUT %s status code is not %d: %d" , path , http .StatusOK , res .StatusCode )
463+ }
464+ return nil
465+ }
0 commit comments