require 'api_cache' require 'moneta' require 'octokit' #TODO - this is actually a github class module Helpers class Github #TODO make this a singleton #TODO add repo, and branch to cache keys def initialize(repo, branch, github_token=ENV['GITHUB_TOKEN']) #TODO: inherit from base class @github_token = github_token if !@github_token || @github_token.empty? $stderr.puts 'github_token missing. Unable to authenticate with Github. Aborting...' exit 1 end @repo = repo @branch = branch @logger = Logging.logger[self] @logger.add_appenders \ Logging.appenders.stdout, Logging.appenders.file('overview.log') @logger.level = :info APICache.store = Moneta.new(:YAML, :file => "#{self.class.name}_cache") APICache.logger.level = Logger::INFO #APICache.store = nil end def client if !@client @client = Octokit::Client.new(:access_token => @github_token) @client.auto_paginate = true @client.default_media_type = "application/vnd.github.moondragon+json" @logger.info "Connected to Github as #{@client.user[:login]}" end @client end def hooks @hooks = client.hooks(@repo) unless @hooks @logger.info "Retrieved hooks for #{@repo}" if @hooks @hooks end def releases #APICache.get("releases") do client.releases(@repo) #end end def sprintly_product_id product_id = hooks.map { |h| h.config.product_id }.compact.first @logger.info "Sprint.ly product_id = #{product_id}" if product_id product_id end def ref(tag) #APICache.get("ref_#{tag}") do client.ref(@repo, "tags/" + tag) #end end def commit(sha) #APICache.get("commit_#{sha}") do client.commit(@repo, sha) #end end def commits_since(since) #APICache.get("commit_since_#{since}") do client.commits_since(@repo, since, @branch) #end end def commits_between(from,to) end def commits_before(to) end end end