Skip to content

Commit 22cb85a

Browse files
committed
added linker for atlasssian stash
1 parent 636748e commit 22cb85a

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- Support Before sha for Bitbucket, from [@jkdev81](https://github.com/jkdev81).
1111
- Support for creating GitHub deployment hooks, from [@bradrydzewski](https://github.com/bradrydzewski).
1212
- Endpoint to get organization membership for GitHub, from [@bradrydzewski](https://github.com/bradrydzewski).
13+
- Functions to generate deep links to git resources, from [@bradrydzewski](https://github.com/bradrydzewski).
1314

1415
### Fixed
1516
- Fix issue getting a GitLab commit by ref, from [@bradrydzewski](https://github.com/bradrydzewski).

scm/driver/stash/linker.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2017 Drone.IO Inc. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package stash
6+
7+
import (
8+
"context"
9+
"fmt"
10+
11+
"github.com/drone/go-scm/scm"
12+
)
13+
14+
type linker struct {
15+
base string
16+
}
17+
18+
// Resource returns a link to the resource.
19+
func (l *linker) Resource(ctx context.Context, repo string, ref scm.Reference) (string, error) {
20+
namespace, name := scm.Split(repo)
21+
switch {
22+
case scm.IsTag(ref.Path):
23+
return fmt.Sprintf("%sprojects/%s/repos/%s/browse?at=%s", l.base, namespace, name, ref.Path), nil
24+
case scm.IsPullRequest(ref.Path):
25+
d := scm.ExtractPullRequest(ref.Path)
26+
return fmt.Sprintf("%sprojects/%s/repos/%s/pull-requests/%d/overview", l.base, namespace, name, d), nil
27+
default:
28+
return fmt.Sprintf("%sprojects/%s/repos/%s/commits/%s", l.base, namespace, name, ref.Sha), nil
29+
}
30+
}
31+
32+
// Diff returns a link to the diff.
33+
func (l *linker) Diff(ctx context.Context, repo string, source, target scm.Reference) (string, error) {
34+
namespace, name := scm.Split(repo)
35+
if scm.IsPullRequest(target.Path) {
36+
d := scm.ExtractPullRequest(target.Path)
37+
return fmt.Sprintf("%sprojects/%s/repos/%s/pull-requests/%d/diff", l.base, namespace, name, d), nil
38+
}
39+
if target.Path != "" && source.Path != "" {
40+
return fmt.Sprintf("%sprojects/%s/repos/%s/compare/diff?sourceBranch=%s&targetBranch=%s", l.base, namespace, name, source.Path, target.Path), nil
41+
}
42+
// TODO(bradrydzewski) bitbucket server does not appear to have
43+
// an endpoint for evaluating diffs of two commits.
44+
return "", scm.ErrNotSupported
45+
}

scm/driver/stash/linker_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright 2017 Drone.IO Inc. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package stash
6+
7+
import (
8+
"context"
9+
"testing"
10+
11+
"github.com/drone/go-scm/scm"
12+
)
13+
14+
func TestLink(t *testing.T) {
15+
tests := []struct {
16+
path string
17+
sha string
18+
want string
19+
}{
20+
{
21+
path: "refs/heads/master",
22+
sha: "a7389057b0eb027e73b32a81e3c5923a71d01dde",
23+
want: "https://stash.acme.com/projects/PRJ/repos/hello-world/commits/a7389057b0eb027e73b32a81e3c5923a71d01dde",
24+
},
25+
{
26+
path: "refs/pull/42/head",
27+
sha: "a7389057b0eb027e73b32a81e3c5923a71d01dde",
28+
want: "https://stash.acme.com/projects/PRJ/repos/hello-world/pull-requests/42/overview",
29+
},
30+
{
31+
path: "refs/tags/v1.0.0",
32+
want: "https://stash.acme.com/projects/PRJ/repos/hello-world/browse?at=refs/tags/v1.0.0",
33+
},
34+
}
35+
36+
for _, test := range tests {
37+
client, _ := New("https://stash.acme.com")
38+
ref := scm.Reference{
39+
Path: test.path,
40+
Sha: test.sha,
41+
}
42+
got, err := client.Linker.Resource(context.Background(), "PRJ/hello-world", ref)
43+
if err != nil {
44+
t.Error(err)
45+
return
46+
}
47+
want := test.want
48+
if got != want {
49+
t.Errorf("Want link %q, got %q", want, got)
50+
}
51+
}
52+
}
53+
54+
func TestDiff(t *testing.T) {
55+
tests := []struct {
56+
source scm.Reference
57+
target scm.Reference
58+
want string
59+
}{
60+
{
61+
source: scm.Reference{Path: "refs/heads/master"},
62+
target: scm.Reference{Path: "refs/heads/develop"},
63+
want: "https://stash.acme.com/projects/PRJ/repos/hello-world/compare/diff?sourceBranch=refs/heads/master&targetBranch=refs/heads/develop",
64+
},
65+
{
66+
target: scm.Reference{Path: "refs/pull/12/head"},
67+
want: "https://stash.acme.com/projects/PRJ/repos/hello-world/pull-requests/12/diff",
68+
},
69+
}
70+
71+
for _, test := range tests {
72+
client, _ := New("https://stash.acme.com")
73+
got, err := client.Linker.Diff(context.Background(), "PRJ/hello-world", test.source, test.target)
74+
if err != nil {
75+
t.Error(err)
76+
return
77+
}
78+
want := test.want
79+
if got != want {
80+
t.Errorf("Want link %q, got %q", want, got)
81+
}
82+
}
83+
84+
source := scm.Reference{}
85+
target := scm.Reference{}
86+
client, _ := New("https://stash.acme.com")
87+
_, err := client.Linker.Diff(context.Background(), "PRJ/hello-world", source, target)
88+
if err != scm.ErrNotSupported {
89+
t.Errorf("Expect ErrNotSupported when refpath is empty")
90+
}
91+
}

scm/driver/stash/stash.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func New(uri string) (*scm.Client, error) {
3333
client.BaseURL = base
3434
// initialize services
3535
client.Driver = scm.DriverStash
36+
client.Linker = &linker{base.String()}
3637
client.Contents = &contentService{client}
3738
client.Git = &gitService{client}
3839
client.Issues = &issueService{client}

0 commit comments

Comments
 (0)