...

Source file src/github.com/xanzy/go-gitlab/generic_packages.go

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, Sune Keller
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  package gitlab
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"io"
    23  	"net/http"
    24  	"time"
    25  )
    26  
    27  // GenericPackagesService handles communication with the packages related
    28  // methods of the GitLab API.
    29  //
    30  // GitLab docs:
    31  // https://docs.gitlab.com/ee/user/packages/generic_packages/index.html
    32  type GenericPackagesService struct {
    33  	client *Client
    34  }
    35  
    36  // GenericPackagesFile represents a GitLab generic package file.
    37  //
    38  // GitLab API docs:
    39  // https://docs.gitlab.com/ee/user/packages/generic_packages/index.html#publish-a-package-file
    40  type GenericPackagesFile struct {
    41  	ID        int        `json:"id"`
    42  	PackageID int        `json:"package_id"`
    43  	CreatedAt *time.Time `json:"created_at"`
    44  	UpdatedAt *time.Time `json:"updated_at"`
    45  	Size      int        `json:"size"`
    46  	FileStore int        `json:"file_store"`
    47  	FileMD5   string     `json:"file_md5"`
    48  	FileSHA1  string     `json:"file_sha1"`
    49  	FileName  string     `json:"file_name"`
    50  	File      struct {
    51  		URL string `json:"url"`
    52  	} `json:"file"`
    53  	FileSHA256             string     `json:"file_sha256"`
    54  	VerificationRetryAt    *time.Time `json:"verification_retry_at"`
    55  	VerifiedAt             *time.Time `json:"verified_at"`
    56  	VerificationFailure    bool       `json:"verification_failure"`
    57  	VerificationRetryCount int        `json:"verification_retry_count"`
    58  	VerificationChecksum   string     `json:"verification_checksum"`
    59  	VerificationState      int        `json:"verification_state"`
    60  	VerificationStartedAt  *time.Time `json:"verification_started_at"`
    61  	NewFilePath            string     `json:"new_file_path"`
    62  }
    63  
    64  // FormatPackageURL returns the GitLab Package Registry URL for the given artifact metadata, without the BaseURL.
    65  // This does not make a GitLab API request, but rather computes it based on their documentation.
    66  func (s *GenericPackagesService) FormatPackageURL(pid interface{}, packageName, packageVersion, fileName string) (string, error) {
    67  	project, err := parseID(pid)
    68  	if err != nil {
    69  		return "", err
    70  	}
    71  	u := fmt.Sprintf(
    72  		"projects/%s/packages/generic/%s/%s/%s",
    73  		PathEscape(project),
    74  		PathEscape(packageName),
    75  		PathEscape(packageVersion),
    76  		PathEscape(fileName),
    77  	)
    78  	return u, nil
    79  }
    80  
    81  // PublishPackageFileOptions represents the available PublishPackageFile()
    82  // options.
    83  //
    84  // GitLab docs:
    85  // https://docs.gitlab.com/ee/user/packages/generic_packages/index.html#publish-a-package-file
    86  type PublishPackageFileOptions struct {
    87  	Status *GenericPackageStatusValue `url:"status,omitempty" json:"status,omitempty"`
    88  	Select *GenericPackageSelectValue `url:"select,omitempty" json:"select,omitempty"`
    89  }
    90  
    91  // PublishPackageFile uploads a file to a project's package registry.
    92  //
    93  // GitLab docs:
    94  // https://docs.gitlab.com/ee/user/packages/generic_packages/index.html#publish-a-package-file
    95  func (s *GenericPackagesService) PublishPackageFile(pid interface{}, packageName, packageVersion, fileName string, content io.Reader, opt *PublishPackageFileOptions, options ...RequestOptionFunc) (*GenericPackagesFile, *Response, error) {
    96  	project, err := parseID(pid)
    97  	if err != nil {
    98  		return nil, nil, err
    99  	}
   100  	u := fmt.Sprintf(
   101  		"projects/%s/packages/generic/%s/%s/%s",
   102  		PathEscape(project),
   103  		PathEscape(packageName),
   104  		PathEscape(packageVersion),
   105  		PathEscape(fileName),
   106  	)
   107  
   108  	// We need to create the request as a GET request to make sure the options
   109  	// are set correctly. After the request is created we will overwrite both
   110  	// the method and the body.
   111  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   112  	if err != nil {
   113  		return nil, nil, err
   114  	}
   115  
   116  	// Overwrite the method and body.
   117  	req.Method = http.MethodPut
   118  	req.SetBody(content)
   119  
   120  	f := new(GenericPackagesFile)
   121  	resp, err := s.client.Do(req, f)
   122  	if err != nil {
   123  		return nil, resp, err
   124  	}
   125  
   126  	return f, resp, nil
   127  }
   128  
   129  // DownloadPackageFile allows you to download the package file.
   130  //
   131  // GitLab docs:
   132  // https://docs.gitlab.com/ee/user/packages/generic_packages/index.html#download-package-file
   133  func (s *GenericPackagesService) DownloadPackageFile(pid interface{}, packageName, packageVersion, fileName string, options ...RequestOptionFunc) ([]byte, *Response, error) {
   134  	project, err := parseID(pid)
   135  	if err != nil {
   136  		return nil, nil, err
   137  	}
   138  	u := fmt.Sprintf(
   139  		"projects/%s/packages/generic/%s/%s/%s",
   140  		PathEscape(project),
   141  		PathEscape(packageName),
   142  		PathEscape(packageVersion),
   143  		PathEscape(fileName),
   144  	)
   145  
   146  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   147  	if err != nil {
   148  		return nil, nil, err
   149  	}
   150  
   151  	var f bytes.Buffer
   152  	resp, err := s.client.Do(req, &f)
   153  	if err != nil {
   154  		return nil, resp, err
   155  	}
   156  
   157  	return f.Bytes(), resp, err
   158  }
   159  

View as plain text