diff --git a/gitlab.go b/gitlab.go index c9055f81e..d56ca2662 100644 --- a/gitlab.go +++ b/gitlab.go @@ -190,6 +190,7 @@ type Client struct { ResourceLabelEvents *ResourceLabelEventsService ResourceMilestoneEvents *ResourceMilestoneEventsService ResourceStateEvents *ResourceStateEventsService + ResourceWeightEvents *ResourceWeightEventsService Runners *RunnersService Search *SearchService Services *ServicesService @@ -393,6 +394,7 @@ func newClient(options ...ClientOptionFunc) (*Client, error) { c.ResourceLabelEvents = &ResourceLabelEventsService{client: c} c.ResourceMilestoneEvents = &ResourceMilestoneEventsService{client: c} c.ResourceStateEvents = &ResourceStateEventsService{client: c} + c.ResourceWeightEvents = &ResourceWeightEventsService{client: c} c.Runners = &RunnersService{client: c} c.Search = &SearchService{client: c} c.Services = &ServicesService{client: c} @@ -902,23 +904,24 @@ func CheckResponse(r *http.Response) error { } // Format: -// { -// "message": { -// "": [ -// "", -// "", -// ... -// ], -// "": { -// "": [ -// "", -// "", -// ... -// ], -// } -// }, -// "error": "" -// } +// +// { +// "message": { +// "": [ +// "", +// "", +// ... +// ], +// "": { +// "": [ +// "", +// "", +// ... +// ], +// } +// }, +// "error": "" +// } func parseError(raw interface{}) string { switch raw := raw.(type) { case string: diff --git a/resource_weight_events.go b/resource_weight_events.go new file mode 100644 index 000000000..e1a4bf56a --- /dev/null +++ b/resource_weight_events.go @@ -0,0 +1,80 @@ +// +// Copyright 2021, Matthias Simon +// +// 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/http" + "time" +) + +// ResourceWeightEventsService handles communication with the event related +// methods of the GitLab API. +// +// GitLab API docs: https://docs.gitlab.com/ee/api/resource_weight_events.html +type ResourceWeightEventsService struct { + client *Client +} + +// WeightEvent represents a resource weight event. +// +// GitLab API docs: https://docs.gitlab.com/ee/api/resource_weight_events.html +type WeightEvent struct { + ID int `json:"id"` + User *BasicUser `json:"user"` + CreatedAt *time.Time `json:"created_at"` + ResourceType string `json:"resource_type"` + ResourceID int `json:"resource_id"` + State EventTypeValue `json:"state"` + IssueID int `json:"issue_id"` + Weight int `json:"weight"` +} + +// ListWeightEventsOptions represents the options for all resource weight events +// list methods. +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/resource_weight_events.html#list-project-issue-weight-events +type ListWeightEventsOptions struct { + ListOptions +} + +// ListIssueWeightEvents retrieves resource weight events for the specified +// project and issue. +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/resource_weight_events.html#list-project-issue-weight-events +func (s *ResourceWeightEventsService) ListIssueWeightEvents(pid interface{}, issue int, opt *ListWeightEventsOptions, options ...RequestOptionFunc) ([]*WeightEvent, *Response, error) { + project, err := parseID(pid) + if err != nil { + return nil, nil, err + } + u := fmt.Sprintf("projects/%s/issues/%d/resource_weight_events", PathEscape(project), issue) + + req, err := s.client.NewRequest(http.MethodGet, u, opt, options) + if err != nil { + return nil, nil, err + } + + var wes []*WeightEvent + resp, err := s.client.Do(req, &wes) + if err != nil { + return nil, resp, err + } + + return wes, resp, err +} diff --git a/resource_weight_events_test.go b/resource_weight_events_test.go new file mode 100644 index 000000000..41ac3ca90 --- /dev/null +++ b/resource_weight_events_test.go @@ -0,0 +1,56 @@ +package gitlab + +import ( + "fmt" + "net/http" + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +func TestResourceWeightEventsService_ListIssueWightEvents(t *testing.T) { + mux, server, client := setup(t) + defer teardown(server) + + mux.HandleFunc("/api/v4/projects/5/issues/11/resource_weight_events", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodGet) + fmt.Fprintf(w, `[ + { + "id": 142, + "user": { + "id": 1, + "name": "Administrator", + "username": "root", + "state": "active", + "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon", + "web_url": "http://gitlab.example.com/root" + }, + "created_at": "2018-08-20T13:38:20.077Z", + "issue_id": 253, + "weight": 3 + } + ]`) + }) + + opt := &ListWeightEventsOptions{ListOptions{Page: 1, PerPage: 10}} + + wes, _, err := client.ResourceWeightEvents.ListIssueWeightEvents(5, 11, opt) + require.NoError(t, err) + + want := []*WeightEvent{{ + ID: 142, + User: &BasicUser{ + ID: 1, + Username: "root", + Name: "Administrator", + State: "active", + AvatarURL: "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon", + WebURL: "http://gitlab.example.com/root", + }, + CreatedAt: Time(time.Date(2018, time.August, 20, 13, 38, 20, 77000000, time.UTC)), + IssueID: 253, + Weight: 3, + }} + require.Equal(t, want, wes) +}