...

Source file src/github.com/xanzy/go-gitlab/milestones.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  	"time"
    23  )
    24  
    25  // MilestonesService handles communication with the milestone related methods
    26  // of the GitLab API.
    27  //
    28  // GitLab API docs: https://docs.gitlab.com/ee/api/milestones.html
    29  type MilestonesService struct {
    30  	client *Client
    31  }
    32  
    33  // Milestone represents a GitLab milestone.
    34  //
    35  // GitLab API docs: https://docs.gitlab.com/ee/api/milestones.html
    36  type Milestone struct {
    37  	ID          int        `json:"id"`
    38  	IID         int        `json:"iid"`
    39  	GroupID     int        `json:"group_id"`
    40  	ProjectID   int        `json:"project_id"`
    41  	Title       string     `json:"title"`
    42  	Description string     `json:"description"`
    43  	StartDate   *ISOTime   `json:"start_date"`
    44  	DueDate     *ISOTime   `json:"due_date"`
    45  	State       string     `json:"state"`
    46  	WebURL      string     `json:"web_url"`
    47  	UpdatedAt   *time.Time `json:"updated_at"`
    48  	CreatedAt   *time.Time `json:"created_at"`
    49  	Expired     *bool      `json:"expired"`
    50  }
    51  
    52  func (m Milestone) String() string {
    53  	return Stringify(m)
    54  }
    55  
    56  // ListMilestonesOptions represents the available ListMilestones() options.
    57  //
    58  // GitLab API docs:
    59  // https://docs.gitlab.com/ee/api/milestones.html#list-project-milestones
    60  type ListMilestonesOptions struct {
    61  	ListOptions
    62  	IIDs                    *[]int  `url:"iids[],omitempty" json:"iids,omitempty"`
    63  	Title                   *string `url:"title,omitempty" json:"title,omitempty"`
    64  	State                   *string `url:"state,omitempty" json:"state,omitempty"`
    65  	Search                  *string `url:"search,omitempty" json:"search,omitempty"`
    66  	IncludeParentMilestones *bool   `url:"include_parent_milestones,omitempty" json:"include_parent_milestones,omitempty"`
    67  }
    68  
    69  // ListMilestones returns a list of project milestones.
    70  //
    71  // GitLab API docs:
    72  // https://docs.gitlab.com/ee/api/milestones.html#list-project-milestones
    73  func (s *MilestonesService) ListMilestones(pid interface{}, opt *ListMilestonesOptions, options ...RequestOptionFunc) ([]*Milestone, *Response, error) {
    74  	project, err := parseID(pid)
    75  	if err != nil {
    76  		return nil, nil, err
    77  	}
    78  	u := fmt.Sprintf("projects/%s/milestones", PathEscape(project))
    79  
    80  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
    81  	if err != nil {
    82  		return nil, nil, err
    83  	}
    84  
    85  	var m []*Milestone
    86  	resp, err := s.client.Do(req, &m)
    87  	if err != nil {
    88  		return nil, resp, err
    89  	}
    90  
    91  	return m, resp, nil
    92  }
    93  
    94  // GetMilestone gets a single project milestone.
    95  //
    96  // GitLab API docs:
    97  // https://docs.gitlab.com/ee/api/milestones.html#get-single-milestone
    98  func (s *MilestonesService) GetMilestone(pid interface{}, milestone int, options ...RequestOptionFunc) (*Milestone, *Response, error) {
    99  	project, err := parseID(pid)
   100  	if err != nil {
   101  		return nil, nil, err
   102  	}
   103  	u := fmt.Sprintf("projects/%s/milestones/%d", PathEscape(project), milestone)
   104  
   105  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   106  	if err != nil {
   107  		return nil, nil, err
   108  	}
   109  
   110  	m := new(Milestone)
   111  	resp, err := s.client.Do(req, m)
   112  	if err != nil {
   113  		return nil, resp, err
   114  	}
   115  
   116  	return m, resp, nil
   117  }
   118  
   119  // CreateMilestoneOptions represents the available CreateMilestone() options.
   120  //
   121  // GitLab API docs:
   122  // https://docs.gitlab.com/ee/api/milestones.html#create-new-milestone
   123  type CreateMilestoneOptions struct {
   124  	Title       *string  `url:"title,omitempty" json:"title,omitempty"`
   125  	Description *string  `url:"description,omitempty" json:"description,omitempty"`
   126  	StartDate   *ISOTime `url:"start_date,omitempty" json:"start_date,omitempty"`
   127  	DueDate     *ISOTime `url:"due_date,omitempty" json:"due_date,omitempty"`
   128  }
   129  
   130  // CreateMilestone creates a new project milestone.
   131  //
   132  // GitLab API docs:
   133  // https://docs.gitlab.com/ee/api/milestones.html#create-new-milestone
   134  func (s *MilestonesService) CreateMilestone(pid interface{}, opt *CreateMilestoneOptions, options ...RequestOptionFunc) (*Milestone, *Response, error) {
   135  	project, err := parseID(pid)
   136  	if err != nil {
   137  		return nil, nil, err
   138  	}
   139  	u := fmt.Sprintf("projects/%s/milestones", PathEscape(project))
   140  
   141  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   142  	if err != nil {
   143  		return nil, nil, err
   144  	}
   145  
   146  	m := new(Milestone)
   147  	resp, err := s.client.Do(req, m)
   148  	if err != nil {
   149  		return nil, resp, err
   150  	}
   151  
   152  	return m, resp, nil
   153  }
   154  
   155  // UpdateMilestoneOptions represents the available UpdateMilestone() options.
   156  //
   157  // GitLab API docs:
   158  // https://docs.gitlab.com/ee/api/milestones.html#edit-milestone
   159  type UpdateMilestoneOptions struct {
   160  	Title       *string  `url:"title,omitempty" json:"title,omitempty"`
   161  	Description *string  `url:"description,omitempty" json:"description,omitempty"`
   162  	StartDate   *ISOTime `url:"start_date,omitempty" json:"start_date,omitempty"`
   163  	DueDate     *ISOTime `url:"due_date,omitempty" json:"due_date,omitempty"`
   164  	StateEvent  *string  `url:"state_event,omitempty" json:"state_event,omitempty"`
   165  }
   166  
   167  // UpdateMilestone updates an existing project milestone.
   168  //
   169  // GitLab API docs:
   170  // https://docs.gitlab.com/ee/api/milestones.html#edit-milestone
   171  func (s *MilestonesService) UpdateMilestone(pid interface{}, milestone int, opt *UpdateMilestoneOptions, options ...RequestOptionFunc) (*Milestone, *Response, error) {
   172  	project, err := parseID(pid)
   173  	if err != nil {
   174  		return nil, nil, err
   175  	}
   176  	u := fmt.Sprintf("projects/%s/milestones/%d", PathEscape(project), milestone)
   177  
   178  	req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
   179  	if err != nil {
   180  		return nil, nil, err
   181  	}
   182  
   183  	m := new(Milestone)
   184  	resp, err := s.client.Do(req, m)
   185  	if err != nil {
   186  		return nil, resp, err
   187  	}
   188  
   189  	return m, resp, nil
   190  }
   191  
   192  // DeleteMilestone deletes a specified project milestone.
   193  //
   194  // GitLab API docs:
   195  // https://docs.gitlab.com/ee/api/milestones.html#delete-project-milestone
   196  func (s *MilestonesService) DeleteMilestone(pid interface{}, milestone int, options ...RequestOptionFunc) (*Response, error) {
   197  	project, err := parseID(pid)
   198  	if err != nil {
   199  		return nil, err
   200  	}
   201  	u := fmt.Sprintf("projects/%s/milestones/%d", PathEscape(project), milestone)
   202  
   203  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   204  	if err != nil {
   205  		return nil, err
   206  	}
   207  	return s.client.Do(req, nil)
   208  }
   209  
   210  // GetMilestoneIssuesOptions represents the available GetMilestoneIssues() options.
   211  //
   212  // GitLab API docs:
   213  // https://docs.gitlab.com/ee/api/milestones.html#get-all-issues-assigned-to-a-single-milestone
   214  type GetMilestoneIssuesOptions ListOptions
   215  
   216  // GetMilestoneIssues gets all issues assigned to a single project milestone.
   217  //
   218  // GitLab API docs:
   219  // https://docs.gitlab.com/ee/api/milestones.html#get-all-issues-assigned-to-a-single-milestone
   220  func (s *MilestonesService) GetMilestoneIssues(pid interface{}, milestone int, opt *GetMilestoneIssuesOptions, options ...RequestOptionFunc) ([]*Issue, *Response, error) {
   221  	project, err := parseID(pid)
   222  	if err != nil {
   223  		return nil, nil, err
   224  	}
   225  	u := fmt.Sprintf("projects/%s/milestones/%d/issues", PathEscape(project), milestone)
   226  
   227  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   228  	if err != nil {
   229  		return nil, nil, err
   230  	}
   231  
   232  	var i []*Issue
   233  	resp, err := s.client.Do(req, &i)
   234  	if err != nil {
   235  		return nil, resp, err
   236  	}
   237  
   238  	return i, resp, nil
   239  }
   240  
   241  // GetMilestoneMergeRequestsOptions represents the available
   242  // GetMilestoneMergeRequests() options.
   243  //
   244  // GitLab API docs:
   245  // https://docs.gitlab.com/ee/api/milestones.html#get-all-merge-requests-assigned-to-a-single-milestone
   246  type GetMilestoneMergeRequestsOptions ListOptions
   247  
   248  // GetMilestoneMergeRequests gets all merge requests assigned to a single
   249  // project milestone.
   250  //
   251  // GitLab API docs:
   252  // https://docs.gitlab.com/ee/api/milestones.html#get-all-merge-requests-assigned-to-a-single-milestone
   253  func (s *MilestonesService) GetMilestoneMergeRequests(pid interface{}, milestone int, opt *GetMilestoneMergeRequestsOptions, options ...RequestOptionFunc) ([]*MergeRequest, *Response, error) {
   254  	project, err := parseID(pid)
   255  	if err != nil {
   256  		return nil, nil, err
   257  	}
   258  	u := fmt.Sprintf("projects/%s/milestones/%d/merge_requests", PathEscape(project), milestone)
   259  
   260  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   261  	if err != nil {
   262  		return nil, nil, err
   263  	}
   264  
   265  	var mr []*MergeRequest
   266  	resp, err := s.client.Do(req, &mr)
   267  	if err != nil {
   268  		return nil, resp, err
   269  	}
   270  
   271  	return mr, resp, nil
   272  }
   273  

View as plain text