Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions github/users_packages.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,6 +8,7 @@ package github
import (
"context"
"fmt"
"net/url"
)

// ListPackages lists the packages for a user. Passing the empty string for "user" will
Expand DownExpand Up@@ -55,9 +56,9 @@ func (s *UsersService) ListPackages(ctx context.Context, user string, opts *Pack
func (s *UsersService) GetPackage(ctx context.Context, user, packageType, packageName string) (*Package, *Response, error) {
var u string
if user != "" {
u = fmt.Sprintf("users/%v/packages/%v/%v", user, packageType, packageName)
u = fmt.Sprintf("users/%v/packages/%v/%v", user, packageType, url.PathEscape(packageName))
} else {
u = fmt.Sprintf("user/packages/%v/%v", packageType, packageName)
u = fmt.Sprintf("user/packages/%v/%v", packageType, url.PathEscape(packageName))
}

req, err := s.client.NewRequest("GET", u, nil)
Expand Down
23 changes: 13 additions & 10 deletions github/users_packages_test.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"

Expand DownExpand Up@@ -131,42 +132,44 @@ func TestUsersService_specifiedUser_GetPackage(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)

mux.HandleFunc("/users/u/packages/container/hello_docker", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/users/u/packages/container/hello%2fhello_docker", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{
_, err := io.WriteString(w, `{
"id": 197,
"name": "hello_docker",
"name": "hello/hello_docker",
"package_type": "container",
"version_count": 1,
"visibility": "private",
"url": "https://api.github.com/orgs/github/packages/container/hello_docker",
"url": "https://api.github.com/orgs/github/packages/container/hello%2Fhello_docker",
"created_at": `+referenceTimeStr+`,
"updated_at": `+referenceTimeStr+`,
"html_url": "https://github.com/orgs/github/packages/container/package/hello_docker"
"html_url": "https://github.com/orgs/github/packages/container/package/hello%2Fhello_docker"
}`)
if err != nil {
t.Fatal("Failed to write test response: ", err)
}
})

ctx := context.Background()
packages, _, err := client.Users.GetPackage(ctx, "u", "container", "hello_docker")
packages, _, err := client.Users.GetPackage(ctx, "u", "container", "hello/hello_docker")
if err != nil {
t.Errorf("Users.GetPackage returned error: %v", err)
}

want := &Package{
ID: Ptr(int64(197)),
Name: Ptr("hello_docker"),
Name: Ptr("hello/hello_docker"),
PackageType: Ptr("container"),
VersionCount: Ptr(int64(1)),
Visibility: Ptr("private"),
URL: Ptr("https://api.github.com/orgs/github/packages/container/hello_docker"),
HTMLURL: Ptr("https://github.com/orgs/github/packages/container/package/hello_docker"),
URL: Ptr("https://api.github.com/orgs/github/packages/container/hello%2Fhello_docker"),
HTMLURL: Ptr("https://github.com/orgs/github/packages/container/package/hello%2Fhello_docker"),
CreatedAt: &Timestamp{referenceTime},
UpdatedAt: &Timestamp{referenceTime},
}
if !cmp.Equal(packages, want) {
t.Errorf("Users.specifiedUser_GetPackage returned %+v, want %+v", packages, want)
}

const methodName = "GetPackage"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Users.GetPackage(ctx, "\n", "\n", "\n")
Expand Down