Skip to content

Commit

Permalink
Merge pull request #1566 from Veres72/add_resource_weight_events
Browse files Browse the repository at this point in the history
Add resoruce weight events
  • Loading branch information
svanharmelen committed Oct 29, 2022
2 parents 1114045 + d160433 commit cc0d6de
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 17 deletions.
37 changes: 20 additions & 17 deletions gitlab.go
Expand Up @@ -190,6 +190,7 @@ type Client struct {
ResourceLabelEvents *ResourceLabelEventsService
ResourceMilestoneEvents *ResourceMilestoneEventsService
ResourceStateEvents *ResourceStateEventsService
ResourceWeightEvents *ResourceWeightEventsService
Runners *RunnersService
Search *SearchService
Services *ServicesService
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -902,23 +904,24 @@ func CheckResponse(r *http.Response) error {
}

// Format:
// {
// "message": {
// "<property-name>": [
// "<error-message>",
// "<error-message>",
// ...
// ],
// "<embed-entity>": {
// "<property-name>": [
// "<error-message>",
// "<error-message>",
// ...
// ],
// }
// },
// "error": "<error-message>"
// }
//
// {
// "message": {
// "<property-name>": [
// "<error-message>",
// "<error-message>",
// ...
// ],
// "<embed-entity>": {
// "<property-name>": [
// "<error-message>",
// "<error-message>",
// ...
// ],
// }
// },
// "error": "<error-message>"
// }
func parseError(raw interface{}) string {
switch raw := raw.(type) {
case string:
Expand Down
80 changes: 80 additions & 0 deletions 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
}
56 changes: 56 additions & 0 deletions 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)
}

0 comments on commit cc0d6de

Please sign in to comment.