wikis.go

  1// Copyright 2017, Stany MARCEL
  2//
  3// Licensed under the Apache License, Version 2.0 (the "License");
  4// you may not use this file except in compliance with the License.
  5// You may obtain a copy of the License at
  6//
  7//     http://www.apache.org/licenses/LICENSE-2.0
  8//
  9// Unless required by applicable law or agreed to in writing, software
 10// distributed under the License is distributed on an "AS IS" BASIS,
 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12// See the License for the specific language governing permissions and
 13// limitations under the License.
 14
 15package gitlab
 16
 17import (
 18	"fmt"
 19	"net/url"
 20)
 21
 22// WikisService handles communication with the wikis related methods of
 23// the Gitlab API.
 24//
 25// GitLab API docs: https://docs.gitlab.com/ce/api/wikis.html
 26type WikisService struct {
 27	client *Client
 28}
 29
 30// WikiFormat represents the available wiki formats.
 31//
 32// GitLab API docs: https://docs.gitlab.com/ce/api/wikis.html
 33type WikiFormat string
 34
 35// The available wiki formats.
 36const (
 37	WikiFormatMarkdown WikiFormat = "markdown"
 38	WikiFormatRFoc     WikiFormat = "rdoc"
 39	WikiFormatASCIIDoc WikiFormat = "asciidoc"
 40)
 41
 42// Wiki represents a GitLab wiki.
 43//
 44// GitLab API docs: https://docs.gitlab.com/ce/api/wikis.html
 45type Wiki struct {
 46	Content string     `json:"content"`
 47	Format  WikiFormat `json:"format"`
 48	Slug    string     `json:"slug"`
 49	Title   string     `json:"title"`
 50}
 51
 52func (w Wiki) String() string {
 53	return Stringify(w)
 54}
 55
 56// ListWikisOptions represents the available ListWikis options.
 57//
 58// GitLab API docs:
 59// https://docs.gitlab.com/ce/api/wikis.html#list-wiki-pages
 60type ListWikisOptions struct {
 61	WithContent *bool `url:"with_content,omitempty" json:"with_content,omitempty"`
 62}
 63
 64// ListWikis lists all pages of the wiki of the given project id.
 65// When with_content is set, it also returns the content of the pages.
 66//
 67// GitLab API docs:
 68// https://docs.gitlab.com/ce/api/wikis.html#list-wiki-pages
 69func (s *WikisService) ListWikis(pid interface{}, opt *ListWikisOptions, options ...OptionFunc) ([]*Wiki, *Response, error) {
 70	project, err := parseID(pid)
 71	if err != nil {
 72		return nil, nil, err
 73	}
 74	u := fmt.Sprintf("projects/%s/wikis", pathEscape(project))
 75
 76	req, err := s.client.NewRequest("GET", u, opt, options)
 77	if err != nil {
 78		return nil, nil, err
 79	}
 80
 81	var w []*Wiki
 82	resp, err := s.client.Do(req, &w)
 83	if err != nil {
 84		return nil, resp, err
 85	}
 86
 87	return w, resp, err
 88}
 89
 90// GetWikiPage gets a wiki page for a given project.
 91//
 92// GitLab API docs:
 93// https://docs.gitlab.com/ce/api/wikis.html#get-a-wiki-page
 94func (s *WikisService) GetWikiPage(pid interface{}, slug string, options ...OptionFunc) (*Wiki, *Response, error) {
 95	project, err := parseID(pid)
 96	if err != nil {
 97		return nil, nil, err
 98	}
 99	u := fmt.Sprintf("projects/%s/wikis/%s", pathEscape(project), url.PathEscape(slug))
100
101	req, err := s.client.NewRequest("GET", u, nil, options)
102	if err != nil {
103		return nil, nil, err
104	}
105
106	var w *Wiki
107	resp, err := s.client.Do(req, &w)
108	if err != nil {
109		return nil, resp, err
110	}
111
112	return w, resp, err
113}
114
115// CreateWikiPageOptions represents options to CreateWikiPage.
116//
117// GitLab API docs:
118// https://docs.gitlab.com/ce/api/wikis.html#create-a-new-wiki-page
119type CreateWikiPageOptions struct {
120	Content *string `url:"content" json:"content"`
121	Title   *string `url:"title" json:"title"`
122	Format  *string `url:"format,omitempty" json:"format,omitempty"`
123}
124
125// CreateWikiPage creates a new wiki page for the given repository with
126// the given title, slug, and content.
127//
128// GitLab API docs:
129// https://docs.gitlab.com/ce/api/wikis.html#create-a-new-wiki-page
130func (s *WikisService) CreateWikiPage(pid interface{}, opt *CreateWikiPageOptions, options ...OptionFunc) (*Wiki, *Response, error) {
131	project, err := parseID(pid)
132	if err != nil {
133		return nil, nil, err
134	}
135	u := fmt.Sprintf("projects/%s/wikis", pathEscape(project))
136
137	req, err := s.client.NewRequest("POST", u, opt, options)
138	if err != nil {
139		return nil, nil, err
140	}
141
142	w := new(Wiki)
143	resp, err := s.client.Do(req, w)
144	if err != nil {
145		return nil, resp, err
146	}
147
148	return w, resp, err
149}
150
151// EditWikiPageOptions represents options to EditWikiPage.
152//
153// GitLab API docs:
154// https://docs.gitlab.com/ce/api/wikis.html#edit-an-existing-wiki-page
155type EditWikiPageOptions struct {
156	Content *string `url:"content" json:"content"`
157	Title   *string `url:"title" json:"title"`
158	Format  *string `url:"format,omitempty" json:"format,omitempty"`
159}
160
161// EditWikiPage Updates an existing wiki page. At least one parameter is
162// required to update the wiki page.
163//
164// GitLab API docs:
165// https://docs.gitlab.com/ce/api/wikis.html#edit-an-existing-wiki-page
166func (s *WikisService) EditWikiPage(pid interface{}, slug string, opt *EditWikiPageOptions, options ...OptionFunc) (*Wiki, *Response, error) {
167	project, err := parseID(pid)
168	if err != nil {
169		return nil, nil, err
170	}
171	u := fmt.Sprintf("projects/%s/wikis/%s", pathEscape(project), url.PathEscape(slug))
172
173	req, err := s.client.NewRequest("PUT", u, opt, options)
174	if err != nil {
175		return nil, nil, err
176	}
177
178	w := new(Wiki)
179	resp, err := s.client.Do(req, w)
180	if err != nil {
181		return nil, resp, err
182	}
183
184	return w, resp, err
185}
186
187// DeleteWikiPage deletes a wiki page with a given slug.
188//
189// GitLab API docs:
190// https://docs.gitlab.com/ce/api/wikis.html#delete-a-wiki-page
191func (s *WikisService) DeleteWikiPage(pid interface{}, slug string, options ...OptionFunc) (*Response, error) {
192	project, err := parseID(pid)
193	if err != nil {
194		return nil, err
195	}
196	u := fmt.Sprintf("projects/%s/wikis/%s", pathEscape(project), url.PathEscape(slug))
197
198	req, err := s.client.NewRequest("DELETE", u, nil, options)
199	if err != nil {
200		return nil, err
201	}
202
203	return s.client.Do(req, nil)
204}