Skip to content

Commit

Permalink
Continue Generic Packages support
Browse files Browse the repository at this point in the history
 * PublishPackageFile now returns the download URL
 * removed forward slash from Generic Pacakge API endpoint urls
 * Publishing package is a PUT operation
 * add GenericPackageStatusValue enums
 * rebased to upstream master

Signed-off-by: Evan Wies <evan@neomantra.net>
  • Loading branch information
neomantra committed Nov 10, 2021
1 parent 229765b commit 8a42f19
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
32 changes: 22 additions & 10 deletions generic_packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ import (
"net/http"
)

// GenericPackageStatusValue represents a GitLab Package Status.
type GenericPackageStatusValue string

// These constants represent all valid package statuses.
const (
Default GenericPackageStatusValue = "default"
Hidden GenericPackageStatusValue = "hidden"
)

// GenericPackagesService handles communication with the packages related methods
// of the GitLab API.
//
Expand All @@ -41,7 +50,7 @@ func (s *GenericPackagesService) DownloadPackageFile(pid interface{}, packageNam
return nil, nil, err
}
u := fmt.Sprintf(
"/projects/%s/packages/generic/%s/%s/%s",
"projects/%s/packages/generic/%s/%s/%s",
pathEscape(project),
pathEscape(packageName),
pathEscape(packageVersion),
Expand All @@ -67,20 +76,21 @@ func (s *GenericPackagesService) DownloadPackageFile(pid interface{}, packageNam
// GitLab docs:
// https://docs.gitlab.com/ee/user/packages/generic_packages/index.html#download-package-file
type PublishPackageFileOptions struct {
Status *GenericPackageStateValue `url:"status,omitempty" json:"status,omitempty"`
Status *GenericPackageStatusValue `url:"status,omitempty" json:"status,omitempty"`
}

// PublishPackageFile allows you to download the package file.
// PublishPackageFile uploads a file to a project's Package Registry.
// Returns the package URL, the response body, the Response, and any error.
//
// GitLab docs:
// https://docs.gitlab.com/ee/user/packages/generic_packages/index.html#download-package-file
func (s *GenericPackagesService) PublishPackageFile(pid interface{}, packageName, packageVersion, fileName string, content io.ReadCloser, opt *PublishPackageFileOptions, options ...RequestOptionFunc) ([]byte, *Response, error) {
func (s *GenericPackagesService) PublishPackageFile(pid interface{}, packageName, packageVersion, fileName string, content io.ReadCloser, opt *PublishPackageFileOptions, options ...RequestOptionFunc) (string, []byte, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
return "", nil, nil, err
}
u := fmt.Sprintf(
"/projects/%s/packages/generic/%s/%s/%s",
"projects/%s/packages/generic/%s/%s/%s",
pathEscape(project),
pathEscape(packageName),
pathEscape(packageVersion),
Expand All @@ -90,16 +100,18 @@ func (s *GenericPackagesService) PublishPackageFile(pid interface{}, packageName
// This is currently the only way to use a PUT request to upload a non-JSON file
options = append(options, WithUploadFile(content))

req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
if err != nil {
return nil, nil, err
return "", nil, nil, err
}

var f bytes.Buffer
resp, err := s.client.Do(req, &f)
if err != nil {
return nil, resp, err
return "", nil, resp, err
}

return f.Bytes(), resp, err
// ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/my_package/0.0.1/file.txt'
downloadURL := fmt.Sprintf("%s%s", s.client.BaseURL(), u)
return downloadURL, f.Bytes(), resp, err
}
9 changes: 7 additions & 2 deletions generic_packages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,24 @@ func TestPublishPackageFile(t *testing.T) {
defer teardown(server)

mux.HandleFunc("/api/v4/projects/1234/packages/generic/foo/0.1.2/bar-baz.txt", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
testMethod(t, r, http.MethodPut)
fmt.Fprint(w, `
{
"message": "201 Created"
}
`)
})

result, _, err := client.GenericPackages.PublishPackageFile(1234, "foo", "0.1.2", "bar-baz.txt", io.NopCloser(strings.NewReader("bar = baz")), &PublishPackageFileOptions{})
url, result, _, err := client.GenericPackages.PublishPackageFile(1234, "foo", "0.1.2", "bar-baz.txt", io.NopCloser(strings.NewReader("bar = baz")), &PublishPackageFileOptions{})
if err != nil {
t.Errorf("GenericPackages.PublishPackageFile returned error: %v", err)
}

goldenURL := client.BaseURL().String() + "projects/1234/packages/generic/foo/0%2E1%2E2/bar-baz%2Etxt"
if url != goldenURL {
t.Errorf("GenericPackages.PublishPackageFile URL was %+v, want %+v", url, goldenURL)
}

body := map[string]interface{}{}
if err := json.Unmarshal(result, &body); err != nil {
t.Errorf("Error decoding body: %v", err)
Expand Down

0 comments on commit 8a42f19

Please sign in to comment.