<commit_before>require 'github_api'
require 'daemons'
require 'time-lord'

#TODO: parameterize
USER = "victorykit"
REPO = "victorykit"

def start
  if !File.exists?(file_path)
    log `mkdir -p #{file_path}`
  end

  head = Github.new.git_data.references.find('victorykit', 'victorykit', 'heads/master')['object']['sha']
  store head
  demonize
  log("commitron is now started")
end

def stop
  demonize
  log "commitron is now stopped"
end

def demonize
  Daemons.run_proc('commitron', :dir_mode => :normal, :dir => '/var/tmp/', :monitor => false) do
    loop do
      find_new_commits
      sleep(60)
    end
  end
end

def find_new_commits
  begin
    all_commits = Github.new.repos.commits.all(USER, REPO)
    index_of_last_known_commit = all_commits.index {|c| c['sha'] == last_known_commit}

    #TODO: handle case where last_known_commit is not in the first page of results
    if(index_of_last_known_commit && index_of_last_known_commit > 0)
      new_commits = all_commits[0..index_of_last_known_commit]

      new_commits.each do |c|
        committer = c['commit']['committer']['name']
        commit_time = Time.parse(c['commit']['committer']['date']).ago_in_words
        message = c['commit']['message']
        small_sha = c['sha'][0..6]
        url = "https://github.com/#{USER}/#{REPO}/commit/#{c['sha']}"
        broadcast_on_skype "New commit: #{small_sha} - #{committer}, #{commit_time} : #{message} \n #{url}"
      end
    end

    store all_commits.first['sha']
  rescue => ex
    log ex
    log ex.backtrace.join
  end
end

def last_known_commit
  IO.read(state_file).strip
end

def store(sha)
  File.open(state_file, 'w') do |file|
    file.puts sha
  end

  log "updated last known commit to #{sha}"<commit_msg>fixed off-by-one bug<commit_after>require 'github_api'
require 'daemons'
require 'time-lord'

#TODO: parameterize
USER = "victorykit"
REPO = "victorykit"

def start
  if !File.exists?(file_path)
    log `mkdir -p #{file_path}`
  end

  head = Github.new.git_data.references.find('victorykit', 'victorykit', 'heads/master')['object']['sha']
  store head
  demonize
  log("commitron is now started")
end

def stop
  demonize
  log "commitron is now stopped"
end

def demonize
  Daemons.run_proc('commitron', :dir_mode => :normal, :dir => '/var/tmp/', :monitor => false) do
    loop do
      find_new_commits
      sleep(60)
    end
  end
end

def find_new_commits
  begin
    all_commits = Github.new.repos.commits.all(USER, REPO)
    index_of_last_known_commit = all_commits.index {|c| c['sha'] == last_known_commit}

    #TODO: handle case where last_known_commit is not in the first page of results
    if(index_of_last_known_commit && index_of_last_known_commit > 0)
      new_commits = all_commits[0..index_of_last_known_commit - 1]

      new_commits.each do |c|
        committer = c['commit']['committer']['name']
        commit_time = Time.parse(c['commit']['committer']['date']).ago_in_words
        message = c['commit']['message']
        small_sha = c['sha'][0..6]
        url = "https://github.com/#{USER}/#{REPO}/commit/#{c['sha']}"
        broadcast_on_skype "New commit: #{small_sha} - #{committer}, #{commit_time} : #{message} \n #{url}"
      end
    end

    store all_commits.first['sha']
  rescue => ex
    log ex
    log ex.backtrace.join
  end
end

def last_known_commit
  IO.read(state_file).strip
end

def store(sha)
  File.open(state_file, 'w') do |file|
    file.puts sha
  end

  log "updated last known commit to #{sha}"
{'fraction_non_alphanumeric': 0.13974175035868006, 'fraction_numerical': 0.0037302725968436155, 'mean_word_length': 3.623342175066313, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 2, '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': '7277823', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1252, 'n_words': 286}