Commit 762df500 authored by Markus Koller's avatar Markus Koller

Merge branch 'fj-223700-add-count-logic-to-snippet-statistics' into 'master'

Add counting logic to snippet statistics

See merge request gitlab-org/gitlab!35118
parents e4565108 c49821c5
......@@ -69,6 +69,7 @@ class Snippet < ApplicationRecord
validates :visibility_level, inclusion: { in: Gitlab::VisibilityLevel.values }
after_save :store_mentions!, if: :any_mentionable_attributes_changed?
after_create :create_statistics
# Scopes
scope :are_internal, -> { where(visibility_level: Snippet::INTERNAL) }
......
......@@ -4,4 +4,32 @@ class SnippetStatistics < ApplicationRecord
belongs_to :snippet
validates :snippet, presence: true
delegate :repository, to: :snippet
def update_commit_count
self.commit_count = repository.commit_count
end
def update_repository_size
self.repository_size = repository.size.megabytes
end
def update_file_count
count = if snippet.repository_exists?
repository.ls_files(repository.root_ref).size
else
0
end
self.file_count = count
end
def refresh!
update_commit_count
update_repository_size
update_file_count
save!
end
end
---
title: Add snippet statistics logic
merge_request: 35118
author:
type: added
......@@ -92,6 +92,17 @@ describe Snippet do
end
end
describe 'callbacks' do
it 'creates snippet statistics when the snippet is created' do
snippet = build(:snippet)
expect(snippet.statistics).to be_nil
snippet.save
expect(snippet.statistics).to be_persisted
end
end
describe '#to_reference' do
context 'when snippet belongs to a project' do
let(:project) { build(:project, name: 'sample-project') }
......
......@@ -3,6 +3,87 @@
require 'spec_helper'
describe SnippetStatistics do
let_it_be(:snippet_without_repo) { create(:snippet) }
let_it_be(:snippet_with_repo) { create(:snippet, :repository) }
let(:statistics) { snippet_with_repo.statistics }
it { is_expected.to belong_to(:snippet) }
it { is_expected.to validate_presence_of(:snippet) }
describe '#update_commit_count' do
subject { statistics.update_commit_count }
it 'updates the count of commits' do
commit_count = snippet_with_repo.repository.commit_count
subject
expect(statistics.commit_count).to eq commit_count
end
context 'when the snippet does not have a repository' do
let(:statistics) { snippet_without_repo.statistics }
it 'returns 0' do
expect(subject).to eq 0
expect(statistics.commit_count).to eq 0
end
end
end
describe '#update_file_count' do
subject { statistics.update_file_count }
it 'updates the count of files' do
file_count = snippet_with_repo.repository.ls_files(nil).count
subject
expect(statistics.file_count).to eq file_count
end
context 'when the snippet does not have a repository' do
let(:statistics) { snippet_without_repo.statistics }
it 'returns 0' do
expect(subject).to eq 0
expect(statistics.file_count).to eq 0
end
end
end
describe '#update_repository_size' do
subject { statistics.update_repository_size }
it 'updates the repository_size' do
repository_size = snippet_with_repo.repository.size.megabytes.to_i
subject
expect(statistics.repository_size).to eq repository_size
end
context 'when the snippet does not have a repository' do
let(:statistics) { snippet_without_repo.statistics }
it 'returns 0' do
expect(subject).to eq 0
expect(statistics.repository_size).to eq 0
end
end
end
describe '#refresh!' do
subject { statistics.refresh! }
it 'retrieves and saves statistic data from repository' do
expect(statistics).to receive(:update_commit_count)
expect(statistics).to receive(:update_file_count)
expect(statistics).to receive(:update_repository_size)
expect(statistics).to receive(:save!)
subject
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