...

Source file src/github.com/xanzy/go-gitlab/wikis.go

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, Stany MARCEL
     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  package gitlab
    17  
    18  import (
    19  	"fmt"
    20  	"net/http"
    21  	"net/url"
    22  )
    23  
    24  // WikisService handles communication with the wikis related methods of
    25  // the Gitlab API.
    26  //
    27  // GitLab API docs: https://docs.gitlab.com/ee/api/wikis.html
    28  type WikisService struct {
    29  	client *Client
    30  }
    31  
    32  // Wiki represents a GitLab wiki.
    33  //
    34  // GitLab API docs: https://docs.gitlab.com/ee/api/wikis.html
    35  type Wiki struct {
    36  	Content  string          `json:"content"`
    37  	Encoding string          `json:"encoding"`
    38  	Format   WikiFormatValue `json:"format"`
    39  	Slug     string          `json:"slug"`
    40  	Title    string          `json:"title"`
    41  }
    42  
    43  func (w Wiki) String() string {
    44  	return Stringify(w)
    45  }
    46  
    47  // ListWikisOptions represents the available ListWikis options.
    48  //
    49  // GitLab API docs:
    50  // https://docs.gitlab.com/ee/api/wikis.html#list-wiki-pages
    51  type ListWikisOptions struct {
    52  	WithContent *bool `url:"with_content,omitempty" json:"with_content,omitempty"`
    53  }
    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/ee/api/wikis.html#list-wiki-pages
    60  func (s *WikisService) ListWikis(pid interface{}, opt *ListWikisOptions, options ...RequestOptionFunc) ([]*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", PathEscape(project))
    66  
    67  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
    68  	if err != nil {
    69  		return nil, nil, err
    70  	}
    71  
    72  	var ws []*Wiki
    73  	resp, err := s.client.Do(req, &ws)
    74  	if err != nil {
    75  		return nil, resp, err
    76  	}
    77  
    78  	return ws, resp, nil
    79  }
    80  
    81  // GetWikiPageOptions represents options to GetWikiPage
    82  //
    83  // GitLab API docs:
    84  // https://docs.gitlab.com/ee/api/wikis.html#get-a-wiki-page
    85  type GetWikiPageOptions struct {
    86  	RenderHTML *bool   `url:"render_html,omitempty" json:"render_html,omitempty"`
    87  	Version    *string `url:"version,omitempty" json:"version,omitempty"`
    88  }
    89  
    90  // GetWikiPage gets a wiki page for a given project.
    91  //
    92  // GitLab API docs:
    93  // https://docs.gitlab.com/ee/api/wikis.html#get-a-wiki-page
    94  func (s *WikisService) GetWikiPage(pid interface{}, slug string, opt *GetWikiPageOptions, options ...RequestOptionFunc) (*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(http.MethodGet, u, opt, options)
   102  	if err != nil {
   103  		return nil, nil, err
   104  	}
   105  
   106  	w := new(Wiki)
   107  	resp, err := s.client.Do(req, w)
   108  	if err != nil {
   109  		return nil, resp, err
   110  	}
   111  
   112  	return w, resp, nil
   113  }
   114  
   115  // CreateWikiPageOptions represents options to CreateWikiPage.
   116  //
   117  // GitLab API docs:
   118  // https://docs.gitlab.com/ee/api/wikis.html#create-a-new-wiki-page
   119  type CreateWikiPageOptions struct {
   120  	Content *string          `url:"content,omitempty" json:"content,omitempty"`
   121  	Title   *string          `url:"title,omitempty" json:"title,omitempty"`
   122  	Format  *WikiFormatValue `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/ee/api/wikis.html#create-a-new-wiki-page
   130  func (s *WikisService) CreateWikiPage(pid interface{}, opt *CreateWikiPageOptions, options ...RequestOptionFunc) (*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(http.MethodPost, 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, nil
   149  }
   150  
   151  // EditWikiPageOptions represents options to EditWikiPage.
   152  //
   153  // GitLab API docs:
   154  // https://docs.gitlab.com/ee/api/wikis.html#edit-an-existing-wiki-page
   155  type EditWikiPageOptions struct {
   156  	Content *string          `url:"content,omitempty" json:"content,omitempty"`
   157  	Title   *string          `url:"title,omitempty" json:"title,omitempty"`
   158  	Format  *WikiFormatValue `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/ee/api/wikis.html#edit-an-existing-wiki-page
   166  func (s *WikisService) EditWikiPage(pid interface{}, slug string, opt *EditWikiPageOptions, options ...RequestOptionFunc) (*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(http.MethodPut, 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, nil
   185  }
   186  
   187  // DeleteWikiPage deletes a wiki page with a given slug.
   188  //
   189  // GitLab API docs:
   190  // https://docs.gitlab.com/ee/api/wikis.html#delete-a-wiki-page
   191  func (s *WikisService) DeleteWikiPage(pid interface{}, slug string, options ...RequestOptionFunc) (*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(http.MethodDelete, u, nil, options)
   199  	if err != nil {
   200  		return nil, err
   201  	}
   202  
   203  	return s.client.Do(req, nil)
   204  }
   205  

View as plain text