...

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

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, Kordian Bruck
     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  	"fmt"
    21  	"net/http"
    22  	"time"
    23  )
    24  
    25  // PackagesService handles communication with the packages related methods
    26  // of the GitLab API.
    27  //
    28  // GitLab API docs: https://docs.gitlab.com/ee/api/packages.html
    29  type PackagesService struct {
    30  	client *Client
    31  }
    32  
    33  // Package represents a GitLab package.
    34  //
    35  // GitLab API docs: https://docs.gitlab.com/ee/api/packages.html
    36  type Package struct {
    37  	ID               int           `json:"id"`
    38  	Name             string        `json:"name"`
    39  	Version          string        `json:"version"`
    40  	PackageType      string        `json:"package_type"`
    41  	Status           string        `json:"status"`
    42  	Links            *PackageLinks `json:"_links"`
    43  	CreatedAt        *time.Time    `json:"created_at"`
    44  	LastDownloadedAt *time.Time    `json:"last_downloaded_at"`
    45  	Tags             []PackageTag  `json:"tags"`
    46  }
    47  
    48  func (s Package) String() string {
    49  	return Stringify(s)
    50  }
    51  
    52  // GroupPackage represents a GitLab group package.
    53  //
    54  // GitLab API docs: https://docs.gitlab.com/ee/api/packages.html
    55  type GroupPackage struct {
    56  	Package
    57  	ProjectID   int    `json:"project_id"`
    58  	ProjectPath string `json:"project_path"`
    59  }
    60  
    61  func (s GroupPackage) String() string {
    62  	return Stringify(s)
    63  }
    64  
    65  // PackageLinks holds links for itself and deleting.
    66  type PackageLinks struct {
    67  	WebPath       string `json:"web_path"`
    68  	DeleteAPIPath string `json:"delete_api_path"`
    69  }
    70  
    71  func (s PackageLinks) String() string {
    72  	return Stringify(s)
    73  }
    74  
    75  // PackageTag holds label information about the package
    76  type PackageTag struct {
    77  	ID        int        `json:"id"`
    78  	PackageID int        `json:"package_id"`
    79  	Name      string     `json:"name"`
    80  	CreatedAt *time.Time `json:"created_at"`
    81  	UpdatedAt *time.Time `json:"updated_at"`
    82  }
    83  
    84  func (s PackageTag) String() string {
    85  	return Stringify(s)
    86  }
    87  
    88  // PackageFile represents one file contained within a package.
    89  //
    90  // GitLab API docs: https://docs.gitlab.com/ee/api/packages.html
    91  type PackageFile struct {
    92  	ID         int         `json:"id"`
    93  	PackageID  int         `json:"package_id"`
    94  	CreatedAt  *time.Time  `json:"created_at"`
    95  	FileName   string      `json:"file_name"`
    96  	Size       int         `json:"size"`
    97  	FileMD5    string      `json:"file_md5"`
    98  	FileSHA1   string      `json:"file_sha1"`
    99  	FileSHA256 string      `json:"file_sha256"`
   100  	Pipeline   *[]Pipeline `json:"pipelines"`
   101  }
   102  
   103  func (s PackageFile) String() string {
   104  	return Stringify(s)
   105  }
   106  
   107  // ListProjectPackagesOptions represents the available ListProjectPackages()
   108  // options.
   109  //
   110  // GitLab API docs:
   111  // https://docs.gitlab.com/ee/api/packages.html#within-a-project
   112  type ListProjectPackagesOptions struct {
   113  	ListOptions
   114  	OrderBy            *string `url:"order_by,omitempty" json:"order_by,omitempty"`
   115  	Sort               *string `url:"sort,omitempty" json:"sort,omitempty"`
   116  	PackageType        *string `url:"package_type,omitempty" json:"package_type,omitempty"`
   117  	PackageName        *string `url:"package_name,omitempty" json:"package_name,omitempty"`
   118  	PackageVersion     *string `url:"package_version,omitempty" json:"package_version,omitempty"`
   119  	IncludeVersionless *bool   `url:"include_versionless,omitempty" json:"include_versionless,omitempty"`
   120  	Status             *string `url:"status,omitempty" json:"status,omitempty"`
   121  }
   122  
   123  // ListProjectPackages gets a list of packages in a project.
   124  //
   125  // GitLab API docs:
   126  // https://docs.gitlab.com/ee/api/packages.html#within-a-project
   127  func (s *PackagesService) ListProjectPackages(pid interface{}, opt *ListProjectPackagesOptions, options ...RequestOptionFunc) ([]*Package, *Response, error) {
   128  	project, err := parseID(pid)
   129  	if err != nil {
   130  		return nil, nil, err
   131  	}
   132  	u := fmt.Sprintf("projects/%s/packages", PathEscape(project))
   133  
   134  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   135  	if err != nil {
   136  		return nil, nil, err
   137  	}
   138  
   139  	var ps []*Package
   140  	resp, err := s.client.Do(req, &ps)
   141  	if err != nil {
   142  		return nil, resp, err
   143  	}
   144  
   145  	return ps, resp, nil
   146  }
   147  
   148  // ListGroupPackagesOptions represents the available ListGroupPackages()
   149  // options.
   150  //
   151  // GitLab API docs:
   152  // https://docs.gitlab.com/ee/api/packages.html#within-a-group
   153  type ListGroupPackagesOptions struct {
   154  	ListOptions
   155  	ExcludeSubGroups   *bool   `url:"exclude_subgroups,omitempty" json:"exclude_subgroups,omitempty"`
   156  	OrderBy            *string `url:"order_by,omitempty" json:"order_by,omitempty"`
   157  	Sort               *string `url:"sort,omitempty" json:"sort,omitempty"`
   158  	PackageType        *string `url:"package_type,omitempty" json:"package_type,omitempty"`
   159  	PackageName        *string `url:"package_name,omitempty" json:"package_name,omitempty"`
   160  	IncludeVersionless *bool   `url:"include_versionless,omitempty" json:"include_versionless,omitempty"`
   161  	Status             *string `url:"status,omitempty" json:"status,omitempty"`
   162  }
   163  
   164  // ListGroupPackages gets a list of packages in a group.
   165  //
   166  // GitLab API docs:
   167  // https://docs.gitlab.com/ee/api/packages.html#within-a-group
   168  func (s *PackagesService) ListGroupPackages(gid interface{}, opt *ListGroupPackagesOptions, options ...RequestOptionFunc) ([]*GroupPackage, *Response, error) {
   169  	group, err := parseID(gid)
   170  	if err != nil {
   171  		return nil, nil, err
   172  	}
   173  	u := fmt.Sprintf("groups/%s/packages", PathEscape(group))
   174  
   175  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   176  	if err != nil {
   177  		return nil, nil, err
   178  	}
   179  
   180  	var ps []*GroupPackage
   181  	resp, err := s.client.Do(req, &ps)
   182  	if err != nil {
   183  		return nil, resp, err
   184  	}
   185  
   186  	return ps, resp, nil
   187  }
   188  
   189  // ListPackageFilesOptions represents the available ListPackageFiles()
   190  // options.
   191  //
   192  // GitLab API docs:
   193  // https://docs.gitlab.com/ee/api/packages.html#list-package-files
   194  type ListPackageFilesOptions ListOptions
   195  
   196  // ListPackageFiles gets a list of files that are within a package
   197  //
   198  // GitLab API docs:
   199  // https://docs.gitlab.com/ee/api/packages.html#list-package-files
   200  func (s *PackagesService) ListPackageFiles(pid interface{}, pkg int, opt *ListPackageFilesOptions, options ...RequestOptionFunc) ([]*PackageFile, *Response, error) {
   201  	project, err := parseID(pid)
   202  	if err != nil {
   203  		return nil, nil, err
   204  	}
   205  	u := fmt.Sprintf(
   206  		"projects/%s/packages/%d/package_files",
   207  		PathEscape(project),
   208  		pkg,
   209  	)
   210  
   211  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   212  	if err != nil {
   213  		return nil, nil, err
   214  	}
   215  
   216  	var pfs []*PackageFile
   217  	resp, err := s.client.Do(req, &pfs)
   218  	if err != nil {
   219  		return nil, resp, err
   220  	}
   221  
   222  	return pfs, resp, nil
   223  }
   224  
   225  // DeleteProjectPackage deletes a package in a project.
   226  //
   227  // GitLab API docs:
   228  // https://docs.gitlab.com/ee/api/packages.html#delete-a-project-package
   229  func (s *PackagesService) DeleteProjectPackage(pid interface{}, pkg int, options ...RequestOptionFunc) (*Response, error) {
   230  	project, err := parseID(pid)
   231  	if err != nil {
   232  		return nil, err
   233  	}
   234  	u := fmt.Sprintf("projects/%s/packages/%d", PathEscape(project), pkg)
   235  
   236  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   237  	if err != nil {
   238  		return nil, err
   239  	}
   240  
   241  	return s.client.Do(req, nil)
   242  }
   243  
   244  // DeletePackageFile deletes a file in project package
   245  //
   246  // GitLab API docs:
   247  // https://docs.gitlab.com/ee/api/packages.html#delete-a-package-file
   248  func (s *PackagesService) DeletePackageFile(pid interface{}, pkg, file int, options ...RequestOptionFunc) (*Response, error) {
   249  	project, err := parseID(pid)
   250  	if err != nil {
   251  		return nil, err
   252  	}
   253  	u := fmt.Sprintf("projects/%s/packages/%d/package_files/%d", PathEscape(project), pkg, file)
   254  
   255  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   256  	if err != nil {
   257  		return nil, err
   258  	}
   259  
   260  	return s.client.Do(req, nil)
   261  }
   262  

View as plain text