Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ddansby/issue 154 add delete branch and tag methods #156

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 16 additions & 27 deletions bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,34 +214,26 @@ type RepositoryRefOptions struct {
BranchFlg bool
}

type RepositoryBranchOptions struct {
Owner string `json:"owner"`
RepoSlug string `json:"repo_slug"`
Query string `json:"query"`
Sort string `json:"sort"`
PageNum int `json:"page"`
Pagelen int `json:"pagelen"`
MaxDepth int `json:"max_depth"`
BranchName string `json:"branch_name"`
}

type RepositoryBranchCreationOptions struct {
Owner string `json:"owner"`
RepoSlug string `json:"repo_slug"`
Name string `json:"name"`
Target RepositoryBranchTarget `json:"target"`
type RepositoryRefTarget struct {
Hash string `json:"hash"`
}

type RepositoryBranchDeleteOptions struct {
type RepositoryBranchOptions struct {
Owner string `json:"owner"`
RepoSlug string `json:"repo_slug"`
RepoUUID string `json:"uuid"`
RefName string `json:"name"`
RefUUID string `json:uuid`
Query string `json:"query"`
Sort string `json:"sort"`
PageNum int `json:"page"`
Pagelen int `json:"pagelen"`
MaxDepth int `json:"max_depth"`
Name string `json:"name"`
}

type RepositoryBranchTarget struct {
Hash string `json:"hash"`
type RepositoryBranchCreationOptions struct {
Owner string `json:"owner"`
RepoSlug string `json:"repo_slug"`
Name string `json:"name"`
Target RepositoryRefTarget `json:"target"`
}

type RepositoryTagOptions struct {
Expand All @@ -252,17 +244,14 @@ type RepositoryTagOptions struct {
PageNum int `json:"page"`
Pagelen int `json:"pagelen"`
MaxDepth int `json:"max_depth"`
Name string `json:"name"`
}

type RepositoryTagCreationOptions struct {
Owner string `json:"owner"`
RepoSlug string `json:"repo_slug"`
Name string `json:"name"`
Target RepositoryTagTarget `json:"target"`
}

type RepositoryTagTarget struct {
Hash string `json:"hash"`
Target RepositoryRefTarget `json:"target"`
}

type PullRequestsOptions struct {
Expand Down
61 changes: 29 additions & 32 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,10 @@ func (r *Repository) ListBranches(rbo *RepositoryBranchOptions) (*RepositoryBran
}

func (r *Repository) GetBranch(rbo *RepositoryBranchOptions) (*RepositoryBranch, error) {
if rbo.BranchName == "" {
if rbo.Name == "" {
return nil, errors.New("Error: Branch Name is empty")
}
urlStr := r.c.requestUrl("/repositories/%s/%s/refs/branches/%s", rbo.Owner, rbo.RepoSlug, rbo.BranchName)
urlStr := r.c.requestUrl("/repositories/%s/%s/refs/branches/%s", rbo.Owner, rbo.RepoSlug, rbo.Name)
response, err := r.c.executeRaw("GET", urlStr, "")
if err != nil {
return nil, err
Expand All @@ -382,21 +382,6 @@ func (r *Repository) GetBranch(rbo *RepositoryBranchOptions) (*RepositoryBranch,
return decodeRepositoryBranch(bodyString)
}

// DeleteBranch https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/refs/branches/%7Bname%7D#delete
func (r *Repository) DeleteBranch(rbo *RepositoryBranchDeleteOptions) error {
repo := rbo.RepoSlug
if rbo.RepoUUID != "" {
repo = rbo.RepoUUID
}
ref := rbo.RefName
if rbo.RefUUID != "" {
ref = rbo.RefUUID
}
urlStr := r.c.requestUrl("/repositories/%s/%s/refs/branches/%s", rbo.Owner, repo, ref)
_, err := r.c.execute("DELETE", urlStr, "")
return err
}

func (r *Repository) CreateBranch(rbo *RepositoryBranchCreationOptions) (*RepositoryBranch, error) {
urlStr := r.c.requestUrl("/repositories/%s/%s/refs/branches", rbo.Owner, rbo.RepoSlug)
data, err := r.buildBranchBody(rbo)
Expand All @@ -418,30 +403,36 @@ func (r *Repository) CreateBranch(rbo *RepositoryBranchCreationOptions) (*Reposi
return decodeRepositoryBranchCreated(bodyString)
}

func (r *Repository) ListTags(rbo *RepositoryTagOptions) (*RepositoryTags, error) {
func (r *Repository) DeleteBranch(rbo *RepositoryBranchOptions) (interface{}, error) {
urlStr := r.c.requestUrl("/repositories/%s/%s/refs/branches/%s", rbo.Owner, rbo.RepoSlug,
rbo.Name)
return r.c.execute("DELETE", urlStr, "")
}

func (r *Repository) ListTags(rto *RepositoryTagOptions) (*RepositoryTags, error) {

params := url.Values{}
if rbo.Query != "" {
params.Add("q", rbo.Query)
if rto.Query != "" {
params.Add("q", rto.Query)
}

if rbo.Sort != "" {
params.Add("sort", rbo.Sort)
if rto.Sort != "" {
params.Add("sort", rto.Sort)
}

if rbo.PageNum > 0 {
params.Add("page", strconv.Itoa(rbo.PageNum))
if rto.PageNum > 0 {
params.Add("page", strconv.Itoa(rto.PageNum))
}

if rbo.Pagelen > 0 {
params.Add("pagelen", strconv.Itoa(rbo.Pagelen))
if rto.Pagelen > 0 {
params.Add("pagelen", strconv.Itoa(rto.Pagelen))
}

if rbo.MaxDepth > 0 {
params.Add("max_depth", strconv.Itoa(rbo.MaxDepth))
if rto.MaxDepth > 0 {
params.Add("max_depth", strconv.Itoa(rto.MaxDepth))
}

urlStr := r.c.requestUrl("/repositories/%s/%s/refs/tags?%s", rbo.Owner, rbo.RepoSlug, params.Encode())
urlStr := r.c.requestUrl("/repositories/%s/%s/refs/tags?%s", rto.Owner, rto.RepoSlug, params.Encode())
response, err := r.c.executeRaw("GET", urlStr, "")
if err != nil {
return nil, err
Expand All @@ -454,9 +445,9 @@ func (r *Repository) ListTags(rbo *RepositoryTagOptions) (*RepositoryTags, error
return decodeRepositoryTags(bodyString)
}

func (r *Repository) CreateTag(rbo *RepositoryTagCreationOptions) (*RepositoryTag, error) {
urlStr := r.c.requestUrl("/repositories/%s/%s/refs/tags", rbo.Owner, rbo.RepoSlug)
data, err := r.buildTagBody(rbo)
func (r *Repository) CreateTag(rto *RepositoryTagCreationOptions) (*RepositoryTag, error) {
urlStr := r.c.requestUrl("/repositories/%s/%s/refs/tags", rto.Owner, rto.RepoSlug)
data, err := r.buildTagBody(rto)
if err != nil {
return nil, err
}
Expand All @@ -475,6 +466,12 @@ func (r *Repository) CreateTag(rbo *RepositoryTagCreationOptions) (*RepositoryTa
return decodeRepositoryTagCreated(bodyString)
}

func (r *Repository) DeleteTag(rto *RepositoryTagOptions) (interface{}, error) {
urlStr := r.c.requestUrl("/repositories/%s/%s/refs/tags/%s", rto.Owner, rto.RepoSlug,
rto.Name)
return r.c.execute("DELETE", urlStr, "")
}

func (r *Repository) Update(ro *RepositoryOptions) (*Repository, error) {
data, err := r.buildRepositoryBody(ro)
if err != nil {
Expand Down
110 changes: 100 additions & 10 deletions tests/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,10 @@ func TestDeleteRepositoryPipelineVariables(t *testing.T) {
}
}

// This test tests the CreateBranch, CreateTag, ListRefs, DeleteBranch, and DeleteTag repo methods
func TestGetRepositoryRefs(t *testing.T) {

// Create Branch, List Refs and search for Branch that was created,
// then test successful Branch deletion
user := os.Getenv("BITBUCKET_TEST_USERNAME")
pass := os.Getenv("BITBUCKET_TEST_PASSWORD")
owner := os.Getenv("BITBUCKET_TEST_OWNER")
Expand All @@ -300,47 +302,135 @@ func TestGetRepositoryRefs(t *testing.T) {
Owner: owner,
RepoSlug: repo,
Name: "TestGetRepoRefsBranch",
Target: bitbucket.RepositoryBranchTarget{Hash: "master"},
Target: bitbucket.RepositoryRefTarget{Hash: "master"},
}

_, err := c.Repositories.Repository.CreateBranch(opt)
if err != nil {
t.Error("Could not create new branch", err)
}

refOpts := &bitbucket.RepositoryRefOptions{
refBranchOpts := &bitbucket.RepositoryRefOptions{
Owner: owner,
RepoSlug: repo,
}

resRefs, err := c.Repositories.Repository.ListRefs(refOpts)
resBranchRefs, err := c.Repositories.Repository.ListRefs(refBranchOpts)
if err != nil {
t.Error("The refs is not found.")
t.Error("The refs/branch is not found.")
}

expected := struct {
branchExpected := struct {
n string
t string
}{}

for _, ref := range resRefs.Refs {
for _, ref := range resBranchRefs.Refs {
for k, v := range ref {
// kCopy := k
vCopy := v
if val, ok := vCopy.(string); ok {
if k == "name" && val == "TestGetRepoRefsBranch" {
expected.n = val
branchExpected.n = val
}
}
if val, ok := vCopy.(string); ok {
if k == "type" && val == "branch" {
expected.t = val
branchExpected.t = val
}
}
}
}

if !(expected.n == "TestGetRepoRefsBranch" && expected.t == "branch") {
if !(branchExpected.n == "TestGetRepoRefsBranch" && branchExpected.t == "branch") {
t.Error("Could not list refs/branch that was created in test setup")
}

delBranchOpt := &bitbucket.RepositoryBranchOptions{
Owner: owner,
RepoSlug: repo,
Name: "TestGetRepoRefsBranch",
}
resBranchDel, err := c.Repositories.Repository.DeleteBranch(delBranchOpt)
if err != nil {
t.Error("Could not delete branch")
}

if mp, ok := resBranchDel.(map[string]interface{}); ok {
for k, v := range mp {
if k == "type" && v == "error" {
t.Error("Delete branch returned an error, when it should have successfully" +
"delete the test branch created during test setup")
}
}
}

// Create Tag, List Refs and search for Tag that was created,
// then test successful Tag deletion
tagOpt := &bitbucket.RepositoryTagCreationOptions{
Owner: owner,
RepoSlug: repo,
Name: "TestGetRepoRefsTag",
Target: bitbucket.RepositoryRefTarget{Hash: "master"},
}

_, err = c.Repositories.Repository.CreateTag(tagOpt)
if err != nil {
t.Error("Could not create new tag", err)
}

refTagOpts := &bitbucket.RepositoryRefOptions{
Owner: owner,
RepoSlug: repo,
}

resTagRefs, err := c.Repositories.Repository.ListRefs(refTagOpts)
if err != nil {
t.Error("The ref/tag is not found.")
}

tagExpected := struct {
n string
t string
}{}

for _, ref := range resTagRefs.Refs {
for k, v := range ref {
// kCopy := k
vCopy := v
if val, ok := vCopy.(string); ok {
if k == "name" && val == "TestGetRepoRefsTag" {
tagExpected.n = val
}
}
if val, ok := vCopy.(string); ok {
if k == "type" && val == "tag" {
tagExpected.t = val
}
}
}
}

if !(tagExpected.n == "TestGetRepoRefsTag" && tagExpected.t == "tag") {
t.Error("Could not list refs/tag that was created in test setup")
}

delTagOpt := &bitbucket.RepositoryTagOptions{
Owner: owner,
RepoSlug: repo,
Name: "TestGetRepoRefsTag",
}
resTagDel, err := c.Repositories.Repository.DeleteTag(delTagOpt)
if err != nil {
t.Error("Could not delete tag")
}

if mp, ok := resTagDel.(map[string]interface{}); ok {
for k, v := range mp {
if k == "type" && v == "error" {
t.Error("Delete tag returned an error, when it should have successfully" +
"delete the test tag created during test setup")
}
}
}
}