123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521 |
- //
- // Copyright 2017, Sander van Harmelen
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- //
- package gitlab
- import (
- "fmt"
- "net/url"
- "time"
- )
- // NotesService handles communication with the notes related methods
- // of the GitLab API.
- //
- // GitLab API docs: https://docs.gitlab.com/ce/api/notes.html
- type NotesService struct {
- client *Client
- }
- // Note represents a GitLab note.
- //
- // GitLab API docs: https://docs.gitlab.com/ce/api/notes.html
- type Note struct {
- ID int `json:"id"`
- Body string `json:"body"`
- Attachment string `json:"attachment"`
- Title string `json:"title"`
- FileName string `json:"file_name"`
- Author struct {
- ID int `json:"id"`
- Username string `json:"username"`
- Email string `json:"email"`
- Name string `json:"name"`
- State string `json:"state"`
- AvatarURL string `json:"avatar_url"`
- WebURL string `json:"web_url"`
- } `json:"author"`
- System bool `json:"system"`
- ExpiresAt *time.Time `json:"expires_at"`
- UpdatedAt *time.Time `json:"updated_at"`
- CreatedAt *time.Time `json:"created_at"`
- NoteableID int `json:"noteable_id"`
- NoteableType string `json:"noteable_type"`
- Position *NotePosition `json:"position"`
- Resolvable bool `json:"resolvable"`
- Resolved bool `json:"resolved"`
- ResolvedBy struct {
- ID int `json:"id"`
- Username string `json:"username"`
- Email string `json:"email"`
- Name string `json:"name"`
- State string `json:"state"`
- AvatarURL string `json:"avatar_url"`
- WebURL string `json:"web_url"`
- } `json:"resolved_by"`
- NoteableIID int `json:"noteable_iid"`
- }
- // NotePosition represents the position attributes of a note.
- type NotePosition struct {
- BaseSHA string `json:"base_sha"`
- StartSHA string `json:"start_sha"`
- HeadSHA string `json:"head_sha"`
- PositionType string `json:"position_type"`
- NewPath string `json:"new_path,omitempty"`
- NewLine int `json:"new_line,omitempty"`
- OldPath string `json:"old_path,omitempty"`
- OldLine int `json:"old_line,omitempty"`
- Width int `json:"width,omitempty"`
- Height int `json:"height,omitempty"`
- X int `json:"x,omitempty"`
- Y int `json:"y,omitempty"`
- }
- func (n Note) String() string {
- return Stringify(n)
- }
- // ListIssueNotesOptions represents the available ListIssueNotes() options.
- //
- // GitLab API docs:
- // https://docs.gitlab.com/ce/api/notes.html#list-project-issue-notes
- type ListIssueNotesOptions struct {
- ListOptions
- OrderBy *string `url:"order_by,omitempty" json:"order_by,omitempty"`
- Sort *string `url:"sort,omitempty" json:"sort,omitempty"`
- }
- // ListIssueNotes gets a list of all notes for a single issue.
- //
- // GitLab API docs:
- // https://docs.gitlab.com/ce/api/notes.html#list-project-issue-notes
- func (s *NotesService) ListIssueNotes(pid interface{}, issue int, opt *ListIssueNotesOptions, options ...OptionFunc) ([]*Note, *Response, error) {
- project, err := parseID(pid)
- if err != nil {
- return nil, nil, err
- }
- u := fmt.Sprintf("projects/%s/issues/%d/notes", url.QueryEscape(project), issue)
- req, err := s.client.NewRequest("GET", u, opt, options)
- if err != nil {
- return nil, nil, err
- }
- var n []*Note
- resp, err := s.client.Do(req, &n)
- if err != nil {
- return nil, resp, err
- }
- return n, resp, err
- }
- // GetIssueNote returns a single note for a specific project issue.
- //
- // GitLab API docs:
- // https://docs.gitlab.com/ce/api/notes.html#get-single-issue-note
- func (s *NotesService) GetIssueNote(pid interface{}, issue, note int, options ...OptionFunc) (*Note, *Response, error) {
- project, err := parseID(pid)
- if err != nil {
- return nil, nil, err
- }
- u := fmt.Sprintf("projects/%s/issues/%d/notes/%d", url.QueryEscape(project), issue, note)
- req, err := s.client.NewRequest("GET", u, nil, options)
- if err != nil {
- return nil, nil, err
- }
- n := new(Note)
- resp, err := s.client.Do(req, n)
- if err != nil {
- return nil, resp, err
- }
- return n, resp, err
- }
- // CreateIssueNoteOptions represents the available CreateIssueNote()
- // options.
- //
- // GitLab API docs:
- // https://docs.gitlab.com/ce/api/notes.html#create-new-issue-note
- type CreateIssueNoteOptions struct {
- Body *string `url:"body,omitempty" json:"body,omitempty"`
- }
- // CreateIssueNote creates a new note to a single project issue.
- //
- // GitLab API docs:
- // https://docs.gitlab.com/ce/api/notes.html#create-new-issue-note
- func (s *NotesService) CreateIssueNote(pid interface{}, issue int, opt *CreateIssueNoteOptions, options ...OptionFunc) (*Note, *Response, error) {
- project, err := parseID(pid)
- if err != nil {
- return nil, nil, err
- }
- u := fmt.Sprintf("projects/%s/issues/%d/notes", url.QueryEscape(project), issue)
- req, err := s.client.NewRequest("POST", u, opt, options)
- if err != nil {
- return nil, nil, err
- }
- n := new(Note)
- resp, err := s.client.Do(req, n)
- if err != nil {
- return nil, resp, err
- }
- return n, resp, err
- }
- // UpdateIssueNoteOptions represents the available UpdateIssueNote()
- // options.
- //
- // GitLab API docs:
- // https://docs.gitlab.com/ce/api/notes.html#modify-existing-issue-note
- type UpdateIssueNoteOptions struct {
- Body *string `url:"body,omitempty" json:"body,omitempty"`
- }
- // UpdateIssueNote modifies existing note of an issue.
- //
- // https://docs.gitlab.com/ce/api/notes.html#modify-existing-issue-note
- func (s *NotesService) UpdateIssueNote(pid interface{}, issue, note int, opt *UpdateIssueNoteOptions, options ...OptionFunc) (*Note, *Response, error) {
- project, err := parseID(pid)
- if err != nil {
- return nil, nil, err
- }
- u := fmt.Sprintf("projects/%s/issues/%d/notes/%d", url.QueryEscape(project), issue, note)
- req, err := s.client.NewRequest("PUT", u, opt, options)
- if err != nil {
- return nil, nil, err
- }
- n := new(Note)
- resp, err := s.client.Do(req, n)
- if err != nil {
- return nil, resp, err
- }
- return n, resp, err
- }
- // DeleteIssueNote deletes an existing note of an issue.
- //
- // https://docs.gitlab.com/ce/api/notes.html#delete-an-issue-note
- func (s *NotesService) DeleteIssueNote(pid interface{}, issue, note int, options ...OptionFunc) (*Response, error) {
- project, err := parseID(pid)
- if err != nil {
- return nil, err
- }
- u := fmt.Sprintf("projects/%s/issues/%d/notes/%d", url.QueryEscape(project), issue, note)
- req, err := s.client.NewRequest("DELETE", u, nil, options)
- if err != nil {
- return nil, err
- }
- return s.client.Do(req, nil)
- }
- // ListSnippetNotesOptions represents the available ListSnippetNotes() options.
- //
- // GitLab API docs:
- // https://docs.gitlab.com/ce/api/notes.html#list-all-snippet-notes
- type ListSnippetNotesOptions ListOptions
- // ListSnippetNotes gets a list of all notes for a single snippet. Snippet
- // notes are comments users can post to a snippet.
- //
- // GitLab API docs:
- // https://docs.gitlab.com/ce/api/notes.html#list-all-snippet-notes
- func (s *NotesService) ListSnippetNotes(pid interface{}, snippet int, opt *ListSnippetNotesOptions, options ...OptionFunc) ([]*Note, *Response, error) {
- project, err := parseID(pid)
- if err != nil {
- return nil, nil, err
- }
- u := fmt.Sprintf("projects/%s/snippets/%d/notes", url.QueryEscape(project), snippet)
- req, err := s.client.NewRequest("GET", u, opt, options)
- if err != nil {
- return nil, nil, err
- }
- var n []*Note
- resp, err := s.client.Do(req, &n)
- if err != nil {
- return nil, resp, err
- }
- return n, resp, err
- }
- // GetSnippetNote returns a single note for a given snippet.
- //
- // GitLab API docs:
- // https://docs.gitlab.com/ce/api/notes.html#get-single-snippet-note
- func (s *NotesService) GetSnippetNote(pid interface{}, snippet, note int, options ...OptionFunc) (*Note, *Response, error) {
- project, err := parseID(pid)
- if err != nil {
- return nil, nil, err
- }
- u := fmt.Sprintf("projects/%s/snippets/%d/notes/%d", url.QueryEscape(project), snippet, note)
- req, err := s.client.NewRequest("GET", u, nil, options)
- if err != nil {
- return nil, nil, err
- }
- n := new(Note)
- resp, err := s.client.Do(req, n)
- if err != nil {
- return nil, resp, err
- }
- return n, resp, err
- }
- // CreateSnippetNoteOptions represents the available CreateSnippetNote()
- // options.
- //
- // GitLab API docs:
- // https://docs.gitlab.com/ce/api/notes.html#create-new-snippet-note
- type CreateSnippetNoteOptions struct {
- Body *string `url:"body,omitempty" json:"body,omitempty"`
- }
- // CreateSnippetNote creates a new note for a single snippet. Snippet notes are
- // comments users can post to a snippet.
- //
- // GitLab API docs:
- // https://docs.gitlab.com/ce/api/notes.html#create-new-snippet-note
- func (s *NotesService) CreateSnippetNote(pid interface{}, snippet int, opt *CreateSnippetNoteOptions, options ...OptionFunc) (*Note, *Response, error) {
- project, err := parseID(pid)
- if err != nil {
- return nil, nil, err
- }
- u := fmt.Sprintf("projects/%s/snippets/%d/notes", url.QueryEscape(project), snippet)
- req, err := s.client.NewRequest("POST", u, opt, options)
- if err != nil {
- return nil, nil, err
- }
- n := new(Note)
- resp, err := s.client.Do(req, n)
- if err != nil {
- return nil, resp, err
- }
- return n, resp, err
- }
- // UpdateSnippetNoteOptions represents the available UpdateSnippetNote()
- // options.
- //
- // GitLab API docs:
- // https://docs.gitlab.com/ce/api/notes.html#modify-existing-snippet-note
- type UpdateSnippetNoteOptions struct {
- Body *string `url:"body,omitempty" json:"body,omitempty"`
- }
- // UpdateSnippetNote modifies existing note of a snippet.
- //
- // https://docs.gitlab.com/ce/api/notes.html#modify-existing-snippet-note
- func (s *NotesService) UpdateSnippetNote(pid interface{}, snippet, note int, opt *UpdateSnippetNoteOptions, options ...OptionFunc) (*Note, *Response, error) {
- project, err := parseID(pid)
- if err != nil {
- return nil, nil, err
- }
- u := fmt.Sprintf("projects/%s/snippets/%d/notes/%d", url.QueryEscape(project), snippet, note)
- req, err := s.client.NewRequest("PUT", u, opt, options)
- if err != nil {
- return nil, nil, err
- }
- n := new(Note)
- resp, err := s.client.Do(req, n)
- if err != nil {
- return nil, resp, err
- }
- return n, resp, err
- }
- // DeleteSnippetNote deletes an existing note of a snippet.
- //
- // https://docs.gitlab.com/ce/api/notes.html#delete-a-snippet-note
- func (s *NotesService) DeleteSnippetNote(pid interface{}, snippet, note int, options ...OptionFunc) (*Response, error) {
- project, err := parseID(pid)
- if err != nil {
- return nil, err
- }
- u := fmt.Sprintf("projects/%s/snippets/%d/notes/%d", url.QueryEscape(project), snippet, note)
- req, err := s.client.NewRequest("DELETE", u, nil, options)
- if err != nil {
- return nil, err
- }
- return s.client.Do(req, nil)
- }
- // ListMergeRequestNotesOptions represents the available ListMergeRequestNotes()
- // options.
- //
- // GitLab API docs:
- // https://docs.gitlab.com/ce/api/notes.html#list-all-merge-request-notes
- type ListMergeRequestNotesOptions ListOptions
- // ListMergeRequestNotes gets a list of all notes for a single merge request.
- //
- // GitLab API docs:
- // https://docs.gitlab.com/ce/api/notes.html#list-all-merge-request-notes
- func (s *NotesService) ListMergeRequestNotes(pid interface{}, mergeRequest int, opt *ListMergeRequestNotesOptions, options ...OptionFunc) ([]*Note, *Response, error) {
- project, err := parseID(pid)
- if err != nil {
- return nil, nil, err
- }
- u := fmt.Sprintf("projects/%s/merge_requests/%d/notes", url.QueryEscape(project), mergeRequest)
- req, err := s.client.NewRequest("GET", u, opt, options)
- if err != nil {
- return nil, nil, err
- }
- var n []*Note
- resp, err := s.client.Do(req, &n)
- if err != nil {
- return nil, resp, err
- }
- return n, resp, err
- }
- // GetMergeRequestNote returns a single note for a given merge request.
- //
- // GitLab API docs:
- // https://docs.gitlab.com/ce/api/notes.html#get-single-merge-request-note
- func (s *NotesService) GetMergeRequestNote(pid interface{}, mergeRequest, note int, options ...OptionFunc) (*Note, *Response, error) {
- project, err := parseID(pid)
- if err != nil {
- return nil, nil, err
- }
- u := fmt.Sprintf("projects/%s/merge_requests/%d/notes/%d", url.QueryEscape(project), mergeRequest, note)
- req, err := s.client.NewRequest("GET", u, nil, options)
- if err != nil {
- return nil, nil, err
- }
- n := new(Note)
- resp, err := s.client.Do(req, n)
- if err != nil {
- return nil, resp, err
- }
- return n, resp, err
- }
- // CreateMergeRequestNoteOptions represents the available
- // CreateMergeRequestNote() options.
- //
- // GitLab API docs:
- // https://docs.gitlab.com/ce/api/notes.html#create-new-merge-request-note
- type CreateMergeRequestNoteOptions struct {
- Body *string `url:"body,omitempty" json:"body,omitempty"`
- }
- // CreateMergeRequestNote creates a new note for a single merge request.
- //
- // GitLab API docs:
- // https://docs.gitlab.com/ce/api/notes.html#create-new-merge-request-note
- func (s *NotesService) CreateMergeRequestNote(pid interface{}, mergeRequest int, opt *CreateMergeRequestNoteOptions, options ...OptionFunc) (*Note, *Response, error) {
- project, err := parseID(pid)
- if err != nil {
- return nil, nil, err
- }
- u := fmt.Sprintf("projects/%s/merge_requests/%d/notes", url.QueryEscape(project), mergeRequest)
- req, err := s.client.NewRequest("POST", u, opt, options)
- if err != nil {
- return nil, nil, err
- }
- n := new(Note)
- resp, err := s.client.Do(req, n)
- if err != nil {
- return nil, resp, err
- }
- return n, resp, err
- }
- // UpdateMergeRequestNoteOptions represents the available
- // UpdateMergeRequestNote() options.
- //
- // GitLab API docs:
- // https://docs.gitlab.com/ce/api/notes.html#modify-existing-merge-request-note
- type UpdateMergeRequestNoteOptions struct {
- Body *string `url:"body,omitempty" json:"body,omitempty"`
- }
- // UpdateMergeRequestNote modifies existing note of a merge request.
- //
- // https://docs.gitlab.com/ce/api/notes.html#modify-existing-merge-request-note
- func (s *NotesService) UpdateMergeRequestNote(pid interface{}, mergeRequest, note int, opt *UpdateMergeRequestNoteOptions, options ...OptionFunc) (*Note, *Response, error) {
- project, err := parseID(pid)
- if err != nil {
- return nil, nil, err
- }
- u := fmt.Sprintf(
- "projects/%s/merge_requests/%d/notes/%d", url.QueryEscape(project), mergeRequest, note)
- req, err := s.client.NewRequest("PUT", u, opt, options)
- if err != nil {
- return nil, nil, err
- }
- n := new(Note)
- resp, err := s.client.Do(req, n)
- if err != nil {
- return nil, resp, err
- }
- return n, resp, err
- }
- // DeleteMergeRequestNote deletes an existing note of a merge request.
- //
- // https://docs.gitlab.com/ce/api/notes.html#delete-a-merge-request-note
- func (s *NotesService) DeleteMergeRequestNote(pid interface{}, mergeRequest, note int, options ...OptionFunc) (*Response, error) {
- project, err := parseID(pid)
- if err != nil {
- return nil, err
- }
- u := fmt.Sprintf(
- "projects/%s/merge_requests/%d/notes/%d", url.QueryEscape(project), mergeRequest, note)
- req, err := s.client.NewRequest("DELETE", u, nil, options)
- if err != nil {
- return nil, err
- }
- return s.client.Do(req, nil)
- }
|