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

Add support for deleting a branch on merge in BitBucket Server #1792

Merged
merged 6 commits into from
Sep 27, 2021
Merged
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
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 @@ -265,6 +270,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