-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
636748e
commit 22cb85a
Showing
4 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2017 Drone.IO Inc. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package stash | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/drone/go-scm/scm" | ||
) | ||
|
||
type linker struct { | ||
base string | ||
} | ||
|
||
// Resource returns a link to the resource. | ||
func (l *linker) Resource(ctx context.Context, repo string, ref scm.Reference) (string, error) { | ||
namespace, name := scm.Split(repo) | ||
switch { | ||
case scm.IsTag(ref.Path): | ||
return fmt.Sprintf("%sprojects/%s/repos/%s/browse?at=%s", l.base, namespace, name, ref.Path), nil | ||
case scm.IsPullRequest(ref.Path): | ||
d := scm.ExtractPullRequest(ref.Path) | ||
return fmt.Sprintf("%sprojects/%s/repos/%s/pull-requests/%d/overview", l.base, namespace, name, d), nil | ||
default: | ||
return fmt.Sprintf("%sprojects/%s/repos/%s/commits/%s", l.base, namespace, name, ref.Sha), nil | ||
} | ||
} | ||
|
||
// Diff returns a link to the diff. | ||
func (l *linker) Diff(ctx context.Context, repo string, source, target scm.Reference) (string, error) { | ||
namespace, name := scm.Split(repo) | ||
if scm.IsPullRequest(target.Path) { | ||
d := scm.ExtractPullRequest(target.Path) | ||
return fmt.Sprintf("%sprojects/%s/repos/%s/pull-requests/%d/diff", l.base, namespace, name, d), nil | ||
} | ||
if target.Path != "" && source.Path != "" { | ||
return fmt.Sprintf("%sprojects/%s/repos/%s/compare/diff?sourceBranch=%s&targetBranch=%s", l.base, namespace, name, source.Path, target.Path), nil | ||
} | ||
// TODO(bradrydzewski) bitbucket server does not appear to have | ||
// an endpoint for evaluating diffs of two commits. | ||
return "", scm.ErrNotSupported | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2017 Drone.IO Inc. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package stash | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/drone/go-scm/scm" | ||
) | ||
|
||
func TestLink(t *testing.T) { | ||
tests := []struct { | ||
path string | ||
sha string | ||
want string | ||
}{ | ||
{ | ||
path: "refs/heads/master", | ||
sha: "a7389057b0eb027e73b32a81e3c5923a71d01dde", | ||
want: "https://stash.acme.com/projects/PRJ/repos/hello-world/commits/a7389057b0eb027e73b32a81e3c5923a71d01dde", | ||
}, | ||
{ | ||
path: "refs/pull/42/head", | ||
sha: "a7389057b0eb027e73b32a81e3c5923a71d01dde", | ||
want: "https://stash.acme.com/projects/PRJ/repos/hello-world/pull-requests/42/overview", | ||
}, | ||
{ | ||
path: "refs/tags/v1.0.0", | ||
want: "https://stash.acme.com/projects/PRJ/repos/hello-world/browse?at=refs/tags/v1.0.0", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
client, _ := New("https://stash.acme.com") | ||
ref := scm.Reference{ | ||
Path: test.path, | ||
Sha: test.sha, | ||
} | ||
got, err := client.Linker.Resource(context.Background(), "PRJ/hello-world", ref) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
want := test.want | ||
if got != want { | ||
t.Errorf("Want link %q, got %q", want, got) | ||
} | ||
} | ||
} | ||
|
||
func TestDiff(t *testing.T) { | ||
tests := []struct { | ||
source scm.Reference | ||
target scm.Reference | ||
want string | ||
}{ | ||
{ | ||
source: scm.Reference{Path: "refs/heads/master"}, | ||
target: scm.Reference{Path: "refs/heads/develop"}, | ||
want: "https://stash.acme.com/projects/PRJ/repos/hello-world/compare/diff?sourceBranch=refs/heads/master&targetBranch=refs/heads/develop", | ||
}, | ||
{ | ||
target: scm.Reference{Path: "refs/pull/12/head"}, | ||
want: "https://stash.acme.com/projects/PRJ/repos/hello-world/pull-requests/12/diff", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
client, _ := New("https://stash.acme.com") | ||
got, err := client.Linker.Diff(context.Background(), "PRJ/hello-world", test.source, test.target) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
want := test.want | ||
if got != want { | ||
t.Errorf("Want link %q, got %q", want, got) | ||
} | ||
} | ||
|
||
source := scm.Reference{} | ||
target := scm.Reference{} | ||
client, _ := New("https://stash.acme.com") | ||
_, err := client.Linker.Diff(context.Background(), "PRJ/hello-world", source, target) | ||
if err != scm.ErrNotSupported { | ||
t.Errorf("Expect ErrNotSupported when refpath is empty") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters