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 // CIYMLTemplatesService handles communication with the gitlab 25 // CI YML templates related methods of the GitLab API. 26 // 27 // GitLab API docs: 28 // https://docs.gitlab.com/ee/api/templates/gitlab_ci_ymls.html 29 type CIYMLTemplatesService struct { 30 client *Client 31 } 32 33 // CIYMLTemplate represents a GitLab CI YML template. 34 // 35 // GitLab API docs: 36 // https://docs.gitlab.com/ee/api/templates/gitlab_ci_ymls.html 37 type CIYMLTemplate struct { 38 Name string `json:"name"` 39 Content string `json:"content"` 40 } 41 42 // CIYMLTemplateListItem represents a GitLab CI YML template from the list. 43 // 44 // GitLab API docs: 45 // https://docs.gitlab.com/ee/api/templates/gitlab_ci_ymls.html 46 type CIYMLTemplateListItem struct { 47 Key string `json:"key"` 48 Name string `json:"name"` 49 } 50 51 // ListCIYMLTemplatesOptions represents the available ListAllTemplates() options. 52 // 53 // GitLab API docs: 54 // https://docs.gitlab.com/ee/api/templates/gitlab_ci_ymls.html#list-gitlab-ci-yaml-templates 55 type ListCIYMLTemplatesOptions ListOptions 56 57 // ListAllTemplates get all GitLab CI YML templates. 58 // 59 // GitLab API docs: 60 // https://docs.gitlab.com/ee/api/templates/gitlab_ci_ymls.html#list-gitlab-ci-yaml-templates 61 func (s *CIYMLTemplatesService) ListAllTemplates(opt *ListCIYMLTemplatesOptions, options ...RequestOptionFunc) ([]*CIYMLTemplateListItem, *Response, error) { 62 req, err := s.client.NewRequest(http.MethodGet, "templates/gitlab_ci_ymls", opt, options) 63 if err != nil { 64 return nil, nil, err 65 } 66 67 var cts []*CIYMLTemplateListItem 68 resp, err := s.client.Do(req, &cts) 69 if err != nil { 70 return nil, resp, err 71 } 72 73 return cts, resp, nil 74 } 75 76 // GetTemplate get a single GitLab CI YML template. 77 // 78 // GitLab API docs: 79 // https://docs.gitlab.com/ee/api/templates/gitlab_ci_ymls.html#single-gitlab-ci-yaml-template 80 func (s *CIYMLTemplatesService) GetTemplate(key string, options ...RequestOptionFunc) (*CIYMLTemplate, *Response, error) { 81 u := fmt.Sprintf("templates/gitlab_ci_ymls/%s", PathEscape(key)) 82 83 req, err := s.client.NewRequest(http.MethodGet, u, nil, options) 84 if err != nil { 85 return nil, nil, err 86 } 87 88 ct := new(CIYMLTemplate) 89 resp, err := s.client.Do(req, ct) 90 if err != nil { 91 return nil, resp, err 92 } 93 94 return ct, resp, nil 95 } 96