...

Source file src/github.com/xanzy/go-gitlab/draft_notes.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  	"fmt"
    21  	"net/http"
    22  )
    23  
    24  type DraftNote struct {
    25  	ID                int           `json:"id"`
    26  	AuthorID          int           `json:"author_id"`
    27  	MergeRequestID    int           `json:"merge_request_id"`
    28  	ResolveDiscussion bool          `json:"resolve_discussion"`
    29  	DiscussionID      string        `json:"discussion_id"`
    30  	Note              string        `json:"note"`
    31  	CommitID          string        `json:"commit_id"`
    32  	LineCode          string        `json:"line_code"`
    33  	Position          *NotePosition `json:"position"`
    34  }
    35  
    36  // DraftNotesService handles communication with the draft notes related methods
    37  // of the GitLab API.
    38  //
    39  // GitLab API docs:
    40  // https://docs.gitlab.com/ee/api/draft_notes.html#list-all-merge-request-draft-notes
    41  type DraftNotesService struct {
    42  	client *Client
    43  }
    44  
    45  // ListDraftNotesOptions represents the available ListDraftNotes()
    46  // options.
    47  //
    48  // GitLab API docs:
    49  // https://docs.gitlab.com/ee/api/draft_notes.html#list-all-merge-request-draft-notes
    50  type ListDraftNotesOptions struct {
    51  	ListOptions
    52  	OrderBy *string `url:"order_by,omitempty" json:"order_by,omitempty"`
    53  	Sort    *string `url:"sort,omitempty" json:"sort,omitempty"`
    54  }
    55  
    56  // ListDraftNotes gets a list of all draft notes for a merge request.
    57  //
    58  // Gitlab API docs:
    59  // https://docs.gitlab.com/ee/api/draft_notes.html#list-all-merge-request-draft-notes
    60  func (s *DraftNotesService) ListDraftNotes(pid interface{}, mergeRequest int, opt *ListDraftNotesOptions, options ...RequestOptionFunc) ([]*DraftNote, *Response, error) {
    61  	project, err := parseID(pid)
    62  	if err != nil {
    63  		return nil, nil, err
    64  	}
    65  	u := fmt.Sprintf("projects/%s/merge_requests/%d/draft_notes", PathEscape(project), mergeRequest)
    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 n []*DraftNote
    73  	resp, err := s.client.Do(req, &n)
    74  	if err != nil {
    75  		return nil, resp, err
    76  	}
    77  
    78  	return n, resp, nil
    79  }
    80  
    81  // GetDraftNote gets a single draft note for a merge request.
    82  //
    83  // Gitlab API docs:
    84  // https://docs.gitlab.com/ee/api/draft_notes.html#get-a-single-draft-note
    85  func (s *DraftNotesService) GetDraftNote(pid interface{}, mergeRequest int, note int, options ...RequestOptionFunc) (*DraftNote, *Response, error) {
    86  	project, err := parseID(pid)
    87  	if err != nil {
    88  		return nil, nil, err
    89  	}
    90  	u := fmt.Sprintf("projects/%s/merge_requests/%d/draft_notes/%d", PathEscape(project), mergeRequest, note)
    91  
    92  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
    93  	if err != nil {
    94  		return nil, nil, err
    95  	}
    96  
    97  	n := new(DraftNote)
    98  	resp, err := s.client.Do(req, &n)
    99  	if err != nil {
   100  		return nil, resp, err
   101  	}
   102  
   103  	return n, resp, nil
   104  }
   105  
   106  // CreateDraftNoteOptions represents the available CreateDraftNote()
   107  // options.
   108  //
   109  // Gitlab API docs:
   110  // https://docs.gitlab.com/ee/api/draft_notes.html#create-a-draft-note
   111  type CreateDraftNoteOptions struct {
   112  	Note                  *string          `url:"note" json:"note"`
   113  	CommitID              *string          `url:"commit_id,omitempty" json:"commit_id,omitempty"`
   114  	InReplyToDiscussionID *string          `url:"in_reply_to_discussion_id,omitempty" json:"in_reply_to_discussion_id,omitempty"`
   115  	ResolveDiscussion     *bool            `url:"resolve_discussion,omitempty" json:"resolve_discussion,omitempty"`
   116  	Position              *PositionOptions `url:"position,omitempty" json:"position,omitempty"`
   117  }
   118  
   119  // CreateDraftNote creates a draft note for a merge request.
   120  //
   121  // Gitlab API docs:
   122  // https://docs.gitlab.com/ee/api/draft_notes.html#create-a-draft-note
   123  func (s *DraftNotesService) CreateDraftNote(pid interface{}, mergeRequest int, opt *CreateDraftNoteOptions, options ...RequestOptionFunc) (*DraftNote, *Response, error) {
   124  	project, err := parseID(pid)
   125  	if err != nil {
   126  		return nil, nil, err
   127  	}
   128  	u := fmt.Sprintf("projects/%s/merge_requests/%d/draft_notes", PathEscape(project), mergeRequest)
   129  
   130  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   131  	if err != nil {
   132  		return nil, nil, err
   133  	}
   134  
   135  	n := new(DraftNote)
   136  	resp, err := s.client.Do(req, &n)
   137  	if err != nil {
   138  		return nil, resp, err
   139  	}
   140  
   141  	return n, resp, nil
   142  }
   143  
   144  // UpdateDraftNoteOptions represents the available UpdateDraftNote()
   145  // options.
   146  //
   147  // Gitlab API docs:
   148  // https://docs.gitlab.com/ee/api/draft_notes.html#create-a-draft-note
   149  type UpdateDraftNoteOptions struct {
   150  	Note     *string          `url:"note,omitempty" json:"note,omitempty"`
   151  	Position *PositionOptions `url:"position,omitempty" json:"position,omitempty"`
   152  }
   153  
   154  // UpdateDraftNote updates a draft note for a merge request.
   155  //
   156  // Gitlab API docs: https://docs.gitlab.com/ee/api/draft_notes.html#create-a-draft-note
   157  func (s *DraftNotesService) UpdateDraftNote(pid interface{}, mergeRequest int, note int, opt *UpdateDraftNoteOptions, options ...RequestOptionFunc) (*DraftNote, *Response, error) {
   158  	project, err := parseID(pid)
   159  	if err != nil {
   160  		return nil, nil, err
   161  	}
   162  	u := fmt.Sprintf("projects/%s/merge_requests/%d/draft_notes/%d", PathEscape(project), mergeRequest, note)
   163  
   164  	req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
   165  	if err != nil {
   166  		return nil, nil, err
   167  	}
   168  
   169  	n := new(DraftNote)
   170  	resp, err := s.client.Do(req, &n)
   171  	if err != nil {
   172  		return nil, resp, err
   173  	}
   174  
   175  	return n, resp, nil
   176  }
   177  
   178  // DeleteDraftNote deletes a single draft note for a merge request.
   179  //
   180  // Gitlab API docs:
   181  // https://docs.gitlab.com/ee/api/draft_notes.html#delete-a-draft-note
   182  func (s *DraftNotesService) DeleteDraftNote(pid interface{}, mergeRequest int, note int, options ...RequestOptionFunc) (*Response, error) {
   183  	project, err := parseID(pid)
   184  	if err != nil {
   185  		return nil, err
   186  	}
   187  	u := fmt.Sprintf("projects/%s/merge_requests/%d/draft_notes/%d", PathEscape(project), mergeRequest, note)
   188  
   189  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   190  	if err != nil {
   191  		return nil, err
   192  	}
   193  
   194  	return s.client.Do(req, nil)
   195  }
   196  
   197  // PublishDraftNote publishes a single draft note for a merge request.
   198  //
   199  // Gitlab API docs:
   200  // https://docs.gitlab.com/ee/api/draft_notes.html#publish-a-draft-note
   201  func (s *DraftNotesService) PublishDraftNote(pid interface{}, mergeRequest int, note int, options ...RequestOptionFunc) (*Response, error) {
   202  	project, err := parseID(pid)
   203  	if err != nil {
   204  		return nil, err
   205  	}
   206  	u := fmt.Sprintf("projects/%s/merge_requests/%d/draft_notes/%d/publish", PathEscape(project), mergeRequest, note)
   207  
   208  	req, err := s.client.NewRequest(http.MethodPut, u, nil, options)
   209  	if err != nil {
   210  		return nil, err
   211  	}
   212  
   213  	return s.client.Do(req, nil)
   214  }
   215  
   216  // PublishAllDraftNotes publishes all draft notes for a merge request that belong to the user.
   217  //
   218  // Gitlab API docs:
   219  // https://docs.gitlab.com/ee/api/draft_notes.html#publish-a-draft-note
   220  func (s *DraftNotesService) PublishAllDraftNotes(pid interface{}, mergeRequest int, options ...RequestOptionFunc) (*Response, error) {
   221  	project, err := parseID(pid)
   222  	if err != nil {
   223  		return nil, err
   224  	}
   225  	u := fmt.Sprintf("projects/%s/merge_requests/%d/draft_notes/bulk_publish", PathEscape(project), mergeRequest)
   226  
   227  	req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
   228  	if err != nil {
   229  		return nil, err
   230  	}
   231  
   232  	return s.client.Do(req, nil)
   233  }
   234  

View as plain text