wikis.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. package gitlab
  15. import (
  16. "fmt"
  17. "net/url"
  18. )
  19. // WikisService handles communication with the wikis related methods of
  20. // the Gitlab API.
  21. //
  22. // GitLab API docs: https://docs.gitlab.com/ce/api/wikis.html
  23. type WikisService struct {
  24. client *Client
  25. }
  26. // WikiFormat represents the available wiki formats.
  27. //
  28. // GitLab API docs: https://docs.gitlab.com/ce/api/wikis.html
  29. type WikiFormat string
  30. // The available wiki formats.
  31. const (
  32. WikiFormatMarkdown WikiFormat = "markdown"
  33. WikiFormatRFoc WikiFormat = "rdoc"
  34. WikiFormatASCIIDoc WikiFormat = "asciidoc"
  35. )
  36. // Wiki represents a GitLab wiki.
  37. //
  38. // GitLab API docs: https://docs.gitlab.com/ce/api/wikis.html
  39. type Wiki struct {
  40. Content string `json:"content"`
  41. Format WikiFormat `json:"format"`
  42. Slug string `json:"slug"`
  43. Title string `json:"title"`
  44. }
  45. func (w Wiki) String() string {
  46. return Stringify(w)
  47. }
  48. // ListWikisOptions represents the available ListWikis options.
  49. //
  50. // GitLab API docs:
  51. // https://docs.gitlab.com/ce/api/wikis.html#list-wiki-pages
  52. type ListWikisOptions struct {
  53. WithContent *bool `url:"with_content,omitempty" json:"with_content,omitempty"`
  54. }
  55. // ListWikis lists all pages of the wiki of the given project id.
  56. // When with_content is set, it also returns the content of the pages.
  57. //
  58. // GitLab API docs:
  59. // https://docs.gitlab.com/ce/api/wikis.html#list-wiki-pages
  60. func (s *WikisService) ListWikis(pid interface{}, opt *ListWikisOptions, options ...OptionFunc) ([]*Wiki, *Response, error) {
  61. project, err := parseID(pid)
  62. if err != nil {
  63. return nil, nil, err
  64. }
  65. u := fmt.Sprintf("projects/%s/wikis", url.QueryEscape(project))
  66. req, err := s.client.NewRequest("GET", u, opt, options)
  67. if err != nil {
  68. return nil, nil, err
  69. }
  70. var w []*Wiki
  71. resp, err := s.client.Do(req, &w)
  72. if err != nil {
  73. return nil, resp, err
  74. }
  75. return w, resp, err
  76. }
  77. // GetWikiPage gets a wiki page for a given project.
  78. //
  79. // GitLab API docs:
  80. // https://docs.gitlab.com/ce/api/wikis.html#get-a-wiki-page
  81. func (s *WikisService) GetWikiPage(pid interface{}, slug string, options ...OptionFunc) (*Wiki, *Response, error) {
  82. project, err := parseID(pid)
  83. if err != nil {
  84. return nil, nil, err
  85. }
  86. u := fmt.Sprintf("projects/%s/wikis/%s", url.QueryEscape(project), url.QueryEscape(slug))
  87. req, err := s.client.NewRequest("GET", u, nil, options)
  88. if err != nil {
  89. return nil, nil, err
  90. }
  91. var w *Wiki
  92. resp, err := s.client.Do(req, &w)
  93. if err != nil {
  94. return nil, resp, err
  95. }
  96. return w, resp, err
  97. }
  98. // CreateWikiPageOptions represents options to CreateWikiPage.
  99. //
  100. // GitLab API docs:
  101. // https://docs.gitlab.com/ce/api/wikis.html#create-a-new-wiki-page
  102. type CreateWikiPageOptions struct {
  103. Content *string `url:"content" json:"content"`
  104. Title *string `url:"title" json:"title"`
  105. Format *string `url:"format,omitempty" json:"format,omitempty"`
  106. }
  107. // CreateWikiPage creates a new wiki page for the given repository with
  108. // the given title, slug, and content.
  109. //
  110. // GitLab API docs:
  111. // https://docs.gitlab.com/ce/api/wikis.html#create-a-new-wiki-page
  112. func (s *WikisService) CreateWikiPage(pid interface{}, opt *CreateWikiPageOptions, options ...OptionFunc) (*Wiki, *Response, error) {
  113. project, err := parseID(pid)
  114. if err != nil {
  115. return nil, nil, err
  116. }
  117. u := fmt.Sprintf("projects/%s/wikis", url.QueryEscape(project))
  118. req, err := s.client.NewRequest("POST", u, opt, options)
  119. if err != nil {
  120. return nil, nil, err
  121. }
  122. w := new(Wiki)
  123. resp, err := s.client.Do(req, w)
  124. if err != nil {
  125. return nil, resp, err
  126. }
  127. return w, resp, err
  128. }
  129. // EditWikiPageOptions represents options to EditWikiPage.
  130. //
  131. // GitLab API docs:
  132. // https://docs.gitlab.com/ce/api/wikis.html#edit-an-existing-wiki-page
  133. type EditWikiPageOptions struct {
  134. Content *string `url:"content" json:"content"`
  135. Title *string `url:"title" json:"title"`
  136. Format *string `url:"format,omitempty" json:"format,omitempty"`
  137. }
  138. // EditWikiPage Updates an existing wiki page. At least one parameter is
  139. // required to update the wiki page.
  140. //
  141. // GitLab API docs:
  142. // https://docs.gitlab.com/ce/api/wikis.html#edit-an-existing-wiki-page
  143. func (s *WikisService) EditWikiPage(pid interface{}, slug string, opt *EditWikiPageOptions, options ...OptionFunc) (*Wiki, *Response, error) {
  144. project, err := parseID(pid)
  145. if err != nil {
  146. return nil, nil, err
  147. }
  148. u := fmt.Sprintf("projects/%s/wikis/%s", url.QueryEscape(project), url.QueryEscape(slug))
  149. req, err := s.client.NewRequest("PUT", u, opt, options)
  150. if err != nil {
  151. return nil, nil, err
  152. }
  153. w := new(Wiki)
  154. resp, err := s.client.Do(req, w)
  155. if err != nil {
  156. return nil, resp, err
  157. }
  158. return w, resp, err
  159. }
  160. // DeleteWikiPage deletes a wiki page with a given slug.
  161. //
  162. // GitLab API docs:
  163. // https://docs.gitlab.com/ce/api/wikis.html#delete-a-wiki-page
  164. func (s *WikisService) DeleteWikiPage(pid interface{}, slug string, options ...OptionFunc) (*Response, error) {
  165. project, err := parseID(pid)
  166. if err != nil {
  167. return nil, err
  168. }
  169. u := fmt.Sprintf("projects/%s/wikis/%s", url.QueryEscape(project), url.QueryEscape(slug))
  170. req, err := s.client.NewRequest("DELETE", u, nil, options)
  171. if err != nil {
  172. return nil, err
  173. }
  174. return s.client.Do(req, nil)
  175. }