diff --git a/ee/app/graphql/types/remote_development/workspace_variable_input_type_enum.rb b/ee/app/graphql/types/remote_development/workspace_variable_input_type_enum.rb index c01fd833febed79be0548fc35452d32b5138f738..11ace4ed358c267c2e2c908785e59302bdb1eb28 100644 --- a/ee/app/graphql/types/remote_development/workspace_variable_input_type_enum.rb +++ b/ee/app/graphql/types/remote_development/workspace_variable_input_type_enum.rb @@ -6,11 +6,11 @@ class WorkspaceVariableInputTypeEnum < BaseEnum graphql_name 'WorkspaceVariableInputType' description 'Enum for the type of the variable to be injected in a workspace.' - from_rails_enum( - ::RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES_FOR_GRAPHQL, - description: "#{%(name).capitalize} type." - ) + include ::RemoteDevelopment::Enums::WorkspaceVariable + from_rails_enum(WORKSPACE_VARIABLE_TYPES_FOR_GRAPHQL, description: "#{%(name).capitalize} type.") + + # @return [Integer] def self.environment enum[:environment] end diff --git a/ee/app/graphql/types/remote_development/workspace_variable_type_enum.rb b/ee/app/graphql/types/remote_development/workspace_variable_type_enum.rb index 17114b61d747f70d8df027d6ef1427af2718ed73..2fcf88709dfa5591487b3cc3d1cc500fb23d9463 100644 --- a/ee/app/graphql/types/remote_development/workspace_variable_type_enum.rb +++ b/ee/app/graphql/types/remote_development/workspace_variable_type_enum.rb @@ -6,7 +6,9 @@ class WorkspaceVariableTypeEnum < BaseEnum graphql_name 'WorkspaceVariableType' description 'Enum for the type of the variable injected in a workspace.' - ::RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES.slice(:environment).each do |name, value| + include ::RemoteDevelopment::Enums::WorkspaceVariable + + WORKSPACE_VARIABLE_TYPES.slice(:environment).each do |name, value| value name.to_s.upcase, value: value, description: "#{name.to_s.capitalize} type." end diff --git a/ee/app/models/remote_development/enums/workspace.rb b/ee/app/models/remote_development/enums/workspace.rb deleted file mode 100644 index 225fa88a004fac7d9235a40f305dfd287f03e623..0000000000000000000000000000000000000000 --- a/ee/app/models/remote_development/enums/workspace.rb +++ /dev/null @@ -1,20 +0,0 @@ -# frozen_string_literal: true - -module RemoteDevelopment - module Enums - module Workspace - extend ActiveSupport::Concern - - WORKSPACE_VARIABLE_TYPES = { - environment: 0, - file: 1 - }.freeze - - # TODO: Add support for file variables in GraphQL - https://gitlab.com/gitlab-org/gitlab/-/issues/465979 - WORKSPACE_VARIABLE_TYPES_FOR_GRAPHQL = WORKSPACE_VARIABLE_TYPES - .except(:file) - .transform_keys { |key| key.to_s.upcase } - .freeze - end - end -end diff --git a/ee/app/models/remote_development/enums/workspace_variable.rb b/ee/app/models/remote_development/enums/workspace_variable.rb new file mode 100644 index 0000000000000000000000000000000000000000..cd2aa3dce5c9352ae9916bc9722fb866064bc1a2 --- /dev/null +++ b/ee/app/models/remote_development/enums/workspace_variable.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +module RemoteDevelopment + module Enums + module WorkspaceVariable + extend ActiveSupport::Concern + + WORKSPACE_VARIABLE_TYPES = { + environment: 0, + file: 1 + }.freeze + + ENVIRONMENT_TYPE = WORKSPACE_VARIABLE_TYPES[:environment].freeze + FILE_TYPE = WORKSPACE_VARIABLE_TYPES[:file].freeze + + # TODO: Add support for file variables in GraphQL - https://gitlab.com/gitlab-org/gitlab/-/issues/465979 + WORKSPACE_VARIABLE_TYPES_FOR_GRAPHQL = + WORKSPACE_VARIABLE_TYPES + .except(:file) + .transform_keys { |key| key.to_s.upcase } + .freeze + end + end +end diff --git a/ee/app/models/remote_development/workspace_variable.rb b/ee/app/models/remote_development/workspace_variable.rb index 890ce302128cacc5e5fb1cb03a60e7012f6e0155..e25dad95fe4422b459e52ee67854ed069471cef0 100644 --- a/ee/app/models/remote_development/workspace_variable.rb +++ b/ee/app/models/remote_development/workspace_variable.rb @@ -3,23 +3,18 @@ module RemoteDevelopment class WorkspaceVariable < ApplicationRecord include Sortable + include Enums::WorkspaceVariable belongs_to :workspace, class_name: 'RemoteDevelopment::Workspace', inverse_of: :workspace_variables - validates :variable_type, presence: true, inclusion: { - in: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES.values - } + validates :variable_type, presence: true, inclusion: { in: WORKSPACE_VARIABLE_TYPES.values } validates :encrypted_value, presence: true validates :key, presence: true, length: { maximum: 255 } - scope :with_variable_type_environment, -> { - where(variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment]) - } - scope :with_variable_type_file, -> { - where(variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:file]) - } + scope :with_variable_type_environment, -> { where(variable_type: ENVIRONMENT_TYPE) } + scope :with_variable_type_file, -> { where(variable_type: FILE_TYPE) } scope :by_workspace_ids, ->(ids) { where(workspace_id: ids) } scope :by_project_ids, ->(ids) { where(project_id: ids) } diff --git a/ee/lib/remote_development/workspace_operations/create/workspace_variables.rb b/ee/lib/remote_development/workspace_operations/create/workspace_variables_builder.rb similarity index 53% rename from ee/lib/remote_development/workspace_operations/create/workspace_variables.rb rename to ee/lib/remote_development/workspace_operations/create/workspace_variables_builder.rb index 01006681d12482eab9eb79e9b9384e640dbc96b0..40847128f73350aba84ff5e93b793e110d05c076 100644 --- a/ee/lib/remote_development/workspace_operations/create/workspace_variables.rb +++ b/ee/lib/remote_development/workspace_operations/create/workspace_variables_builder.rb @@ -3,9 +3,10 @@ module RemoteDevelopment module WorkspaceOperations module Create - class WorkspaceVariables + class WorkspaceVariablesBuilder include CreateConstants include Files + include Enums::WorkspaceVariable # @param [String] name # @param [String] dns_zone @@ -13,123 +14,143 @@ class WorkspaceVariables # @param [String] user_name # @param [String] user_email # @param [Integer] workspace_id + # @param [Integer] workspace_actual_state # @param [Hash] vscode_extension_marketplace # @param [Array] variables # @return [Array] - def self.variables( + def self.build( name:, dns_zone:, personal_access_token_value:, user_name:, user_email:, workspace_id:, vscode_extension_marketplace:, variables: ) vscode_extension_marketplace => { - service_url: String => vscode_extensions_gallery_service_url, - item_url: String => vscode_extensions_gallery_item_url, - resource_url_template: String => vscode_extensions_gallery_resource_url_template, + service_url: String => vscode_extension_marketplace_service_url, + item_url: String => vscode_extension_marketplace_item_url, + resource_url_template: String => vscode_extension_marketplace_resource_url_template, } internal_variables = [ + + #------------------------------------------------------------------- + # The user's workspace-specific personal access token which is injected into the workspace, and used for + # authentication. For example, in the credential.helper script below. { key: File.basename(TOKEN_FILE), value: personal_access_token_value, - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:file], + variable_type: FILE_TYPE, + workspace_id: workspace_id + }, + { + key: "GL_TOKEN_FILE_PATH", + value: TOKEN_FILE, + variable_type: ENVIRONMENT_TYPE, workspace_id: workspace_id }, + #------------------------------------------------------------------- + + #------------------------------------------------------------------- + # Standard git ENV vars which configure git on the workspace. See https://git-scm.com/docs/git-config { + # This script is set as the value of `credential.helper` below in `GIT_CONFIG_VALUE_0` key: File.basename(GIT_CREDENTIAL_STORE_SCRIPT_FILE), value: WORKSPACE_VARIABLES_GIT_CREDENTIAL_STORE_SCRIPT, - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:file], + variable_type: FILE_TYPE, workspace_id: workspace_id }, { key: "GIT_CONFIG_COUNT", value: "3", - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "GIT_CONFIG_KEY_0", value: "credential.helper", - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "GIT_CONFIG_VALUE_0", value: GIT_CREDENTIAL_STORE_SCRIPT_FILE, - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "GIT_CONFIG_KEY_1", value: "user.name", - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "GIT_CONFIG_VALUE_1", value: user_name, - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "GIT_CONFIG_KEY_2", value: "user.email", - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "GIT_CONFIG_VALUE_2", value: user_email, - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], - workspace_id: workspace_id - }, - { - key: "GL_GIT_CREDENTIAL_STORE_SCRIPT_FILE", - value: GIT_CREDENTIAL_STORE_SCRIPT_FILE, - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], - workspace_id: workspace_id - }, - { - key: "GL_TOKEN_FILE_PATH", - value: TOKEN_FILE, - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: ENVIRONMENT_TYPE, workspace_id: workspace_id }, + #------------------------------------------------------------------- + + #------------------------------------------------------------------- + # The GL_WORKSPACE_DOMAIN_TEMPLATE variable is used by the GitLab Development Kit (GDK) script to configure + # the GDK in a workspce: `support/gitlab-remote-development/setup_workspace.rb` { key: "GL_WORKSPACE_DOMAIN_TEMPLATE", value: "${PORT}-#{name}.#{dns_zone}", - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: ENVIRONMENT_TYPE, workspace_id: workspace_id }, + #------------------------------------------------------------------- + + #------------------------------------------------------------------- + # Variables with prefix `GL_EDITOR_EXTENSIONS_GALLERY` are used for configuring the + # GitLab fork of VS Code which is injected into the workspace. + # TODO: Rename these to be `GL_VSCODE_EXTENSION_MARKETPLACE_*` to be consistent with the new naming standard + # See https://gitlab.com/gitlab-org/gitlab/-/issues/520884 { key: "GL_EDITOR_EXTENSIONS_GALLERY_SERVICE_URL", - value: vscode_extensions_gallery_service_url, - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + value: vscode_extension_marketplace_service_url, + variable_type: ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "GL_EDITOR_EXTENSIONS_GALLERY_ITEM_URL", - value: vscode_extensions_gallery_item_url, - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + value: vscode_extension_marketplace_item_url, + variable_type: ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "GL_EDITOR_EXTENSIONS_GALLERY_RESOURCE_URL_TEMPLATE", - value: vscode_extensions_gallery_resource_url_template, - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + value: vscode_extension_marketplace_resource_url_template, + variable_type: ENVIRONMENT_TYPE, workspace_id: workspace_id }, - # variables with prefix `GITLAB_WORKFLOW_` are used for configured GitLab Workflow extension for VS Code + #------------------------------------------------------------------- + + #------------------------------------------------------------------- + # Variables with prefix `GITLAB_WORKFLOW_` are used for configured GitLab Workflow extension for VS Code { key: "GITLAB_WORKFLOW_INSTANCE_URL", value: Gitlab::Routing.url_helpers.root_url, - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "GITLAB_WORKFLOW_TOKEN_FILE", value: TOKEN_FILE, - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: ENVIRONMENT_TYPE, workspace_id: workspace_id } + #------------------------------------------------------------------- ] user_provided_variables = variables.map do |variable| diff --git a/ee/lib/remote_development/workspace_operations/create/workspace_variables_creator.rb b/ee/lib/remote_development/workspace_operations/create/workspace_variables_creator.rb index 2efe530511a2a4dc19c2f9622a0c77cfb14a39c3..d3580e28d514fb7fb53435bd880a22b23d03ca9f 100644 --- a/ee/lib/remote_development/workspace_operations/create/workspace_variables_creator.rb +++ b/ee/lib/remote_development/workspace_operations/create/workspace_variables_creator.rb @@ -22,7 +22,7 @@ def self.create(context) # When we have the ability to define variables for workspaces # at project/group/instance level, add them here. variables = user_provided_variables - workspace_variables = WorkspaceVariables.variables( + workspace_variables = WorkspaceVariablesBuilder.build( name: workspace.name, dns_zone: workspace.workspaces_agent_config.dns_zone, personal_access_token_value: personal_access_token.token, diff --git a/ee/spec/factories/remote_development/workspace_variables.rb b/ee/spec/factories/remote_development/workspace_variables.rb index a4b50e6d09b1a3bbc57350b44b099f5ee5082383..4b61b8ae34d13867c2d904fd942cb90e62819f18 100644 --- a/ee/spec/factories/remote_development/workspace_variables.rb +++ b/ee/spec/factories/remote_development/workspace_variables.rb @@ -7,6 +7,6 @@ key { 'my_key' } value { 'my_value' } user_provided { false } - variable_type { RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:file] } + variable_type { RemoteDevelopment::Enums::WorkspaceVariable::FILE_TYPE } end end diff --git a/ee/spec/factories/remote_development/workspaces.rb b/ee/spec/factories/remote_development/workspaces.rb index 5762451f3b36e5311b98a63d313b0dd6bbb82e95..30a8f19d269c46b3cdeea3c74a511cbde85fee14 100644 --- a/ee/spec/factories/remote_development/workspaces.rb +++ b/ee/spec/factories/remote_development/workspaces.rb @@ -77,7 +77,7 @@ ) else unless evaluator.without_workspace_variables - workspace_variables = RemoteDevelopment::WorkspaceOperations::Create::WorkspaceVariables.variables( + workspace_variables = RemoteDevelopment::WorkspaceOperations::Create::WorkspaceVariablesBuilder.build( name: workspace.name, dns_zone: workspace.workspaces_agent_config.dns_zone, personal_access_token_value: workspace.personal_access_token.token, diff --git a/ee/spec/lib/remote_development/workspace_operations/create/main_integration_spec.rb b/ee/spec/lib/remote_development/workspace_operations/create/main_integration_spec.rb index dad5aaf1993f5082a4c21436aa8ab979f11dbd3b..e2de368b6c52707229bba22f982ef54b8b3039fd 100644 --- a/ee/spec/lib/remote_development/workspace_operations/create/main_integration_spec.rb +++ b/ee/spec/lib/remote_development/workspace_operations/create/main_integration_spec.rb @@ -135,7 +135,8 @@ RemoteDevelopment::WorkspaceVariable.where( workspace: workspace, key: variable[:key], - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES_FOR_GRAPHQL[variable[:type]] + variable_type: + RemoteDevelopment::Enums::WorkspaceVariable::WORKSPACE_VARIABLE_TYPES_FOR_GRAPHQL[variable[:type]] ).first&.value ).to eq(variable[:value]) end diff --git a/ee/spec/lib/remote_development/workspace_operations/create/workspace_variables_spec.rb b/ee/spec/lib/remote_development/workspace_operations/create/workspace_variables_builder_spec.rb similarity index 67% rename from ee/spec/lib/remote_development/workspace_operations/create/workspace_variables_spec.rb rename to ee/spec/lib/remote_development/workspace_operations/create/workspace_variables_builder_spec.rb index b6eae65fcc1124a2c31e46f602cab11ddc56d77c..65876b9720844eeb1bfc340bee0f701784aab859 100644 --- a/ee/spec/lib/remote_development/workspace_operations/create/workspace_variables_spec.rb +++ b/ee/spec/lib/remote_development/workspace_operations/create/workspace_variables_builder_spec.rb @@ -2,7 +2,7 @@ require "fast_spec_helper" -RSpec.describe ::RemoteDevelopment::WorkspaceOperations::Create::WorkspaceVariables, feature_category: :workspaces do +RSpec.describe ::RemoteDevelopment::WorkspaceOperations::Create::WorkspaceVariablesBuilder, feature_category: :workspaces do let(:name) { "name" } let(:dns_zone) { "example.dns.zone" } let(:personal_access_token_value) { "example-pat-value" } @@ -40,124 +40,118 @@ { key: "gl_token", value: "example-pat-value", - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:file], + variable_type: RemoteDevelopment::Enums::WorkspaceVariable::FILE_TYPE, + workspace_id: workspace_id + }, + { + key: "GL_TOKEN_FILE_PATH", + value: "/.workspace-data/variables/file/gl_token", + variable_type: RemoteDevelopment::Enums::WorkspaceVariable::ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "gl_git_credential_store.sh", value: git_credential_store_script, - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:file], + variable_type: RemoteDevelopment::Enums::WorkspaceVariable::FILE_TYPE, workspace_id: workspace_id }, { key: "GIT_CONFIG_COUNT", value: "3", - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: RemoteDevelopment::Enums::WorkspaceVariable::ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "GIT_CONFIG_KEY_0", value: "credential.helper", - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: RemoteDevelopment::Enums::WorkspaceVariable::ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "GIT_CONFIG_VALUE_0", value: "/.workspace-data/variables/file/gl_git_credential_store.sh", - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: RemoteDevelopment::Enums::WorkspaceVariable::ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "GIT_CONFIG_KEY_1", value: "user.name", - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: RemoteDevelopment::Enums::WorkspaceVariable::ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "GIT_CONFIG_VALUE_1", value: "example.user.name", - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: RemoteDevelopment::Enums::WorkspaceVariable::ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "GIT_CONFIG_KEY_2", value: "user.email", - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: RemoteDevelopment::Enums::WorkspaceVariable::ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "GIT_CONFIG_VALUE_2", value: "example@user.email", - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], - workspace_id: workspace_id - }, - { - key: "GL_GIT_CREDENTIAL_STORE_SCRIPT_FILE", - value: "/.workspace-data/variables/file/gl_git_credential_store.sh", - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], - workspace_id: workspace_id - }, - { - key: "GL_TOKEN_FILE_PATH", - value: "/.workspace-data/variables/file/gl_token", - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: RemoteDevelopment::Enums::WorkspaceVariable::ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "GL_WORKSPACE_DOMAIN_TEMPLATE", value: "${PORT}-name.example.dns.zone", - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: RemoteDevelopment::Enums::WorkspaceVariable::ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "GL_EDITOR_EXTENSIONS_GALLERY_SERVICE_URL", value: vscode_extensions_gallery_service_url, - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: RemoteDevelopment::Enums::WorkspaceVariable::ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "GL_EDITOR_EXTENSIONS_GALLERY_ITEM_URL", value: vscode_extensions_gallery_item_url, - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: RemoteDevelopment::Enums::WorkspaceVariable::ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "GL_EDITOR_EXTENSIONS_GALLERY_RESOURCE_URL_TEMPLATE", value: vscode_extensions_gallery_resource_url_template, - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: RemoteDevelopment::Enums::WorkspaceVariable::ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "GITLAB_WORKFLOW_INSTANCE_URL", value: Gitlab::Routing.url_helpers.root_url, - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: RemoteDevelopment::Enums::WorkspaceVariable::ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "GITLAB_WORKFLOW_TOKEN_FILE", value: "/.workspace-data/variables/file/gl_token", - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: RemoteDevelopment::Enums::WorkspaceVariable::ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "VAR1", value: "value 1", user_provided: true, - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment], + variable_type: RemoteDevelopment::Enums::WorkspaceVariable::ENVIRONMENT_TYPE, workspace_id: workspace_id }, { key: "/path/to/file", value: "value 2", user_provided: true, - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:file], + variable_type: RemoteDevelopment::Enums::WorkspaceVariable::FILE_TYPE, workspace_id: workspace_id } ] end subject(:variables) do - described_class.variables( + described_class.build( name: name, dns_zone: dns_zone, personal_access_token_value: personal_access_token_value, @@ -173,12 +167,12 @@ { key: "VAR1", value: "value 1", - type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment] + type: RemoteDevelopment::Enums::WorkspaceVariable::ENVIRONMENT_TYPE }, { key: "/path/to/file", value: "value 2", - type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:file] + type: RemoteDevelopment::Enums::WorkspaceVariable::FILE_TYPE } ] ) diff --git a/ee/spec/lib/remote_development/workspace_operations/create/workspace_variables_creator_spec.rb b/ee/spec/lib/remote_development/workspace_operations/create/workspace_variables_creator_spec.rb index 89460a974aff02f245af6b472a91127963c8ce34..17eb664c466859f41de737651b52a9eae1d1d215 100644 --- a/ee/spec/lib/remote_development/workspace_operations/create/workspace_variables_creator_spec.rb +++ b/ee/spec/lib/remote_development/workspace_operations/create/workspace_variables_creator_spec.rb @@ -1,47 +1,33 @@ # frozen_string_literal: true -require 'spec_helper' +require "spec_helper" RSpec.describe ::RemoteDevelopment::WorkspaceOperations::Create::WorkspaceVariablesCreator, feature_category: :workspaces do include ResultMatchers - include_context 'with remote development shared fixtures' + include_context "with remote development shared fixtures" + # noinspection RubyArgCount -- https://handbook.gitlab.com/handbook/tools-and-tips/editors-and-ides/jetbrains-ides/tracked-jetbrains-issues/#ruby-31542 let_it_be(:user) { create(:user) } let_it_be(:personal_access_token) { create(:personal_access_token, user: user) } let_it_be(:workspace) { create(:workspace, user: user, personal_access_token: personal_access_token) } - let(:vscode_extension_marketplace) { { some_key: "some-value" } } - let(:user_provided_variables) { [{ key: "VAR1", value: "value 1" }, { key: "VAR2", value: "value 2" }] } - let(:returned_workspace_variables) do - [ - { - key: "key1", - value: "value1", - variable_type: RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:file], - workspace_id: workspace.id - }, - { - key: "key2", - value: "value2", - variable_type: variable_type, - workspace_id: workspace.id - } - ] - end - - let(:workspace_variables_params) do + let(:vscode_extension_marketplace) do { - name: workspace.name, - dns_zone: workspace.workspaces_agent_config.dns_zone, - personal_access_token_value: personal_access_token.token, - user_name: user.name, - user_email: user.email, - workspace_id: workspace.id, - vscode_extension_marketplace: vscode_extension_marketplace, - variables: user_provided_variables + service_url: "service_url", + item_url: "item_url", + resource_url_template: "resource_url_template" } end + let(:variable_type) { RemoteDevelopment::Enums::WorkspaceVariable::ENVIRONMENT_TYPE } + + let(:user_provided_variables) do + [ + { key: "key1", value: "value 1", type: variable_type }, + { key: "key2", value: "value 2", type: variable_type } + ] + end + let(:context) do { workspace: workspace, @@ -58,36 +44,33 @@ described_class.create(context) # rubocop:disable Rails/SaveBang -- this is not an ActiveRecord method end - before do - allow(RemoteDevelopment::WorkspaceOperations::Create::WorkspaceVariables) - .to receive(:variables).with(workspace_variables_params) { returned_workspace_variables } - end - - context 'when workspace variables create is successful' do - let(:valid_variable_type) { RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment] } + context "when workspace variables create is successful" do + let(:valid_variable_type) { RemoteDevelopment::Enums::WorkspaceVariable::ENVIRONMENT_TYPE } let(:variable_type) { valid_variable_type } + let(:expected_number_of_records_saved) { 18 } - it 'creates the workspace variable records and returns ok result containing original context' do - expect { result }.to change { workspace.workspace_variables.count }.by(2) + it "creates the workspace variable records and returns ok result containing original context" do + expect { result }.to change { workspace.workspace_variables.count }.by(expected_number_of_records_saved) - expect(RemoteDevelopment::WorkspaceVariable.find_by_key('key1').value).to eq('value1') - expect(RemoteDevelopment::WorkspaceVariable.find_by_key('key2').value).to eq('value2') + expect(RemoteDevelopment::WorkspaceVariable.find_by_key("key1").value).to eq("value 1") + expect(RemoteDevelopment::WorkspaceVariable.find_by_key("key2").value).to eq("value 2") expect(result).to be_ok_result(context) end end - context 'when workspace create fails' do + context "when workspace create fails" do let(:invalid_variable_type) { 9999999 } let(:variable_type) { invalid_variable_type } + let(:expected_number_of_records_saved) { 16 } - it 'does not create the invalid workspace variable records and returns an error result with model errors' do - # NOTE: Any valid records will be saved if they are first in the array before the invalid record, but that's OK, + it "does not create the invalid workspace variable records and returns an error result with model errors" do + # NOTE: Any valid records will be saved if they are first in the array before the invalid record, but that"s OK, # because if we return an err_result, the entire transaction will be rolled back at a higher level. - expect { result }.to change { workspace.workspace_variables.count }.by(1) + expect { result }.to change { workspace.workspace_variables.count }.by(expected_number_of_records_saved) - expect(RemoteDevelopment::WorkspaceVariable.find_by_key('key1').value).to eq('value1') - expect(RemoteDevelopment::WorkspaceVariable.find_by_key('key2')).to be_nil + expect(RemoteDevelopment::WorkspaceVariable.find_by_key("key1")).to be_nil + expect(RemoteDevelopment::WorkspaceVariable.find_by_key("key2")).to be_nil expect(result).to be_err_result do |message| expect(message).to be_a(RemoteDevelopment::Messages::WorkspaceVariablesModelCreateFailed) diff --git a/ee/spec/models/remote_development/enums/workspace_variable_spec.rb b/ee/spec/models/remote_development/enums/workspace_variable_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..394b0e32e0421cdc6fed9cc3cdbebd9e25abdc90 --- /dev/null +++ b/ee/spec/models/remote_development/enums/workspace_variable_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' + +RSpec.describe RemoteDevelopment::Enums::WorkspaceVariable, feature_category: :workspaces do + describe "WORKSPACE_VARIABLE_TYPES_FOR_GRAPHQL constant" do + subject(:constant) do + described_class::WORKSPACE_VARIABLE_TYPES_FOR_GRAPHQL + end + + it "has correct value" do + expect(constant).to eq({ "ENVIRONMENT" => 0 }) + end + end +end diff --git a/ee/spec/models/remote_development/workspace_variable_spec.rb b/ee/spec/models/remote_development/workspace_variable_spec.rb index 15b19644c278b569318360be8f49cdf7b9ec36ca..9de10f576eb9d57ab26bd8508758a08b1b284be6 100644 --- a/ee/spec/models/remote_development/workspace_variable_spec.rb +++ b/ee/spec/models/remote_development/workspace_variable_spec.rb @@ -6,8 +6,8 @@ let(:key) { 'key_1' } let(:current_value) { 'value_1' } let(:value) { current_value } - let(:variable_type_environment) { RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:environment] } - let(:variable_type_file) { RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES[:file] } + let(:variable_type_environment) { RemoteDevelopment::Enums::WorkspaceVariable::ENVIRONMENT_TYPE } + let(:variable_type_file) { RemoteDevelopment::Enums::WorkspaceVariable::FILE_TYPE } let(:variable_type) { variable_type_file } let(:variable_type_values) do [ diff --git a/ee/spec/requests/api/graphql/remote_development/workspace_variables/shared.rb b/ee/spec/requests/api/graphql/remote_development/workspace_variables/shared.rb index 0c8b04b69346d99f088ec62514a23ad724a4c3af..d42275f379350d46eb996cd1183849e28d89dbce 100644 --- a/ee/spec/requests/api/graphql/remote_development/workspace_variables/shared.rb +++ b/ee/spec/requests/api/graphql/remote_development/workspace_variables/shared.rb @@ -86,10 +86,10 @@ it 'includes only the correct type of workspace_variables' do expect(workspace_variable_types).to include( - ::RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES.key(0).to_s.upcase + ::RemoteDevelopment::Enums::WorkspaceVariable::WORKSPACE_VARIABLE_TYPES.key(0).to_s.upcase ) expect(workspace_variable_types).not_to include( - ::RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES.key(1).to_s.upcase + ::RemoteDevelopment::Enums::WorkspaceVariable::WORKSPACE_VARIABLE_TYPES.key(1).to_s.upcase ) end end diff --git a/ee/spec/requests/remote_development/integration_spec.rb b/ee/spec/requests/remote_development/integration_spec.rb index b0e40dd4a369ffe8f9c83804e7a6b132e05d4008..a91c5a5db30c34786b4dd6e3944e2f5d61861782 100644 --- a/ee/spec/requests/remote_development/integration_spec.rb +++ b/ee/spec/requests/remote_development/integration_spec.rb @@ -88,6 +88,9 @@ let(:expected_internal_variables) do # rubocop:disable Layout/LineLength -- keep them on one line for easier readability and editability [ + { key: "gl_token", type: :file, value: /glpat-.+/ }, + { key: "GL_TOKEN_FILE_PATH", type: :environment, value: "/.workspace-data/variables/file/gl_token" }, + { key: "gl_git_credential_store.sh", type: :file, value: RemoteDevelopment::Files::WORKSPACE_VARIABLES_GIT_CREDENTIAL_STORE_SCRIPT }, { key: "GIT_CONFIG_COUNT", type: :environment, value: "3" }, { key: "GIT_CONFIG_KEY_0", type: :environment, value: "credential.helper" }, { key: "GIT_CONFIG_KEY_1", type: :environment, value: "user.name" }, @@ -98,13 +101,9 @@ { key: "GL_EDITOR_EXTENSIONS_GALLERY_ITEM_URL", type: :environment, value: "https://open-vsx.org/vscode/item" }, { key: "GL_EDITOR_EXTENSIONS_GALLERY_RESOURCE_URL_TEMPLATE", type: :environment, value: "https://open-vsx.org/vscode/asset/{publisher}/{name}/{version}/Microsoft.VisualStudio.Code.WebResources/{path}" }, { key: "GL_EDITOR_EXTENSIONS_GALLERY_SERVICE_URL", type: :environment, value: "https://open-vsx.org/vscode/gallery" }, - { key: "GL_GIT_CREDENTIAL_STORE_SCRIPT_FILE", type: :environment, value: "/.workspace-data/variables/file/gl_git_credential_store.sh" }, - { key: "GL_TOKEN_FILE_PATH", type: :environment, value: "/.workspace-data/variables/file/gl_token" }, { key: "GL_WORKSPACE_DOMAIN_TEMPLATE", type: :environment, value: "${PORT}-workspace-#{agent.id}-#{user.id}-#{random_string}.#{dns_zone}" }, { key: "GITLAB_WORKFLOW_INSTANCE_URL", type: :environment, value: Gitlab::Routing.url_helpers.root_url }, - { key: "GITLAB_WORKFLOW_TOKEN_FILE", type: :environment, value: "/.workspace-data/variables/file/gl_token" }, - { key: "gl_git_credential_store.sh", type: :file, value: RemoteDevelopment::Files::WORKSPACE_VARIABLES_GIT_CREDENTIAL_STORE_SCRIPT }, - { key: "gl_token", type: :file, value: /glpat-.+/ } + { key: "GITLAB_WORKFLOW_TOKEN_FILE", type: :environment, value: "/.workspace-data/variables/file/gl_token" } ] # rubocop:enable Layout/LineLength end @@ -244,7 +243,7 @@ def do_create_workspace all_expected_vars = (expected_internal_variables + user_provided_variables).sort_by { |v| v[:key] } # NOTE: We convert the actual records into hashes and sort them as a hash rather than ordering in # ActiveRecord, to account for platform- or db-specific sorting differences. - types = RemoteDevelopment::Enums::Workspace::WORKSPACE_VARIABLE_TYPES + types = RemoteDevelopment::Enums::WorkspaceVariable::WORKSPACE_VARIABLE_TYPES all_actual_vars = RemoteDevelopment::WorkspaceVariable.where(workspace: workspace) # noinspection RailsParamDefResolve -- RubyMine is incorrectly detecting this as ActiveRecord #select method diff --git a/ee/spec/support/fast_spec/remote_development/fast_spec_helper_support.rb b/ee/spec/support/fast_spec/remote_development/fast_spec_helper_support.rb index ba86761e07ca3c1ff4ed0a653cb1a8e4ec6e9bb6..59b7936df18be86a2a653bf89edf5ab3ac651a36 100644 --- a/ee/spec/support/fast_spec/remote_development/fast_spec_helper_support.rb +++ b/ee/spec/support/fast_spec/remote_development/fast_spec_helper_support.rb @@ -4,4 +4,4 @@ require 'devfile' require_relative '../../../support/shared_contexts/remote_development/agent_info_status_fixture_not_implemented_error' require_relative '../../../support/shared_contexts/remote_development/remote_development_shared_contexts' -require_relative '../../../../app/models/remote_development/enums/workspace' +require_relative '../../../../app/models/remote_development/enums/workspace_variable'