Skip to content

Commit a2552b9

Browse files
committed
added gitea and gogs linker
1 parent 12852c5 commit a2552b9

File tree

6 files changed

+320
-0
lines changed

6 files changed

+320
-0
lines changed

scm/driver/gitea/gitea.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func New(uri string) (*scm.Client, error) {
3131
client.BaseURL = base
3232
// initialize services
3333
client.Driver = scm.DriverGitea
34+
client.Linker = &linker{base.String()}
3435
client.Contents = &contentService{client}
3536
client.Git = &gitService{client}
3637
client.Issues = &issueService{client}

scm/driver/gitea/linker.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 gitea
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+
switch {
21+
case scm.IsTag(ref.Path):
22+
t := scm.TrimRef(ref.Path)
23+
return fmt.Sprintf("%s%s/tag/%s", l.base, repo, t), nil
24+
case scm.IsPullRequest(ref.Path):
25+
d := scm.ExtractPullRequest(ref.Path)
26+
return fmt.Sprintf("%s%s/pulls/%d", l.base, repo, d), nil
27+
default:
28+
return fmt.Sprintf("%s%s/commit/%s", l.base, repo, 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+
if scm.IsPullRequest(target.Path) {
35+
d := scm.ExtractPullRequest(target.Path)
36+
return fmt.Sprintf("%s%s/pulls/%d/files", l.base, repo, d), nil
37+
}
38+
39+
s := source.Sha
40+
t := target.Sha
41+
if s == "" {
42+
s = scm.TrimRef(source.Path)
43+
}
44+
if t == "" {
45+
t = scm.TrimRef(target.Path)
46+
}
47+
48+
return fmt.Sprintf("%s%s/compare/%s...%s", l.base, repo, s, t), nil
49+
}

scm/driver/gitea/linker_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 gitea
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://try.gitea.io/octocat/hello-world/commit/a7389057b0eb027e73b32a81e3c5923a71d01dde",
24+
},
25+
{
26+
path: "refs/pull/42/head",
27+
sha: "a7389057b0eb027e73b32a81e3c5923a71d01dde",
28+
want: "https://try.gitea.io/octocat/hello-world/pulls/42",
29+
},
30+
{
31+
path: "refs/tags/v1.0.0",
32+
want: "https://try.gitea.io/octocat/hello-world/tag/v1.0.0",
33+
},
34+
}
35+
36+
for _, test := range tests {
37+
client, _ := New("https://try.gitea.io")
38+
ref := scm.Reference{
39+
Path: test.path,
40+
Sha: test.sha,
41+
}
42+
got, err := client.Linker.Resource(context.Background(), "octocat/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{Sha: "a7389057b0eb027e73b32a81e3c5923a71d01dde"},
62+
target: scm.Reference{Sha: "49bbaf4a113bbebfa21cf604cad9aa1503c3f04d"},
63+
want: "https://try.gitea.io/octocat/hello-world/compare/a7389057b0eb027e73b32a81e3c5923a71d01dde...49bbaf4a113bbebfa21cf604cad9aa1503c3f04d",
64+
},
65+
{
66+
source: scm.Reference{Path: "refs/heads/master"},
67+
target: scm.Reference{Sha: "49bbaf4a113bbebfa21cf604cad9aa1503c3f04d"},
68+
want: "https://try.gitea.io/octocat/hello-world/compare/master...49bbaf4a113bbebfa21cf604cad9aa1503c3f04d",
69+
},
70+
{
71+
source: scm.Reference{Sha: "a7389057b0eb027e73b32a81e3c5923a71d01dde"},
72+
target: scm.Reference{Path: "refs/heads/master"},
73+
want: "https://try.gitea.io/octocat/hello-world/compare/a7389057b0eb027e73b32a81e3c5923a71d01dde...master",
74+
},
75+
{
76+
target: scm.Reference{Path: "refs/pull/12/head"},
77+
want: "https://try.gitea.io/octocat/hello-world/pulls/12/files",
78+
},
79+
}
80+
81+
for _, test := range tests {
82+
client, _ := New("https://try.gitea.io")
83+
got, err := client.Linker.Diff(context.Background(), "octocat/hello-world", test.source, test.target)
84+
if err != nil {
85+
t.Error(err)
86+
return
87+
}
88+
want := test.want
89+
if got != want {
90+
t.Errorf("Want link %q, got %q", want, got)
91+
}
92+
}
93+
}
94+
95+
func TestLink_Base(t *testing.T) {
96+
client, _ := New("https://foo.bar.com/baz")
97+
ref := scm.Reference{
98+
Path: "refs/heads/master",
99+
Sha: "a7389057b0eb027e73b32a81e3c5923a71d01dde",
100+
}
101+
got, err := client.Linker.Resource(context.Background(), "octocat/hello-world", ref)
102+
if err != nil {
103+
t.Error(err)
104+
return
105+
}
106+
want := "https://foo.bar.com/baz/octocat/hello-world/commit/a7389057b0eb027e73b32a81e3c5923a71d01dde"
107+
if got != want {
108+
t.Errorf("Want link %q, got %q", want, got)
109+
}
110+
}

