...

Source file src/github.com/xanzy/go-gitlab/branches.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  	"fmt"
    21  	"net/http"
    22  	"net/url"
    23  )
    24  
    25  // BranchesService handles communication with the branch related methods
    26  // of the GitLab API.
    27  //
    28  // GitLab API docs: https://docs.gitlab.com/ee/api/branches.html
    29  type BranchesService struct {
    30  	client *Client
    31  }
    32  
    33  // Branch represents a GitLab branch.
    34  //
    35  // GitLab API docs: https://docs.gitlab.com/ee/api/branches.html
    36  type Branch struct {
    37  	Commit             *Commit `json:"commit"`
    38  	Name               string  `json:"name"`
    39  	Protected          bool    `json:"protected"`
    40  	Merged             bool    `json:"merged"`
    41  	Default            bool    `json:"default"`
    42  	CanPush            bool    `json:"can_push"`
    43  	DevelopersCanPush  bool    `json:"developers_can_push"`
    44  	DevelopersCanMerge bool    `json:"developers_can_merge"`
    45  	WebURL             string  `json:"web_url"`
    46  }
    47  
    48  func (b Branch) String() string {
    49  	return Stringify(b)
    50  }
    51  
    52  // ListBranchesOptions represents the available ListBranches() options.
    53  //
    54  // GitLab API docs:
    55  // https://docs.gitlab.com/ee/api/branches.html#list-repository-branches
    56  type ListBranchesOptions struct {
    57  	ListOptions
    58  	Search *string `url:"search,omitempty" json:"search,omitempty"`
    59  	Regex  *string `url:"regex,omitempty" json:"regex,omitempty"`
    60  }
    61  
    62  // ListBranches gets a list of repository branches from a project, sorted by
    63  // name alphabetically.
    64  //
    65  // GitLab API docs:
    66  // https://docs.gitlab.com/ee/api/branches.html#list-repository-branches
    67  func (s *BranchesService) ListBranches(pid interface{}, opts *ListBranchesOptions, options ...RequestOptionFunc) ([]*Branch, *Response, error) {
    68  	project, err := parseID(pid)
    69  	if err != nil {
    70  		return nil, nil, err
    71  	}
    72  	u := fmt.Sprintf("projects/%s/repository/branches", PathEscape(project))
    73  
    74  	req, err := s.client.NewRequest(http.MethodGet, u, opts, options)
    75  	if err != nil {
    76  		return nil, nil, err
    77  	}
    78  
    79  	var b []*Branch
    80  	resp, err := s.client.Do(req, &b)
    81  	if err != nil {
    82  		return nil, resp, err
    83  	}
    84  
    85  	return b, resp, nil
    86  }
    87  
    88  // GetBranch gets a single project repository branch.
    89  //
    90  // GitLab API docs:
    91  // https://docs.gitlab.com/ee/api/branches.html#get-single-repository-branch
    92  func (s *BranchesService) GetBranch(pid interface{}, branch string, options ...RequestOptionFunc) (*Branch, *Response, error) {
    93  	project, err := parseID(pid)
    94  	if err != nil {
    95  		return nil, nil, err
    96  	}
    97  	u := fmt.Sprintf("projects/%s/repository/branches/%s", PathEscape(project), url.PathEscape(branch))
    98  
    99  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   100  	if err != nil {
   101  		return nil, nil, err
   102  	}
   103  
   104  	b := new(Branch)
   105  	resp, err := s.client.Do(req, b)
   106  	if err != nil {
   107  		return nil, resp, err
   108  	}
   109  
   110  	return b, resp, nil
   111  }
   112  
   113  // ProtectBranchOptions represents the available ProtectBranch() options.
   114  //
   115  // GitLab API docs:
   116  // https://docs.gitlab.com/ee/api/branches.html#protect-repository-branch
   117  type ProtectBranchOptions struct {
   118  	DevelopersCanPush  *bool `url:"developers_can_push,omitempty" json:"developers_can_push,omitempty"`
   119  	DevelopersCanMerge *bool `url:"developers_can_merge,omitempty" json:"developers_can_merge,omitempty"`
   120  }
   121  
   122  // ProtectBranch protects a single project repository branch. This is an
   123  // idempotent function, protecting an already protected repository branch
   124  // still returns a 200 OK status code.
   125  //
   126  // Deprecated: This endpoint has been replaced by
   127  // ProtectedBranchesService.ProtectRepositoryBranches()
   128  //
   129  // GitLab API docs:
   130  // https://docs.gitlab.com/ee/api/branches.html#protect-repository-branch
   131  func (s *BranchesService) ProtectBranch(pid interface{}, branch string, opts *ProtectBranchOptions, options ...RequestOptionFunc) (*Branch, *Response, error) {
   132  	project, err := parseID(pid)
   133  	if err != nil {
   134  		return nil, nil, err
   135  	}
   136  	u := fmt.Sprintf("projects/%s/repository/branches/%s/protect", PathEscape(project), url.PathEscape(branch))
   137  
   138  	req, err := s.client.NewRequest(http.MethodPut, u, opts, options)
   139  	if err != nil {
   140  		return nil, nil, err
   141  	}
   142  
   143  	b := new(Branch)
   144  	resp, err := s.client.Do(req, b)
   145  	if err != nil {
   146  		return nil, resp, err
   147  	}
   148  
   149  	return b, resp, nil
   150  }
   151  
   152  // UnprotectBranch unprotects a single project repository branch. This is an
   153  // idempotent function, unprotecting an already unprotected repository branch
   154  // still returns a 200 OK status code.
   155  //
   156  // Deprecated: This endpoint has been replaced by
   157  // ProtectedBranchesService.UnprotectRepositoryBranches()
   158  //
   159  // GitLab API docs:
   160  // https://docs.gitlab.com/ee/api/branches.html#unprotect-repository-branch
   161  func (s *BranchesService) UnprotectBranch(pid interface{}, branch string, options ...RequestOptionFunc) (*Branch, *Response, error) {
   162  	project, err := parseID(pid)
   163  	if err != nil {
   164  		return nil, nil, err
   165  	}
   166  	u := fmt.Sprintf("projects/%s/repository/branches/%s/unprotect", PathEscape(project), url.PathEscape(branch))
   167  
   168  	req, err := s.client.NewRequest(http.MethodPut, u, nil, options)
   169  	if err != nil {
   170  		return nil, nil, err
   171  	}
   172  
   173  	b := new(Branch)
   174  	resp, err := s.client.Do(req, b)
   175  	if err != nil {
   176  		return nil, resp, err
   177  	}
   178  
   179  	return b, resp, nil
   180  }
   181  
   182  // CreateBranchOptions represents the available CreateBranch() options.
   183  //
   184  // GitLab API docs:
   185  // https://docs.gitlab.com/ee/api/branches.html#create-repository-branch
   186  type CreateBranchOptions struct {
   187  	Branch *string `url:"branch,omitempty" json:"branch,omitempty"`
   188  	Ref    *string `url:"ref,omitempty" json:"ref,omitempty"`
   189  }
   190  
   191  // CreateBranch creates branch from commit SHA or existing branch.
   192  //
   193  // GitLab API docs:
   194  // https://docs.gitlab.com/ee/api/branches.html#create-repository-branch
   195  func (s *BranchesService) CreateBranch(pid interface{}, opt *CreateBranchOptions, options ...RequestOptionFunc) (*Branch, *Response, error) {
   196  	project, err := parseID(pid)
   197  	if err != nil {
   198  		return nil, nil, err
   199  	}
   200  	u := fmt.Sprintf("projects/%s/repository/branches", PathEscape(project))
   201  
   202  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   203  	if err != nil {
   204  		return nil, nil, err
   205  	}
   206  
   207  	b := new(Branch)
   208  	resp, err := s.client.Do(req, b)
   209  	if err != nil {
   210  		return nil, resp, err
   211  	}
   212  
   213  	return b, resp, nil
   214  }
   215  
   216  // DeleteBranch deletes an existing branch.
   217  //
   218  // GitLab API docs:
   219  // https://docs.gitlab.com/ee/api/branches.html#delete-repository-branch
   220  func (s *BranchesService) DeleteBranch(pid interface{}, branch string, options ...RequestOptionFunc) (*Response, error) {
   221  	project, err := parseID(pid)
   222  	if err != nil {
   223  		return nil, err
   224  	}
   225  	u := fmt.Sprintf("projects/%s/repository/branches/%s", PathEscape(project), url.PathEscape(branch))
   226  
   227  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   228  	if err != nil {
   229  		return nil, err
   230  	}
   231  
   232  	return s.client.Do(req, nil)
   233  }
   234  
   235  // DeleteMergedBranches deletes all branches that are merged into the project's default branch.
   236  //
   237  // GitLab API docs:
   238  // https://docs.gitlab.com/ee/api/branches.html#delete-merged-branches
   239  func (s *BranchesService) DeleteMergedBranches(pid interface{}, options ...RequestOptionFunc) (*Response, error) {
   240  	project, err := parseID(pid)
   241  	if err != nil {
   242  		return nil, err
   243  	}
   244  	u := fmt.Sprintf("projects/%s/repository/merged_branches", PathEscape(project))
   245  
   246  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   247  	if err != nil {
   248  		return nil, err
   249  	}
   250  
   251  	return s.client.Do(req, nil)
   252  }
   253  

View as plain text