...

Source file src/github.com/xanzy/go-gitlab/deploy_keys.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  	"time"
    23  )
    24  
    25  // DeployKeysService handles communication with the keys related methods
    26  // of the GitLab API.
    27  //
    28  // GitLab API docs: https://docs.gitlab.com/ee/api/deploy_keys.html
    29  type DeployKeysService struct {
    30  	client *Client
    31  }
    32  
    33  // InstanceDeployKey represents a GitLab deploy key with the associated
    34  // projects it has write access to.
    35  type InstanceDeployKey struct {
    36  	ID                      int                 `json:"id"`
    37  	Title                   string              `json:"title"`
    38  	CreatedAt               *time.Time          `json:"created_at"`
    39  	Key                     string              `json:"key"`
    40  	Fingerprint             string              `json:"fingerprint"`
    41  	ProjectsWithWriteAccess []*DeployKeyProject `json:"projects_with_write_access"`
    42  }
    43  
    44  func (k InstanceDeployKey) String() string {
    45  	return Stringify(k)
    46  }
    47  
    48  // DeployKeyProject refers to a project an InstanceDeployKey has write access to.
    49  type DeployKeyProject struct {
    50  	ID                int        `json:"id"`
    51  	Description       string     `json:"description"`
    52  	Name              string     `json:"name"`
    53  	NameWithNamespace string     `json:"name_with_namespace"`
    54  	Path              string     `json:"path"`
    55  	PathWithNamespace string     `json:"path_with_namespace"`
    56  	CreatedAt         *time.Time `json:"created_at"`
    57  }
    58  
    59  func (k DeployKeyProject) String() string {
    60  	return Stringify(k)
    61  }
    62  
    63  // ProjectDeployKey represents a GitLab project deploy key.
    64  type ProjectDeployKey struct {
    65  	ID        int        `json:"id"`
    66  	Title     string     `json:"title"`
    67  	Key       string     `json:"key"`
    68  	CreatedAt *time.Time `json:"created_at"`
    69  	CanPush   bool       `json:"can_push"`
    70  }
    71  
    72  func (k ProjectDeployKey) String() string {
    73  	return Stringify(k)
    74  }
    75  
    76  // ListProjectDeployKeysOptions represents the available ListAllDeployKeys()
    77  // options.
    78  //
    79  // GitLab API docs:
    80  // https://docs.gitlab.com/ee/api/deploy_keys.html#list-all-deploy-keys
    81  type ListInstanceDeployKeysOptions struct {
    82  	ListOptions
    83  	Public *bool `url:"public,omitempty" json:"public,omitempty"`
    84  }
    85  
    86  // ListAllDeployKeys gets a list of all deploy keys
    87  //
    88  // GitLab API docs:
    89  // https://docs.gitlab.com/ee/api/deploy_keys.html#list-all-deploy-keys
    90  func (s *DeployKeysService) ListAllDeployKeys(opt *ListInstanceDeployKeysOptions, options ...RequestOptionFunc) ([]*InstanceDeployKey, *Response, error) {
    91  	req, err := s.client.NewRequest(http.MethodGet, "deploy_keys", opt, options)
    92  	if err != nil {
    93  		return nil, nil, err
    94  	}
    95  
    96  	var ks []*InstanceDeployKey
    97  	resp, err := s.client.Do(req, &ks)
    98  	if err != nil {
    99  		return nil, resp, err
   100  	}
   101  
   102  	return ks, resp, nil
   103  }
   104  
   105  // ListProjectDeployKeysOptions represents the available ListProjectDeployKeys()
   106  // options.
   107  //
   108  // GitLab API docs:
   109  // https://docs.gitlab.com/ee/api/deploy_keys.html#list-deploy-keys-for-project
   110  type ListProjectDeployKeysOptions ListOptions
   111  
   112  // ListProjectDeployKeys gets a list of a project's deploy keys
   113  //
   114  // GitLab API docs:
   115  // https://docs.gitlab.com/ee/api/deploy_keys.html#list-deploy-keys-for-project
   116  func (s *DeployKeysService) ListProjectDeployKeys(pid interface{}, opt *ListProjectDeployKeysOptions, options ...RequestOptionFunc) ([]*ProjectDeployKey, *Response, error) {
   117  	project, err := parseID(pid)
   118  	if err != nil {
   119  		return nil, nil, err
   120  	}
   121  	u := fmt.Sprintf("projects/%s/deploy_keys", PathEscape(project))
   122  
   123  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   124  	if err != nil {
   125  		return nil, nil, err
   126  	}
   127  
   128  	var ks []*ProjectDeployKey
   129  	resp, err := s.client.Do(req, &ks)
   130  	if err != nil {
   131  		return nil, resp, err
   132  	}
   133  
   134  	return ks, resp, nil
   135  }
   136  
   137  // GetDeployKey gets a single deploy key.
   138  //
   139  // GitLab API docs:
   140  // https://docs.gitlab.com/ee/api/deploy_keys.html#get-a-single-deploy-key
   141  func (s *DeployKeysService) GetDeployKey(pid interface{}, deployKey int, options ...RequestOptionFunc) (*ProjectDeployKey, *Response, error) {
   142  	project, err := parseID(pid)
   143  	if err != nil {
   144  		return nil, nil, err
   145  	}
   146  	u := fmt.Sprintf("projects/%s/deploy_keys/%d", PathEscape(project), deployKey)
   147  
   148  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   149  	if err != nil {
   150  		return nil, nil, err
   151  	}
   152  
   153  	k := new(ProjectDeployKey)
   154  	resp, err := s.client.Do(req, k)
   155  	if err != nil {
   156  		return nil, resp, err
   157  	}
   158  
   159  	return k, resp, nil
   160  }
   161  
   162  // AddDeployKeyOptions represents the available ADDDeployKey() options.
   163  //
   164  // GitLab API docs:
   165  // https://docs.gitlab.com/ee/api/deploy_keys.html#add-deploy-key
   166  type AddDeployKeyOptions struct {
   167  	Title   *string `url:"title,omitempty" json:"title,omitempty"`
   168  	Key     *string `url:"key,omitempty" json:"key,omitempty"`
   169  	CanPush *bool   `url:"can_push,omitempty" json:"can_push,omitempty"`
   170  }
   171  
   172  // AddDeployKey creates a new deploy key for a project. If deploy key already
   173  // exists in another project - it will be joined to project but only if
   174  // original one was is accessible by same user.
   175  //
   176  // GitLab API docs:
   177  // https://docs.gitlab.com/ee/api/deploy_keys.html#add-deploy-key
   178  func (s *DeployKeysService) AddDeployKey(pid interface{}, opt *AddDeployKeyOptions, options ...RequestOptionFunc) (*ProjectDeployKey, *Response, error) {
   179  	project, err := parseID(pid)
   180  	if err != nil {
   181  		return nil, nil, err
   182  	}
   183  	u := fmt.Sprintf("projects/%s/deploy_keys", PathEscape(project))
   184  
   185  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   186  	if err != nil {
   187  		return nil, nil, err
   188  	}
   189  
   190  	k := new(ProjectDeployKey)
   191  	resp, err := s.client.Do(req, k)
   192  	if err != nil {
   193  		return nil, resp, err
   194  	}
   195  
   196  	return k, resp, nil
   197  }
   198  
   199  // DeleteDeployKey deletes a deploy key from a project.
   200  //
   201  // GitLab API docs:
   202  // https://docs.gitlab.com/ee/api/deploy_keys.html#delete-deploy-key
   203  func (s *DeployKeysService) DeleteDeployKey(pid interface{}, deployKey int, options ...RequestOptionFunc) (*Response, error) {
   204  	project, err := parseID(pid)
   205  	if err != nil {
   206  		return nil, err
   207  	}
   208  	u := fmt.Sprintf("projects/%s/deploy_keys/%d", PathEscape(project), deployKey)
   209  
   210  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   211  	if err != nil {
   212  		return nil, err
   213  	}
   214  
   215  	return s.client.Do(req, nil)
   216  }
   217  
   218  // EnableDeployKey enables a deploy key.
   219  //
   220  // GitLab API docs:
   221  // https://docs.gitlab.com/ee/api/deploy_keys.html#enable-a-deploy-key
   222  func (s *DeployKeysService) EnableDeployKey(pid interface{}, deployKey int, options ...RequestOptionFunc) (*ProjectDeployKey, *Response, error) {
   223  	project, err := parseID(pid)
   224  	if err != nil {
   225  		return nil, nil, err
   226  	}
   227  	u := fmt.Sprintf("projects/%s/deploy_keys/%d/enable", PathEscape(project), deployKey)
   228  
   229  	req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
   230  	if err != nil {
   231  		return nil, nil, err
   232  	}
   233  
   234  	k := new(ProjectDeployKey)
   235  	resp, err := s.client.Do(req, k)
   236  	if err != nil {
   237  		return nil, resp, err
   238  	}
   239  
   240  	return k, resp, nil
   241  }
   242  
   243  // UpdateDeployKeyOptions represents the available UpdateDeployKey() options.
   244  //
   245  // GitLab API docs:
   246  // https://docs.gitlab.com/ee/api/deploy_keys.html#update-deploy-key
   247  type UpdateDeployKeyOptions struct {
   248  	Title   *string `url:"title,omitempty" json:"title,omitempty"`
   249  	CanPush *bool   `url:"can_push,omitempty" json:"can_push,omitempty"`
   250  }
   251  
   252  // UpdateDeployKey updates a deploy key for a project.
   253  //
   254  // GitLab API docs:
   255  // https://docs.gitlab.com/ee/api/deploy_keys.html#update-deploy-key
   256  func (s *DeployKeysService) UpdateDeployKey(pid interface{}, deployKey int, opt *UpdateDeployKeyOptions, options ...RequestOptionFunc) (*ProjectDeployKey, *Response, error) {
   257  	project, err := parseID(pid)
   258  	if err != nil {
   259  		return nil, nil, err
   260  	}
   261  	u := fmt.Sprintf("projects/%s/deploy_keys/%d", PathEscape(project), deployKey)
   262  
   263  	req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
   264  	if err != nil {
   265  		return nil, nil, err
   266  	}
   267  
   268  	k := new(ProjectDeployKey)
   269  	resp, err := s.client.Do(req, k)
   270  	if err != nil {
   271  		return nil, resp, err
   272  	}
   273  
   274  	return k, resp, nil
   275  }
   276  

View as plain text