Commit f627fdb8 authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch '293720-agent-created-by' into 'master'

Add created_by_user to cluster_agents

See merge request gitlab-org/gitlab!54011
parents f6e9e634 887c973a
......@@ -4,6 +4,7 @@ module Clusters
class Agent < ApplicationRecord
self.table_name = 'cluster_agents'
belongs_to :created_by_user, class_name: 'User', optional: true
belongs_to :project, class_name: '::Project' # Otherwise, it will load ::Clusters::Project
has_many :agent_tokens, class_name: 'Clusters::AgentToken'
......
---
title: Add created_by_user to cluster agents
merge_request: 54011
author:
type: added
# frozen_string_literal: true
class AddCreatedByToClusterAgent < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
INDEX_NAME = 'index_cluster_agents_on_created_by_user_id'
disable_ddl_transaction!
def up
unless column_exists?(:cluster_agents, :created_by_user_id)
with_lock_retries do
add_column :cluster_agents, :created_by_user_id, :bigint
end
end
add_concurrent_index :cluster_agents, :created_by_user_id, name: INDEX_NAME
add_concurrent_foreign_key :cluster_agents, :users, column: :created_by_user_id, on_delete: :nullify
end
def down
with_lock_retries do
remove_column :cluster_agents, :created_by_user_id
end
end
end
8c676b4142db828b1d2d5dc6bd891eb929d12ab13e9073693ab7d830bcea599a
\ No newline at end of file
......@@ -11043,6 +11043,7 @@ CREATE TABLE cluster_agents (
updated_at timestamp with time zone NOT NULL,
project_id bigint NOT NULL,
name text NOT NULL,
created_by_user_id bigint,
CONSTRAINT check_3498369510 CHECK ((char_length(name) <= 255))
);
......@@ -21848,6 +21849,8 @@ CREATE INDEX index_cluster_agent_tokens_on_created_by_user_id ON cluster_agent_t
CREATE UNIQUE INDEX index_cluster_agent_tokens_on_token_encrypted ON cluster_agent_tokens USING btree (token_encrypted);
CREATE INDEX index_cluster_agents_on_created_by_user_id ON cluster_agents USING btree (created_by_user_id);
CREATE UNIQUE INDEX index_cluster_agents_on_project_id_and_name ON cluster_agents USING btree (project_id, name);
CREATE UNIQUE INDEX index_cluster_groups_on_cluster_id_and_group_id ON cluster_groups USING btree (cluster_id, group_id);
......@@ -24777,6 +24780,9 @@ ALTER TABLE ONLY design_management_designs_versions
ALTER TABLE ONLY analytics_devops_adoption_segments
ADD CONSTRAINT fk_f5aa768998 FOREIGN KEY (namespace_id) REFERENCES namespaces(id) ON DELETE CASCADE;
ALTER TABLE ONLY cluster_agents
ADD CONSTRAINT fk_f7d43dee13 FOREIGN KEY (created_by_user_id) REFERENCES users(id) ON DELETE SET NULL;
ALTER TABLE ONLY protected_tag_create_access_levels
ADD CONSTRAINT fk_f7dfda8c51 FOREIGN KEY (protected_tag_id) REFERENCES protected_tags(id) ON DELETE CASCADE;
......@@ -3394,6 +3394,11 @@ type ClusterAgent {
"""
createdAt: Time
"""
User object, containing information about the person who created the agent.
"""
createdByUser: User
"""
ID of the cluster agent.
"""
......
......@@ -9175,6 +9175,20 @@
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "createdByUser",
"description": "User object, containing information about the person who created the agent.",
"args": [
],
"type": {
"kind": "OBJECT",
"name": "User",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "id",
"description": "ID of the cluster agent.",
......@@ -536,6 +536,7 @@ Autogenerated return type of CiCdSettingsUpdate.
| Field | Type | Description |
| ----- | ---- | ----------- |
| `createdAt` | Time | Timestamp the cluster agent was created. |
| `createdByUser` | User | User object, containing information about the person who created the agent. |
| `id` | ID! | ID of the cluster agent. |
| `name` | String | Name of the cluster agent. |
| `project` | Project | The project this cluster agent is associated with. |
......
......@@ -14,6 +14,11 @@ module Types
null: true,
description: 'Timestamp the cluster agent was created.'
field :created_by_user,
Types::UserType,
null: true,
description: 'User object, containing information about the person who created the agent.'
field :id, GraphQL::ID_TYPE,
null: false,
description: 'ID of the cluster agent.'
......
......@@ -8,7 +8,7 @@ module Clusters
return error_not_premium_plan unless project.feature_available?(:cluster_agents)
return error_no_permissions unless cluster_agent_permissions?
agent = ::Clusters::Agent.new(name: name, project: project)
agent = ::Clusters::Agent.new(name: name, project: project, created_by_user: current_user)
if agent.save
success.merge(cluster_agent: agent)
......
......@@ -3,7 +3,7 @@
require 'spec_helper'
RSpec.describe GitlabSchema.types['ClusterAgent'] do
let(:fields) { %i[created_at id name project updated_at tokens] }
let(:fields) { %i[created_at created_by_user id name project updated_at tokens] }
it { expect(described_class.graphql_name).to eq('ClusterAgent') }
......
......@@ -53,6 +53,13 @@ RSpec.describe Clusters::Agents::CreateService do
expect(result[:message]).to be_nil
end
it 'returns agent values', :aggregate_failures do
new_agent = service.execute(name: 'new-agent')[:cluster_agent]
expect(new_agent.name).to eq('new-agent')
expect(new_agent.created_by_user).to eq(user)
end
it 'generates an error message when name is invalid' do
expect(service.execute(name: '@bad_agent_name!')).to eq({
status: :error,
......
......@@ -5,6 +5,7 @@ require 'spec_helper'
RSpec.describe Clusters::Agent do
subject { create(:cluster_agent) }
it { is_expected.to belong_to(:created_by_user).class_name('User').optional }
it { is_expected.to belong_to(:project).class_name('::Project') }
it { is_expected.to have_many(:agent_tokens).class_name('Clusters::AgentToken') }
......
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