Class: Githabit::App
- Inherits:
-
Object
- Object
- Githabit::App
- Defined in:
- lib/githabit/app.rb
Instance Method Summary collapse
- #check_for_events ⇒ Object
- #find_tasks ⇒ Object
-
#initialize ⇒ App
constructor
A new instance of App.
- #setup ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize ⇒ App
Returns a new instance of App.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/githabit/app.rb', line 10 def initialize() @log = Logger.new(STDOUT) # Load config file @settings = YAML::load_file(File.("~/.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 |
Instance Method Details
#check_for_events ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# 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 ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# 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 ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# 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 ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# 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 |