Commit 2b86dffc authored by Adam Hegyi's avatar Adam Hegyi

Merge branch '353467_add_creator_user_id_to_deploy_keys' into 'master'

Add creator_id to deploy_tokens

See merge request gitlab-org/gitlab!83023
parents 13bb7614 40c4143a
...@@ -14,6 +14,8 @@ class DeployToken < ApplicationRecord ...@@ -14,6 +14,8 @@ class DeployToken < ApplicationRecord
default_value_for(:expires_at) { Forever.date } default_value_for(:expires_at) { Forever.date }
belongs_to :user, foreign_key: :creator_id, optional: true
has_many :project_deploy_tokens, inverse_of: :deploy_token has_many :project_deploy_tokens, inverse_of: :deploy_token
has_many :projects, through: :project_deploy_tokens has_many :projects, through: :project_deploy_tokens
......
# frozen_string_literal: true # frozen_string_literal: true
module DeployTokenMethods module DeployTokenMethods
def create_deploy_token_for(entity, params) def create_deploy_token_for(entity, current_user, params)
params[:deploy_token_type] = DeployToken.deploy_token_types["#{entity.class.name.downcase}_type".to_sym] params[:deploy_token_type] = DeployToken.deploy_token_types["#{entity.class.name.downcase}_type".to_sym]
entity.deploy_tokens.create(params) do |deploy_token| entity.deploy_tokens.create(params) do |deploy_token|
deploy_token.username = params[:username].presence deploy_token.username = params[:username].presence
deploy_token.creator_id = current_user.id
end end
end end
......
...@@ -6,7 +6,7 @@ module Groups ...@@ -6,7 +6,7 @@ module Groups
include DeployTokenMethods include DeployTokenMethods
def execute def execute
deploy_token = create_deploy_token_for(@group, params) deploy_token = create_deploy_token_for(@group, current_user, params)
create_deploy_token_payload_for(deploy_token) create_deploy_token_payload_for(deploy_token)
end end
......
...@@ -6,7 +6,7 @@ module Projects ...@@ -6,7 +6,7 @@ module Projects
include DeployTokenMethods include DeployTokenMethods
def execute def execute
deploy_token = create_deploy_token_for(@project, params) deploy_token = create_deploy_token_for(@project, current_user, params)
create_deploy_token_payload_for(deploy_token) create_deploy_token_payload_for(deploy_token)
end end
......
# frozen_string_literal: true
class AddCreatorIdToDeployTokens < Gitlab::Database::Migration[1.0]
def change
add_column :deploy_tokens, :creator_id, :bigint
end
end
# frozen_string_literal: true
class AddIndexToDeployTokensOnCreatorId < Gitlab::Database::Migration[1.0]
disable_ddl_transaction!
INDEX_NAME = 'index_deploy_tokens_on_creator_id'
def up
add_concurrent_index :deploy_tokens, :creator_id, name: INDEX_NAME
end
def down
remove_concurrent_index :deploy_tokens, :creator_id, name: INDEX_NAME
end
end
# frozen_string_literal: true
class AddUserForeignKeyToDeployTokens < Gitlab::Database::Migration[1.0]
disable_ddl_transaction!
def up
add_concurrent_foreign_key :deploy_tokens, :users, column: :creator_id, on_delete: :nullify, reverse_lock_order: true
end
def down
with_lock_retries do
remove_foreign_key :deploy_tokens, column: :creator_id
end
end
end
b2da8b0f113470d3836a361b61dc4e71b0031779b8d05af52e488bed076ebb1d
\ No newline at end of file
13acd0a862301cb5be7b9faacc02688f15e6253543fa7400c2d5f807bc254a25
\ No newline at end of file
43afb0103ee0069087d6476f5c0f617db5fdb2de3cd583762967e43d5a3a1138
\ No newline at end of file
...@@ -14256,7 +14256,8 @@ CREATE TABLE deploy_tokens ( ...@@ -14256,7 +14256,8 @@ CREATE TABLE deploy_tokens (
deploy_token_type smallint DEFAULT 2 NOT NULL, deploy_token_type smallint DEFAULT 2 NOT NULL,
write_registry boolean DEFAULT false NOT NULL, write_registry boolean DEFAULT false NOT NULL,
read_package_registry boolean DEFAULT false NOT NULL, read_package_registry boolean DEFAULT false NOT NULL,
write_package_registry boolean DEFAULT false NOT NULL write_package_registry boolean DEFAULT false NOT NULL,
creator_id bigint
); );
CREATE SEQUENCE deploy_tokens_id_seq CREATE SEQUENCE deploy_tokens_id_seq
...@@ -27373,6 +27374,8 @@ CREATE INDEX index_deploy_keys_projects_on_deploy_key_id ON deploy_keys_projects ...@@ -27373,6 +27374,8 @@ CREATE INDEX index_deploy_keys_projects_on_deploy_key_id ON deploy_keys_projects
CREATE INDEX index_deploy_keys_projects_on_project_id ON deploy_keys_projects USING btree (project_id); CREATE INDEX index_deploy_keys_projects_on_project_id ON deploy_keys_projects USING btree (project_id);
CREATE INDEX index_deploy_tokens_on_creator_id ON deploy_tokens USING btree (creator_id);
CREATE UNIQUE INDEX index_deploy_tokens_on_token ON deploy_tokens USING btree (token); CREATE UNIQUE INDEX index_deploy_tokens_on_token ON deploy_tokens USING btree (token);
CREATE INDEX index_deploy_tokens_on_token_and_expires_at_and_id ON deploy_tokens USING btree (token, expires_at, id) WHERE (revoked IS FALSE); CREATE INDEX index_deploy_tokens_on_token_and_expires_at_and_id ON deploy_tokens USING btree (token, expires_at, id) WHERE (revoked IS FALSE);
...@@ -31327,6 +31330,9 @@ ALTER TABLE ONLY terraform_state_versions ...@@ -31327,6 +31330,9 @@ ALTER TABLE ONLY terraform_state_versions
ALTER TABLE ONLY protected_environment_approval_rules ALTER TABLE ONLY protected_environment_approval_rules
ADD CONSTRAINT fk_6ee8249821 FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE; ADD CONSTRAINT fk_6ee8249821 FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
ALTER TABLE ONLY deploy_tokens
ADD CONSTRAINT fk_7082f8a288 FOREIGN KEY (creator_id) REFERENCES users(id) ON DELETE SET NULL;
ALTER TABLE ONLY protected_branch_push_access_levels ALTER TABLE ONLY protected_branch_push_access_levels
ADD CONSTRAINT fk_7111b68cdb FOREIGN KEY (group_id) REFERENCES namespaces(id) ON DELETE CASCADE; ADD CONSTRAINT fk_7111b68cdb FOREIGN KEY (group_id) REFERENCES namespaces(id) ON DELETE CASCADE;
...@@ -9,6 +9,7 @@ RSpec.describe DeployToken do ...@@ -9,6 +9,7 @@ RSpec.describe DeployToken do
it { is_expected.to have_many(:projects).through(:project_deploy_tokens) } it { is_expected.to have_many(:projects).through(:project_deploy_tokens) }
it { is_expected.to have_many :group_deploy_tokens } it { is_expected.to have_many :group_deploy_tokens }
it { is_expected.to have_many(:groups).through(:group_deploy_tokens) } it { is_expected.to have_many(:groups).through(:group_deploy_tokens) }
it { is_expected.to belong_to(:user).with_foreign_key('creator_id') }
it_behaves_like 'having unique enum values' it_behaves_like 'having unique enum values'
......
...@@ -19,6 +19,10 @@ RSpec.shared_examples 'a deploy token creation service' do ...@@ -19,6 +19,10 @@ RSpec.shared_examples 'a deploy token creation service' do
it 'returns a DeployToken' do it 'returns a DeployToken' do
expect(subject[:deploy_token]).to be_an_instance_of DeployToken expect(subject[:deploy_token]).to be_an_instance_of DeployToken
end end
it 'sets the creator_id as the id of the current_user' do
expect(subject[:deploy_token].read_attribute(:creator_id)).to eq(user.id)
end
end end
context 'when expires at date is not passed' do context 'when expires at date is not passed' do
......
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