Commit 847b346f authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'fj-39279-include-all-files-in-snippet-js-requests' into 'master'

Include all files in snippet JS requests

See merge request gitlab-org/gitlab!37412
parents 6b5247fd 641a5961
...@@ -29,6 +29,12 @@ module RendersBlob ...@@ -29,6 +29,12 @@ module RendersBlob
end end
def conditionally_expand_blob(blob) def conditionally_expand_blob(blob)
blob.expand! if params[:expanded] == 'true' conditionally_expand_blobs([blob])
end
def conditionally_expand_blobs(blobs)
return unless params[:expanded] == 'true'
blobs.each { |blob| blob.expand! }
end end
end end
...@@ -55,10 +55,9 @@ module SnippetsActions ...@@ -55,10 +55,9 @@ module SnippetsActions
# rubocop:disable Gitlab/ModuleWithInstanceVariables # rubocop:disable Gitlab/ModuleWithInstanceVariables
def show def show
conditionally_expand_blob(blob)
respond_to do |format| respond_to do |format|
format.html do format.html do
conditionally_expand_blob(blob)
@note = Note.new(noteable: @snippet, project: @snippet.project) @note = Note.new(noteable: @snippet, project: @snippet.project)
@noteable = @snippet @noteable = @snippet
...@@ -68,11 +67,14 @@ module SnippetsActions ...@@ -68,11 +67,14 @@ module SnippetsActions
end end
format.json do format.json do
conditionally_expand_blob(blob)
render_blob_json(blob) render_blob_json(blob)
end end
format.js do format.js do
if @snippet.embeddable? if @snippet.embeddable?
conditionally_expand_blobs(blobs)
render 'shared/snippets/show' render 'shared/snippets/show'
else else
head :not_found head :not_found
...@@ -109,12 +111,14 @@ module SnippetsActions ...@@ -109,12 +111,14 @@ module SnippetsActions
# rubocop:disable Gitlab/ModuleWithInstanceVariables # rubocop:disable Gitlab/ModuleWithInstanceVariables
def blob def blob
return unless snippet @blob ||= blobs.first
end
@blob ||= if snippet.empty_repo? def blobs
snippet.blob @blobs ||= if snippet.empty_repo?
[snippet.blob]
else else
snippet.blobs.first snippet.blobs
end end
end end
# rubocop:enable Gitlab/ModuleWithInstanceVariables # rubocop:enable Gitlab/ModuleWithInstanceVariables
......
# frozen_string_literal: true # frozen_string_literal: true
class Projects::Snippets::BlobsController < Projects::Snippets::ApplicationController class Projects::Snippets::BlobsController < Projects::Snippets::ApplicationController
include Snippets::BlobsActions include ::Snippets::BlobsActions
end end
...@@ -74,21 +74,20 @@ module SnippetsHelper ...@@ -74,21 +74,20 @@ module SnippetsHelper
end end
end end
def embedded_raw_snippet_button def embedded_raw_snippet_button(snippet, blob)
blob = @snippet.blob
return if blob.empty? || blob.binary? || blob.stored_externally? return if blob.empty? || blob.binary? || blob.stored_externally?
link_to(external_snippet_icon('doc-code'), link_to(external_snippet_icon('doc-code'),
gitlab_raw_snippet_url(@snippet), gitlab_raw_snippet_blob_url(snippet, blob.path),
class: 'btn', class: 'btn',
target: '_blank', target: '_blank',
rel: 'noopener noreferrer', rel: 'noopener noreferrer',
title: 'Open raw') title: 'Open raw')
end end
def embedded_snippet_download_button def embedded_snippet_download_button(snippet, blob)
link_to(external_snippet_icon('download'), link_to(external_snippet_icon('download'),
gitlab_raw_snippet_url(@snippet, inline: false), gitlab_raw_snippet_blob_url(snippet, blob.path, nil, inline: false),
class: 'btn', class: 'btn',
target: '_blank', target: '_blank',
title: 'Download', title: 'Download',
......
...@@ -5,17 +5,17 @@ ...@@ -5,17 +5,17 @@
%strong.file-title-name %strong.file-title-name
%a.gitlab-embedded-snippets-title{ href: url_for(only_path: false, overwrite_params: nil) } %a.gitlab-embedded-snippets-title{ href: url_for(only_path: false, overwrite_params: nil) }
= @blob.name = blob.name
%small %small
= number_to_human_size(@blob.raw_size) = number_to_human_size(blob.size)
%a.gitlab-logo-wrapper{ href: url_for(only_path: false, overwrite_params: nil), title: 'view on gitlab' } %a.gitlab-logo-wrapper{ href: url_for(only_path: false, overwrite_params: nil), title: 'view on gitlab' }
%img.gitlab-logo{ src: image_url('ext_snippet_icons/logo.svg'), alt: "GitLab logo" } %img.gitlab-logo{ src: image_url('ext_snippet_icons/logo.svg'), alt: "GitLab logo" }
.file-actions.d-none.d-sm-block .file-actions.d-none.d-sm-block
.btn-group{ role: "group" }< .btn-group{ role: "group" }<
= embedded_raw_snippet_button = embedded_raw_snippet_button(@snippet, blob)
= embedded_snippet_download_button = embedded_snippet_download_button(@snippet, blob)
%article.file-holder.snippet-file-content %article.file-holder.snippet-file-content
= render 'projects/blob/viewer', viewer: @blob.simple_viewer, load_async: false, external_embed: true = render 'projects/blob/viewer', viewer: blob.simple_viewer, load_async: false, external_embed: true
document.write('#{escape_javascript(stylesheet_link_tag("#{stylesheet_url 'snippets'}"))}'); document.write('#{escape_javascript(stylesheet_link_tag("#{stylesheet_url 'snippets'}"))}');
document.write('#{escape_javascript(render('shared/snippets/embed'))}'); document.write('#{escape_javascript(render(partial: 'shared/snippets/embed', collection: @blobs, as: :blob))}');
---
title: Show all snippet files when embedding
merge_request: 37412
author:
type: changed
...@@ -416,12 +416,13 @@ RSpec.describe Projects::SnippetsController do ...@@ -416,12 +416,13 @@ RSpec.describe Projects::SnippetsController do
describe "GET #show for embeddable content" do describe "GET #show for embeddable content" do
let(:project_snippet) { create(:project_snippet, :repository, snippet_permission, project: project, author: user) } let(:project_snippet) { create(:project_snippet, :repository, snippet_permission, project: project, author: user) }
let(:extra_params) { {} }
before do before do
sign_in(user) sign_in(user)
end end
subject { get :show, params: { namespace_id: project.namespace, project_id: project, id: project_snippet.to_param }, format: :js } subject { get :show, params: { namespace_id: project.namespace, project_id: project, id: project_snippet.to_param, **extra_params }, format: :js }
context 'when snippet is private' do context 'when snippet is private' do
let(:snippet_permission) { :private } let(:snippet_permission) { :private }
...@@ -436,7 +437,29 @@ RSpec.describe Projects::SnippetsController do ...@@ -436,7 +437,29 @@ RSpec.describe Projects::SnippetsController do
context 'when snippet is public' do context 'when snippet is public' do
let(:snippet_permission) { :public } let(:snippet_permission) { :public }
it_behaves_like 'successful response' it 'renders the blob from the repository' do
subject
expect(assigns(:snippet)).to eq(project_snippet)
expect(assigns(:blobs)).to eq(project_snippet.blobs)
expect(response).to have_gitlab_http_status(:ok)
end
it 'does not show the blobs expanded by default' do
subject
expect(project_snippet.blobs.map(&:expanded?)).to be_all(false)
end
context 'when param expanded is set' do
let(:extra_params) { { expanded: true } }
it 'shows all blobs expanded' do
subject
expect(project_snippet.blobs.map(&:expanded?)).to be_all(true)
end
end
end end
context 'when the project is private' do context 'when the project is private' do
......
...@@ -3,11 +3,13 @@ ...@@ -3,11 +3,13 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe 'Embedded Snippets' do RSpec.describe 'Embedded Snippets' do
let(:snippet) { create(:personal_snippet, :public, file_name: 'random_dir.rb', content: content) } let_it_be(:snippet) { create(:personal_snippet, :public, :repository) }
let(:content) { "require 'fileutils'\nFileUtils.mkdir_p 'some/random_dir'\n" } let(:blobs) { snippet.blobs.first(3) }
it 'loads snippet', :js do it 'loads snippet', :js do
script_url = "http://#{Capybara.current_session.server.host}:#{Capybara.current_session.server.port}/#{snippet_path(snippet, format: 'js')}" expect_any_instance_of(Snippet).to receive(:blobs).and_return(blobs)
script_url = "http://#{Capybara.current_session.server.host}:#{Capybara.current_session.server.port}#{snippet_path(snippet, format: 'js')}"
embed_body = "<html><body><script src=\"#{script_url}\"></script></body></html>" embed_body = "<html><body><script src=\"#{script_url}\"></script></body></html>"
rack_app = proc do rack_app = proc do
...@@ -19,9 +21,15 @@ RSpec.describe 'Embedded Snippets' do ...@@ -19,9 +21,15 @@ RSpec.describe 'Embedded Snippets' do
visit("http://#{server.host}:#{server.port}/embedded_snippet.html") visit("http://#{server.host}:#{server.port}/embedded_snippet.html")
expect(page).to have_content("random_dir.rb") wait_for_requests
expect(page).to have_content("require 'fileutils'")
expect(page).to have_link('Open raw') aggregate_failures do
expect(page).to have_link('Download') blobs.each do |blob|
expect(page).to have_content(blob.path)
expect(page.find(".snippet-file-content .blob-content[data-blob-id='#{blob.id}'] code")).to have_content(blob.data.squish)
expect(page).to have_link('Open raw', href: /-\/snippets\/#{snippet.id}\/raw\/master\/#{blob.path}/)
expect(page).to have_link('Download', href: /-\/snippets\/#{snippet.id}\/raw\/master\/#{blob.path}\?inline=false/)
end
end
end end
end end
...@@ -6,21 +6,29 @@ RSpec.describe SnippetsHelper do ...@@ -6,21 +6,29 @@ RSpec.describe SnippetsHelper do
include Gitlab::Routing include Gitlab::Routing
include IconsHelper include IconsHelper
let_it_be(:public_personal_snippet) { create(:personal_snippet, :public) } let_it_be(:public_personal_snippet) { create(:personal_snippet, :public, :repository) }
let_it_be(:public_project_snippet) { create(:project_snippet, :public) } let_it_be(:public_project_snippet) { create(:project_snippet, :public, :repository) }
describe '#embedded_raw_snippet_button' do describe '#embedded_raw_snippet_button' do
subject { embedded_raw_snippet_button.to_s } let(:blob) { snippet.blobs.first }
let(:ref) { blob.repository.root_ref }
it 'returns view raw button of embedded snippets for personal snippets' do subject { embedded_raw_snippet_button(snippet, blob) }
@snippet = create(:personal_snippet, :public)
expect(subject).to eq(download_link("http://test.host/-/snippets/#{@snippet.id}/raw")) context 'for Personal Snippets' do
let(:snippet) { public_personal_snippet }
it 'returns view raw button of embedded snippets' do
expect(subject).to eq(download_link("http://test.host/-/snippets/#{snippet.id}/raw/#{ref}/#{blob.path}"))
end
end end
it 'returns view raw button of embedded snippets for project snippets' do context 'for Project Snippets' do
@snippet = create(:project_snippet, :public) let(:snippet) { public_project_snippet }
expect(subject).to eq(download_link("http://test.host/#{@snippet.project.path_with_namespace}/-/snippets/#{@snippet.id}/raw")) it 'returns view raw button of embedded snippets' do
expect(subject).to eq(download_link("http://test.host/#{snippet.project.path_with_namespace}/-/snippets/#{snippet.id}/raw/#{ref}/#{blob.path}"))
end
end end
def download_link(url) def download_link(url)
...@@ -29,18 +37,25 @@ RSpec.describe SnippetsHelper do ...@@ -29,18 +37,25 @@ RSpec.describe SnippetsHelper do
end end
describe '#embedded_snippet_download_button' do describe '#embedded_snippet_download_button' do
subject { embedded_snippet_download_button } let(:blob) { snippet.blobs.first }
let(:ref) { blob.repository.root_ref }
it 'returns download button of embedded snippets for personal snippets' do subject { embedded_snippet_download_button(snippet, blob) }
@snippet = create(:personal_snippet, :public)
expect(subject).to eq(download_link("http://test.host/-/snippets/#{@snippet.id}/raw")) context 'for Personal Snippets' do
let(:snippet) { public_personal_snippet }
it 'returns download button of embedded snippets' do
expect(subject).to eq(download_link("http://test.host/-/snippets/#{snippet.id}/raw/#{ref}/#{blob.path}"))
end
end end
it 'returns download button of embedded snippets for project snippets' do context 'for Project Snippets' do
@snippet = create(:project_snippet, :public) let(:snippet) { public_project_snippet }
expect(subject).to eq(download_link("http://test.host/#{@snippet.project.path_with_namespace}/-/snippets/#{@snippet.id}/raw")) it 'returns download button of embedded snippets' do
expect(subject).to eq(download_link("http://test.host/#{snippet.project.path_with_namespace}/-/snippets/#{snippet.id}/raw/#{ref}/#{blob.path}"))
end
end end
def download_link(url) def download_link(url)
......
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