<commit_before>require 'eventmachine'

module DTK
  class Component_moduleController < AuthController
    helper :module_helper
    helper :remotes_helper

    #### create and delete actions ###
    def rest__create
      # setup needed data
      module_name = ret_non_null_request_params(:module_name)
      namespace   = ret_request_param_module_namespace?()
      config_agent_type =  ret_config_agent_type()
      project = get_default_project()

      # local_params encapsulates local module branch params
      opts_local_params = (namespace ? { namespace: namespace } : {})
      local_params = local_params(:component_module, module_name, opts_local_params)

      opts_create_mod = Opts.new(
        config_agent_type: ret_config_agent_type()
      )
      module_repo_info = ComponentModule.create_module(project, local_params, opts_create_mod)[:module_repo_info]

      # only when creating via import-git command
      git_url = ret_request_params(:module_git_url)
      unless git_url.empty?
        add_git_url(project.model_handle(:repo_remote), module_repo_info[:repo_id], git_url)
      end

      rest_ok_response module_repo_info
    end

    def rest__update_from_initial_create
      component_module = create_obj(:component_module_id)
      repo_id, commit_sha = ret_non_null_request_params(:repo_id, :commit_sha)
      git_import = ret_request_params(:git_import)
      repo_idh = id_handle(repo_id, :repo)
      version = ret_version()
      scaffold = ret_request_params(:scaffold_if_no_dsl)
      opts = { scaffold_if_no_dsl: scaffold, do_not_raise: true, process_provider_specific_dependencies: true }
      opts.merge!(commit_dsl: true) if ret_request_params(:commit_dsl)

      response =
        if git_import
          component_module.import_from_git(commit_sha, repo_idh, version, opts)
        else
          component_module.import_from_file(commit_sha, repo_idh, version, opts)
        end

      rest_ok_response response
    end

    def rest__update_model_from_clone
      component_module = create_obj(:component_module_id)
      commit_sha    = ret_non_null_request_params(:commit_sha)
      version       = ret_version()
      diffs_summary = ret_diffs_summary()

      opts =  {}
      if ret_request_param_boolean(:internal_trigger)
        opts.merge!(do_not_raise: true)
      end
      if ret_request_param_boolean(:force_parse)
        opts.merge!(force_parse: true)
      end
      if ret_request_params(:set_parsed_false)
        opts.merge!(dsl_parsed_false: true)
      end
      if ret_request_params(:update_from_includes)
        opts.merge!(update_from_includes: true)
      end
      if ret_request_params(:service_instance_module)
        opts.merge!(service_instance_module: true)
      end
      if current_branch_sha = ret_request_params(:current_branch_sha)
        opts.merge!(current_branch_sha: current_branch_sha)
      end
      if force = ret_request_params(:force)
        opts.merge!(force: force)
      end
      if generate_docs = ret_request_param_boolean(:generate_docs)
        opts.merge!(generate_docs: generate_docs)
      end

      module_dsl_info = component_module.update_model_from_clone_changes?(commit_sha, diffs_summary, version, opts)
      response = module_dsl_info.hash_subset(:dsl_parse_error, :dsl_updated_info, :dsl_created_info, :external_dependencies, :component_module_refs)
      # the possible keys in response are (with the subkeys) are
      # the possible keys in module_dsl_info are (with the subkeys) are
      #  :dsl_parse_error: ModuleDSL::ParsingError obj
      #  :dsl_updated_info:
      #    :msg
      #    :commit_sha
      #  :dsl_created_info
      #    :path
      #    :content - only if want this dsl file to be added on cleint side
      #  :external_dependencies
      #    :inconsistent
      #    :possibly_missing
      #    :ambiguous
      #  :component_module_refs
      rest_ok_response response
    end

    def rest__create_new_version
      component_module = create_obj(:component_module_id)
      version       = ret_version()
      diffs_summary = ret_diffs_summary()

      opts = {}
      opts.merge!(force_parse: true)
      opts.merge!(update_from_includes: true)
      opts.merge!(force: true)

      if ret_request_param_boolean(:internal_trigger)
        opts.merge!(do_not_raise: true)
      end

      if generate_docs = ret_request_param_boolean(:generate_docs)
        opts.merge!(generate_docs: generate_docs)
      end

      module_dsl_info = component_module.create_new_module_version(version, diffs_summary, opts)
    end

    def rest__delete
      component_module = create_obj(:component_module_id)
      module_info = component_module.delete_object()
      rest_ok_response module_info
    end

    def rest__delete_version<commit_msg>Return valid response when create-new-version<commit_after>require 'eventmachine'

