<commit_before># frozen_string_literal: true

module GlobalHelpers
  def create_user_with_permissions(project, permissions: [], login: nil)
    role = Role.find_by name: 'Manager'
    role = FactoryBot.create :role, name: 'Manager' if role.nil?
    role.permissions += permissions
    role.save!

    user = if login.nil?
             FactoryBot.create :user
           else
             FactoryBot.create :user, login: login
           end

    member = Member.new role_ids: [role.id], user_id: user.id
    project.members << member

    user
  end

  def create_project(identifier = nil)
    if identifier.nil?
      FactoryBot.create :project
    else
      project = Project.find_by identifier: identifier
      project = FactoryBot.create :project, identifier: identifier if project.nil?
      project
    end
  end

  def set_session_user(user)
    request.session[:user_id] = user.id
  end

  def create_anonymous_user
    create_user 'git_anonymous'
  end

  def create_admin_user
    create_user 'git_admin', admin: true
  end

  def create_user(login, admin: false)
    user = User.find_by login: login
    user = FactoryBot.create :user, login: login, admin: admin if user.nil?
    user
  end

  def create_ssh_key(**opts)
    FactoryBot.create :gitolite_public_key, opts
  end

  def build_ssh_key(**opts)
    FactoryBot.build :gitolite_public_key, opts
  end

  def build_git_repository(**opts)
    FactoryBot.build :repository_gitolite, opts
  end

  def find_or_create_git_repository(**opts)
    repository = Repository::Xitolite.find_by identifier: opts[:identifier]
    if repository.nil?
      repository = FactoryBot.create :repository_gitolite, opts
      build_extra repository
    end
    repository
  end

  def create_git_repository(**opts)
    repository = FactoryBot.create :repository_gitolite, opts
    build_extra repository
    repository
  end

  def build_extra(repository)
    extra = repository.build_extra default_branch: 'master', key: RedmineGitHosting::Utils::Crypto.generate_secret(64)
    extra.save!
  end

  def create_svn_repository(**opts)
    FactoryBot.create :repository_svn, opts
  end

  def load_yaml_fixture(fixture)
    YAML.load load_fixture(fixture) # rubocop: disable Security/YAMLLoad
  end

  def load_fixture(fixture)
    File.read RedmineGitHosting.plugin_spec_dir('fixtures', fixture)<commit_msg>Correct a Psych::DisallowedClass error in tests<commit_after># frozen_string_literal: true

module GlobalHelpers
  def create_user_with_permissions(project, permissions: [], login: nil)
    role = Role.find_by name: 'Manager'
    role = FactoryBot.create :role, name: 'Manager' if role.nil?
    role.permissions += permissions
    role.save!

    user = if login.nil?
             FactoryBot.create :user
           else
             FactoryBot.create :user, login: login
           end

    member = Member.new role_ids: [role.id], user_id: user.id
    project.members << member

    user
  end

  def create_project(identifier = nil)
    if identifier.nil?
      FactoryBot.create :project
    else
      project = Project.find_by identifier: identifier
      project = FactoryBot.create :project, identifier: identifier if project.nil?
      project
    end
  end

  def set_session_user(user)
    request.session[:user_id] = user.id
  end

  def create_anonymous_user
    create_user 'git_anonymous'
  end

  def create_admin_user
    create_user 'git_admin', admin: true
  end

  def create_user(login, admin: false)
    user = User.find_by login: login
    user = FactoryBot.create :user, login: login, admin: admin if user.nil?
    user
  end

  def create_ssh_key(**opts)
    FactoryBot.create :gitolite_public_key, opts
  end

  def build_ssh_key(**opts)
    FactoryBot.build :gitolite_public_key, opts
  end

  def build_git_repository(**opts)
    FactoryBot.build :repository_gitolite, opts
  end

  def find_or_create_git_repository(**opts)
    repository = Repository::Xitolite.find_by identifier: opts[:identifier]
    if repository.nil?
      repository = FactoryBot.create :repository_gitolite, opts
      build_extra repository
    end
    repository
  end

  def create_git_repository(**opts)
    repository = FactoryBot.create :repository_gitolite, opts
    build_extra repository
    repository
  end

  def build_extra(repository)
    extra = repository.build_extra default_branch: 'master', key: RedmineGitHosting::Utils::Crypto.generate_secret(64)
    extra.save!
  end

  def create_svn_repository(**opts)
    FactoryBot.create :repository_svn, opts
  end

  def load_yaml_fixture(fixture)
    if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.1.0')
      YAML.load load_fixture(fixture), # rubocop: disable Security/YAMLLoad
        permitted_classes: [Symbol, Time]
    else
      YAML.load load_fixture(fixture) # rubocop: disable Security/YAMLLoad
    end
  end

  def load_fixture(fixture)
    File.read RedmineGitHosting.plugin_spec_dir('fixtures', fixture)
{'fraction_non_alphanumeric': 0.08222222222222222, 'fraction_numerical': 0.0014141414141414141, 'mean_word_length': 3.5297346752058556, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 4, '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': '100506', 'n_tokens_mistral': 1573, 'n_tokens_neox': 1503, 'n_words': 371}