diff options
author | Kamil TrzciĆski <ayufan@ayufan.eu> | 2018-04-06 00:07:36 +0200 |
---|---|---|
committer | Mayra Cabrera <mcabrera@gitlab.com> | 2018-04-06 21:20:16 -0500 |
commit | 2c6c61815edada16c4477c938209c24c647e1798 (patch) | |
tree | 1d896b8ac1c5160b33116bc6d60e785df51cce79 | |
parent | 6b2954ec8a2e17c913872c32386cb2b08f2db1c2 (diff) | |
download | gitlab-ce-2c6c61815edada16c4477c938209c24c647e1798.tar.gz |
Get rid of Redis when dealing with deploy tokens
We use controller actions to pass a newly created token and errors
-rw-r--r-- | app/controllers/projects/deploy_tokens_controller.rb | 10 | ||||
-rw-r--r-- | app/controllers/projects/settings/repository_controller.rb | 28 | ||||
-rw-r--r-- | app/helpers/deploy_tokens_helper.rb | 4 | ||||
-rw-r--r-- | app/presenters/projects/settings/deploy_tokens_presenter.rb | 23 | ||||
-rw-r--r-- | app/services/deploy_tokens/create_service.rb | 31 | ||||
-rw-r--r-- | app/views/projects/deploy_tokens/_form.html.haml | 2 | ||||
-rw-r--r-- | app/views/projects/deploy_tokens/_index.html.haml | 16 | ||||
-rw-r--r-- | app/views/projects/deploy_tokens/_new_deploy_token.html.haml | 12 | ||||
-rw-r--r-- | app/views/projects/deploy_tokens/_table.html.haml | 2 | ||||
-rw-r--r-- | config/routes/project.rb | 6 | ||||
-rw-r--r-- | db/migrate/20180405142733_create_project_deploy_tokens.rb | 5 |
11 files changed, 52 insertions, 87 deletions
diff --git a/app/controllers/projects/deploy_tokens_controller.rb b/app/controllers/projects/deploy_tokens_controller.rb index e3a2e5697b5..5d236966894 100644 --- a/app/controllers/projects/deploy_tokens_controller.rb +++ b/app/controllers/projects/deploy_tokens_controller.rb @@ -1,16 +1,6 @@ class Projects::DeployTokensController < Projects::ApplicationController before_action :authorize_admin_project! - def create - @token = DeployTokens::CreateService.new(@project, current_user, deploy_token_params).execute - - if @token.valid? - flash[:notice] = 'Your new project deploy token has been created.' - end - - redirect_to project_settings_repository_path(project) - end - def revoke @token = @project.deploy_tokens.find(params[:id]) @token.revoke! diff --git a/app/controllers/projects/settings/repository_controller.rb b/app/controllers/projects/settings/repository_controller.rb index b6b8963948c..c085870dacd 100644 --- a/app/controllers/projects/settings/repository_controller.rb +++ b/app/controllers/projects/settings/repository_controller.rb @@ -4,14 +4,30 @@ module Projects before_action :authorize_admin_project! def show + render_show + end + + def create_deploy_token + @new_deploy_token = DeployTokens::CreateService.new(@project, current_user, deploy_token_params).execute + + if @new_deploy_token.valid? + flash[:notice] = 'Your new project deploy token has been created.' + end + + render_show + end + + private + + def render_show @deploy_keys = DeployKeysPresenter.new(@project, current_user: current_user) @deploy_tokens = DeployTokensPresenter.new(@project.deploy_tokens.active, current_user: current_user, project: project) define_deploy_token define_protected_refs - end - private + render 'show' + end def define_protected_refs @protected_branches = @project.protected_branches.order(:name).page(params[:page]) @@ -55,9 +71,11 @@ module Projects end def define_deploy_token - attributes = @deploy_tokens.attributes_deploy_token - @deploy_token = DeployToken.new(attributes) - @deploy_token.valid? unless attributes.empty? + @new_deploy_token ||= DeployToken.new + end + + def deploy_token_params + params.require(:deploy_token).permit(:name, :expires_at, :read_repository, :read_registry) end end end diff --git a/app/helpers/deploy_tokens_helper.rb b/app/helpers/deploy_tokens_helper.rb index 68e1d56e951..3ca567a2bdd 100644 --- a/app/helpers/deploy_tokens_helper.rb +++ b/app/helpers/deploy_tokens_helper.rb @@ -1,6 +1,6 @@ module DeployTokensHelper - def expand_deploy_tokens_section?(temporal_token, deploy_token) - temporal_token.present? || + def expand_deploy_tokens_section?(deploy_token) + deploy_token.persisted? || deploy_token.errors.present? || Rails.env.test? end diff --git a/app/presenters/projects/settings/deploy_tokens_presenter.rb b/app/presenters/projects/settings/deploy_tokens_presenter.rb index f052324a219..ee82e516bfc 100644 --- a/app/presenters/projects/settings/deploy_tokens_presenter.rb +++ b/app/presenters/projects/settings/deploy_tokens_presenter.rb @@ -14,29 +14,6 @@ module Projects yield deploy_token end end - - def temporal_token - @temporal_token ||= Gitlab::Redis::SharedState.with do |redis| - token = redis.get(deploy_token_key) - redis.del(deploy_token_key) - token - end - end - - def attributes_deploy_token - @attributes_deploy_token ||= Gitlab::Redis::SharedState.with do |redis| - attributes_key = deploy_token_key + ":attributes" - attributes_content = redis.get(attributes_key) || '{}' - redis.del(attributes_key) - JSON.parse(attributes_content) - end - end - - private - - def deploy_token_key - @deploy_token_key ||= DeployToken.redis_shared_state_key(current_user.id) - end end end end diff --git a/app/services/deploy_tokens/create_service.rb b/app/services/deploy_tokens/create_service.rb index 0555d62540c..a5d31dd9fa5 100644 --- a/app/services/deploy_tokens/create_service.rb +++ b/app/services/deploy_tokens/create_service.rb @@ -1,36 +1,7 @@ module DeployTokens class CreateService < BaseService def execute - @project.deploy_tokens.build.tap do |deploy_token| - deploy_token.attributes = params - deploy_token.save - store_deploy_token_info_in_redis(deploy_token) - end - end - - private - - def store_deploy_token_info_in_redis(deploy_token) - deploy_token_key = DeployToken.redis_shared_state_key(current_user.id) - - if deploy_token.persisted? - store_in_redis(deploy_token_key, deploy_token.token) - else - store_deploy_attributes(deploy_token_key, deploy_token) - end - end - - def store_deploy_attributes(deploy_token_key, deploy_token) - attributes = deploy_token.attributes.slice("name", "expires_at") - deploy_token_attributes_key = deploy_token_key + ":attributes" - - store_in_redis(deploy_token_attributes_key, attributes.to_json) - end - - def store_in_redis(key, value) - Gitlab::Redis::SharedState.with do |redis| - redis.set(key, value, ex: 3.minutes) - end + @project.deploy_tokens.build(params).tap(&:save) end end end diff --git a/app/views/projects/deploy_tokens/_form.html.haml b/app/views/projects/deploy_tokens/_form.html.haml index 3e83a2aae46..d027b2ca4b4 100644 --- a/app/views/projects/deploy_tokens/_form.html.haml +++ b/app/views/projects/deploy_tokens/_form.html.haml @@ -1,7 +1,7 @@ %p.profile-settings-content = s_("DeployTokens|Pick a name for the application, and we'll give you a unique deploy token.") -= form_for token, url: project_deploy_tokens_path(project), method: :post do |f| += form_for token, url: create_deploy_token_namespace_project_settings_repository_path(project.namespace, project), method: :post do |f| = form_errors(token) .form-group diff --git a/app/views/projects/deploy_tokens/_index.html.haml b/app/views/projects/deploy_tokens/_index.html.haml index 2ef9d1fb4a4..50e5950ced4 100644 --- a/app/views/projects/deploy_tokens/_index.html.haml +++ b/app/views/projects/deploy_tokens/_index.html.haml @@ -1,4 +1,4 @@ -- expanded = expand_deploy_tokens_section?(@deploy_tokens.temporal_token, @deploy_token) +- expanded = expand_deploy_tokens_section?(@new_deploy_token) %section.settings.no-animate{ class: ('expanded' if expanded) } .settings-header @@ -8,11 +8,11 @@ %p = s_('DeployTokens|Deploy tokens allow read-only access to your repository and registry images.') .settings-content - - if @deploy_tokens.temporal_token - = render 'projects/deploy_tokens/new_deploy_token', new_token: @deploy_tokens.temporal_token - - %h5.prepend-top-0 - = s_('DeployTokens|Add a deploy token') - = render 'projects/deploy_tokens/form', project: @project, token: @deploy_token, presenter: @deploy_tokens - %hr + - if @new_deploy_token.persisted? + = render 'projects/deploy_tokens/new_deploy_token', deploy_token: @new_deploy_token + - else + %h5.prepend-top-0 + = s_('DeployTokens|Add a deploy token') + = render 'projects/deploy_tokens/form', project: @project, token: @new_deploy_token, presenter: @deploy_tokens + %hr = render 'projects/deploy_tokens/table', project: @project, active_tokens: @deploy_tokens diff --git a/app/views/projects/deploy_tokens/_new_deploy_token.html.haml b/app/views/projects/deploy_tokens/_new_deploy_token.html.haml index a701817ddab..82268e7900c 100644 --- a/app/views/projects/deploy_tokens/_new_deploy_token.html.haml +++ b/app/views/projects/deploy_tokens/_new_deploy_token.html.haml @@ -1,9 +1,15 @@ .created-deploy-token-container %h5.prepend-top-0 = s_('DeployTokens|Your New Deploy Token') + + .form-group + = text_field_tag 'deploy-token-user', deploy_token.username, readonly: true, class: 'deploy-token-field form-control js-select-on-focus' + = clipboard_button(text: deploy_token.username, title: s_('DeployTokens|Copy deploy token username to clipboard'), placement: 'left') + %span.help-block.prepend-top-5.text-success= s_("DeployTokens|Use this username as a login.") + .form-group - = text_field_tag 'deploy-token', new_token, readonly: true, class: 'deploy-token-field form-control js-select-on-focus' - = clipboard_button(text: new_token, title: s_('DeployTokens|Copy deploy token to clipboard'), placement: 'left') - %span.deploy-token-help-block.prepend-top-5.text-danger= s_("DeployTokens|Make sure you save it - you won't be able to access it again.") + = text_field_tag 'deploy-token', deploy_token.token, readonly: true, class: 'deploy-token-field form-control js-select-on-focus' + = clipboard_button(text: deploy_token.token, title: s_('DeployTokens|Copy deploy token to clipboard'), placement: 'left') + %span.help-block.prepend-top-5.text-danger= s_("DeployTokens|Use this token as a password. Make sure you save it - you won't be able to access it again.") %hr diff --git a/app/views/projects/deploy_tokens/_table.html.haml b/app/views/projects/deploy_tokens/_table.html.haml index 7ef135df0f6..5013a9b250d 100644 --- a/app/views/projects/deploy_tokens/_table.html.haml +++ b/app/views/projects/deploy_tokens/_table.html.haml @@ -6,6 +6,7 @@ %thead %tr %th= s_('DeployTokens|Name') + %th= s_('DeployTokens|Username') %th= s_('DeployTokens|Created') %th= s_('DeployTokens|Expires') %th= s_('DeployTokens|Scopes') @@ -14,6 +15,7 @@ - active_tokens.each do |token| %tr %td= token.name + %td= token.username %td= token.created_at.to_date.to_s(:medium) %td - if token.expires? diff --git a/config/routes/project.rb b/config/routes/project.rb index 27d3569829f..e760a9d7ed2 100644 --- a/config/routes/project.rb +++ b/config/routes/project.rb @@ -88,7 +88,7 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do end end - resources :deploy_tokens, constraints: { id: /\d+/ }, only: :create do + resources :deploy_tokens, constraints: { id: /\d+/ }, only: [] do member do put :revoke end @@ -432,7 +432,9 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do post :reset_cache end resource :integrations, only: [:show] - resource :repository, only: [:show], controller: :repository + resource :repository, only: [:show], controller: :repository do + post :create_deploy_token, path: 'deploy_token/create' + end end # Since both wiki and repository routing contains wildcard characters diff --git a/db/migrate/20180405142733_create_project_deploy_tokens.rb b/db/migrate/20180405142733_create_project_deploy_tokens.rb index 6cfb092a420..adc4c526c9b 100644 --- a/db/migrate/20180405142733_create_project_deploy_tokens.rb +++ b/db/migrate/20180405142733_create_project_deploy_tokens.rb @@ -7,13 +7,12 @@ class CreateProjectDeployTokens < ActiveRecord::Migration create_table :project_deploy_tokens do |t| t.integer :project_id, null: false t.integer :deploy_token_id, null: false + t.timestamps null: false t.foreign_key :deploy_tokens, column: :deploy_token_id, on_delete: :cascade t.foreign_key :projects, column: :project_id, on_delete: :cascade - t.timestamps null: false + t.index [:project_id, :deploy_token_id], unique: true end - - add_index :project_deploy_tokens, [:project_id, :deploy_token_id], unique: true end end |