...

Source file src/github.com/xanzy/go-gitlab/project_templates.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  // ProjectTemplatesService handles communication with the project templates
    25  // related methods of the GitLab API.
    26  //
    27  // GitLab API docs: https://docs.gitlab.com/ee/api/project_templates.html
    28  type ProjectTemplatesService struct {
    29  	client *Client
    30  }
    31  
    32  // ProjectTemplate represents a GitLab ProjectTemplate.
    33  //
    34  // GitLab API docs: https://docs.gitlab.com/ee/api/project_templates.html
    35  type ProjectTemplate struct {
    36  	Key         string   `json:"key"`
    37  	Name        string   `json:"name"`
    38  	Nickname    string   `json:"nickname"`
    39  	Popular     bool     `json:"popular"`
    40  	HTMLURL     string   `json:"html_url"`
    41  	SourceURL   string   `json:"source_url"`
    42  	Description string   `json:"description"`
    43  	Conditions  []string `json:"conditions"`
    44  	Permissions []string `json:"permissions"`
    45  	Limitations []string `json:"limitations"`
    46  	Content     string   `json:"content"`
    47  }
    48  
    49  func (s ProjectTemplate) String() string {
    50  	return Stringify(s)
    51  }
    52  
    53  // ListProjectTemplatesOptions represents the available ListSnippets() options.
    54  //
    55  // GitLab API docs:
    56  // https://docs.gitlab.com/ee/api/project_templates.html#get-all-templates-of-a-particular-type
    57  type ListProjectTemplatesOptions struct {
    58  	ListOptions
    59  	ID   *int    `url:"id,omitempty" json:"id,omitempty"`
    60  	Type *string `url:"type,omitempty" json:"type,omitempty"`
    61  }
    62  
    63  // ListTemplates gets a list of project templates.
    64  //
    65  // GitLab API docs: https://docs.gitlab.com/ee/api/project_templates.html#get-all-templates-of-a-particular-type
    66  func (s *ProjectTemplatesService) ListTemplates(pid interface{}, templateType string, opt *ListProjectTemplatesOptions, options ...RequestOptionFunc) ([]*ProjectTemplate, *Response, error) {
    67  	project, err := parseID(pid)
    68  	if err != nil {
    69  		return nil, nil, err
    70  	}
    71  	u := fmt.Sprintf("projects/%s/templates/%s", PathEscape(project), templateType)
    72  
    73  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
    74  	if err != nil {
    75  		return nil, nil, err
    76  	}
    77  
    78  	var pt []*ProjectTemplate
    79  	resp, err := s.client.Do(req, &pt)
    80  	if err != nil {
    81  		return nil, resp, err
    82  	}
    83  
    84  	return pt, resp, nil
    85  }
    86  
    87  // GetProjectTemplate gets a single project template.
    88  //
    89  // GitLab API docs:
    90  // https://docs.gitlab.com/ee/api/project_templates.html#get-one-template-of-a-particular-type
    91  func (s *ProjectTemplatesService) GetProjectTemplate(pid interface{}, templateType string, templateName string, options ...RequestOptionFunc) (*ProjectTemplate, *Response, error) {
    92  	project, err := parseID(pid)
    93  	if err != nil {
    94  		return nil, nil, err
    95  	}
    96  	u := fmt.Sprintf("projects/%s/templates/%s/%s", PathEscape(project), templateType, templateName)
    97  
    98  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
    99  	if err != nil {
   100  		return nil, nil, err
   101  	}
   102  
   103  	ptd := new(ProjectTemplate)
   104  	resp, err := s.client.Do(req, ptd)
   105  	if err != nil {
   106  		return nil, resp, err
   107  	}
   108  
   109  	return ptd, resp, nil
   110  }
   111  

View as plain text