Skip to content

Commit 920c55a

Browse files
authored
add support for listing pipeline variables (#117)
Based on existing support for listing repository tags. Signed-off-by: Kent R. Spillner <[email protected]>
1 parent 248acff commit 920c55a

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

bitbucket.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type repository interface {
3434
ListForks(opt RepositoryOptions) (interface{}, error)
3535
ListDefaultReviewers(opt RepositoryOptions) (interface{}, error)
3636
UpdatePipelineConfig(opt RepositoryPipelineOptions) (*Pipeline, error)
37+
ListPipelineVariables(opt RepositoryPipelineVariablesOptions) (*PipelineVariables, error)
3738
AddPipelineVariable(opt RepositoryPipelineVariableOptions) (*PipelineVariable, error)
3839
AddPipelineKeyPair(opt RepositoryPipelineKeyPairOptions) (*PipelineKeyPair, error)
3940
UpdatePipelineBuildNumber(opt RepositoryPipelineBuildNumberOptions) (*PipelineBuildNumber, error)
@@ -252,6 +253,16 @@ type RepositoryPipelineOptions struct {
252253
Enabled bool `json:"has_pipelines"`
253254
}
254255

256+
type RepositoryPipelineVariablesOptions struct {
257+
Owner string `json:"owner"`
258+
RepoSlug string `json:"repo_slug"`
259+
Query string `json:"q"`
260+
Sort string `json:"sort"`
261+
PageNum int `json:"page"`
262+
Pagelen int `json:"pagelen"`
263+
MaxDepth int `json:"max_depth"`
264+
}
265+
255266
type RepositoryPipelineVariableOptions struct {
256267
Owner string `json:"owner"`
257268
RepoSlug string `json:"repo_slug"`

repository.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ type Pipeline struct {
9494
Repository Repository
9595
}
9696

97+
type PipelineVariables struct {
98+
Page int
99+
Pagelen int
100+
MaxDepth int
101+
Size int
102+
Next string
103+
Variables []PipelineVariable
104+
}
105+
97106
type PipelineVariable struct {
98107
Type string
99108
Uuid string
@@ -331,6 +340,42 @@ func (r *Repository) UpdatePipelineConfig(rpo *RepositoryPipelineOptions) (*Pipe
331340
return decodePipelineRepository(response)
332341
}
333342

343+
func (r *Repository) ListPipelineVariables(opt *RepositoryPipelineVariablesOptions) (*PipelineVariables, error) {
344+
345+
params := url.Values{}
346+
if opt.Query != "" {
347+
params.Add("q", opt.Query)
348+
}
349+
350+
if opt.Sort != "" {
351+
params.Add("sort", opt.Sort)
352+
}
353+
354+
if opt.PageNum > 0 {
355+
params.Add("page", strconv.Itoa(opt.PageNum))
356+
}
357+
358+
if opt.Pagelen > 0 {
359+
params.Add("pagelen", strconv.Itoa(opt.Pagelen))
360+
}
361+
362+
if opt.MaxDepth > 0 {
363+
params.Add("max_depth", strconv.Itoa(opt.MaxDepth))
364+
}
365+
366+
urlStr := r.c.requestUrl("/repositories/%s/%s/pipelines_config/variables/?%s", opt.Owner, opt.RepoSlug, params.Encode())
367+
response, err := r.c.executeRaw("GET", urlStr, "")
368+
if err != nil {
369+
return nil, err
370+
}
371+
bodyBytes, err := ioutil.ReadAll(response)
372+
if err != nil {
373+
return nil, err
374+
}
375+
bodyString := string(bodyBytes)
376+
return decodePipelineVariables(bodyString)
377+
}
378+
334379
func (r *Repository) AddPipelineVariable(rpvo *RepositoryPipelineVariableOptions) (*PipelineVariable, error) {
335380
data := r.buildPipelineVariableBody(rpvo)
336381
urlStr := r.c.requestUrl("/repositories/%s/%s/pipelines_config/variables/", rpvo.Owner, rpvo.RepoSlug)
@@ -696,6 +741,58 @@ func decodePipelineRepository(repoResponse interface{}) (*Pipeline, error) {
696741
return pipeline, nil
697742
}
698743

744+
func decodePipelineVariables(responseStr string) (*PipelineVariables, error) {
745+
746+
var responseMap map[string]interface{}
747+
err := json.Unmarshal([]byte(responseStr), &responseMap)
748+
if err != nil {
749+
return nil, err
750+
}
751+
752+
values := responseMap["values"].([]interface{})
753+
var variables []PipelineVariable
754+
for _, variable := range values {
755+
var pipelineVariable PipelineVariable
756+
err = mapstructure.Decode(variable, &pipelineVariable)
757+
if err == nil {
758+
variables = append(variables, pipelineVariable)
759+
}
760+
}
761+
762+
page, ok := responseMap["page"].(float64)
763+
if !ok {
764+
page = 0
765+
}
766+
767+
pagelen, ok := responseMap["pagelen"].(float64)
768+
if !ok {
769+
pagelen = 0
770+
}
771+
max_depth, ok := responseMap["max_depth"].(float64)
772+
if !ok {
773+
max_depth = 0
774+
}
775+
size, ok := responseMap["size"].(float64)
776+
if !ok {
777+
size = 0
778+
}
779+
780+
next, ok := responseMap["next"].(string)
781+
if !ok {
782+
next = ""
783+
}
784+
785+
pipelineVariables := PipelineVariables{
786+
Page: int(page),
787+
Pagelen: int(pagelen),
788+
MaxDepth: int(max_depth),
789+
Size: int(size),
790+
Next: next,
791+
Variables: variables,
792+
}
793+
return &pipelineVariables, nil
794+
}
795+
699796
func decodePipelineVariableRepository(repoResponse interface{}) (*PipelineVariable, error) {
700797
repoMap := repoResponse.(map[string]interface{})
701798

tests/repository_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,40 @@ func TestGetRepositoryRepositories(t *testing.T) {
4444
t.Error("Cannot catch repos full name.")
4545
}
4646
}
47+
48+
func TestGetRepositoryPipelineVariables(t *testing.T) {
49+
50+
user := os.Getenv("BITBUCKET_TEST_USERNAME")
51+
pass := os.Getenv("BITBUCKET_TEST_PASSWORD")
52+
owner := os.Getenv("BITBUCKET_TEST_OWNER")
53+
repo := os.Getenv("BITBUCKET_TEST_REPOSLUG")
54+
55+
if user == "" {
56+
t.Error("BITBUCKET_TEST_USERNAME is empty.")
57+
}
58+
if pass == "" {
59+
t.Error("BITBUCKET_TEST_PASSWORD is empty.")
60+
}
61+
if owner == "" {
62+
t.Error("BITBUCKET_TEST_OWNER is empty.")
63+
}
64+
if repo == "" {
65+
t.Error("BITBUCKET_TEST_REPOSLUG is empty.")
66+
}
67+
68+
c := bitbucket.NewBasicAuth(user, pass)
69+
70+
opt := &bitbucket.RepositoryPipelineVariablesOptions{
71+
Owner: owner,
72+
RepoSlug: repo,
73+
}
74+
75+
res, err := c.Repositories.Repository.ListPipelineVariables(opt)
76+
if err != nil {
77+
t.Error(err)
78+
}
79+
80+
if res == nil {
81+
t.Error("Cannot list pipeline variables")
82+
}
83+
}

0 commit comments

Comments
 (0)