From f4382d1345cf68a5e000a4f63c4e0f61531827cd Mon Sep 17 00:00:00 2001 From: William Martin Date: Tue, 18 Mar 2025 13:13:06 +0100 Subject: [PATCH] Revert "Merge pull request #10384 from iamazeem/9798-gh-api-encode-package-name" This reverts commit ed2c322a73f150fd424f4fd21b95edab7d129a2e, reversing changes made to f019cf7cea157f40e1f08bcf5573ffa9f0e796ec. --- pkg/cmd/api/api.go | 37 ----------------------- pkg/cmd/api/api_test.go | 66 ----------------------------------------- 2 files changed, 103 deletions(-) diff --git a/pkg/cmd/api/api.go b/pkg/cmd/api/api.go index d25b4ce64..586aeae93 100644 --- a/pkg/cmd/api/api.go +++ b/pkg/cmd/api/api.go @@ -7,7 +7,6 @@ import ( "fmt" "io" "net/http" - "net/url" "os" "path/filepath" "regexp" @@ -265,8 +264,6 @@ func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command return err } - opts.RequestPath = escapePackageNameInPath(opts.RequestPath) - if runF != nil { return runF(&opts) } @@ -694,37 +691,3 @@ func previewNamesToMIMETypes(names []string) string { } return strings.Join(types, ", ") } - -// The package name part in the `packages` endpoints may contain slashes and -// other characters that need to be URL encoded. -// -// The `escapePackageNameInPath` function extracts and normalizes package names -// in the path. The regex `pathWithPackageNameRE` is being used to extract the -// package name with a capture group named `package`. -// -// See https://docs.github.com/en/rest/packages/packages APIs for more details. -// -// Here's an example: -// -// The package name `orders/cache` needs to be URL encoded because it contains -// a slash `/`. The `escapePackageNameInPath` function will extract the -// `orders/cache` part, perform the URL encoding, and return the normalized API -// endpoint with `%2F` replacing the slash `/` in the package name part only. -// -// - Package name: `orders/cache` -// - API endpoint: `/users/USER/packages/container/orders/cache` -// - Normalized: `/users/USER/packages/container/orders%2Fcache` - -var pathWithPackageNameRE = regexp.MustCompile(`^\/(?:orgs|user|users)(?:\/.*)?\/packages\/(?:npm|maven|rubygems|docker|nuget|container)\/(?.*?)(?:\/(?:restore|versions)|$)`) - -func escapePackageNameInPath(path string) string { - matches := pathWithPackageNameRE.FindStringSubmatch(path) - if len(matches) > 0 { - i := pathWithPackageNameRE.SubexpIndex("package") - packageName := matches[i] - if packageName != "" { - return strings.Replace(path, packageName, url.QueryEscape(packageName), 1) - } - } - return path -} diff --git a/pkg/cmd/api/api_test.go b/pkg/cmd/api/api_test.go index ee58d55d0..321f7b7c0 100644 --- a/pkg/cmd/api/api_test.go +++ b/pkg/cmd/api/api_test.go @@ -367,72 +367,6 @@ func Test_NewCmdApi(t *testing.T) { }, wantsErr: false, }, - { - name: "request path with container package name containing slashes", - cli: "/user/packages/container/github.com/username/package_name --verbose", - wants: ApiOptions{ - Hostname: "", - RequestMethod: "GET", - RequestMethodPassed: false, - RequestPath: "/user/packages/container/github.com%2Fusername%2Fpackage_name", - RequestInputFile: "", - RawFields: []string(nil), - MagicFields: []string(nil), - RequestHeaders: []string(nil), - ShowResponseHeaders: false, - Paginate: false, - Silent: false, - CacheTTL: 0, - Template: "", - FilterOutput: "", - Verbose: true, - }, - wantsErr: false, - }, - { - name: "request path with container package name containing slashes and restore", - cli: "/user/packages/container/github.com/username/package_name/restore --verbose", - wants: ApiOptions{ - Hostname: "", - RequestMethod: "GET", - RequestMethodPassed: false, - RequestPath: "/user/packages/container/github.com%2Fusername%2Fpackage_name/restore", - RequestInputFile: "", - RawFields: []string(nil), - MagicFields: []string(nil), - RequestHeaders: []string(nil), - ShowResponseHeaders: false, - Paginate: false, - Silent: false, - CacheTTL: 0, - Template: "", - FilterOutput: "", - Verbose: true, - }, - wantsErr: false, - }, - { - name: "request path with container package name containing slashes and versions", - cli: "/user/packages/container/github.com/username/package_name/versions --verbose", - wants: ApiOptions{ - Hostname: "", - RequestMethod: "GET", - RequestMethodPassed: false, - RequestPath: "/user/packages/container/github.com%2Fusername%2Fpackage_name/versions", - RequestInputFile: "", - RawFields: []string(nil), - MagicFields: []string(nil), - RequestHeaders: []string(nil), - ShowResponseHeaders: false, - Paginate: false, - Silent: false, - CacheTTL: 0, - Template: "", - FilterOutput: "", - Verbose: true, - }, - wantsErr: false, - }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {