Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1566 from Veres72/add_resource_weight_events
Add resoruce weight events
- Loading branch information
Showing
3 changed files
with
156 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |