class Nailed::Github

Attributes

client[R]

Public Class Methods

new() click to toggle source
# File lib/nailed/github.rb, line 5
def initialize
  Octokit.auto_paginate = true
  @client = Octokit::Client.new(:netrc => true)
end

Public Instance Methods

get_open_pulls() click to toggle source
# File lib/nailed/github.rb, line 10
def get_open_pulls
  Nailed.get_config["products"].each do |product,values|
    organization = values["organization"]
    repos = values["repos"]
    repos.each do |repo|
      Nailed.log("info", "#{__method__}: Getting open pullrequests for #{organization}/#{repo}")
      pulls = @client.pull_requests("#{organization}/#{repo}")
      pulls.each do |pr|
        attributes = {:pr_number => pr.number,
                     :title => pr.title,
                     :state => pr.state,
                     :url => pr.html_url,
                     :created_at => pr.created_at,
                     :repository_rname => repo}

        # if pr exists dont create a new record
        pull_to_update = Pullrequest.all(:pr_number => pr.number, :repository_rname => repo)
        if pull_to_update
          if pr.state == "closed"
            # delete record if pr.state changed to "closed"
            pull_to_update.destroy
            Nailed.log("info", "#{__method__}: Destroyed closed pullrequest #{pr.repo} ##{pr.number}")
          else
            # update saves the state, so we dont need a db_handler
            # TODO check return code for true if saved correctly
            pull_to_update.update(attributes)
            Nailed.log("info", "#{__method__}: Updated #{pr.repo} ##{pr.number} with #{attributes.inspect}")
          end
        else
          db_handler = Pullrequest.first_or_create(attributes)
          Nailed.log("info", "#{__method__}: Created new pullrequest #{pr.repo} ##{pr.number} with #{attributes.inspect}")
        end

        Nailed.save_state(db_handler) unless defined? db_handler
        Nailed.log("info", "#{__method__}: Saved #{attributes.inspect}")
      end unless pulls.empty?
      write_pull_trends(repo)
    end unless repos.nil?
  end
end