Sync snippet after git action

parent 67502abd
......@@ -197,6 +197,8 @@ class Snippet < ApplicationRecord
end
def blobs
return [] unless repository_exists?
repository.ls_files(repository.root_ref).map { |file| Blob.lazy(self, repository.root_ref, file) }
end
......
......@@ -77,6 +77,11 @@ class PostReceive # rubocop:disable Scalability/IdempotentWorker
return false unless user
# We can remove once we implement multi-file snippets
# https://gitlab.com/gitlab-org/gitlab/-/issues/39269
blob = snippet.blobs.first
snippet.update(file_name: blob.path, content: blob.data) if blob
# At the moment, we only expires the repository caches.
# In the future we might need to call ProjectCacheWorker
# (or the custom class we create) to update the snippet
......
---
title: Sync snippet after Git action
merge_request: 26565
author:
type: changed
......@@ -511,6 +511,32 @@ describe Snippet do
end
end
describe '#blobs' do
let(:snippet) { create(:snippet) }
context 'when repository does not exist' do
it 'returns empty array' do
expect(snippet.blobs).to be_empty
end
end
context 'when repository exists' do
let(:snippet) { create(:snippet, :repository) }
it 'returns array of blobs' do
expect(snippet.blobs).to all(be_a(Blob))
end
end
it 'returns a blob representing the snippet data' do
blob = snippet.blob
expect(blob).to be_a(Blob)
expect(blob.path).to eq(snippet.file_name)
expect(blob.data).to eq(snippet.content)
end
end
describe '#to_json' do
let(:snippet) { build(:snippet) }
......
......@@ -421,17 +421,35 @@ describe PostReceive do
perform
end
end
it 'updates the snippet db information' do
blob = snippet.blobs.first
expect(snippet).to receive(:update).with(file_name: blob.path, content: blob.data)
perform
end
context 'when snippet does not have any blob' do
it 'does not update snippet db information' do
allow(snippet).to receive(:blobs).and_return([])
expect(snippet).not_to receive(:update)
perform
end
end
end
end
context 'with PersonalSnippet' do
let!(:snippet) { create(:personal_snippet, author: project.owner) }
let!(:snippet) { create(:personal_snippet, :repository, author: project.owner) }
it_behaves_like 'snippet changes actions'
end
context 'with ProjectSnippet' do
let!(:snippet) { create(:project_snippet, project: project, author: project.owner) }
let!(:snippet) { create(:project_snippet, :repository, project: project, author: project.owner) }
it_behaves_like 'snippet changes actions'
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