diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/projects/deploy_tokens_controller.rb | 6 | ||||
-rw-r--r-- | app/controllers/projects/settings/repository_controller.rb | 8 | ||||
-rw-r--r-- | app/helpers/deploy_tokens_helper.rb | 7 | ||||
-rw-r--r-- | app/models/deploy_token.rb | 4 | ||||
-rw-r--r-- | app/presenters/projects/settings/deploy_tokens_presenter.rb | 15 | ||||
-rw-r--r-- | app/services/deploy_tokens/create_service.rb | 32 | ||||
-rw-r--r-- | app/views/projects/deploy_tokens/_index.html.haml | 6 | ||||
-rw-r--r-- | app/views/projects/deploy_tokens/_revoke_modal.html.haml | 4 |
8 files changed, 54 insertions, 28 deletions
diff --git a/app/controllers/projects/deploy_tokens_controller.rb b/app/controllers/projects/deploy_tokens_controller.rb index ecc6db50f2f..1b1bd461b27 100644 --- a/app/controllers/projects/deploy_tokens_controller.rb +++ b/app/controllers/projects/deploy_tokens_controller.rb @@ -3,16 +3,12 @@ class Projects::DeployTokensController < Projects::ApplicationController def create @token = DeployTokens::CreateService.new(@project, current_user, deploy_token_params).execute - token_params = {} if @token.valid? flash[:notice] = 'Your new project deploy token has been created.' - else - token_params = @token.attributes.slice("name", "scopes", "expires_at") - flash[:alert] = @token.errors.full_messages.join(', ').html_safe end - redirect_to project_settings_repository_path(project, deploy_token: token_params) + redirect_to project_settings_repository_path(project) end def revoke diff --git a/app/controllers/projects/settings/repository_controller.rb b/app/controllers/projects/settings/repository_controller.rb index 28897cc5946..ab6d8b3b10c 100644 --- a/app/controllers/projects/settings/repository_controller.rb +++ b/app/controllers/projects/settings/repository_controller.rb @@ -55,11 +55,9 @@ module Projects end def define_deploy_token - @deploy_token = @project.deploy_tokens.build(deploy_token_attributes) - end - - def deploy_token_attributes - params.fetch(:deploy_token, {}).permit(:name, :expires_at, scopes: []) + attributes = @deploy_tokens.attributes_deploy_token + @deploy_token = @project.deploy_tokens.build(attributes) + @deploy_token.valid? unless attributes.empty? end end end diff --git a/app/helpers/deploy_tokens_helper.rb b/app/helpers/deploy_tokens_helper.rb new file mode 100644 index 00000000000..68e1d56e951 --- /dev/null +++ b/app/helpers/deploy_tokens_helper.rb @@ -0,0 +1,7 @@ +module DeployTokensHelper + def expand_deploy_tokens_section?(temporal_token, deploy_token) + temporal_token.present? || + deploy_token.errors.present? || + Rails.env.test? + end +end diff --git a/app/models/deploy_token.rb b/app/models/deploy_token.rb index 089b4343f41..475ad06906a 100644 --- a/app/models/deploy_token.rb +++ b/app/models/deploy_token.rb @@ -19,8 +19,8 @@ class DeployToken < ActiveRecord::Base update!(revoked: true) end - def self.redis_shared_state_key(user_id) - "gitlab:personal_access_token:#{user_id}" + def redis_shared_state_key(user_id) + "gitlab:deploy_token:#{project_id}:#{user_id}" end def active? diff --git a/app/presenters/projects/settings/deploy_tokens_presenter.rb b/app/presenters/projects/settings/deploy_tokens_presenter.rb index cbad35431b3..bdb89bb9f02 100644 --- a/app/presenters/projects/settings/deploy_tokens_presenter.rb +++ b/app/presenters/projects/settings/deploy_tokens_presenter.rb @@ -23,14 +23,23 @@ module Projects end end - def new_deploy_token - @new_deploy_token ||= Gitlab::Redis::SharedState.with do |redis| + 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 scope_descriptions @@ -41,7 +50,7 @@ module Projects end def deploy_token_key - DeployToken.redis_shared_state_key(current_user.id) + @deploy_token_key ||= project.deploy_tokens.new.redis_shared_state_key(current_user.id) end end end diff --git a/app/services/deploy_tokens/create_service.rb b/app/services/deploy_tokens/create_service.rb index e93c021557e..0332bb54167 100644 --- a/app/services/deploy_tokens/create_service.rb +++ b/app/services/deploy_tokens/create_service.rb @@ -3,22 +3,36 @@ module DeployTokens REDIS_EXPIRY_TIME = 3.minutes def execute - @deploy_token = @project.deploy_tokens.create(params) - store_in_redis if @deploy_token.persisted? - - @deploy_token + @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_in_redis - Gitlab::Redis::SharedState.with do |redis| - redis.set(deploy_token_key, @deploy_token.token, ex: REDIS_EXPIRY_TIME) + def store_deploy_token_info_in_redis(deploy_token) + deploy_token_key = deploy_token.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 deploy_token_key - DeployToken.redis_shared_state_key(current_user.id) + 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: REDIS_EXPIRY_TIME) + end end end end diff --git a/app/views/projects/deploy_tokens/_index.html.haml b/app/views/projects/deploy_tokens/_index.html.haml index 7d2bfe291c3..e1d80354f5c 100644 --- a/app/views/projects/deploy_tokens/_index.html.haml +++ b/app/views/projects/deploy_tokens/_index.html.haml @@ -1,4 +1,4 @@ -- expanded = Rails.env.test? +- expanded = expand_deploy_tokens_section?(@deploy_tokens.temporal_token, @deploy_token) %section.settings.no-animate{ class: ('expanded' if expanded) } .settings-header @@ -9,8 +9,8 @@ %p Deploy tokens allow read-only access to your repository and registry images. .settings-content - - if @deploy_tokens.new_deploy_token - = render 'projects/deploy_tokens/new_deploy_token', new_token: @deploy_tokens.new_deploy_token + - if @deploy_tokens.temporal_token + = render 'projects/deploy_tokens/new_deploy_token', new_token: @deploy_tokens.temporal_token %h5.prepend-top-0 Add a deploy token diff --git a/app/views/projects/deploy_tokens/_revoke_modal.html.haml b/app/views/projects/deploy_tokens/_revoke_modal.html.haml index a859aac015d..0700f0f7bd0 100644 --- a/app/views/projects/deploy_tokens/_revoke_modal.html.haml +++ b/app/views/projects/deploy_tokens/_revoke_modal.html.haml @@ -9,7 +9,9 @@ %span{ "aria-hidden" => "true" } × .modal-body %p - Are you sure you want to revoke this Deploy Token? This action cannot be undone + You are about to revoke + %b #{token.name}. + This action cannot be undone. .modal-footer %a{ href: '#', data: { dismiss: 'modal' }, class: 'btn btn-default' } Cancel = link_to "Revoke #{token.name}", revoke_project_deploy_token_path(project, token), method: :put, class: 'btn btn-danger' |