...

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

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, Andrea Perizzato
     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  // ManagedLicensesService handles communication with the managed licenses
    25  // methods of the GitLab API.
    26  //
    27  // GitLab API docs: https://docs.gitlab.com/ee/api/managed_licenses.html
    28  type ManagedLicensesService struct {
    29  	client *Client
    30  }
    31  
    32  // ManagedLicense represents a managed license.
    33  //
    34  // GitLab API docs: https://docs.gitlab.com/ee/api/managed_licenses.html
    35  type ManagedLicense struct {
    36  	ID             int                        `json:"id"`
    37  	Name           string                     `json:"name"`
    38  	ApprovalStatus LicenseApprovalStatusValue `json:"approval_status"`
    39  }
    40  
    41  // ListManagedLicenses returns a list of managed licenses from a project.
    42  //
    43  // GitLab API docs:
    44  // https://docs.gitlab.com/ee/api/managed_licenses.html#list-managed-licenses
    45  func (s *ManagedLicensesService) ListManagedLicenses(pid interface{}, options ...RequestOptionFunc) ([]*ManagedLicense, *Response, error) {
    46  	project, err := parseID(pid)
    47  	if err != nil {
    48  		return nil, nil, err
    49  	}
    50  	u := fmt.Sprintf("projects/%s/managed_licenses", PathEscape(project))
    51  
    52  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
    53  	if err != nil {
    54  		return nil, nil, err
    55  	}
    56  
    57  	var mls []*ManagedLicense
    58  	resp, err := s.client.Do(req, &mls)
    59  	if err != nil {
    60  		return nil, resp, err
    61  	}
    62  
    63  	return mls, resp, nil
    64  }
    65  
    66  // GetManagedLicense returns an existing managed license.
    67  //
    68  // GitLab API docs:
    69  // https://docs.gitlab.com/ee/api/managed_licenses.html#show-an-existing-managed-license
    70  func (s *ManagedLicensesService) GetManagedLicense(pid, mlid interface{}, options ...RequestOptionFunc) (*ManagedLicense, *Response, error) {
    71  	project, err := parseID(pid)
    72  	if err != nil {
    73  		return nil, nil, err
    74  	}
    75  	license, err := parseID(mlid)
    76  	if err != nil {
    77  		return nil, nil, err
    78  	}
    79  	u := fmt.Sprintf("projects/%s/managed_licenses/%s", PathEscape(project), PathEscape(license))
    80  
    81  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
    82  	if err != nil {
    83  		return nil, nil, err
    84  	}
    85  
    86  	ml := new(ManagedLicense)
    87  	resp, err := s.client.Do(req, ml)
    88  	if err != nil {
    89  		return nil, resp, err
    90  	}
    91  
    92  	return ml, resp, nil
    93  }
    94  
    95  // AddManagedLicenseOptions represents the available AddManagedLicense() options.
    96  //
    97  // GitLab API docs:
    98  // https://docs.gitlab.com/ee/api/managed_licenses.html#create-a-new-managed-license
    99  type AddManagedLicenseOptions struct {
   100  	Name           *string                     `url:"name,omitempty" json:"name,omitempty"`
   101  	ApprovalStatus *LicenseApprovalStatusValue `url:"approval_status,omitempty" json:"approval_status,omitempty"`
   102  }
   103  
   104  // AddManagedLicense adds a managed license to a project.
   105  //
   106  // GitLab API docs:
   107  // https://docs.gitlab.com/ee/api/managed_licenses.html#create-a-new-managed-license
   108  func (s *ManagedLicensesService) AddManagedLicense(pid interface{}, opt *AddManagedLicenseOptions, options ...RequestOptionFunc) (*ManagedLicense, *Response, error) {
   109  	project, err := parseID(pid)
   110  	if err != nil {
   111  		return nil, nil, err
   112  	}
   113  	u := fmt.Sprintf("projects/%s/managed_licenses", PathEscape(project))
   114  
   115  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   116  	if err != nil {
   117  		return nil, nil, err
   118  	}
   119  
   120  	ml := new(ManagedLicense)
   121  	resp, err := s.client.Do(req, ml)
   122  	if err != nil {
   123  		return nil, resp, err
   124  	}
   125  
   126  	return ml, resp, nil
   127  }
   128  
   129  // DeleteManagedLicense deletes a managed license with a given ID.
   130  //
   131  // GitLab API docs:
   132  // https://docs.gitlab.com/ee/api/managed_licenses.html#delete-a-managed-license
   133  func (s *ManagedLicensesService) DeleteManagedLicense(pid, mlid interface{}, options ...RequestOptionFunc) (*Response, error) {
   134  	project, err := parseID(pid)
   135  	if err != nil {
   136  		return nil, err
   137  	}
   138  	license, err := parseID(mlid)
   139  	if err != nil {
   140  		return nil, err
   141  	}
   142  	u := fmt.Sprintf("projects/%s/managed_licenses/%s", PathEscape(project), PathEscape(license))
   143  
   144  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  
   149  	return s.client.Do(req, nil)
   150  }
   151  
   152  // EditManagedLicenceOptions represents the available EditManagedLicense() options.
   153  //
   154  // GitLab API docs:
   155  // https://docs.gitlab.com/ee/api/managed_licenses.html#edit-an-existing-managed-license
   156  type EditManagedLicenceOptions struct {
   157  	ApprovalStatus *LicenseApprovalStatusValue `url:"approval_status,omitempty" json:"approval_status,omitempty"`
   158  }
   159  
   160  // EditManagedLicense updates an existing managed license with a new approval
   161  // status.
   162  //
   163  // GitLab API docs:
   164  // https://docs.gitlab.com/ee/api/managed_licenses.html#edit-an-existing-managed-license
   165  func (s *ManagedLicensesService) EditManagedLicense(pid, mlid interface{}, opt *EditManagedLicenceOptions, options ...RequestOptionFunc) (*ManagedLicense, *Response, error) {
   166  	project, err := parseID(pid)
   167  	if err != nil {
   168  		return nil, nil, err
   169  	}
   170  	license, err := parseID(mlid)
   171  	if err != nil {
   172  		return nil, nil, err
   173  	}
   174  	u := fmt.Sprintf("projects/%s/managed_licenses/%s", PathEscape(project), PathEscape(license))
   175  
   176  	req, err := s.client.NewRequest(http.MethodPatch, u, opt, options)
   177  	if err != nil {
   178  		return nil, nil, err
   179  	}
   180  
   181  	ml := new(ManagedLicense)
   182  	resp, err := s.client.Do(req, ml)
   183  	if err != nil {
   184  		return nil, resp, err
   185  	}
   186  
   187  	return ml, resp, nil
   188  }
   189  

View as plain text