Skip to content

Commit

Permalink
Allow updating the Pipelines build number (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
panychek authored and ktrysmt committed Dec 27, 2019
1 parent 08ab101 commit 06d419f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
7 changes: 7 additions & 0 deletions bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type repository interface {
UpdatePipelineConfig(opt RepositoryPipelineOptions) (*Pipeline, error)
AddPipelineVariable(opt RepositoryPipelineVariableOptions) (*PipelineVariable, error)
AddPipelineKeyPair(opt RepositoryPipelineKeyPairOptions) (*PipelineKeyPair, error)
UpdatePipelineBuildNumber(opt RepositoryPipelineBuildNumberOptions) (*PipelineBuildNumber, error)
ListFiles(opt RepositoryFilesOptions) (*[]RepositoryFile, error)
GetFileBlob(opt RepositoryBlobOptions) (*RepositoryBlob, error)
ListBranches(opt RepositoryBranchOptions) (*RepositoryBranches, error)
Expand Down Expand Up @@ -229,6 +230,12 @@ type RepositoryPipelineKeyPairOptions struct {
PublicKey string `json:"public_key"`
}

type RepositoryPipelineBuildNumberOptions struct {
Owner string `json:"owner"`
RepoSlug string `json:"repo_slug"`
Next int `json:"next"`
}

type DownloadsOptions struct {
Owner string `json:"owner"`
RepoSlug string `json:"repo_slug"`
Expand Down
42 changes: 42 additions & 0 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ type PipelineKeyPair struct {
PrivateKey string
}

type PipelineBuildNumber struct {
Type string
Next int
}

func (r *Repository) Create(ro *RepositoryOptions) (*Repository, error) {
data := r.buildRepositoryBody(ro)
urlStr := r.c.requestUrl("/repositories/%s/%s", ro.Owner, ro.RepoSlug)
Expand Down Expand Up @@ -259,6 +264,18 @@ func (r *Repository) AddPipelineKeyPair(rpkpo *RepositoryPipelineKeyPairOptions)
return decodePipelineKeyPairRepository(response)
}

func (r *Repository) UpdatePipelineBuildNumber(rpbno *RepositoryPipelineBuildNumberOptions) (*PipelineBuildNumber, error) {
data := r.buildPipelineBuildNumberBody(rpbno)
urlStr := r.c.requestUrl("/repositories/%s/%s/pipelines_config/build_number", rpbno.Owner, rpbno.RepoSlug)

response, err := r.c.execute("PUT", urlStr, data)
if err != nil {
return nil, err
}

return decodePipelineBuildNumberRepository(response)
}

func (r *Repository) buildJsonBody(body map[string]interface{}) string {

data, err := json.Marshal(body)
Expand Down Expand Up @@ -344,6 +361,15 @@ func (r *Repository) buildPipelineKeyPairBody(rpkpo *RepositoryPipelineKeyPairOp
return r.buildJsonBody(body)
}

func (r *Repository) buildPipelineBuildNumberBody(rpbno *RepositoryPipelineBuildNumberOptions) string {

body := map[string]interface{}{}

body["next"] = rpbno.Next

return r.buildJsonBody(body)
}

func decodeRepository(repoResponse interface{}) (*Repository, error) {
repoMap := repoResponse.(map[string]interface{})

Expand Down Expand Up @@ -528,6 +554,22 @@ func decodePipelineKeyPairRepository(repoResponse interface{}) (*PipelineKeyPair
return pipelineKeyPair, nil
}

func decodePipelineBuildNumberRepository(repoResponse interface{}) (*PipelineBuildNumber, error) {
repoMap := repoResponse.(map[string]interface{})

if repoMap["type"] == "error" {
return nil, DecodeError(repoMap)
}

var pipelineBuildNumber = new(PipelineBuildNumber)
err := mapstructure.Decode(repoMap, pipelineBuildNumber)
if err != nil {
return nil, err
}

return pipelineBuildNumber, nil
}

func (rf RepositoryFile) String() string {
return rf.Path
}
Expand Down

0 comments on commit 06d419f

Please sign in to comment.