class Takoyaki::Commands::Activities

Attributes

from[R]
to[R]

Public Class Methods

new() click to toggle source
# File lib/takoyaki/commands/activities.rb, line 8
def initialize
  now = Time.now
  to = now + 24 * 60 * 60
  @to = Time.new(to.year, to.month, to.day)
  from = to - 7 * 24 * 60 * 60
  @from = Time.new(from.year, from.month, from.day)
end

Public Instance Methods

execute() click to toggle source
# File lib/takoyaki/commands/activities.rb, line 16
def execute
  events = []
  page = 0
  res = client.user_events(user, per_page: 100, page: page)
  loop do
    res.each do |event|
      if from <= event.created_at && event.created_at < to
        events << event
      end
    end
    break if res.last.created_at < from
    page += 1
    res = client.user_events(user, per_page: 100, page: page)
  end
  repo_events = Hash.new { |h, k| h[k] = {} }
  events.each do |event|
    next unless repositories.include?(event.repo&.name)
    case event.type
    when "IssueCommentEvent", "IssuesEvent"
      repo_events[event.repo.name][event.payload.issue.number] = event
    when "PullRequestReviewCommentEvent", "PullRequestEvent"
      repo_events[event.repo.name][event.payload.pull_request.number] = event
    when "CreateEvent", "DeleteEvent", "PushEvent"
    else
      $stderr.puts "UnknownEventType"
    end
  end
  repo_events.each do |repo, events|
    puts "### #{repo}"
    events.sort { |a, b| a[0] <=> b[0] }.each do |number, event|
      case event.type
      when /^Issue/
        puts "- [##{number} #{event.payload.issue.title}](#{event.payload.issue.html_url})"
      when /^PullRequest/
        puts "- [##{number} #{event.payload.pull_request.title}](#{event.payload.pull_request.html_url})"
      end
    end
    puts
  end
end

Private Instance Methods

client() click to toggle source
# File lib/takoyaki/commands/activities.rb, line 59
def client
  @client ||= Octokit::Client.new(access_token: config["access_token"])
end
config() click to toggle source
# File lib/takoyaki/commands/activities.rb, line 63
def config
  @config ||= YAML.load_file(File.expand_path("~/.takoyaki.yaml"))
end
event_types() click to toggle source
# File lib/takoyaki/commands/activities.rb, line 67
def event_types
  @event_types ||=
    %w(
      IssueCommentEvent
      PullRequestReviewCommentEvent
      IssuesEvent
      PullRequestEvent
    )
end
repositories() click to toggle source
# File lib/takoyaki/commands/activities.rb, line 77
def repositories
  @repositories ||=
    config["repositories"].map do |org, repos|
      repos.map { |repo| "#{org}/#{repo}" }
    end.flatten
end
user() click to toggle source
# File lib/takoyaki/commands/activities.rb, line 84
def user
  client.user.login
end