Module: Cinch::Plugins::Octospy::Recording

Included in:
Cinch::Plugins::Octospy
Defined in:
lib/cinch/plugins/octospy/recording.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/cinch/plugins/octospy/recording.rb', line 5

def self.included(base)
  base.class_eval do
    match(/watch ([\w\-\.]+)\/([\w\-\.]+)$/, method: :watch_repository)
    match(/unwatch ([\w\-\.]+)\/([\w\-\.]+)$/, method: :unwatch_repository)
    match(/watch ([\w\-\.]+)$/, method: :watch_repositories)
    match(/unwatch ([\w\-\.]+)$/, method: :unwatch_repositories)
    match(/show watched( repos(itories)?)?/, method: :show_watched_repositories)
  end
end

Instance Method Details

#show_watched_repositories(m) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cinch/plugins/octospy/recording.rb', line 87

def show_watched_repositories(m)
  channel = ::Octospy::Recordable.channel(m.channel.name)

  if channel.nil? || channel.repos.nil? || channel.repos.empty?
    m.reply 'nothing'
    return
  end

  channel.repos.each.with_index(1) do |repo, i|
    m.reply "#{i} #{repo}"
  end
end

#unwatch_all(m) ⇒ Object



83
84
85
# File 'lib/cinch/plugins/octospy/recording.rb', line 83

def unwatch_all(m)
  ::Octospy::Recordable.remove_channel(m.channel.name)
end

#unwatch_repositories(m, owner) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cinch/plugins/octospy/recording.rb', line 65

def unwatch_repositories(m, owner)
  repos = ::Octospy::Recordable.channel(m.channel.name).repos.each_with_object([]) do |repo, obj|
    next unless repo.split('/').first == owner
    ::Octospy::Recordable.channel(m.channel.name).remove_repo(repo)
    opj << repo
  end

  if repos.count > 0
    if ::Octospy::Recordable.channel(m.channel.name).repos.count > 0
      m.reply "stopped to watch events of #{repos.count} repositories"
      restart(m)
    else
      m.reply "stopped job so no watched repository"
      stop(m)
    end
  end
end

#unwatch_repository(m, owner, project) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cinch/plugins/octospy/recording.rb', line 51

def unwatch_repository(m, owner, project)
  repo = "#{owner}/#{project}"

  unless ::Octokit.repository?(repo)
    m.reply "sorry, '#{repo}' not found"
    return
  end

  ::Octospy::Recordable.channel(m.channel.name).remove_repo(repo)

  restart(m)
  m.reply "stopped to watch the #{repo} events"
end

#watch_repositories(m, owner) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cinch/plugins/octospy/recording.rb', line 30

def watch_repositories(m, owner)
  user = ::Octokit.user?(owner)

  unless user
    m.reply "Sorry, '#{owner}' not found"
    return
  end

  ::Octospy::Recordable.add_channel m.channel.name
  method = "#{'org_' if user.type == 'Organization'}repos".to_sym
  repos = ::Octokit.send(method, owner).map { |repo|
    ::Octospy::Recordable.channel(m.channel.name).add_repo(repo.full_name)
    repo.full_name
  }

  if repos.count > 0
    m.reply "started to watch events of #{repos.count} repositories"
    restart(m)
  end
end

#watch_repository(m, owner, project) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cinch/plugins/octospy/recording.rb', line 15

def watch_repository(m, owner, project)
  repo = "#{owner}/#{project}"

  unless ::Octokit.repository?(repo)
    m.reply "Sorry, '#{repo}' not found"
    return
  end

  ::Octospy::Recordable.add_channel m.channel.name
  ::Octospy::Recordable.channel(m.channel.name).add_repo(repo)

  restart(m)
  m.reply "started to watch the #{repo} events"
end