class Githabit::App

Public Class Methods

new() click to toggle source
# File lib/githabit/app.rb, line 10
def initialize()
  @log = Logger.new(STDOUT)

  # Load config file
  @settings = YAML::load_file(File.expand_path("~/.githabit.yml"))

  # Init apis
  @log.debug("Loading APIs")
  @habit_api = HabitRPG_API.new(@settings['habitrpg']['user'], @settings['habitrpg']['token'])
  @github_api = GithubAPI.new(@settings['github']['user'], @settings['github']['password'])

  # Initialize cache
  @cache = Cache.new
end

Public Instance Methods

check_for_events() click to toggle source
# File lib/githabit/app.rb, line 100
def check_for_events()
  new_events_found = 0

  @log.info("Checking github for new events")

  begin
    events = @github_api.get_user_events(@settings['github']['monitor_user'])
    # For now only push events
    events.select { |e| e['type'] == 'PushEvent'}.each do |event|
      # We only want pushes to master branches
      next if event['payload']['ref'] != "refs/heads/master"

      # Find how many commits
      event['payload']['commits'].each do |commit|

        # If already in the cache - skip it, we've already rewarded it
        next if @cache.cached? ("Commit:#{commit['sha']}")

        @log.info("Rewarding commit: " + commit['sha'] + " to repo: " + event['repo']['name'] + " Message: " + commit['message'])
        @habit_api.score_task(@github_commit_task_id, 'up', nil)

        new_events_found += 1
      end

      @cache.cache(event)

    end

    events.select { |e| e['type'] == 'IssuesEvent'}.each do |event|
      next if @cache.cached? ("Issue:#{event['id']}") or event['payload']['action'] != 'closed'

      @log.info("Rewarding closed issue for id: #{event['id']} Issue: #{event['payload']['issue']['title']}")

      @habit_api.score_task(@github_issue_task_id, 'up', nil)
      new_events_found += 1

      @cache.cache(event)

    end

    @log.info("Found " + new_events_found.to_s + " new events this run")
  end
rescue => e
  @log.error("There was a problem communicating with the server")
  @log.error(e)
end
find_tasks() click to toggle source
# File lib/githabit/app.rb, line 83
def find_tasks()
  @log.info("Finding HabitRPG task ids")

  @habit_api.list_tasks.each do |task|
    if (task['text'] == 'Make a Github commit')
      @log.info("Found github task id: " + task['id'])
      @github_commit_task_id = task['id']
    else
      if (task['text'] == "Close Github issue")
        @log.info("Found Close issue task: #{task['id']}")
        @github_issue_task_id = task['id']
      end
    end
  end
end
setup() click to toggle source
# File lib/githabit/app.rb, line 25
def setup()
  @log.info("Running setup - Caching all current events, no rewards will be generated for them")

  # Create tasks
  print "Do you want to attempt to create the Githabit tasks automatically? [Y/n] "
  response = $stdin.gets.chomp!

  if (response != "n")
    task = {
      "up" => true,
      "down" => false,
      "type" => "habit",
      "priority" => 1
    }

    task['text'] = "Make a Github commit"

    @habit_api.create_task(task)

    task['text'] = "Close Github issue"

    @habit_api.create_task(task)

    @log.info("Tasks created");
  end

  # Initialize cache
  @log.info("Checking github for new events")
  events = @github_api.get_user_events(@settings['github']['monitor_user'])

  events.select { |e| e['type'] == 'PushEvent'}.each do |event|
    @log.debug("Caching push event - #{event['id']}")
    @cache.cache(event)
  end

  events.select { |e| e['type'] == 'IssuesEvent'}.each do |event|
      next if event['payload']['action'] != 'closed'
      @log.debug("Caching issue close event - #{event['id']}")
      @cache.cache(event)
  end

  @log.info("Setup completed, starting main loop")
end
start() click to toggle source
# File lib/githabit/app.rb, line 69
def start()
  find_tasks()

  if (@settings['github']['autowatch'] == true)
    @log.info("Starting auto watch with delay of #{@settings['github']['frequency']} minutes")
    while (true)
      check_for_events()
      sleep(60 * @settings['github']['frequency'].to_i)
    end
  else
    check_for_events()
  end
end