...

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

Documentation: github.com/xanzy/go-gitlab

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

View as plain text