pull/175/head
Brad Rydzewski 2 years ago
parent 6d77e85824
commit 6e8f06a69f

@ -35,6 +35,15 @@ func (s *gitService) FindBranch(ctx context.Context, repo, name string) (*scm.Re
}
func (s *gitService) FindCommit(ctx context.Context, repo, ref string) (*scm.Commit, *scm.Response, error) {
// github and gitlab permit fetching a commit by sha
// or branch. This code emulates the github and gitlab
// behavior for bitbucket by fetching the commit sha
// for the branch and using in the subsequent API call.
if scm.IsHash(ref) == false {
if branch, _, err := s.FindBranch(ctx, repo, scm.TrimRef(ref)); err == nil {
ref = branch.Sha // replace ref with sha
}
}
path := fmt.Sprintf("2.0/repositories/%s/commit/%s", repo, ref)
out := new(commit)
res, err := s.client.do(ctx, "GET", path, nil, out)
@ -217,9 +226,9 @@ func convertDiffstat(from *diffstat) *scm.Change {
Deleted: from.Status == "removed",
}
if (response.Renamed) {
if response.Renamed {
response.PrevFilePath = from.Old.Path
} else if (response.Deleted) {
} else if response.Deleted {
response.Path = from.Old.Path
}

@ -41,6 +41,37 @@ func TestGitFindCommit(t *testing.T) {
}
}
func TestGitFindCommitForBranch(t *testing.T) {
defer gock.Off()
gock.New("https://api.bitbucket.org").
Get("/2.0/repositories/atlassian/stash-example-plugin/commit/a6e5e7d797edf751cbd839d6bd4aef86c941eec9").
Reply(200).
Type("application/json").
File("testdata/commit.json")
gock.New("https://api.bitbucket.org").
Get("/2.0/repositories/atlassian/stash-example-plugin/refs/branches/master").
Reply(200).
Type("application/json").
File("testdata/branch.json")
client, _ := New("https://api.bitbucket.org")
got, _, err := client.Git.FindCommit(context.Background(), "atlassian/stash-example-plugin", "master")
if err != nil {
t.Error(err)
}
want := new(scm.Commit)
raw, _ := ioutil.ReadFile("testdata/commit.json.golden")
json.Unmarshal(raw, &want)
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
func TestGitCreateBranch(t *testing.T) {
defer gock.Off()

@ -14,6 +14,13 @@ import (
// from the git ref (e.g. refs/pulls/{d}/head)
var re = regexp.MustCompile("\\d+")
// regular expressions to test whether or not a string is
// a sha1 or sha256 commit hash.
var (
sha1 = regexp.MustCompile("^([a-f0-9]{40})$")
sha256 = regexp.MustCompile("^([a-f0-9]{64})$")
)
// Split splits the full repository name into segments.
func Split(s string) (owner, name string) {
parts := strings.SplitN(s, "/", 2)
@ -78,3 +85,7 @@ func IsPullRequest(ref string) bool {
strings.HasPrefix(ref, "refs/merge-requests/")
}
// IsHash returns true if the string is a commit hash.
func IsHash(s string) bool {
return sha1.MatchString(s) || sha256.MatchString(s)
}

@ -193,3 +193,49 @@ func TestExtractPullRequest(t *testing.T) {
}
}
}
func TestIsHash(t *testing.T) {
tests := []struct {
name string
tag bool
}{
{
name: "aacad6eca956c3a340ae5cd5856aa9c4a3755408",
tag: true,
},
{
name: "3da541559918a808c2402bba5012f6c60b27661c",
tag: true,
},
{
name: "f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b",
tag: true,
},
// not a sha
{
name: "aacad6e",
tag: false,
},
{
name: "master",
tag: false,
},
{
name: "refs/heads/master",
tag: false,
},
{
name: "issue/42",
tag: false,
},
{
name: "feature/foo",
tag: false,
},
}
for _, test := range tests {
if got, want := IsHash(test.name), test.tag; got != want {
t.Errorf("Got IsHash %v, want %v", got, want)
}
}
}

Loading…
Cancel
Save