Skip to content

Commit

Permalink
Add support for deleting a branch on merge in BitBucket Server (runat…
Browse files Browse the repository at this point in the history
…lantis#1792)

* Add support for deleting a branch on merge in BitBucket Server

* Add support for deleting a branch on merge in BitBucket Server

* Making linter happy although the code would fall through and return err anyway.

* Add client_test for deleting source branch

* Added err check

Co-authored-by: Wendell Beckwith <[email protected]>
  • Loading branch information
2 people authored and krrrr38 committed Dec 16, 2022
1 parent c64f333 commit 6008602
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
20 changes: 20 additions & 0 deletions server/events/vcs/bitbucketserver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ type Client struct {
AtlantisURL string
}

type DeleteSourceBranch struct {
Name string `json:"name"`
DryRun bool `json:"dryRun"`
}

// NewClient builds a bitbucket cloud client. Returns an error if the baseURL is
// malformed. httpClient is the client to use to make the requests, username
// and password are used as basic auth in the requests, baseURL is the API's
Expand Down Expand Up @@ -267,6 +272,21 @@ func (b *Client) MergePull(pull models.PullRequest, pullOptions models.PullReque
}
path = fmt.Sprintf("%s/rest/api/1.0/projects/%s/repos/%s/pull-requests/%d/merge?version=%d", b.BaseURL, projectKey, pull.BaseRepo.Name, pull.Num, *pullResp.Version)
_, err = b.makeRequest("POST", path, nil)
if err != nil {
return err
}
if pullOptions.DeleteSourceBranchOnMerge {
bodyBytes, err := json.Marshal(DeleteSourceBranch{Name: "refs/heads/" + pull.HeadBranch, DryRun: false})
if err != nil {
return errors.Wrap(err, "json encoding")
}

path = fmt.Sprintf("%s/rest/branch-utils/1.0/projects/%s/repos/%s/branches", b.BaseURL, projectKey, pull.BaseRepo.Name)
_, err = b.makeRequest("DELETE", path, bytes.NewBuffer(bodyBytes))
if err != nil {
return err
}
}
return err
}

Expand Down
60 changes: 60 additions & 0 deletions server/events/vcs/bitbucketserver/client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bitbucketserver_test

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -183,6 +184,65 @@ func TestClient_MergePull(t *testing.T) {
Ok(t, err)
}

// Test that we delete the source branch in our call to merge the pull
// request.
func TestClient_MergePullDeleteSourceBranch(t *testing.T) {
pullRequest, err := ioutil.ReadFile(filepath.Join("testdata", "pull-request.json"))
Ok(t, err)
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.RequestURI {
// The first request should hit this URL.
case "/rest/api/1.0/projects/ow/repos/repo/pull-requests/1":
w.Write(pullRequest) // nolint: errcheck
return
case "/rest/api/1.0/projects/ow/repos/repo/pull-requests/1/merge?version=3":
Equals(t, "POST", r.Method)
w.Write(pullRequest) // nolint: errcheck
case "/rest/branch-utils/1.0/projects/ow/repos/repo/branches":
Equals(t, "DELETE", r.Method)
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
Ok(t, err)
var payload bitbucketserver.DeleteSourceBranch
err = json.Unmarshal(b, &payload)
Ok(t, err)
Equals(t, "refs/heads/foo", payload.Name)
w.WriteHeader(http.StatusNoContent) // nolint: errcheck
default:
t.Errorf("got unexpected request at %q", r.RequestURI)
http.Error(w, "not found", http.StatusNotFound)
return
}
}))
defer testServer.Close()

client, err := bitbucketserver.NewClient(http.DefaultClient, "user", "pass", testServer.URL, "runatlantis.io")
Ok(t, err)

err = client.MergePull(models.PullRequest{
Num: 1,
HeadCommit: "",
URL: "",
HeadBranch: "foo",
BaseBranch: "",
Author: "",
State: 0,
BaseRepo: models.Repo{
FullName: "owner/repo",
Owner: "owner",
Name: "repo",
SanitizedCloneURL: fmt.Sprintf("%s/scm/ow/repo.git", testServer.URL),
VCSHost: models.VCSHost{
Type: models.BitbucketServer,
Hostname: "bitbucket.org",
},
},
}, models.PullRequestOptions{
DeleteSourceBranchOnMerge: true,
})
Ok(t, err)
}

func TestClient_MarkdownPullLink(t *testing.T) {
client, err := bitbucketserver.NewClient(nil, "u", "p", "https://base-url", "atlantis-url")
Ok(t, err)
Expand Down

0 comments on commit 6008602

Please sign in to comment.