Commit 916cd6bd authored by Sean Arnold's avatar Sean Arnold

Add update metric image API

parent 00c3a3f6
......@@ -2480,6 +2480,39 @@ Example response:
]
```
## Update metric image
Available only for Incident issues.
```plaintext
PUT /projects/:id/issues/:issue_iid/metric_images/:image_id
```
| Attribute | Type | Required | Description |
|-------------|---------|----------|--------------------------------------|
| `id` | integer/string | yes | The ID or [URL-encoded path of the project](index.md#namespaced-path-encoding) owned by the authenticated user |
| `issue_iid` | integer | yes | The internal ID of a project's issue |
| `image_id` | integer | yes | The ID of the image |
| `url` | string | no | The URL to view more metric information |
| `url_text` | string | no | A description of the image or URL |
```shell
curl --header "PRIVATE-TOKEN: <your_access_token>" --request PUT --form 'url=http://example.com' --form 'url_text=Example website' "https://gitlab.example.com/api/v4/projects/5/issues/93/metric_images/1"
```
Example response:
```json
{
"id": 23,
"created_at": "2020-11-13T00:06:18.084Z",
"filename": "file.png",
"file_path": "/uploads/-/system/issuable_metric_image/file/23/file.png",
"url": "http://example.com",
"url_text": "Example website"
}
```
## Delete metric image
Available only for Incident issues.
......
......@@ -72,6 +72,30 @@ module EE
end
end
desc 'Update a metric image for an issue' do
success Entities::IssuableMetricImage
end
params do
requires :metric_image_id, type: Integer, desc: 'The ID of metric image'
optional :url, type: String, desc: 'The url to view more metric info'
optional :url_text, type: String, desc: 'A description of the image or URL'
end
put ':metric_image_id' do
issue = find_project_issue(params[:issue_iid])
authorize!(:destroy_issuable_metric_image, issue)
metric_image = issue.metric_images.find_by_id(params[:metric_image_id])
render_api_error!('Metric image not found', 404) unless metric_image
if metric_image&.update(params.slice(:url, :url_text))
present metric_image, with: Entities::IssuableMetricImage, current_user: current_user, project: user_project
else
render_api_error!('Metric image could not be updated', 400)
end
end
desc 'Remove a metric image for an issue' do
success Entities::IssuableMetricImage
end
......
......@@ -739,6 +739,104 @@ RSpec.describe API::Issues, :mailer do
end
end
describe 'PUT /projects/:id/issues/:issue_iid/metric_images/:metric_image_id' do
using RSpec::Parameterized::TableSyntax
let_it_be(:project) do
create(:project, :public, creator_id: user.id, namespace: user.namespace)
end
let!(:image) { create(:issuable_metric_image, issue: issue) }
let(:params) { { url: 'http://test.example.com', url_text: 'Example website 123' } }
subject { put api("/projects/#{project.id}/issues/#{issue.iid}/metric_images/#{image.id}", user2), params: params }
shared_examples 'can_update_metric_image' do
it 'can update the metric images' do
subject
expect(response).to have_gitlab_http_status(:success)
expect(json_response['url']).to eq(params[:url])
expect(json_response['url_text']).to eq(params[:url_text])
end
end
shared_examples 'unauthorized_update' do
it 'cannot delete the metric image' do
subject
expect(response).to have_gitlab_http_status(:forbidden)
expect(image.reload).to eq(image)
end
end
shared_examples 'not_found' do
it 'cannot delete the metric image' do
subject
expect(response).to have_gitlab_http_status(:not_found)
expect(image.reload).to eq(image)
end
end
where(:user_role, :own_issue, :issue_confidential, :expected_status) do
:not_member | false | false | :unauthorized_update
:not_member | true | false | :unauthorized_update
:not_member | true | true | :unauthorized_update
:guest | false | true | :not_found
:guest | false | false | :unauthorized_update
:guest | true | false | :can_update_metric_image
:reporter | true | false | :can_update_metric_image
:reporter | false | false | :can_update_metric_image
end
with_them do
before do
stub_licensed_features(incident_metric_upload: true)
project.send("add_#{user_role}", user2) unless user_role == :not_member
end
let!(:issue) do
author = own_issue ? user2 : user
confidential = issue_confidential
create(:incident, project: project, confidential: confidential, author: author)
end
it_behaves_like "#{params[:expected_status]}"
end
context 'user has access' do
let(:issue) { create(:incident, project: project) }
before do
project.add_reporter(user2)
end
context 'metric image not found' do
subject { delete api("/projects/#{project.id}/issues/#{issue.iid}/metric_images/#{non_existing_record_id}", user2) }
it 'returns an error' do
subject
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['message']).to eq('Metric image not found')
end
end
context 'metric image cannot be updated' do
let(:params) { { url_text: 'something_long' * 100 } }
it 'returns an error' do
subject
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['message']).to eq('Metric image could not be updated')
end
end
end
end
describe 'DELETE /projects/:id/issues/:issue_iid/metric_images/:metric_image_id' do
using RSpec::Parameterized::TableSyntax
......
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