Skip to content

Commit

Permalink
Escape tagName path in release related URLs
Browse files Browse the repository at this point in the history
Signed-off-by: Sune Keller <[email protected]>
  • Loading branch information
sirlatrom committed May 27, 2021
1 parent 5b322a0 commit 0ed3277
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 8 deletions.
48 changes: 48 additions & 0 deletions fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,54 @@ const (
}
}`

// exampleReleaseResponse provides fixture for Releases tests.
exampleReleaseWithMetadataResponse = `{
"tag_name": "v0.1.2+example-metadata",
"description": "description",
"name": "Awesome app v0.1 alpha",
"description_html": "description_html",
"created_at": "2019-01-03T01:55:18.203Z",
"author": {
"id": 1,
"name": "Administrator",
"username": "root",
"state": "active",
"avatar_url": "https://www.gravatar.com/avatar/",
"web_url": "http://localhost:3000/root"
},
"commit": {
"id": "f8d3d94cbd347e924aa7b715845e439d00e80ca4",
"short_id": "f8d3d94c",
"title": "Initial commit",
"created_at": "2019-01-03T01:53:28.000Z",
"parent_ids": [],
"message": "Initial commit",
"author_name": "Administrator",
"author_email": "[email protected]",
"authored_date": "2019-01-03T01:53:28.000Z",
"committer_name": "Administrator",
"committer_email": "[email protected]",
"committed_date": "2019-01-03T01:53:28.000Z"
},
"assets": {
"count": 2,
"sources": [
{
"format": "zip",
"url": "http://localhost:3000/archive/v0.1/awesome-app-v0.1.zip"
},
{
"format": "tar.gz",
"url": "http://localhost:3000/archive/v0.1/awesome-app-v0.1.tar.gz"
}
],
"links": []
}
}`

// exampleTagName provides a fixture for a tag name.
exampleTagName = "v0.1"

// exampleTagName provides a fixture for a tag name.
exampleTagNameWithMetadata = "v0.1.2+example-metadata"
)
10 changes: 5 additions & 5 deletions releaselinks.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (s *ReleaseLinksService) ListReleaseLinks(pid interface{}, tagName string,
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/releases/%s/assets/links", pathEscape(project), tagName)
u := fmt.Sprintf("projects/%s/releases/%s/assets/links", pathEscape(project), pathEscape(tagName))

req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
if err != nil {
Expand All @@ -80,7 +80,7 @@ func (s *ReleaseLinksService) GetReleaseLink(pid interface{}, tagName string, li
}
u := fmt.Sprintf("projects/%s/releases/%s/assets/links/%d",
pathEscape(project),
tagName,
pathEscape(tagName),
link)

req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
Expand Down Expand Up @@ -115,7 +115,7 @@ func (s *ReleaseLinksService) CreateReleaseLink(pid interface{}, tagName string,
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/releases/%s/assets/links", pathEscape(project), tagName)
u := fmt.Sprintf("projects/%s/releases/%s/assets/links", pathEscape(project), pathEscape(tagName))

req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
if err != nil {
Expand Down Expand Up @@ -153,7 +153,7 @@ func (s *ReleaseLinksService) UpdateReleaseLink(pid interface{}, tagName string,
}
u := fmt.Sprintf("projects/%s/releases/%s/assets/links/%d",
pathEscape(project),
tagName,
pathEscape(tagName),
link)

req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
Expand All @@ -180,7 +180,7 @@ func (s *ReleaseLinksService) DeleteReleaseLink(pid interface{}, tagName string,
}
u := fmt.Sprintf("projects/%s/releases/%s/assets/links/%d",
pathEscape(project),
tagName,
pathEscape(tagName),
link,
)

Expand Down
6 changes: 3 additions & 3 deletions releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (s *ReleasesService) GetRelease(pid interface{}, tagName string, options ..
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/releases/%s", pathEscape(project), tagName)
u := fmt.Sprintf("projects/%s/releases/%s", pathEscape(project), pathEscape(tagName))

req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
if err != nil {
Expand Down Expand Up @@ -192,7 +192,7 @@ func (s *ReleasesService) UpdateRelease(pid interface{}, tagName string, opts *U
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/releases/%s", pathEscape(project), tagName)
u := fmt.Sprintf("projects/%s/releases/%s", pathEscape(project), pathEscape(tagName))

req, err := s.client.NewRequest(http.MethodPut, u, opts, options)
if err != nil {
Expand All @@ -217,7 +217,7 @@ func (s *ReleasesService) DeleteRelease(pid interface{}, tagName string, options
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/releases/%s", pathEscape(project), tagName)
u := fmt.Sprintf("projects/%s/releases/%s", pathEscape(project), pathEscape(tagName))

req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
if err != nil {
Expand Down
50 changes: 50 additions & 0 deletions releases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,56 @@ func TestReleasesService_CreateReleaseWithAsset(t *testing.T) {
}
}

func TestReleasesService_CreateReleaseWithAssetAndNameMetadata(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

mux.HandleFunc("/api/v4/projects/1/releases",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatalf("unable to read request body")
}
if !strings.Contains(string(b), exampleTagNameWithMetadata) {
t.Errorf("expected request body to contain %s, got %s",
exampleTagNameWithMetadata, string(b))
}
if !strings.Contains(string(b), "assets") {
t.Errorf("expected request body to have assets, got %s",
string(b))
}
if strings.Contains(string(b), "milestones") {
t.Errorf("expected request body not to have milestones, got %s",
string(b))
}
if strings.Contains(string(b), "released_at") {
t.Errorf("expected request body not to have released_at, got %s",
string(b))
}
fmt.Fprint(w, exampleReleaseWithMetadataResponse)
})

opts := &CreateReleaseOptions{
Name: String("name"),
TagName: String(exampleTagNameWithMetadata),
Description: String("Description"),
Assets: &ReleaseAssets{
Links: []*ReleaseAssetLink{
{"sldkf", "sldkfj"},
},
},
}

release, _, err := client.Releases.CreateRelease(1, opts)
if err != nil {
t.Error(err)
}
if release.TagName != exampleTagNameWithMetadata {
t.Errorf("expected tag %s, got %s", exampleTagNameWithMetadata, release.TagName)
}
}

func TestReleasesService_CreateReleaseWithMilestones(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)
Expand Down

0 comments on commit 0ed3277

Please sign in to comment.