Commit cdac54e2 authored by Mayra Cabrera's avatar Mayra Cabrera

Refactor deploy token methods on Ci::Build

Also include a class method for retriving the gitlab_deploy_token on
DeployTokens
parent 800ee75a
...@@ -605,8 +605,7 @@ module Ci ...@@ -605,8 +605,7 @@ module Ci
.append(key: 'CI_REGISTRY_USER', value: CI_REGISTRY_USER) .append(key: 'CI_REGISTRY_USER', value: CI_REGISTRY_USER)
.append(key: 'CI_REGISTRY_PASSWORD', value: token, public: false) .append(key: 'CI_REGISTRY_PASSWORD', value: token, public: false)
.append(key: 'CI_REPOSITORY_URL', value: repo_url, public: false) .append(key: 'CI_REPOSITORY_URL', value: repo_url, public: false)
.concat(deploy_token_variables)
variables.concat(deploy_token_variables) if gitlab_deploy_token
end end
end end
...@@ -659,8 +658,10 @@ module Ci ...@@ -659,8 +658,10 @@ module Ci
def deploy_token_variables def deploy_token_variables
Gitlab::Ci::Variables::Collection.new.tap do |variables| Gitlab::Ci::Variables::Collection.new.tap do |variables|
break variables unless gitlab_deploy_token
variables.append(key: 'CI_DEPLOY_USER', value: gitlab_deploy_token.name) variables.append(key: 'CI_DEPLOY_USER', value: gitlab_deploy_token.name)
variables.append(key: 'CI_DEPLOY_PASSWORD', value: gitlab_deploy_token.token) variables.append(key: 'CI_DEPLOY_PASSWORD', value: gitlab_deploy_token.token, public: false)
end end
end end
......
...@@ -18,6 +18,10 @@ class DeployToken < ActiveRecord::Base ...@@ -18,6 +18,10 @@ class DeployToken < ActiveRecord::Base
scope :active, -> { where("revoked = false AND expires_at >= NOW()") } scope :active, -> { where("revoked = false AND expires_at >= NOW()") }
def self.gitlab_deploy_token
active.find_by(name: GITLAB_DEPLOY_TOKEN_NAME)
end
def revoke! def revoke!
update!(revoked: true) update!(revoked: true)
end end
......
...@@ -1880,8 +1880,7 @@ class Project < ActiveRecord::Base ...@@ -1880,8 +1880,7 @@ class Project < ActiveRecord::Base
end end
def gitlab_deploy_token def gitlab_deploy_token
@gitlab_deploy_token ||= @gitlab_deploy_token ||= deploy_tokens.gitlab_deploy_token
deploy_tokens.active.find_by(name: DeployToken::GITLAB_DEPLOY_TOKEN_NAME)
end end
private private
......
...@@ -2042,7 +2042,7 @@ describe Ci::Build do ...@@ -2042,7 +2042,7 @@ describe Ci::Build do
let(:deploy_token_variables) do let(:deploy_token_variables) do
[ [
{ key: 'CI_DEPLOY_USER', value: deploy_token.name, public: true }, { key: 'CI_DEPLOY_USER', value: deploy_token.name, public: true },
{ key: 'CI_DEPLOY_PASSWORD', value: deploy_token.token, public: true } { key: 'CI_DEPLOY_PASSWORD', value: deploy_token.token, public: false }
] ]
end end
......
...@@ -142,4 +142,23 @@ describe DeployToken do ...@@ -142,4 +142,23 @@ describe DeployToken do
end end
end end
end end
describe '.gitlab_deploy_token' do
let(:project) { create(:project ) }
subject { project.deploy_tokens.gitlab_deploy_token }
context 'with a gitlab deploy token associated' do
it 'should return the gitlab deploy token' do
deploy_token = create(:deploy_token, :gitlab_deploy_token, projects: [project])
is_expected.to eq(deploy_token)
end
end
context 'with no gitlab deploy token associated' do
it 'should return nil' do
is_expected.to be_nil
end
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment