class Mumukit::Sync::Store::Github::Bot

Attributes

email[RW]
name[RW]
token[RW]

Public Class Methods

from_env() click to toggle source
# 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
new(name, email, token) click to toggle source
# 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

Public Instance Methods

authenticated?() click to toggle source
# File lib/mumukit/sync/store/github/bot.rb, line 36
def authenticated?
  !!token
end
clone_into(repo, dir) { |dir, local_repo| ... } click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

create!(slug, private) click to toggle source
# File lib/mumukit/sync/store/github/bot.rb, line 53
def create!(slug, private)
  octokit.create_repository(slug.repository, organization: slug.organization)
  try_set_private!(slug) if private
rescue Octokit::Forbidden => e
  raise raise Mumukit::Sync::SyncError, "Mumuki is not allowed to create repositories in organization #{slug.organization}"
end
exists?(slug) click to toggle source
# File lib/mumukit/sync/store/github/bot.rb, line 46
def exists?(slug)
  Git.ls_remote(writable_github_url_for(slug))
  true
rescue Git::GitExecuteError
  false
end
octokit() click to toggle source
# File lib/mumukit/sync/store/github/bot.rb, line 70
def octokit
  Octokit::Client.new(access_token: token)
end
private_repo_error(message) click to toggle source
# File lib/mumukit/sync/store/github/bot.rb, line 74
def private_repo_error(message)
  ['could not read Username', 'Invalid username or password'].any? { |it| message.include? it }
end
try_set_private!(slug) click to toggle source
# File lib/mumukit/sync/store/github/bot.rb, line 60
def try_set_private!(slug)
  octokit.set_private(slug.to_s)
rescue Octokit::NotFound
  puts "#{slug.to_s} repository can't be set as private"
end
writable_github_url_for(slug) click to toggle source
# File lib/mumukit/sync/store/github/bot.rb, line 66
def writable_github_url_for(slug)
  "https://#{name}:#{token}@github.com/#{slug}"
end