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