Move snippet raw_url to base entity

When we return a snippet from the API,
we're not returning the `raw_url` attribute
for project snippets. These snippets also
have this endpoint, so we should return it
as well.
parent 840b1ea5
---
title: Move snippet raw_url attribute to base entity
merge_request: 29776
author:
type: fixed
...@@ -60,7 +60,9 @@ Parameters: ...@@ -60,7 +60,9 @@ Parameters:
}, },
"updated_at": "2012-06-28T10:52:04Z", "updated_at": "2012-06-28T10:52:04Z",
"created_at": "2012-06-28T10:52:04Z", "created_at": "2012-06-28T10:52:04Z",
"web_url": "http://example.com/example/example/snippets/1" "project_id": 1,
"web_url": "http://example.com/example/example/snippets/1",
"raw_url": "http://example.com/example/example/snippets/1/raw"
} }
``` ```
......
...@@ -47,13 +47,13 @@ Example response: ...@@ -47,13 +47,13 @@ Example response:
"username": "user0", "username": "user0",
"state": "active", "state": "active",
"avatar_url": "https://www.gravatar.com/avatar/52e4ce24a915fb7e51e1ad3b57f4b00a?s=80&d=identicon", "avatar_url": "https://www.gravatar.com/avatar/52e4ce24a915fb7e51e1ad3b57f4b00a?s=80&d=identicon",
"web_url": "http://localhost:3000/user0" "web_url": "http://example.com/user0"
}, },
"updated_at": "2018-09-18T01:12:26.383Z", "updated_at": "2018-09-18T01:12:26.383Z",
"created_at": "2018-09-18T01:12:26.383Z", "created_at": "2018-09-18T01:12:26.383Z",
"project_id": null, "project_id": null,
"web_url": "http://localhost:3000/snippets/42", "web_url": "http://example.com/snippets/42",
"raw_url": "http://localhost:3000/snippets/42/raw" "raw_url": "http://example.com/snippets/42/raw"
}, },
{ {
"id": 41, "id": 41,
...@@ -67,13 +67,13 @@ Example response: ...@@ -67,13 +67,13 @@ Example response:
"username": "user0", "username": "user0",
"state": "active", "state": "active",
"avatar_url": "https://www.gravatar.com/avatar/52e4ce24a915fb7e51e1ad3b57f4b00a?s=80&d=identicon", "avatar_url": "https://www.gravatar.com/avatar/52e4ce24a915fb7e51e1ad3b57f4b00a?s=80&d=identicon",
"web_url": "http://localhost:3000/user0" "web_url": "http://example.com/user0"
}, },
"updated_at": "2018-09-18T01:12:26.360Z", "updated_at": "2018-09-18T01:12:26.360Z",
"created_at": "2018-09-18T01:12:26.360Z", "created_at": "2018-09-18T01:12:26.360Z",
"project_id": null, "project_id": 1,
"web_url": "http://localhost:3000/snippets/41", "web_url": "http://example.com/gitlab-org/gitlab-test/snippets/41",
"raw_url": "http://localhost:3000/snippets/41/raw" "raw_url": "http://example.com/gitlab-org/gitlab-test/snippets/41/raw"
} }
] ]
``` ```
...@@ -118,7 +118,9 @@ Example response: ...@@ -118,7 +118,9 @@ Example response:
"expires_at": null, "expires_at": null,
"updated_at": "2012-06-28T10:52:04Z", "updated_at": "2012-06-28T10:52:04Z",
"created_at": "2012-06-28T10:52:04Z", "created_at": "2012-06-28T10:52:04Z",
"project_id": null,
"web_url": "http://example.com/snippets/1", "web_url": "http://example.com/snippets/1",
"raw_url": "http://example.com/snippets/1/raw"
} }
``` ```
...@@ -199,7 +201,9 @@ Example response: ...@@ -199,7 +201,9 @@ Example response:
"expires_at": null, "expires_at": null,
"updated_at": "2012-06-28T10:52:04Z", "updated_at": "2012-06-28T10:52:04Z",
"created_at": "2012-06-28T10:52:04Z", "created_at": "2012-06-28T10:52:04Z",
"project_id": null,
"web_url": "http://example.com/snippets/1", "web_url": "http://example.com/snippets/1",
"raw_url": "http://example.com/snippets/1/raw"
} }
``` ```
...@@ -255,7 +259,9 @@ Example response: ...@@ -255,7 +259,9 @@ Example response:
"expires_at": null, "expires_at": null,
"updated_at": "2012-06-28T10:52:04Z", "updated_at": "2012-06-28T10:52:04Z",
"created_at": "2012-06-28T10:52:04Z", "created_at": "2012-06-28T10:52:04Z",
"project_id": null,
"web_url": "http://example.com/snippets/1", "web_url": "http://example.com/snippets/1",
"raw_url": "http://example.com/snippets/1/raw"
} }
``` ```
...@@ -318,15 +324,16 @@ Example response: ...@@ -318,15 +324,16 @@ Example response:
"name": "Libby Rolfson", "name": "Libby Rolfson",
"state": "active", "state": "active",
"username": "elton_wehner", "username": "elton_wehner",
"web_url": "http://localhost:3000/elton_wehner" "web_url": "http://example.com/elton_wehner"
}, },
"created_at": "2016-11-25T16:53:34.504Z", "created_at": "2016-11-25T16:53:34.504Z",
"file_name": "oconnerrice.rb", "file_name": "oconnerrice.rb",
"id": 49, "id": 49,
"raw_url": "http://localhost:3000/snippets/49/raw",
"title": "Ratione cupiditate et laborum temporibus.", "title": "Ratione cupiditate et laborum temporibus.",
"updated_at": "2016-11-25T16:53:34.504Z", "updated_at": "2016-11-25T16:53:34.504Z",
"web_url": "http://localhost:3000/snippets/49" "project_id": null,
"web_url": "http://example.com/snippets/49",
"raw_url": "http://example.com/snippets/49/raw"
}, },
{ {
"author": { "author": {
...@@ -335,15 +342,16 @@ Example response: ...@@ -335,15 +342,16 @@ Example response:
"name": "Llewellyn Flatley", "name": "Llewellyn Flatley",
"state": "active", "state": "active",
"username": "adaline", "username": "adaline",
"web_url": "http://localhost:3000/adaline" "web_url": "http://example.com/adaline"
}, },
"created_at": "2016-11-25T16:53:34.479Z", "created_at": "2016-11-25T16:53:34.479Z",
"file_name": "muellershields.rb", "file_name": "muellershields.rb",
"id": 48, "id": 48,
"raw_url": "http://localhost:3000/snippets/48/raw",
"title": "Minus similique nesciunt vel fugiat qui ullam sunt.", "title": "Minus similique nesciunt vel fugiat qui ullam sunt.",
"updated_at": "2016-11-25T16:53:34.479Z", "updated_at": "2016-11-25T16:53:34.479Z",
"web_url": "http://localhost:3000/snippets/48", "project_id": null,
"web_url": "http://example.com/snippets/48",
"raw_url": "http://example.com/snippets/49/raw",
"visibility": "public" "visibility": "public"
} }
] ]
......
...@@ -3,9 +3,6 @@ ...@@ -3,9 +3,6 @@
module API module API
module Entities module Entities
class PersonalSnippet < Snippet class PersonalSnippet < Snippet
expose :raw_url do |snippet|
Gitlab::UrlBuilder.build(snippet, raw: true)
end
end end
end end
end end
...@@ -10,6 +10,9 @@ module API ...@@ -10,6 +10,9 @@ module API
expose :web_url do |snippet| expose :web_url do |snippet|
Gitlab::UrlBuilder.build(snippet) Gitlab::UrlBuilder.build(snippet)
end end
expose :raw_url do |snippet|
Gitlab::UrlBuilder.build(snippet, raw: true)
end
expose :ssh_url_to_repo, :http_url_to_repo, if: ->(snippet) { snippet.versioned_enabled_for?(options[:current_user]) } expose :ssh_url_to_repo, :http_url_to_repo, if: ->(snippet) { snippet.versioned_enabled_for?(options[:current_user]) }
end end
end end
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
"description": { "type": ["string", "null"] }, "description": { "type": ["string", "null"] },
"visibility": { "type": "string" }, "visibility": { "type": "string" },
"web_url": { "type": "string" }, "web_url": { "type": "string" },
"raw_url": { "type": "string" },
"created_at": { "type": "date" }, "created_at": { "type": "date" },
"updated_at": { "type": "date" }, "updated_at": { "type": "date" },
"author": { "author": {
...@@ -27,7 +28,7 @@ ...@@ -27,7 +28,7 @@
}, },
"required": [ "required": [
"id", "title", "file_name", "description", "web_url", "id", "title", "file_name", "description", "web_url",
"created_at", "updated_at", "author" "created_at", "updated_at", "author", "raw_url"
], ],
"additionalProperties": false "additionalProperties": false
} }
......
# frozen_string_literal: true
require 'spec_helper'
describe ::API::Entities::Snippet do
let_it_be(:user) { create(:user) }
let_it_be(:personal_snippet) { create(:personal_snippet, :repository, author: user ) }
let_it_be(:project_snippet) { create(:project_snippet, :repository, author: user) }
let(:entity) { described_class.new(snippet) }
subject { entity.as_json }
shared_examples 'common attributes' do
it { expect(subject[:id]).to eq snippet.id }
it { expect(subject[:title]).to eq snippet.title }
it { expect(subject[:description]).to eq snippet.description }
it { expect(subject[:updated_at]).to eq snippet.updated_at }
it { expect(subject[:created_at]).to eq snippet.created_at }
it { expect(subject[:project_id]).to eq snippet.project_id }
it { expect(subject[:visibility]).to eq snippet.visibility }
it { expect(subject[:file_name]).to eq snippet.file_name }
it { expect(subject).to include(:author) }
describe 'ssh_url_to_repo' do
it 'returns attribute' do
expect(subject[:ssh_url_to_repo]).to eq snippet.ssh_url_to_repo
end
context 'when feature flag :version_snippets is disabled' do
it 'does not include attribute' do
stub_feature_flags(version_snippets: false)
expect(subject).not_to include(:ssh_url_to_repo)
end
end
context 'when repository does not exist' do
it 'does not include attribute' do
allow(snippet).to receive(:repository_exists?).and_return(false)
expect(subject).not_to include(:ssh_url_to_repo)
end
end
end
describe 'http_url_to_repo' do
it 'returns attribute' do
expect(subject[:http_url_to_repo]).to eq snippet.http_url_to_repo
end
context 'when feature flag :version_snippets is disabled' do
it 'does not include attribute' do
stub_feature_flags(version_snippets: false)
expect(subject).not_to include(:http_url_to_repo)
end
end
context 'when repository does not exist' do
it 'does not include attribute' do
allow(snippet).to receive(:repository_exists?).and_return(false)
expect(subject).not_to include(:http_url_to_repo)
end
end
end
end
context 'with PersonalSnippet' do
let(:snippet) { personal_snippet }
it_behaves_like 'common attributes'
it 'returns snippet web_url attribute' do
expect(subject[:web_url]).to match("/snippets/#{snippet.id}")
end
it 'returns snippet raw_url attribute' do
expect(subject[:raw_url]).to match("/snippets/#{snippet.id}/raw")
end
end
context 'with ProjectSnippet' do
let(:snippet) { project_snippet }
it_behaves_like 'common attributes'
it 'returns snippet web_url attribute' do
expect(subject[:web_url]).to match("#{snippet.project.full_path}/snippets/#{snippet.id}")
end
it 'returns snippet raw_url attribute' do
expect(subject[:raw_url]).to match("#{snippet.project.full_path}/snippets/#{snippet.id}/raw")
end
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