snippets.go

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