<commit_before>require 'open3'
require_relative 'gitlab_config'

class GitlabShell
  attr_accessor :username, :repo_name, :git_cmd, :repos_path

  def initialize
    @username = ARGV.shift
    @origin_cmd = ENV['SSH_ORIGINAL_COMMAND']
    @repos_path = GitlabConfig.new.repos_path
  end

  def exec
    if @origin_cmd
      parse_cmd

      if git_cmds.include?(@git_cmd)
        ENV['GL_USER'] = @username

        process_cmd
      else
        puts 'Not allowed command'
      end
    else
      puts "Welcome #{@username}!"
    end
  end

  protected

  def parse_cmd
    args = @origin_cmd.split(' ')
    @git_cmd = args.shift
    @repo_name = args.shift
  end

  def git_cmds
    %w(git-upload-pack git-receive-pack git-upload-archive)
  end

  def process_cmd
    repo_full_path = File.join(repos_path, repo_name)
    system("#{@git_cmd} #{repo_full_path}")
  end
end<commit_msg>validate access via api<commit_after>require 'open3'
require 'net/http'

require_relative 'gitlab_config'

class GitlabShell
  attr_accessor :username, :repo_name, :git_cmd, :repos_path

  def initialize
    @username = ARGV.shift
    @origin_cmd = ENV['SSH_ORIGINAL_COMMAND']
    @repos_path = GitlabConfig.new.repos_path
  end

  def exec
    if @origin_cmd
      parse_cmd

      if git_cmds.include?(@git_cmd)
        ENV['GL_USER'] = @username

        if validate_access
          process_cmd
        end
      else
        puts 'Not allowed command'
      end
    else
      puts "Welcome #{@username}!"
    end
  end

  protected

  def parse_cmd
    args = @origin_cmd.split(' ')
    @git_cmd = args.shift
    @repo_name = args.shift
  end

  def git_cmds
    %w(git-upload-pack git-receive-pack git-upload-archive)
  end

  def process_cmd
    repo_full_path = File.join(repos_path, repo_name)
    system("#{@git_cmd} #{repo_full_path}")
  end

  def validate_access
    @ref_name = 'master' # just hardcode it cause we dont know ref
    project_name = @repo_name.gsub("'", "")
    project_name = project_name.gsub(/\.git$/, "")
    url = "http://1172.16.58.3:3000/api/v3/allowed?project=#{project_name}&username=#{@username}&action=#{@git_cmd}&ref=#{@ref_name}"
    resp = Net::HTTP.get_response(URI.parse(url))
    resp.code == '200' && resp.body == 'true'
  end
end
{'fraction_non_alphanumeric': 0.11484098939929328, 'fraction_numerical': 0.008392226148409895, 'mean_word_length': 3.044642857142857, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '69978', 'n_tokens_mistral': 830, 'n_tokens_neox': 796, 'n_words': 166}