Commit a9c655c9 authored by Patrick Derichs's avatar Patrick Derichs

Add api endpoint to retrieve resource weight events

Also extract base class for ResourceEventsFinder and update specs.
parent 894fe92a
---
stage: Plan
group: Project Management
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#designated-technical-writers
---
# Resource weight events API
Resource weight events keep track of what happens to GitLab [issues](../user/project/issues/).
Use them to track which weight was set, who did it, and when it happened.
## Issues
### List project issue weight events
Gets a list of all weight events for a single issue.
```plaintext
GET /projects/:id/issues/:issue_iid/resource_weight_events
```
| Attribute | Type | Required | Description |
| ----------- | -------------- | -------- | ------------------------------------------------------------------------------- |
| `id` | integer/string | yes | The ID or [URL-encoded path of the project](README.md#namespaced-path-encoding) |
| `issue_iid` | integer | yes | The IID of an issue |
Example request:
```shell
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/issues/11/resource_weight_events"
```
Example response:
```json
[
{
"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
},
{
"id": 143,
"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-21T14:38:20.077Z",
"issue_id": 253,
"weight": 2
}
]
```
### Get single issue weight event
Returns a single weight event for a specific project issue
```plaintext
GET /projects/:id/issues/:issue_iid/resource_weight_events/:resource_weight_event_id
```
Parameters:
| Attribute | Type | Required | Description |
| ----------------------------- | -------------- | -------- | ------------------------------------------------------------------------------- |
| `id` | integer/string | yes | The ID or [URL-encoded path](README.md#namespaced-path-encoding) of the project |
| `issue_iid` | integer | yes | The IID of an issue |
| `resource_weight_event_id` | integer | yes | The ID of a weight event |
Example request:
```shell
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/issues/11/resource_weight_events/143"
```
Example response:
```json
{
"id": 143,
"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-21T14:38:20.077Z",
"issue_id": 253,
"weight": 2
}
```
---
title: Add api endpoint to retrieve resource weight events
merge_request: 32542
author:
type: added
# frozen_string_literal: true
module API
class ResourceWeightEvents < Grape::API
include PaginationParams
helpers ::API::Helpers::NotesHelpers
before { authenticate! }
params do
requires :id, type: String, desc: "The ID of a project"
end
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
desc "Get a list of issue resource weight events" do
success EE::API::Entities::ResourceWeightEvent
end
params do
requires :eventable_id, types: [Integer, String], desc: 'The ID of the eventable'
use :pagination
end
get ":id/issues/:eventable_id/resource_weight_events" do
eventable = find_noteable(Issue, params[:eventable_id])
events = if Ability.allowed?(current_user, :read_issue, eventable)
eventable.resource_weight_events
else
ResourceWeightEvent.none
end
present paginate(events), with: EE::API::Entities::ResourceWeightEvent
end
desc "Get a single issue resource weight event" do
success EE::API::Entities::ResourceWeightEvent
end
params do
requires :event_id, type: String, desc: 'The ID of a resource weight event'
requires :eventable_id, types: [Integer, String], desc: 'The ID of the eventable'
end
get ":id/issues/:eventable_id/resource_weight_events/:event_id" do
eventable = find_noteable(Issue, params[:eventable_id])
event = eventable.resource_weight_events.find(params[:event_id])
not_found!('ResourceWeightEvent') unless can?(current_user, :read_issue, event.issue)
present event, with: EE::API::Entities::ResourceWeightEvent
end
end
end
end
......@@ -59,6 +59,7 @@ module EE
mount ::API::Analytics::CodeReviewAnalytics
mount ::API::Analytics::GroupActivityAnalytics
mount ::API::ProtectedEnvironments
mount ::API::ResourceWeightEvents
version 'v3', using: :path do
# Although the following endpoints are kept behind V3 namespace,
......
# frozen_string_literal: true
module EE
module API
module Entities
class ResourceWeightEvent < Grape::Entity
expose :id
expose :user, using: ::API::Entities::UserBasic
expose :created_at
expose :issue_id
expose :weight
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe ::API::ResourceWeightEvents do
let_it_be(:user) { create(:user) }
let_it_be(:project, reload: true) { create(:project, :public, namespace: user.namespace) }
let_it_be(:issue) { create(:issue, project: project, author: user) }
before do
project.add_developer(user)
end
describe "GET /projects/:id/issues/:noteable_id/resource_weight_events" do
let!(:event) { create_event }
it "returns an array of resource weight events" do
get api("/projects/#{project.id}/issues/#{issue.iid}/resource_weight_events", user)
expect(response).to have_gitlab_http_status(:ok)
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
expect(json_response.first['id']).to eq(event.id)
end
it "returns a 404 error when issue id not found" do
get api("/projects/#{project.id}/issues/#{non_existing_record_id}/resource_weight_events", user)
expect(response).to have_gitlab_http_status(:not_found)
end
it "returns 404 when not authorized" do
project.update!(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
private_user = create(:user)
get api("/projects/#{project.id}/issues/#{issue.iid}/resource_weight_events", private_user)
expect(response).to have_gitlab_http_status(:not_found)
end
end
describe "GET /projects/:id/issues/:noteable_id/resource_weight_events/:event_id" do
let!(:event) { create_event }
it "returns a resource weight event by id" do
get api("/projects/#{project.id}/issues/#{issue.iid}/resource_weight_events/#{event.id}", user)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['id']).to eq(event.id)
end
it "returns 404 when not authorized" do
project.update!(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
private_user = create(:user)
get api("/projects/#{project.id}/issues/#{issue.iid}/resource_weight_events/#{event.id}", private_user)
expect(response).to have_gitlab_http_status(:not_found)
end
it "returns a 404 error if resource weight event not found" do
get api("/projects/#{project.id}/issues/#{issue.iid}/resource_weight_events/#{non_existing_record_id}", user)
expect(response).to have_gitlab_http_status(:not_found)
end
end
describe 'pagination' do
let!(:event1) { create_event }
let!(:event2) { create_event }
# https://gitlab.com/gitlab-org/gitlab/-/issues/220192
it "returns the second page" do
get api("/projects/#{project.id}/issues/#{issue.iid}/resource_weight_events?page=2&per_page=1", user)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response.count).to eq(1)
expect(json_response.first['id']).to eq(event2.id)
end
end
def create_event
create(:resource_weight_event, issue: issue, weight: 2)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment