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
end
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
......@@ -55,10 +55,9 @@ module SnippetsActions
# rubocop:disable Gitlab/ModuleWithInstanceVariables
def show
conditionally_expand_blob(blob)
respond_to do |format|
format.html do
conditionally_expand_blob(blob)
@note = Note.new(noteable: @snippet, project: @snippet.project)
@noteable = @snippet
......@@ -68,11 +67,14 @@ module SnippetsActions
end
format.json do
conditionally_expand_blob(blob)
render_blob_json(blob)
end
format.js do
if @snippet.embeddable?
conditionally_expand_blobs(blobs)
render 'shared/snippets/show'
else
head :not_found
......@@ -109,13 +111,15 @@ module SnippetsActions
# rubocop:disable Gitlab/ModuleWithInstanceVariables
def blob
return unless snippet
@blob ||= blobs.first
end
@blob ||= if snippet.empty_repo?
snippet.blob
else
snippet.blobs.first
end
def blobs
@blobs ||= if snippet.empty_repo?
[snippet.blob]
else
snippet.blobs
end
end
# rubocop:enable Gitlab/ModuleWithInstanceVariables
......
# frozen_string_literal: true
class Projects::Snippets::BlobsController < Projects::Snippets::ApplicationController
include Snippets::BlobsActions
include ::Snippets::BlobsActions
end
......@@ -74,21 +74,20 @@ module SnippetsHelper
end
end
def embedded_raw_snippet_button
blob = @snippet.blob
def embedded_raw_snippet_button(snippet, blob)
return if blob.empty? || blob.binary? || blob.stored_externally?
link_to(external_snippet_icon('doc-code'),
gitlab_raw_snippet_url(@snippet),
gitlab_raw_snippet_blob_url(snippet, blob.path),
class: 'btn',
target: '_blank',
rel: 'noopener noreferrer',
title: 'Open raw')
end
def embedded_snippet_download_button
def embedded_snippet_download_button(snippet, blob)
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',
target: '_blank',
title: 'Download',
......
......@@ -5,17 +5,17 @@
%strong.file-title-name
%a.gitlab-embedded-snippets-title{ href: url_for(only_path: false, overwrite_params: nil) }
= @blob.name
= blob.name
%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' }
%img.gitlab-logo{ src: image_url('ext_snippet_icons/logo.svg'), alt: "GitLab logo" }
.file-actions.d-none.d-sm-block
.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
= 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(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
describe "GET #show for embeddable content" do
let(:project_snippet) { create(:project_snippet, :repository, snippet_permission, project: project, author: user) }
let(:extra_params) { {} }
before do
sign_in(user)
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
let(:snippet_permission) { :private }
......@@ -436,7 +437,29 @@ RSpec.describe Projects::SnippetsController do
context 'when snippet is public' do
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
context 'when the project is private' do
......
......@@ -3,11 +3,13 @@
require 'spec_helper'
RSpec.describe 'Embedded Snippets' do
let(:snippet) { create(:personal_snippet, :public, file_name: 'random_dir.rb', content: content) }
let(:content) { "require 'fileutils'\nFileUtils.mkdir_p 'some/random_dir'\n" }
let_it_be(:snippet) { create(:personal_snippet, :public, :repository) }
let(:blobs) { snippet.blobs.first(3) }
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>"
rack_app = proc do
......@@ -19,9 +21,15 @@ RSpec.describe 'Embedded Snippets' do
visit("http://#{server.host}:#{server.port}/embedded_snippet.html")
expect(page).to have_content("random_dir.rb")
expect(page).to have_content("require 'fileutils'")
expect(page).to have_link('Open raw')
expect(page).to have_link('Download')
wait_for_requests
aggregate_failures do
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
......@@ -6,21 +6,29 @@ RSpec.describe SnippetsHelper do
include Gitlab::Routing
include IconsHelper
let_it_be(:public_personal_snippet) { create(:personal_snippet, :public) }
let_it_be(:public_project_snippet) { create(:project_snippet, :public) }
let_it_be(:public_personal_snippet) { create(:personal_snippet, :public, :repository) }
let_it_be(:public_project_snippet) { create(:project_snippet, :public, :repository) }
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
@snippet = create(:personal_snippet, :public)
expect(subject).to eq(download_link("http://test.host/-/snippets/#{@snippet.id}/raw"))
subject { embedded_raw_snippet_button(snippet, blob) }
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
it 'returns view raw button of embedded snippets for project snippets' do
@snippet = create(:project_snippet, :public)
context 'for Project Snippets' do
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
def download_link(url)
......@@ -29,18 +37,25 @@ RSpec.describe SnippetsHelper do
end
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
@snippet = create(:personal_snippet, :public)
subject { embedded_snippet_download_button(snippet, blob) }
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
it 'returns download button of embedded snippets for project snippets' do
@snippet = create(:project_snippet, :public)
context 'for Project Snippets' do
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
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