Class: Mumukit::Sync::Store::Github::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/mumukit/sync/store/github/bot.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, email, token) ⇒ Bot

Returns a new instance of Bot.



5
6
7
8
9
10
# File 'lib/mumukit/sync/store/github/bot.rb', line 5

def initialize(name, email, token)
  ensure_present! name, email
  @name = name
  @email = email
  @token = token
end

Instance Attribute Details

#emailObject

Returns the value of attribute email.



3
4
5
# File 'lib/mumukit/sync/store/github/bot.rb', line 3

def email
  @email
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/mumukit/sync/store/github/bot.rb', line 3

def name
  @name
end

#tokenObject

Returns the value of attribute token.



3
4
5
# File 'lib/mumukit/sync/store/github/bot.rb', line 3

def token
  @token
end

Class Method Details

.from_envObject



40
41
42
# File 'lib/mumukit/sync/store/github/bot.rb', line 40

def self.from_env
  new ENV['MUMUKI_BOT_USERNAME'], ENV['MUMUKI_BOT_EMAIL'], ENV['MUMUKI_BOT_API_TOKEN']
end

Instance Method Details

#authenticated?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/mumukit/sync/store/github/bot.rb', line 36

def authenticated?
  !!token
end

#clone_into(repo, dir, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/mumukit/sync/store/github/bot.rb', line 16

def clone_into(repo, dir, &block)
  local_repo = Git.clone(writable_github_url_for(repo), '.', path: dir)
  local_repo.config('user.name', name)
  local_repo.config('user.email', email)
  yield dir, local_repo
rescue Git::GitExecuteError => e
  raise Mumukit::Sync::SyncError, 'Repository is private or does not exist' if private_repo_error(e.message)
  raise Mumukit::Sync::SyncError, "Clone of #{repo} failed"
end

#ensure_exists!(slug, private) ⇒ Object



12
13
14
# File 'lib/mumukit/sync/store/github/bot.rb', line 12

def ensure_exists!(slug, private)
  create!(slug, private) unless exists?(slug)
end

#register_post_commit_hook!(slug, web_hook_base_url) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/mumukit/sync/store/github/bot.rb', line 26

def register_post_commit_hook!(slug, web_hook_base_url)
  octokit.create_hook(
    slug.to_s, 'web',
    {url: "#{web_hook_base_url}/#{slug.to_s}", content_type: 'json'},
    {events: ['push'],
     active: true})
rescue => e
  puts "not registering post commit hook: #{e.message}"
end