Read about our upcoming Code of Conduct on this issue

This instance was upgraded to Heptapod 0.28.0 today

create_service.rb 1.02 KB
Newer Older
1
2
3
module DeployTokens
  class CreateService < BaseService
    def execute
Mayra Cabrera's avatar
Mayra Cabrera committed
4
5
6
7
8
      @project.deploy_tokens.build.tap do |deploy_token|
        deploy_token.attributes = params
        deploy_token.save
        store_deploy_token_info_in_redis(deploy_token)
      end
9
10
11
12
    end

    private

Mayra Cabrera's avatar
Mayra Cabrera committed
13
    def store_deploy_token_info_in_redis(deploy_token)
Mayra Cabrera's avatar
Mayra Cabrera committed
14
      deploy_token_key = DeployToken.redis_shared_state_key(current_user.id)
Mayra Cabrera's avatar
Mayra Cabrera committed
15
16
17
18
19

      if deploy_token.persisted?
        store_in_redis(deploy_token_key, deploy_token.token)
      else
        store_deploy_attributes(deploy_token_key, deploy_token)
20
21
22
      end
    end

Mayra Cabrera's avatar
Mayra Cabrera committed
23
24
25
26
27
28
29
30
31
    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|
Mayra Cabrera's avatar
Mayra Cabrera committed
32
        redis.set(key, value, ex: 3.minutes)
Mayra Cabrera's avatar
Mayra Cabrera committed
33
      end
34
35
36
    end
  end
end