diff --git a/app/controllers/references_controller.rb b/app/controllers/references_controller.rb index cffd59bb53..7f42c0e579 100644 --- a/app/controllers/references_controller.rb +++ b/app/controllers/references_controller.rb @@ -1,11 +1,15 @@ # frozen_string_literal: true class ReferencesController < ApplicationController include CurrentProject + CACHE_TTL = Integer(ENV['REFERENCES_CACHE_TTL'].presence || 10.minutes.to_s) before_action :authorize_project_deployer! def index - @references = ReferencesService.new(@project).find_git_references - render json: @references, root: false + references = Rails.cache.fetch("#{@project.id}_git_references", expires_in: CACHE_TTL) do + repository = @project.repository + (repository.branches + repository.tags).sort_by! { |ref| [-ref.length, ref] }.reverse! + end + render json: references, root: false end end diff --git a/config/application.rb b/config/application.rb index 81c4933ed4..63d538e7f3 100644 --- a/config/application.rb +++ b/config/application.rb @@ -116,7 +116,6 @@ class Application < Rails::Application config.samson.github.web_url = deprecated_url.call("GITHUB_WEB_URL") || 'https://github.com' config.samson.github.api_url = deprecated_url.call("GITHUB_API_URL") || 'https://api.github.com' config.samson.github.status_url = deprecated_url.call("GITHUB_STATUS_URL") || 'https://status.github.com' - config.samson.references_cache_ttl = ENV['REFERENCES_CACHE_TTL'].presence || 10.minutes # Configuration for LDAP config.samson.ldap = ActiveSupport::OrderedOptions.new diff --git a/lib/references_service.rb b/lib/references_service.rb deleted file mode 100644 index 65868f9e8f..0000000000 --- a/lib/references_service.rb +++ /dev/null @@ -1,32 +0,0 @@ -# frozen_string_literal: true -class ReferencesService - cattr_accessor(:lock_timeout, instance_writer: false) { 2.minutes } - - attr_reader :project - - def initialize(project) - @project = project - end - - def find_git_references - Rails.cache.fetch(cache_key, expires_in: references_ttl) { references_from_cached_repo } || [] - end - - private - - def repository - project.repository - end - - def cache_key - "#{project.id}_git_references" - end - - def references_ttl - Rails.application.config.samson.references_cache_ttl.to_i - end - - def references_from_cached_repo - (repository.branches + repository.tags).sort_by { |ref| [-ref.length, ref] }.reverse - end -end diff --git a/test/controllers/references_controller_test.rb b/test/controllers/references_controller_test.rb index 97c8418933..a39a2a3ea0 100644 --- a/test/controllers/references_controller_test.rb +++ b/test/controllers/references_controller_test.rb @@ -4,22 +4,31 @@ SingleCov.covered! describe ReferencesController do - before do - reference_service = ReferencesService.new(projects(:test)) - reference_service.stubs(:find_git_references).returns(%w[master test_user/test_branch]) - ReferencesService.stubs(:new).returns(reference_service) - end - as_a_viewer do unauthorized :get, :index, project_id: :foo end as_a_project_deployer do describe '#index' do - it 'returns the git references for the project test' do - get :index, params: {project_id: projects(:test).to_param, format: :json} + let(:project) { projects(:test) } + + include GitRepoTestHelper + with_project_on_remote_repo + + it 'shows git references for the project' do + get :index, params: {project_id: project.to_param, format: :json} response.content_type.must_equal 'application/json' - assigns(:references).must_equal %w[master test_user/test_branch] + JSON.parse(response.body).must_equal ["master"] + end + + it 'sorts tags/branches by length and shows new tags first' do + execute_on_remote_repo "git tag v2" + execute_on_remote_repo "git tag v1" + execute_on_remote_repo "git checkout -b foo" + execute_on_remote_repo "git checkout -b baz" + execute_on_remote_repo "git checkout -b bar" + get :index, params: {project_id: project.to_param, format: :json} + JSON.parse(response.body).must_equal ["v2", "v1", "foo", "baz", "bar", "master"] end end end diff --git a/test/lib/references_service_test.rb b/test/lib/references_service_test.rb deleted file mode 100644 index 3fdb92942f..0000000000 --- a/test/lib/references_service_test.rb +++ /dev/null @@ -1,34 +0,0 @@ -# frozen_string_literal: true -require_relative '../test_helper' - -SingleCov.covered! - -describe ReferencesService do - include GitRepoTestHelper - - let!(:repository_url) do - create_repo_with_tags - execute_on_remote_repo("git checkout -b test_user/test_branch") - end - - let!(:project) { Project.create!(name: 'test_project', repository_url: repo_temp_dir) } - - after do - FileUtils.rm_rf(repo_temp_dir) - project.repository.clean! - end - - it 'returns a sorted set of tags and branches' do - ReferencesService.new(project).find_git_references.must_equal %w[v1 master test_user/test_branch] - end - - it 'returns a sorted set of tags and branches from cached repo' do - ReferencesService.new(project).send(:references_from_cached_repo).must_equal %w[v1 master test_user/test_branch] - end - - it 'the ttl threshold should always return an integer' do - Rails.application.config.samson.stubs(:references_cache_ttl).returns('10') - references_service = ReferencesService.new(project) - references_service.send(:references_ttl).must_equal 10 - end -end