Skip to content

Commit

Permalink
Merge pull request #2954 from zendesk/grosser/refs
Browse files Browse the repository at this point in the history
remove indirections from references controller/service/settings
  • Loading branch information
grosser authored Sep 25, 2018
2 parents 1031f31 + 8b703ed commit 2723fa1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 78 deletions.
8 changes: 6 additions & 2 deletions app/controllers/references_controller.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 0 additions & 32 deletions lib/references_service.rb

This file was deleted.

27 changes: 18 additions & 9 deletions test/controllers/references_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 0 additions & 34 deletions test/lib/references_service_test.rb

This file was deleted.

0 comments on commit 2723fa1

Please sign in to comment.