scm/driver/gogs/gogs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func New(uri string) (*scm.Client, error) {
3131
client.BaseURL = base
3232
// initialize services
3333
client.Driver = scm.DriverGogs
34+
client.Linker = &linker{base.String()}
3435
client.Contents = &contentService{client}
3536
client.Git = &gitService{client}
3637
client.Issues = &issueService{client}

scm/driver/gogs/linker.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 gogs
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+
switch {
21+
case scm.IsTag(ref.Path):
22+
t := scm.TrimRef(ref.Path)
23+
return fmt.Sprintf("%s%s/tag/%s", l.base, repo, t), nil
24+
case scm.IsPullRequest(ref.Path):
25+
d := scm.ExtractPullRequest(ref.Path)
26+
return fmt.Sprintf("%s%s/pulls/%d", l.base, repo, d), nil
27+
default:
28+
return fmt.Sprintf("%s%s/commit/%s", l.base, repo, 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+
if scm.IsPullRequest(target.Path) {
35+
d := scm.ExtractPullRequest(target.Path)
36+
return fmt.Sprintf("%s%s/pulls/%d/files", l.base, repo, d), nil
37+
}
38+
39+
s := source.Sha
40+
t := target.Sha
41+
if s == "" {
42+
s = scm.TrimRef(source.Path)
43+
}
44+
if t == "" {
45+
t = scm.TrimRef(target.Path)
46+
}
47+
48+
return fmt.Sprintf("%s%s/compare/%s...%s", l.base, repo, s, t), nil
49+
}

scm/driver/gogs/linker_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 gogs
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://try.gogs.io/octocat/hello-world/commit/a7389057b0eb027e73b32a81e3c5923a71d01dde",
24+
},
25+
{
26+
path: "refs/pull/42/head",
27+
sha: "a7389057b0eb027e73b32a81e3c5923a71d01dde",
28+
want: "https://try.gogs.io/octocat/hello-world/pulls/42",
29+
},
30+
{
31+
path: "refs/tags/v1.0.0",
32+
want: "https://try.gogs.io/octocat/hello-world/tag/v1.0.0",
33+
},
34+
}
35+
36+
for _, test := range tests {
37+
client, _ := New("https://try.gogs.io")
38+
ref := scm.Reference{
39+
Path: test.path,
40+
Sha: test.sha,
41+
}
42+
got, err := client.Linker.Resource(context.Background(), "octocat/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{Sha: "a7389057b0eb027e73b32a81e3c5923a71d01dde"},
62+
target: scm.Reference{Sha: "49bbaf4a113bbebfa21cf604cad9aa1503c3f04d"},
63+
want: "https://try.gogs.io/octocat/hello-world/compare/a7389057b0eb027e73b32a81e3c5923a71d01dde...49bbaf4a113bbebfa21cf604cad9aa1503c3f04d",
64+
},
65+
{
66+
source: scm.Reference{Path: "refs/heads/master"},
67+
target: scm.Reference{Sha: "49bbaf4a113bbebfa21cf604cad9aa1503c3f04d"},
68+
want: "https://try.gogs.io/octocat/hello-world/compare/master...49bbaf4a113bbebfa21cf604cad9aa1503c3f04d",
69+
},
70+
{
71+
source: scm.Reference{Sha: "a7389057b0eb027e73b32a81e3c5923a71d01dde"},
72+
target: scm.Reference{Path: "refs/heads/master"},
73+
want: "https://try.gogs.io/octocat/hello-world/compare/a7389057b0eb027e73b32a81e3c5923a71d01dde...master",
74+
},
75+
{
76+
target: scm.Reference{Path: "refs/pull/12/head"},
77+
want: "https://try.gogs.io/octocat/hello-world/pulls/12/files",
78+
},
79+
}
80+
81+
for _, test := range tests {
82+
client, _ := New("https://try.gogs.io")
83+
got, err := client.Linker.Diff(context.Background(), "octocat/hello-world", test.source, test.target)
84+
if err != nil {
85+
t.Error(err)
86+
return
87+
}
88+
want := test.want
89+
if got != want {
90+
t.Errorf("Want link %q, got %q", want, got)
91+
}
92+
}
93+
}
94+
95+
func TestLink_Base(t *testing.T) {
96+
client, _ := New("https://foo.bar.com/baz")
97+
ref := scm.Reference{
98+
Path: "refs/heads/master",
99+
Sha: "a7389057b0eb027e73b32a81e3c5923a71d01dde",
100+
}
101+
got, err := client.Linker.Resource(context.Background(), "octocat/hello-world", ref)
102+
if err != nil {
103+
t.Error(err)
104+
return
105+
}
106+
want := "https://foo.bar.com/baz/octocat/hello-world/commit/a7389057b0eb027e73b32a81e3c5923a71d01dde"
107+
if got != want {
108+
t.Errorf("Want link %q, got %q", want, got)
109+
}
110+
}

0 commit comments

Comments
 (0)