...

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

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, Sander van Harmelen
     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  	"net/url"
    25  )
    26  
    27  // RepositoriesService handles communication with the repositories related
    28  // methods of the GitLab API.
    29  //
    30  // GitLab API docs: https://docs.gitlab.com/ee/api/repositories.html
    31  type RepositoriesService struct {
    32  	client *Client
    33  }
    34  
    35  // TreeNode represents a GitLab repository file or directory.
    36  //
    37  // GitLab API docs: https://docs.gitlab.com/ee/api/repositories.html
    38  type TreeNode struct {
    39  	ID   string `json:"id"`
    40  	Name string `json:"name"`
    41  	Type string `json:"type"`
    42  	Path string `json:"path"`
    43  	Mode string `json:"mode"`
    44  }
    45  
    46  func (t TreeNode) String() string {
    47  	return Stringify(t)
    48  }
    49  
    50  // ListTreeOptions represents the available ListTree() options.
    51  //
    52  // GitLab API docs:
    53  // https://docs.gitlab.com/ee/api/repositories.html#list-repository-tree
    54  type ListTreeOptions struct {
    55  	ListOptions
    56  	Path      *string `url:"path,omitempty" json:"path,omitempty"`
    57  	Ref       *string `url:"ref,omitempty" json:"ref,omitempty"`
    58  	Recursive *bool   `url:"recursive,omitempty" json:"recursive,omitempty"`
    59  }
    60  
    61  // ListTree gets a list of repository files and directories in a project.
    62  //
    63  // GitLab API docs:
    64  // https://docs.gitlab.com/ee/api/repositories.html#list-repository-tree
    65  func (s *RepositoriesService) ListTree(pid interface{}, opt *ListTreeOptions, options ...RequestOptionFunc) ([]*TreeNode, *Response, error) {
    66  	project, err := parseID(pid)
    67  	if err != nil {
    68  		return nil, nil, err
    69  	}
    70  	u := fmt.Sprintf("projects/%s/repository/tree", PathEscape(project))
    71  
    72  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
    73  	if err != nil {
    74  		return nil, nil, err
    75  	}
    76  
    77  	var t []*TreeNode
    78  	resp, err := s.client.Do(req, &t)
    79  	if err != nil {
    80  		return nil, resp, err
    81  	}
    82  
    83  	return t, resp, nil
    84  }
    85  
    86  // Blob gets information about blob in repository like size and content. Note
    87  // that blob content is Base64 encoded.
    88  //
    89  // GitLab API docs:
    90  // https://docs.gitlab.com/ee/api/repositories.html#get-a-blob-from-repository
    91  func (s *RepositoriesService) Blob(pid interface{}, sha string, options ...RequestOptionFunc) ([]byte, *Response, error) {
    92  	project, err := parseID(pid)
    93  	if err != nil {
    94  		return nil, nil, err
    95  	}
    96  	u := fmt.Sprintf("projects/%s/repository/blobs/%s", PathEscape(project), url.PathEscape(sha))
    97  
    98  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
    99  	if err != nil {
   100  		return nil, nil, err
   101  	}
   102  
   103  	var b bytes.Buffer
   104  	resp, err := s.client.Do(req, &b)
   105  	if err != nil {
   106  		return nil, resp, err
   107  	}
   108  
   109  	return b.Bytes(), resp, err
   110  }
   111  
   112  // RawBlobContent gets the raw file contents for a blob by blob SHA.
   113  //
   114  // GitLab API docs:
   115  // https://docs.gitlab.com/ee/api/repositories.html#raw-blob-content
   116  func (s *RepositoriesService) RawBlobContent(pid interface{}, sha string, options ...RequestOptionFunc) ([]byte, *Response, error) {
   117  	project, err := parseID(pid)
   118  	if err != nil {
   119  		return nil, nil, err
   120  	}
   121  	u := fmt.Sprintf("projects/%s/repository/blobs/%s/raw", PathEscape(project), url.PathEscape(sha))
   122  
   123  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   124  	if err != nil {
   125  		return nil, nil, err
   126  	}
   127  
   128  	var b bytes.Buffer
   129  	resp, err := s.client.Do(req, &b)
   130  	if err != nil {
   131  		return nil, resp, err
   132  	}
   133  
   134  	return b.Bytes(), resp, err
   135  }
   136  
   137  // ArchiveOptions represents the available Archive() options.
   138  //
   139  // GitLab API docs:
   140  // https://docs.gitlab.com/ee/api/repositories.html#get-file-archive
   141  type ArchiveOptions struct {
   142  	Format *string `url:"-" json:"-"`
   143  	Path   *string `url:"path,omitempty" json:"path,omitempty"`
   144  	SHA    *string `url:"sha,omitempty" json:"sha,omitempty"`
   145  }
   146  
   147  // Archive gets an archive of the repository.
   148  //
   149  // GitLab API docs:
   150  // https://docs.gitlab.com/ee/api/repositories.html#get-file-archive
   151  func (s *RepositoriesService) Archive(pid interface{}, opt *ArchiveOptions, options ...RequestOptionFunc) ([]byte, *Response, error) {
   152  	project, err := parseID(pid)
   153  	if err != nil {
   154  		return nil, nil, err
   155  	}
   156  	u := fmt.Sprintf("projects/%s/repository/archive", PathEscape(project))
   157  
   158  	// Set an optional format for the archive.
   159  	if opt != nil && opt.Format != nil {
   160  		u = fmt.Sprintf("%s.%s", u, *opt.Format)
   161  	}
   162  
   163  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   164  	if err != nil {
   165  		return nil, nil, err
   166  	}
   167  
   168  	var b bytes.Buffer
   169  	resp, err := s.client.Do(req, &b)
   170  	if err != nil {
   171  		return nil, resp, err
   172  	}
   173  
   174  	return b.Bytes(), resp, err
   175  }
   176  
   177  // StreamArchive streams an archive of the repository to the provided
   178  // io.Writer.
   179  //
   180  // GitLab API docs:
   181  // https://docs.gitlab.com/ee/api/repositories.html#get-file-archive
   182  func (s *RepositoriesService) StreamArchive(pid interface{}, w io.Writer, opt *ArchiveOptions, options ...RequestOptionFunc) (*Response, error) {
   183  	project, err := parseID(pid)
   184  	if err != nil {
   185  		return nil, err
   186  	}
   187  	u := fmt.Sprintf("projects/%s/repository/archive", PathEscape(project))
   188  
   189  	// Set an optional format for the archive.
   190  	if opt != nil && opt.Format != nil {
   191  		u = fmt.Sprintf("%s.%s", u, *opt.Format)
   192  	}
   193  
   194  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   195  	if err != nil {
   196  		return nil, err
   197  	}
   198  
   199  	return s.client.Do(req, w)
   200  }
   201  
   202  // Compare represents the result of a comparison of branches, tags or commits.
   203  //
   204  // GitLab API docs:
   205  // https://docs.gitlab.com/ee/api/repositories.html#compare-branches-tags-or-commits
   206  type Compare struct {
   207  	Commit         *Commit   `json:"commit"`
   208  	Commits        []*Commit `json:"commits"`
   209  	Diffs          []*Diff   `json:"diffs"`
   210  	CompareTimeout bool      `json:"compare_timeout"`
   211  	CompareSameRef bool      `json:"compare_same_ref"`
   212  }
   213  
   214  func (c Compare) String() string {
   215  	return Stringify(c)
   216  }
   217  
   218  // CompareOptions represents the available Compare() options.
   219  //
   220  // GitLab API docs:
   221  // https://docs.gitlab.com/ee/api/repositories.html#compare-branches-tags-or-commits
   222  type CompareOptions struct {
   223  	From     *string `url:"from,omitempty" json:"from,omitempty"`
   224  	To       *string `url:"to,omitempty" json:"to,omitempty"`
   225  	Straight *bool   `url:"straight,omitempty" json:"straight,omitempty"`
   226  	Unidiff  *bool   `url:"unidiff,omitempty" json:"unidiff,omitempty"`
   227  }
   228  
   229  // Compare compares branches, tags or commits.
   230  //
   231  // GitLab API docs:
   232  // https://docs.gitlab.com/ee/api/repositories.html#compare-branches-tags-or-commits
   233  func (s *RepositoriesService) Compare(pid interface{}, opt *CompareOptions, options ...RequestOptionFunc) (*Compare, *Response, error) {
   234  	project, err := parseID(pid)
   235  	if err != nil {
   236  		return nil, nil, err
   237  	}
   238  	u := fmt.Sprintf("projects/%s/repository/compare", PathEscape(project))
   239  
   240  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   241  	if err != nil {
   242  		return nil, nil, err
   243  	}
   244  
   245  	c := new(Compare)
   246  	resp, err := s.client.Do(req, c)
   247  	if err != nil {
   248  		return nil, resp, err
   249  	}
   250  
   251  	return c, resp, nil
   252  }
   253  
   254  // Contributor represents a GitLap contributor.
   255  //
   256  // GitLab API docs: https://docs.gitlab.com/ee/api/repositories.html#contributors
   257  type Contributor struct {
   258  	Name      string `json:"name"`
   259  	Email     string `json:"email"`
   260  	Commits   int    `json:"commits"`
   261  	Additions int    `json:"additions"`
   262  	Deletions int    `json:"deletions"`
   263  }
   264  
   265  func (c Contributor) String() string {
   266  	return Stringify(c)
   267  }
   268  
   269  // ListContributorsOptions represents the available ListContributors() options.
   270  //
   271  // GitLab API docs: https://docs.gitlab.com/ee/api/repositories.html#contributors
   272  type ListContributorsOptions struct {
   273  	ListOptions
   274  	OrderBy *string `url:"order_by,omitempty" json:"order_by,omitempty"`
   275  	Sort    *string `url:"sort,omitempty" json:"sort,omitempty"`
   276  }
   277  
   278  // Contributors gets the repository contributors list.
   279  //
   280  // GitLab API docs: https://docs.gitlab.com/ee/api/repositories.html#contributors
   281  func (s *RepositoriesService) Contributors(pid interface{}, opt *ListContributorsOptions, options ...RequestOptionFunc) ([]*Contributor, *Response, error) {
   282  	project, err := parseID(pid)
   283  	if err != nil {
   284  		return nil, nil, err
   285  	}
   286  	u := fmt.Sprintf("projects/%s/repository/contributors", PathEscape(project))
   287  
   288  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   289  	if err != nil {
   290  		return nil, nil, err
   291  	}
   292  
   293  	var c []*Contributor
   294  	resp, err := s.client.Do(req, &c)
   295  	if err != nil {
   296  		return nil, resp, err
   297  	}
   298  
   299  	return c, resp, nil
   300  }
   301  
   302  // MergeBaseOptions represents the available MergeBase() options.
   303  //
   304  // GitLab API docs:
   305  // https://docs.gitlab.com/ee/api/repositories.html#merge-base
   306  type MergeBaseOptions struct {
   307  	Ref *[]string `url:"refs[],omitempty" json:"refs,omitempty"`
   308  }
   309  
   310  // MergeBase gets the common ancestor for 2 refs (commit SHAs, branch
   311  // names or tags).
   312  //
   313  // GitLab API docs:
   314  // https://docs.gitlab.com/ee/api/repositories.html#merge-base
   315  func (s *RepositoriesService) MergeBase(pid interface{}, opt *MergeBaseOptions, options ...RequestOptionFunc) (*Commit, *Response, error) {
   316  	project, err := parseID(pid)
   317  	if err != nil {
   318  		return nil, nil, err
   319  	}
   320  	u := fmt.Sprintf("projects/%s/repository/merge_base", PathEscape(project))
   321  
   322  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   323  	if err != nil {
   324  		return nil, nil, err
   325  	}
   326  
   327  	c := new(Commit)
   328  	resp, err := s.client.Do(req, c)
   329  	if err != nil {
   330  		return nil, resp, err
   331  	}
   332  
   333  	return c, resp, nil
   334  }
   335  
   336  // AddChangelogOptions represents the available AddChangelog() options.
   337  //
   338  // GitLab API docs:
   339  // https://docs.gitlab.com/ee/api/repositories.html#add-changelog-data-to-a-changelog-file
   340  type AddChangelogOptions struct {
   341  	Version    *string  `url:"version,omitempty" json:"version,omitempty"`
   342  	Branch     *string  `url:"branch,omitempty" json:"branch,omitempty"`
   343  	ConfigFile *string  `url:"config_file,omitempty" json:"config_file,omitempty"`
   344  	Date       *ISOTime `url:"date,omitempty" json:"date,omitempty"`
   345  	File       *string  `url:"file,omitempty" json:"file,omitempty"`
   346  	From       *string  `url:"from,omitempty" json:"from,omitempty"`
   347  	Message    *string  `url:"message,omitempty" json:"message,omitempty"`
   348  	To         *string  `url:"to,omitempty" json:"to,omitempty"`
   349  	Trailer    *string  `url:"trailer,omitempty" json:"trailer,omitempty"`
   350  }
   351  
   352  // AddChangelog generates changelog data based on commits in a repository.
   353  //
   354  // Gitlab API docs:
   355  // https://docs.gitlab.com/ee/api/repositories.html#add-changelog-data-to-a-changelog-file
   356  func (s *RepositoriesService) AddChangelog(pid interface{}, opt *AddChangelogOptions, options ...RequestOptionFunc) (*Response, error) {
   357  	project, err := parseID(pid)
   358  	if err != nil {
   359  		return nil, err
   360  	}
   361  	u := fmt.Sprintf("projects/%s/repository/changelog", PathEscape(project))
   362  
   363  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   364  	if err != nil {
   365  		return nil, err
   366  	}
   367  
   368  	return s.client.Do(req, nil)
   369  }
   370  
   371  // ChangelogData represents the generated changelog data.
   372  //
   373  // GitLab API docs:
   374  // https://docs.gitlab.com/ee/api/repositories.html#generate-changelog-data
   375  type ChangelogData struct {
   376  	Notes string `json:"notes"`
   377  }
   378  
   379  func (c ChangelogData) String() string {
   380  	return Stringify(c)
   381  }
   382  
   383  // GenerateChangelogDataOptions represents the available GenerateChangelogData()
   384  // options.
   385  //
   386  // GitLab API docs:
   387  // https://docs.gitlab.com/ee/api/repositories.html#generate-changelog-data
   388  type GenerateChangelogDataOptions struct {
   389  	Version    *string  `url:"version,omitempty" json:"version,omitempty"`
   390  	ConfigFile *string  `url:"config_file,omitempty" json:"config_file,omitempty"`
   391  	Date       *ISOTime `url:"date,omitempty" json:"date,omitempty"`
   392  	From       *string  `url:"from,omitempty" json:"from,omitempty"`
   393  	To         *string  `url:"to,omitempty" json:"to,omitempty"`
   394  	Trailer    *string  `url:"trailer,omitempty" json:"trailer,omitempty"`
   395  }
   396  
   397  // GenerateChangelogData generates changelog data based on commits in a
   398  // repository, without committing them to a changelog file.
   399  //
   400  // Gitlab API docs:
   401  // https://docs.gitlab.com/ee/api/repositories.html#generate-changelog-data
   402  func (s *RepositoriesService) GenerateChangelogData(pid interface{}, opt GenerateChangelogDataOptions, options ...RequestOptionFunc) (*ChangelogData, *Response, error) {
   403  	project, err := parseID(pid)
   404  	if err != nil {
   405  		return nil, nil, err
   406  	}
   407  	u := fmt.Sprintf("projects/%s/repository/changelog", project)
   408  
   409  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   410  	if err != nil {
   411  		return nil, nil, err
   412  	}
   413  
   414  	cd := new(ChangelogData)
   415  	resp, err := s.client.Do(req, cd)
   416  	if err != nil {
   417  		return nil, resp, err
   418  	}
   419  
   420  	return cd, resp, nil
   421  }
   422  

View as plain text