Skip to content

Commit

Permalink
added linker for atlasssian stash
Browse files Browse the repository at this point in the history
  • Loading branch information
bradrydzewski committed Sep 20, 2019
1 parent 636748e commit 22cb85a
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Support Before sha for Bitbucket, from [@jkdev81](https://github.com/jkdev81).
- Support for creating GitHub deployment hooks, from [@bradrydzewski](https://github.com/bradrydzewski).
- Endpoint to get organization membership for GitHub, from [@bradrydzewski](https://github.com/bradrydzewski).
- Functions to generate deep links to git resources, from [@bradrydzewski](https://github.com/bradrydzewski).

### Fixed
- Fix issue getting a GitLab commit by ref, from [@bradrydzewski](https://github.com/bradrydzewski).
Expand Down
45 changes: 45 additions & 0 deletions scm/driver/stash/linker.go
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
}
91 changes: 91 additions & 0 deletions scm/driver/stash/linker_test.go
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")
}
}
1 change: 1 addition & 0 deletions scm/driver/stash/stash.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func New(uri string) (*scm.Client, error) {
client.BaseURL = base
// initialize services
client.Driver = scm.DriverStash
client.Linker = &linker{base.String()}
client.Contents = &contentService{client}
client.Git = &gitService{client}
client.Issues = &issueService{client}
Expand Down

0 comments on commit 22cb85a

Please sign in to comment.