Class: Takoyaki::Commands::Activities

Inherits:
Object
  • Object
show all
Defined in:
lib/takoyaki/commands/activities.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeActivities

Returns a new instance of Activities.



8
9
10
11
12
13
14
# 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

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



6
7
8
# File 'lib/takoyaki/commands/activities.rb', line 6

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



6
7
8
# File 'lib/takoyaki/commands/activities.rb', line 6

def to
  @to
end

Instance Method Details

#clientObject



57
58
59
# File 'lib/takoyaki/commands/activities.rb', line 57

def client
  @client ||= Octokit::Client.new(access_token: config["access_token"])
end

#configObject



61
62
63
# File 'lib/takoyaki/commands/activities.rb', line 61

def config
  @config ||= YAML.load_file(File.expand_path("~/.takoyaki.yaml"))
end

#event_typesObject



65
66
67
68
69
70
71
72
73
# File 'lib/takoyaki/commands/activities.rb', line 65

def event_types
  @event_types ||=
    %w(
      IssueCommentEvent
      PullRequestReviewCommentEvent
      IssuesEvent
      PullRequestEvent
    )
end

#executeObject



16
17
18
19
20
21
22
23
24
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
# 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
      binding.pry
    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

#repositoriesObject



75
76
77
78
79
80
# File 'lib/takoyaki/commands/activities.rb', line 75

def repositories
  @repositories ||=
    config["repositories"].map do |org, repos|
      repos.map { |repo| "#{org}/#{repo}" }
    end.flatten
end

#userObject



82
83
84
# File 'lib/takoyaki/commands/activities.rb', line 82

def user
  client.user.
end