...

Source file src/github.com/xanzy/go-gitlab/protected_environments.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  )
    23  
    24  // ProtectedEnvironmentsService handles communication with the protected
    25  // environment methods of the GitLab API.
    26  //
    27  // GitLab API docs:
    28  // https://docs.gitlab.com/ee/api/protected_environments.html
    29  type ProtectedEnvironmentsService struct {
    30  	client *Client
    31  }
    32  
    33  // ProtectedEnvironment represents a protected environment.
    34  //
    35  // GitLab API docs:
    36  // https://docs.gitlab.com/ee/api/protected_environments.html
    37  type ProtectedEnvironment struct {
    38  	Name                  string                          `json:"name"`
    39  	DeployAccessLevels    []*EnvironmentAccessDescription `json:"deploy_access_levels"`
    40  	RequiredApprovalCount int                             `json:"required_approval_count"`
    41  	ApprovalRules         []*EnvironmentApprovalRule      `json:"approval_rules"`
    42  }
    43  
    44  // EnvironmentAccessDescription represents the access decription for a protected
    45  // environment.
    46  //
    47  // GitLab API docs:
    48  // https://docs.gitlab.com/ee/api/protected_environments.html
    49  type EnvironmentAccessDescription struct {
    50  	ID                     int              `json:"id"`
    51  	AccessLevel            AccessLevelValue `json:"access_level"`
    52  	AccessLevelDescription string           `json:"access_level_description"`
    53  	UserID                 int              `json:"user_id"`
    54  	GroupID                int              `json:"group_id"`
    55  	GroupInheritanceType   int              `json:"group_inheritance_type"`
    56  }
    57  
    58  // EnvironmentApprovalRule represents the approval rules for a protected
    59  // environment.
    60  //
    61  // GitLab API docs:
    62  // https://docs.gitlab.com/ee/api/protected_environments.html#protect-a-single-environment
    63  type EnvironmentApprovalRule struct {
    64  	ID                     int              `json:"id"`
    65  	UserID                 int              `json:"user_id"`
    66  	GroupID                int              `json:"group_id"`
    67  	AccessLevel            AccessLevelValue `json:"access_level"`
    68  	AccessLevelDescription string           `json:"access_level_description"`
    69  	RequiredApprovalCount  int              `json:"required_approvals"`
    70  	GroupInheritanceType   int              `json:"group_inheritance_type"`
    71  }
    72  
    73  // ListProtectedEnvironmentsOptions represents the available
    74  // ListProtectedEnvironments() options.
    75  //
    76  // GitLab API docs:
    77  // https://docs.gitlab.com/ee/api/protected_environments.html#list-protected-environments
    78  type ListProtectedEnvironmentsOptions ListOptions
    79  
    80  // ListProtectedEnvironments returns a list of protected environments from a
    81  // project.
    82  //
    83  // GitLab API docs:
    84  // https://docs.gitlab.com/ee/api/protected_environments.html#list-protected-environments
    85  func (s *ProtectedEnvironmentsService) ListProtectedEnvironments(pid interface{}, opt *ListProtectedEnvironmentsOptions, options ...RequestOptionFunc) ([]*ProtectedEnvironment, *Response, error) {
    86  	project, err := parseID(pid)
    87  	if err != nil {
    88  		return nil, nil, err
    89  	}
    90  	u := fmt.Sprintf("projects/%s/protected_environments", PathEscape(project))
    91  
    92  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
    93  	if err != nil {
    94  		return nil, nil, err
    95  	}
    96  
    97  	var pes []*ProtectedEnvironment
    98  	resp, err := s.client.Do(req, &pes)
    99  	if err != nil {
   100  		return nil, resp, err
   101  	}
   102  
   103  	return pes, resp, nil
   104  }
   105  
   106  // GetProtectedEnvironment returns a single protected environment or wildcard
   107  // protected environment.
   108  //
   109  // GitLab API docs:
   110  // https://docs.gitlab.com/ee/api/protected_environments.html#get-a-single-protected-environment
   111  func (s *ProtectedEnvironmentsService) GetProtectedEnvironment(pid interface{}, environment string, options ...RequestOptionFunc) (*ProtectedEnvironment, *Response, error) {
   112  	project, err := parseID(pid)
   113  	if err != nil {
   114  		return nil, nil, err
   115  	}
   116  	u := fmt.Sprintf("projects/%s/protected_environments/%s", PathEscape(project), PathEscape(environment))
   117  
   118  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   119  	if err != nil {
   120  		return nil, nil, err
   121  	}
   122  
   123  	pe := new(ProtectedEnvironment)
   124  	resp, err := s.client.Do(req, pe)
   125  	if err != nil {
   126  		return nil, resp, err
   127  	}
   128  
   129  	return pe, resp, nil
   130  }
   131  
   132  // ProtectRepositoryEnvironmentsOptions represents the available
   133  // ProtectRepositoryEnvironments() options.
   134  //
   135  // GitLab API docs:
   136  // https://docs.gitlab.com/ee/api/protected_environments.html#protect-a-single-environment
   137  type ProtectRepositoryEnvironmentsOptions struct {
   138  	Name                  *string                            `url:"name,omitempty" json:"name,omitempty"`
   139  	DeployAccessLevels    *[]*EnvironmentAccessOptions       `url:"deploy_access_levels,omitempty" json:"deploy_access_levels,omitempty"`
   140  	RequiredApprovalCount *int                               `url:"required_approval_count,omitempty" json:"required_approval_count,omitempty"`
   141  	ApprovalRules         *[]*EnvironmentApprovalRuleOptions `url:"approval_rules,omitempty" json:"approval_rules,omitempty"`
   142  }
   143  
   144  // EnvironmentAccessOptions represents the options for an access decription for
   145  // a protected environment.
   146  //
   147  // GitLab API docs:
   148  // https://docs.gitlab.com/ee/api/protected_environments.html#protect-a-single-environment
   149  type EnvironmentAccessOptions struct {
   150  	AccessLevel          *AccessLevelValue `url:"access_level,omitempty" json:"access_level,omitempty"`
   151  	UserID               *int              `url:"user_id,omitempty" json:"user_id,omitempty"`
   152  	GroupID              *int              `url:"group_id,omitempty" json:"group_id,omitempty"`
   153  	GroupInheritanceType *int              `url:"group_inheritance_type,omitempty" json:"group_inheritance_type,omitempty"`
   154  }
   155  
   156  // EnvironmentApprovalRuleOptions represents the approval rules for a protected
   157  // environment.
   158  //
   159  // GitLab API docs:
   160  // https://docs.gitlab.com/ee/api/protected_environments.html#protect-a-single-environment
   161  type EnvironmentApprovalRuleOptions struct {
   162  	UserID                 *int              `url:"user_id,omitempty" json:"user_id,omitempty"`
   163  	GroupID                *int              `url:"group_id,omitempty" json:"group_id,omitempty"`
   164  	AccessLevel            *AccessLevelValue `url:"access_level,omitempty" json:"access_level,omitempty"`
   165  	AccessLevelDescription *string           `url:"access_level_description,omitempty" json:"access_level_description,omitempty"`
   166  	RequiredApprovalCount  *int              `url:"required_approvals,omitempty" json:"required_approvals,omitempty"`
   167  	GroupInheritanceType   *int              `url:"group_inheritance_type,omitempty" json:"group_inheritance_type,omitempty"`
   168  }
   169  
   170  // ProtectRepositoryEnvironments protects a single repository environment or
   171  // several project repository environments using wildcard protected environment.
   172  //
   173  // GitLab API docs:
   174  // https://docs.gitlab.com/ee/api/protected_environments.html#protect-a-single-environment
   175  func (s *ProtectedEnvironmentsService) ProtectRepositoryEnvironments(pid interface{}, opt *ProtectRepositoryEnvironmentsOptions, options ...RequestOptionFunc) (*ProtectedEnvironment, *Response, error) {
   176  	project, err := parseID(pid)
   177  	if err != nil {
   178  		return nil, nil, err
   179  	}
   180  	u := fmt.Sprintf("projects/%s/protected_environments", PathEscape(project))
   181  
   182  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   183  	if err != nil {
   184  		return nil, nil, err
   185  	}
   186  
   187  	pe := new(ProtectedEnvironment)
   188  	resp, err := s.client.Do(req, pe)
   189  	if err != nil {
   190  		return nil, resp, err
   191  	}
   192  
   193  	return pe, resp, nil
   194  }
   195  
   196  // UpdateProtectedEnvironmentsOptions represents the available
   197  // UpdateProtectedEnvironments() options.
   198  //
   199  // GitLab API docs:
   200  // https://docs.gitlab.com/ee/api/protected_environments.html#update-a-protected-environment
   201  type UpdateProtectedEnvironmentsOptions struct {
   202  	Name                  *string                                  `url:"name,omitempty" json:"name,omitempty"`
   203  	DeployAccessLevels    *[]*UpdateEnvironmentAccessOptions       `url:"deploy_access_levels,omitempty" json:"deploy_access_levels,omitempty"`
   204  	RequiredApprovalCount *int                                     `url:"required_approval_count,omitempty" json:"required_approval_count,omitempty"`
   205  	ApprovalRules         *[]*UpdateEnvironmentApprovalRuleOptions `url:"approval_rules,omitempty" json:"approval_rules,omitempty"`
   206  }
   207  
   208  // UpdateEnvironmentAccessOptions represents the options for updates to an
   209  // access decription for a protected environment.
   210  //
   211  // GitLab API docs:
   212  // https://docs.gitlab.com/ee/api/protected_environments.html#update-a-protected-environment
   213  type UpdateEnvironmentAccessOptions struct {
   214  	AccessLevel          *AccessLevelValue `url:"access_level,omitempty" json:"access_level,omitempty"`
   215  	ID                   *int              `url:"id,omitempty" json:"id,omitempty"`
   216  	UserID               *int              `url:"user_id,omitempty" json:"user_id,omitempty"`
   217  	GroupID              *int              `url:"group_id,omitempty" json:"group_id,omitempty"`
   218  	GroupInheritanceType *int              `url:"group_inheritance_type,omitempty" json:"group_inheritance_type,omitempty"`
   219  	Destroy              *bool             `url:"_destroy,omitempty" json:"_destroy,omitempty"`
   220  }
   221  
   222  // UpdateEnvironmentApprovalRuleOptions represents the updates to the approval
   223  // rules for a protected environment.
   224  //
   225  // GitLab API docs:
   226  // https://docs.gitlab.com/ee/api/protected_environments.html#update-a-protected-environment
   227  type UpdateEnvironmentApprovalRuleOptions struct {
   228  	ID                     *int              `url:"id,omitempty" json:"id,omitempty"`
   229  	UserID                 *int              `url:"user_id,omitempty" json:"user_id,omitempty"`
   230  	GroupID                *int              `url:"group_id,omitempty" json:"group_id,omitempty"`
   231  	AccessLevel            *AccessLevelValue `url:"access_level,omitempty" json:"access_level,omitempty"`
   232  	AccessLevelDescription *string           `url:"access_level_description,omitempty" json:"access_level_description,omitempty"`
   233  	RequiredApprovalCount  *int              `url:"required_approvals,omitempty" json:"required_approvals,omitempty"`
   234  	GroupInheritanceType   *int              `url:"group_inheritance_type,omitempty" json:"group_inheritance_type,omitempty"`
   235  	Destroy                *bool             `url:"_destroy,omitempty" json:"_destroy,omitempty"`
   236  }
   237  
   238  // UpdateProtectedEnvironments updates a single repository environment or
   239  // several project repository environments using wildcard protected environment.
   240  //
   241  // GitLab API docs:
   242  // https://docs.gitlab.com/ee/api/protected_environments.html#update-a-protected-environment
   243  func (s *ProtectedEnvironmentsService) UpdateProtectedEnvironments(pid interface{}, environment string, opt *UpdateProtectedEnvironmentsOptions, options ...RequestOptionFunc) (*ProtectedEnvironment, *Response, error) {
   244  	project, err := parseID(pid)
   245  	if err != nil {
   246  		return nil, nil, err
   247  	}
   248  	u := fmt.Sprintf("projects/%s/protected_environments/%s", PathEscape(project), PathEscape(environment))
   249  
   250  	req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
   251  	if err != nil {
   252  		return nil, nil, err
   253  	}
   254  
   255  	pe := new(ProtectedEnvironment)
   256  	resp, err := s.client.Do(req, pe)
   257  	if err != nil {
   258  		return nil, resp, err
   259  	}
   260  
   261  	return pe, resp, nil
   262  }
   263  
   264  // UnprotectEnvironment unprotects the given protected environment or wildcard
   265  // protected environment.
   266  //
   267  // GitLab API docs:
   268  // https://docs.gitlab.com/ee/api/protected_environments.html#unprotect-a-single-environment
   269  func (s *ProtectedEnvironmentsService) UnprotectEnvironment(pid interface{}, environment string, options ...RequestOptionFunc) (*Response, error) {
   270  	project, err := parseID(pid)
   271  	if err != nil {
   272  		return nil, err
   273  	}
   274  	u := fmt.Sprintf("projects/%s/protected_environments/%s", PathEscape(project), PathEscape(environment))
   275  
   276  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   277  	if err != nil {
   278  		return nil, err
   279  	}
   280  
   281  	return s.client.Do(req, nil)
   282  }
   283  

View as plain text