module DTK
  class Component_moduleController < AuthController
    helper :module_helper
    helper :remotes_helper

    #### create and delete actions ###
    def rest__create
      # setup needed data
      module_name = ret_non_null_request_params(:module_name)
      namespace   = ret_request_param_module_namespace?()
      config_agent_type =  ret_config_agent_type()
      project = get_default_project()

      # local_params encapsulates local module branch params
      opts_local_params = (namespace ? { namespace: namespace } : {})
      local_params = local_params(:component_module, module_name, opts_local_params)

      opts_create_mod = Opts.new(
        config_agent_type: ret_config_agent_type()
      )
      module_repo_info = ComponentModule.create_module(project, local_params, opts_create_mod)[:module_repo_info]

      # only when creating via import-git command
      git_url = ret_request_params(:module_git_url)
      unless git_url.empty?
        add_git_url(project.model_handle(:repo_remote), module_repo_info[:repo_id], git_url)
      end

      rest_ok_response module_repo_info
    end

    def rest__update_from_initial_create
      component_module = create_obj(:component_module_id)
      repo_id, commit_sha = ret_non_null_request_params(:repo_id, :commit_sha)
      git_import = ret_request_params(:git_import)
      repo_idh = id_handle(repo_id, :repo)
      version = ret_version()
      scaffold = ret_request_params(:scaffold_if_no_dsl)
      opts = { scaffold_if_no_dsl: scaffold, do_not_raise: true, process_provider_specific_dependencies: true }
      opts.merge!(commit_dsl: true) if ret_request_params(:commit_dsl)

      response =
        if git_import
          component_module.import_from_git(commit_sha, repo_idh, version, opts)
        else
          component_module.import_from_file(commit_sha, repo_idh, version, opts)
        end

      rest_ok_response response
    end

    def rest__update_model_from_clone
      component_module = create_obj(:component_module_id)
      commit_sha    = ret_non_null_request_params(:commit_sha)
      version       = ret_version()
      diffs_summary = ret_diffs_summary()

      opts =  {}
      if ret_request_param_boolean(:internal_trigger)
        opts.merge!(do_not_raise: true)
      end
      if ret_request_param_boolean(:force_parse)
        opts.merge!(force_parse: true)
      end
      if ret_request_params(:set_parsed_false)
        opts.merge!(dsl_parsed_false: true)
      end
      if ret_request_params(:update_from_includes)
        opts.merge!(update_from_includes: true)
      end
      if ret_request_params(:service_instance_module)
        opts.merge!(service_instance_module: true)
      end
      if current_branch_sha = ret_request_params(:current_branch_sha)
        opts.merge!(current_branch_sha: current_branch_sha)
      end
      if force = ret_request_params(:force)
        opts.merge!(force: force)
      end
      if generate_docs = ret_request_param_boolean(:generate_docs)
        opts.merge!(generate_docs: generate_docs)
      end

      module_dsl_info = component_module.update_model_from_clone_changes?(commit_sha, diffs_summary, version, opts)
      response = module_dsl_info.hash_subset(:dsl_parse_error, :dsl_updated_info, :dsl_created_info, :external_dependencies, :component_module_refs)
      # the possible keys in response are (with the subkeys) are
      # the possible keys in module_dsl_info are (with the subkeys) are
      #  :dsl_parse_error: ModuleDSL::ParsingError obj
      #  :dsl_updated_info:
      #    :msg
      #    :commit_sha
      #  :dsl_created_info
      #    :path
      #    :content - only if want this dsl file to be added on cleint side
      #  :external_dependencies
      #    :inconsistent
      #    :possibly_missing
      #    :ambiguous
      #  :component_module_refs
      rest_ok_response response
    end

    def rest__create_new_version
      component_module = create_obj(:component_module_id)
      version       = ret_version()
      diffs_summary = ret_diffs_summary()

      opts = {}
      opts.merge!(force_parse: true)
      opts.merge!(update_from_includes: true)
      opts.merge!(force: true)

      if ret_request_param_boolean(:internal_trigger)
        opts.merge!(do_not_raise: true)
      end

      if generate_docs = ret_request_param_boolean(:generate_docs)
        opts.merge!(generate_docs: generate_docs)
      end

      rest_ok_response component_module.create_new_module_version(version, diffs_summary, opts)
    end

    def rest__delete
      component_module = create_obj(:component_module_id)
      module_info = component_module.delete_object()
      rest_ok_response module_info
    end

    def rest__delete_version
{'fraction_non_alphanumeric': 0.07024664377146425, 'fraction_numerical': 0.0, 'mean_word_length': 3.380127620783956, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '401143', 'n_tokens_mistral': 2945, 'n_tokens_neox': 2894, 'n_words': 537}