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