class Sambot::SourceControl

Constants

ET_GITHUB
ET_GITHUB_API
ET_GITHUB_HOST
ROLE_COOKBOOKS
WRAPPER_COOKBOOKS

Public Class Methods

add_deploy_key(config) click to toggle source
# File lib/sambot/source_control.rb, line 25
def add_deploy_key(config)
  # Create deploy key
  # Add it to Vault
  # Run Chef-Client against TeamCity
end
checkout(organization) click to toggle source
# File lib/sambot/source_control.rb, line 58
def checkout(organization)
  repositories = list_organization_repositories(organization)
  repositories.each do |repository|
    if Dir.exist?(repository.name)
      UI.info("#{repository.name} already exists and will be updated")
      Dir.chdir(repository.name) do
        `git pull`
      end
    else
      `git clone #{repository.ssh_url}`
    end
  end
end
create_repository(config) click to toggle source
# File lib/sambot/source_control.rb, line 31
def create_repository(config)
  target = identify_repository(config)
  if api.repos.get user: target[:organization], repo: target[:name]
    UI.info("The repository #{target[:organization]}/#{target[:name]} exists and will not be recreated")
    return 1
  else
    api.repos.create "name": target[:name], org: target[:organization],
        "private": false,
        "has_issues": true,
        "has_wiki": false,
        "has_downloads": true
      UI.info("The repository #{target[:organization]}/#{target[:name]} has been created")
    return 0
  end
end
delete_repository(config) click to toggle source
# File lib/sambot/source_control.rb, line 47
def delete_repository(config)
  target = identify_repository(config)
  if api.repos.get "#{target[:organization]}/#{target[:name]}"
    UI.info("The repository #{target[:organization]}/#{arget[:name]} does not exist and will not be deleted")
    return 1
  else
    api.repos.delete organization, repo
    return 0
  end
end
edit_file(organization, repository, path, contents, file, msg) click to toggle source
# File lib/sambot/source_control.rb, line 80
def edit_file(organization, repository, path, contents, file, msg)
  contents_api.update organization, repository, path,
    content: contents,
    path: path,
    message: msg,
    sha: file.sha
end
get_file(organization, repository, path) click to toggle source
# File lib/sambot/source_control.rb, line 76
def get_file(organization, repository, path)
  contents_api.find user: organization, repo: repository, path: path
end
list_organization_repositories(organization) click to toggle source
# File lib/sambot/source_control.rb, line 72
def list_organization_repositories(organization)
  api.repos.list org: organization
end
set_git_remote(config) click to toggle source
# File lib/sambot/source_control.rb, line 18
def set_git_remote(config)
  target = identify_repository(config)
  cmd = "git remote set-url origin git@#{ET_GITHUB_HOST}:#{target[:organization]}/#{target[:name]}"
  UI.info("Running the following command: #{cmd}")
  `#{cmd}`
end

Private Class Methods

api() click to toggle source
# File lib/sambot/source_control.rb, line 101
def api
  check_configuration
  @github ||= Github.new do |c|
    c.basic_auth = "#{ENV['GITHUB_USERNAME']}:#{ENV['GITHUB_PERSONAL_TOKEN']}"
    c.endpoint = ET_GITHUB_API
    c.site = ET_GITHUB
  end
end
check_configuration() click to toggle source
# File lib/sambot/source_control.rb, line 110
def check_configuration
  unless ENV['GITHUB_USERNAME']
    UI.error("Please ensure the GITHUB_USERNAME environment variable is set before running Github tasks.")
    exit
  end
  unless ENV['GITHUB_PERSONAL_TOKEN']
    UI.error("Please ensure the GITHUB_PERSONAL_TOKEN environment variable is set before running Github tasks. A personal token can be obtained from your Github account settings.")
    exit
  end
end
contents_api() click to toggle source
# File lib/sambot/source_control.rb, line 97
def contents_api
  api.repos.contents
end
identify_repository(config) click to toggle source
# File lib/sambot/source_control.rb, line 90
def identify_repository(config)
  {
    name: config.name,
    organization: config.is_role_cookbook? ? ROLE_COOKBOOKS : WRAPPER_COOKBOOKS
  }
end