<commit_before>require 'json'

class GithubHookController < ApplicationController

  GIT_BIN = Redmine::Configuration['scm_git_command'] || "git"
  skip_before_filter :verify_authenticity_token, :check_if_login_required

  def index
    if request.post?
      repositories = find_repositories

      repositories.each do |repository|
        # Fetch the changes from Github
        update_repository(repository)

        # Fetch the new changesets into Redmine
        repository.fetch_changesets
        
        logger.info { "  GithubHook: Redmine repository updated: #{repository.identifier}" }
      end
    end

    render(:text => 'OK')
  end

  def welcome
    # Render the default layout
  end

  private

  def system(command)
    Kernel.system(command)
  end

  # Executes shell command. Returns true if the shell command exits with a
  # success status code.
  #
  # If directory is given the current directory will be changed to that
  # directory before executing command.
  def exec(command, directory)
    logger.debug { "  GithubHook: Executing command: '#{command}'" }

    # Get a path to a temp file
    logfile = Tempfile.new('github_hook_exec')
    logfile.close

    full_command = "#{command} > #{logfile.path} 2>&1"
    success = if directory.present?
      Dir.chdir(directory) do
        system(full_command)<commit_msg>add profiling/debug info in log about how long the fetch and redmine import commands have taken<commit_after>require 'json'

class GithubHookController < ApplicationController

  GIT_BIN = Redmine::Configuration['scm_git_command'] || "git"
  skip_before_filter :verify_authenticity_token, :check_if_login_required

  def index
    if request.post?
      repositories = find_repositories

      repositories.each do |repository|
            tg1 = Time.now
        # Fetch the changes from Github
        update_repository(repository)
            tg2 = Time.now
        
            tr1 = Time.now
        # Fetch the new changesets into Redmine
        repository.fetch_changesets
            tr2 = Time.now
        
        logger.info { "  GithubHook: Redmine repository updated: #{repository.identifier} (Git: #{time_diff_milli(tg1,tg2)}ms, Redmine: #{time_diff_milli(tr1,tr2)}ms)" }
      end
    end

    render(:text => 'OK')
  end

  def welcome
    # Render the default layout
  end

  private

  def system(command)
    Kernel.system(command)
  end
  
  def time_diff_milli(start, finish)
    ((finish - start) * 1000.0).round(1)
  end

  # Executes shell command. Returns true if the shell command exits with a
  # success status code.
  #
  # If directory is given the current directory will be changed to that
  # directory before executing command.
  def exec(command, directory)
    logger.debug { "  GithubHook: Executing command: '#{command}'" }

    # Get a path to a temp file
    logfile = Tempfile.new('github_hook_exec')
    logfile.close

    full_command = "#{command} > #{logfile.path} 2>&1"
    success = if directory.present?
      Dir.chdir(directory) do
        system(full_command)
{'fraction_non_alphanumeric': 0.08278795811518325, 'fraction_numerical': 0.0058900523560209425, 'mean_word_length': 3.0223684210526316, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '4613944', 'n_tokens_mistral': 921, 'n_tokens_neox': 876, 'n_words': 263}