1//
2// Copyright 2017, 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 "bytes"
21 "fmt"
22)
23
24// ProjectSnippetsService handles communication with the project snippets
25// related methods of the GitLab API.
26//
27// GitLab API docs: https://docs.gitlab.com/ce/api/project_snippets.html
28type ProjectSnippetsService struct {
29 client *Client
30}
31
32// ListProjectSnippetsOptions represents the available ListSnippets() options.
33//
34// GitLab API docs: https://docs.gitlab.com/ce/api/project_snippets.html#list-snippets
35type ListProjectSnippetsOptions ListOptions
36
37// ListSnippets gets a list of project snippets.
38//
39// GitLab API docs: https://docs.gitlab.com/ce/api/project_snippets.html#list-snippets
40func (s *ProjectSnippetsService) ListSnippets(pid interface{}, opt *ListProjectSnippetsOptions, options ...OptionFunc) ([]*Snippet, *Response, error) {
41 project, err := parseID(pid)
42 if err != nil {
43 return nil, nil, err
44 }
45 u := fmt.Sprintf("projects/%s/snippets", pathEscape(project))
46
47 req, err := s.client.NewRequest("GET", u, opt, options)
48 if err != nil {
49 return nil, nil, err
50 }
51
52 var ps []*Snippet
53 resp, err := s.client.Do(req, &ps)
54 if err != nil {
55 return nil, resp, err
56 }
57
58 return ps, resp, err
59}
60
61// GetSnippet gets a single project snippet
62//
63// GitLab API docs:
64// https://docs.gitlab.com/ce/api/project_snippets.html#single-snippet
65func (s *ProjectSnippetsService) GetSnippet(pid interface{}, snippet int, options ...OptionFunc) (*Snippet, *Response, error) {
66 project, err := parseID(pid)
67 if err != nil {
68 return nil, nil, err
69 }
70 u := fmt.Sprintf("projects/%s/snippets/%d", pathEscape(project), snippet)
71
72 req, err := s.client.NewRequest("GET", u, nil, options)
73 if err != nil {
74 return nil, nil, err
75 }
76
77 ps := new(Snippet)
78 resp, err := s.client.Do(req, ps)
79 if err != nil {
80 return nil, resp, err
81 }
82
83 return ps, resp, err
84}
85
86// CreateProjectSnippetOptions represents the available CreateSnippet() options.
87//
88// GitLab API docs:
89// https://docs.gitlab.com/ce/api/project_snippets.html#create-new-snippet
90type CreateProjectSnippetOptions struct {
91 Title *string `url:"title,omitempty" json:"title,omitempty"`
92 FileName *string `url:"file_name,omitempty" json:"file_name,omitempty"`
93 Description *string `url:"description,omitempty" json:"description,omitempty"`
94 Code *string `url:"code,omitempty" json:"code,omitempty"`
95 Visibility *VisibilityValue `url:"visibility,omitempty" json:"visibility,omitempty"`
96}
97
98// CreateSnippet creates a new project snippet. The user must have permission
99// to create new snippets.
100//
101// GitLab API docs:
102// https://docs.gitlab.com/ce/api/project_snippets.html#create-new-snippet
103func (s *ProjectSnippetsService) CreateSnippet(pid interface{}, opt *CreateProjectSnippetOptions, options ...OptionFunc) (*Snippet, *Response, error) {
104 project, err := parseID(pid)
105 if err != nil {
106 return nil, nil, err
107 }
108 u := fmt.Sprintf("projects/%s/snippets", pathEscape(project))
109
110 req, err := s.client.NewRequest("POST", u, opt, options)
111 if err != nil {
112 return nil, nil, err
113 }
114
115 ps := new(Snippet)
116 resp, err := s.client.Do(req, ps)
117 if err != nil {
118 return nil, resp, err
119 }
120
121 return ps, resp, err
122}
123
124// UpdateProjectSnippetOptions represents the available UpdateSnippet() options.
125//
126// GitLab API docs:
127// https://docs.gitlab.com/ce/api/project_snippets.html#update-snippet
128type UpdateProjectSnippetOptions struct {
129 Title *string `url:"title,omitempty" json:"title,omitempty"`
130 FileName *string `url:"file_name,omitempty" json:"file_name,omitempty"`
131 Description *string `url:"description,omitempty" json:"description,omitempty"`
132 Code *string `url:"code,omitempty" json:"code,omitempty"`
133 Visibility *VisibilityValue `url:"visibility,omitempty" json:"visibility,omitempty"`
134}
135
136// UpdateSnippet updates an existing project snippet. The user must have
137// permission to change an existing snippet.
138//
139// GitLab API docs:
140// https://docs.gitlab.com/ce/api/project_snippets.html#update-snippet
141func (s *ProjectSnippetsService) UpdateSnippet(pid interface{}, snippet int, opt *UpdateProjectSnippetOptions, options ...OptionFunc) (*Snippet, *Response, error) {
142 project, err := parseID(pid)
143 if err != nil {
144 return nil, nil, err
145 }
146 u := fmt.Sprintf("projects/%s/snippets/%d", pathEscape(project), snippet)
147
148 req, err := s.client.NewRequest("PUT", u, opt, options)
149 if err != nil {
150 return nil, nil, err
151 }
152
153 ps := new(Snippet)
154 resp, err := s.client.Do(req, ps)
155 if err != nil {
156 return nil, resp, err
157 }
158
159 return ps, resp, err
160}
161
162// DeleteSnippet deletes an existing project snippet. This is an idempotent
163// function and deleting a non-existent snippet still returns a 200 OK status
164// code.
165//
166// GitLab API docs:
167// https://docs.gitlab.com/ce/api/project_snippets.html#delete-snippet
168func (s *ProjectSnippetsService) DeleteSnippet(pid interface{}, snippet int, options ...OptionFunc) (*Response, error) {
169 project, err := parseID(pid)
170 if err != nil {
171 return nil, err
172 }
173 u := fmt.Sprintf("projects/%s/snippets/%d", pathEscape(project), snippet)
174
175 req, err := s.client.NewRequest("DELETE", u, nil, options)
176 if err != nil {
177 return nil, err
178 }
179
180 return s.client.Do(req, nil)
181}
182
183// SnippetContent returns the raw project snippet as plain text.
184//
185// GitLab API docs:
186// https://docs.gitlab.com/ce/api/project_snippets.html#snippet-content
187func (s *ProjectSnippetsService) SnippetContent(pid interface{}, snippet int, options ...OptionFunc) ([]byte, *Response, error) {
188 project, err := parseID(pid)
189 if err != nil {
190 return nil, nil, err
191 }
192 u := fmt.Sprintf("projects/%s/snippets/%d/raw", pathEscape(project), snippet)
193
194 req, err := s.client.NewRequest("GET", u, nil, options)
195 if err != nil {
196 return nil, nil, err
197 }
198
199 var b bytes.Buffer
200 resp, err := s.client.Do(req, &b)
201 if err != nil {
202 return nil, resp, err
203 }
204
205 return b.Bytes(), resp, err
206}