Skip to content

Commit 18b3b41

Browse files
authored
Merge pull request #291 from senjucanon2/master
Added support for branch names containing '&' and '#' for GetFile Operations.
2 parents 93042b7 + 474ee0b commit 18b3b41

File tree

9 files changed

+97
-7
lines changed

9 files changed

+97
-7
lines changed

scm/driver/azure/content.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"encoding/base64"
1010
"fmt"
11+
"net/url"
1112

1213
"github.com/drone/go-scm/scm"
1314
)
@@ -21,8 +22,9 @@ func (s *contentService) Find(ctx context.Context, repo, path, ref string) (*scm
2122
if s.client.project == "" {
2223
return nil, nil, ProjectRequiredError()
2324
}
25+
urlEncodedRef := url.QueryEscape(ref)
2426
endpoint := fmt.Sprintf("%s/%s/_apis/git/repositories/%s/items?path=%s&includeContent=true&$format=json", s.client.owner, s.client.project, repo, path)
25-
endpoint += generateURIFromRef(ref)
27+
endpoint += generateURIFromRef(urlEncodedRef)
2628
endpoint += "&api-version=6.0"
2729
out := new(content)
2830
res, err := s.client.do(ctx, "GET", endpoint, nil, out)

scm/driver/azure/content_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func TestContentFind(t *testing.T) {
1717
gock.New("https:/dev.azure.com/").
1818
Get("/ORG/PROJ/_apis/git/repositories/REPOID/items").
1919
MatchParam("path", "README").
20+
MatchParam("versionDescriptor.version", "b1&b2").
2021
Reply(200).
2122
Type("application/json").
2223
File("testdata/content.json")
@@ -26,7 +27,7 @@ func TestContentFind(t *testing.T) {
2627
context.Background(),
2728
"REPOID",
2829
"README",
29-
"",
30+
"b1&b2",
3031
)
3132
if err != nil {
3233
t.Error(err)

scm/driver/bitbucket/content.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bytes"
99
"context"
1010
"fmt"
11+
"net/url"
1112

1213
"github.com/drone/go-scm/scm"
1314
)
@@ -17,7 +18,8 @@ type contentService struct {
1718
}
1819

1920
func (s *contentService) Find(ctx context.Context, repo, path, ref string) (*scm.Content, *scm.Response, error) {
20-
endpoint := fmt.Sprintf("/2.0/repositories/%s/src/%s/%s", repo, ref, path)
21+
urlEncodedRef := url.QueryEscape(ref)
22+
endpoint := fmt.Sprintf("/2.0/repositories/%s/src/%s/%s", repo, urlEncodedRef, path)
2123
out := new(bytes.Buffer)
2224
res, err := s.client.do(ctx, "GET", endpoint, nil, out)
2325
content := &scm.Content{
@@ -27,7 +29,7 @@ func (s *contentService) Find(ctx context.Context, repo, path, ref string) (*scm
2729
if err != nil {
2830
return content, res, err
2931
}
30-
metaEndpoint := fmt.Sprintf("/2.0/repositories/%s/src/%s/%s?format=meta", repo, ref, path)
32+
metaEndpoint := fmt.Sprintf("/2.0/repositories/%s/src/%s/%s?format=meta", repo, urlEncodedRef, path)
3133
metaOut := new(metaContent)
3234
metaRes, metaErr := s.client.do(ctx, "GET", metaEndpoint, nil, metaOut)
3335
if metaErr == nil {

scm/driver/github/content.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"encoding/base64"
1010
"fmt"
11+
"net/url"
1112

1213
"github.com/drone/go-scm/scm"
1314
)
@@ -17,7 +18,8 @@ type contentService struct {
1718
}
1819

1920
func (s *contentService) Find(ctx context.Context, repo, path, ref string) (*scm.Content, *scm.Response, error) {
20-
endpoint := fmt.Sprintf("repos/%s/contents/%s?ref=%s", repo, path, ref)
21+
urlEncodedRef := url.QueryEscape(ref)
22+
endpoint := fmt.Sprintf("repos/%s/contents/%s?ref=%s", repo, path, urlEncodedRef)
2123
out := new(content)
2224
res, err := s.client.do(ctx, "GET", endpoint, nil, out)
2325
raw, _ := base64.StdEncoding.DecodeString(out.Content)

scm/driver/github/content_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,35 @@ func TestContentFind(t *testing.T) {
4747
t.Log(diff)
4848
}
4949

50+
gock.New("https://api.github.com").
51+
Get("/repos/octocat/hello-world/contents/README").
52+
MatchParam("ref", "b1&b2").
53+
Reply(200).
54+
Type("application/json").
55+
SetHeaders(mockHeaders).
56+
File("testdata/content.json")
57+
58+
client = NewDefault()
59+
got, res, err = client.Contents.Find(
60+
context.Background(),
61+
"octocat/hello-world",
62+
"README",
63+
"b1&b2",
64+
)
65+
if err != nil {
66+
t.Error(err)
67+
return
68+
}
69+
70+
want = new(scm.Content)
71+
raw, _ = ioutil.ReadFile("testdata/content.json.golden")
72+
_ = json.Unmarshal(raw, want)
73+
74+
if diff := cmp.Diff(got, want); diff != "" {
75+
t.Errorf("Unexpected Results")
76+
t.Log(diff)
77+
}
78+
5079
t.Run("Request", testRequest(res))
5180
t.Run("Rate", testRate(res))
5281
}

scm/driver/gitlab/content.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ type contentService struct {
1919
}
2020

2121
func (s *contentService) Find(ctx context.Context, repo, path, ref string) (*scm.Content, *scm.Response, error) {
22-
endpoint := fmt.Sprintf("api/v4/projects/%s/repository/files/%s?ref=%s", encode(repo), encodePath(path), ref)
22+
urlEncodedRef := url.QueryEscape(ref)
23+
endpoint := fmt.Sprintf("api/v4/projects/%s/repository/files/%s?ref=%s", encode(repo), encodePath(path), urlEncodedRef)
2324
out := new(content)
2425
res, err := s.client.do(ctx, "GET", endpoint, nil, out)
2526
raw, berr := base64.StdEncoding.DecodeString(out.Content)

scm/driver/gitlab/content_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,35 @@ func TestContentFind(t *testing.T) {
4848
t.Log(diff)
4949
}
5050

51+
gock.New("https://gitlab.com").
52+
Get("/api/v4/projects/diaspora/diaspora/repository/files/app/models/key.rb").
53+
MatchParam("ref", "b1&b2").
54+
Reply(200).
55+
Type("application/json").
56+
SetHeaders(mockHeaders).
57+
File("testdata/content.json")
58+
59+
client = NewDefault()
60+
got, res, err = client.Contents.Find(
61+
context.Background(),
62+
"diaspora/diaspora",
63+
"app/models/key.rb",
64+
"b1&b2",
65+
)
66+
if err != nil {
67+
t.Error(err)
68+
return
69+
}
70+
71+
want = new(scm.Content)
72+
raw, _ = ioutil.ReadFile("testdata/content.json.golden")
73+
json.Unmarshal(raw, want)
74+
75+
if diff := cmp.Diff(got, want); diff != "" {
76+
t.Errorf("Unexpected Results")
77+
t.Log(diff)
78+
}
79+
5180
t.Run("Request", testRequest(res))
5281
t.Run("Rate", testRate(res))
5382
}

scm/driver/stash/content.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bytes"
99
"context"
1010
"fmt"
11+
"net/url"
1112

1213
"github.com/drone/go-scm/scm"
1314
)
@@ -17,8 +18,9 @@ type contentService struct {
1718
}
1819

1920
func (s *contentService) Find(ctx context.Context, repo, path, ref string) (*scm.Content, *scm.Response, error) {
21+
urlEncodedRef := url.QueryEscape(ref)
2022
namespace, name := scm.Split(repo)
21-
endpoint := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/raw/%s?at=%s", namespace, name, path, ref)
23+
endpoint := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/raw/%s?at=%s", namespace, name, path, urlEncodedRef)
2224
out := new(bytes.Buffer)
2325
res, err := s.client.do(ctx, "GET", endpoint, nil, out)
2426
return &scm.Content{

scm/driver/stash/content_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ func TestContentFind(t *testing.T) {
4040
t.Errorf("Unexpected Results")
4141
t.Log(diff)
4242
}
43+
44+
gock.New("http://example.com:7990").
45+
Get("/rest/api/1.0/projects/PRJ/repos/my-repo/raw/README").
46+
MatchParam("at", "b1&b2").
47+
Reply(200).
48+
Type("text/plain").
49+
File("testdata/content.txt")
50+
51+
client, _ = New("http://example.com:7990")
52+
got, _, err = client.Contents.Find(context.Background(), "PRJ/my-repo", "README", "b1&b2")
53+
if err != nil {
54+
t.Error(err)
55+
}
56+
57+
want = new(scm.Content)
58+
raw, _ = ioutil.ReadFile("testdata/content.json.golden")
59+
_ = json.Unmarshal(raw, want)
60+
61+
if diff := cmp.Diff(got, want); diff != "" {
62+
t.Errorf("Unexpected Results")
63+
t.Log(diff)
64+
}
4365
}
4466

4567
func TestContentCreate(t *testing.T) {

0 commit comments

Comments
 (0)