class GHBulk

The class definition

Constants

VERSION

Public Instance Methods

abort_script(message) click to toggle source
# File lib/ghbulk.rb, line 11
def abort_script(message)
    puts message.colorize(:light_red)
    abort
end
connect_client(gh_token) click to toggle source
# File lib/ghbulk.rb, line 112
def connect_client(gh_token)
    return Octokit::Client.new(:access_token => gh_token)
end
create_payload(repo) click to toggle source
# File lib/ghbulk.rb, line 41
def create_payload(repo)
    payload = {}

    payload[:allow_merge_commit]     = repo['allow_merge_commit']     if repo.key?('allow_merge_commit')
    payload[:allow_rebase_merge]     = repo['allow_rebase_merge']     if repo.key?('allow_rebase_merge')
    payload[:allow_squash_merge]     = repo['allow_squash_merge']     if repo.key?('allow_squash_merge')
    payload[:archived]               = repo['archived']               if repo.key?('archived')
    payload[:auto_init]              = repo['auto_init']              if repo.key?('auto_init')
    payload[:delete_branch_on_merge] = repo['delete_branch_on_merge'] if repo.key?('delete_branch_on_merge')
    payload[:description]            = repo['description']            if repo.key?('description')
    payload[:gitignore_template]     = repo['gitignore_template']     if repo.key?('gitignore_template')
    payload[:has_issues]             = repo['has_issues']             if repo.key?('has_issues')
    payload[:has_projects]           = repo['has_projects']           if repo.key?('has_projects')
    payload[:has_wiki]               = repo['has_wiki']               if repo.key?('has_wiki')
    payload[:homepage]               = repo['homepage']               if repo.key?('homepage')
    payload[:is_template]            = repo['is_template']            if repo.key?('is_template')
    payload[:license_template]       = repo['license_template']       if repo.key?('license_template')
    payload[:organization]           = repo['organization']           if repo.key?('organization')
    payload[:private]                = repo['private']                if repo.key?('private')
    payload[:team_id]                = repo['team_id']                if repo.key?('team_id')

    return payload
end
create_repositories(gh_token, filename) click to toggle source
# File lib/ghbulk.rb, line 116
def create_repositories(gh_token, filename)
    repo_list = load_config(filename)

    client = connect_client(gh_token)
    begin
        username = client.user.login
    rescue Octokit::Error
        abort('Inavlid Token')
    end
    repo_list.each do |repo|
        payload = create_payload(repo)

        repo_name = repo['name']
        full_repo_name = if repo.key?('organization')
                             "#{repo['organization']}/#{repo['name']}"
                         else
                             "#{username}/#{repo['name']}"
                         end

        if repo['action'] && repo['action'] == 'delete'
            delete_repository(client, full_repo_name)
        else
            create_repository(client, repo_name, full_repo_name, payload)
        end
    end
end
create_repository(client, repo_name, full_repo_name, payload) click to toggle source
# File lib/ghbulk.rb, line 69
def create_repository(client, repo_name, full_repo_name, payload)
    if does_repo_exist(client, full_repo_name)
        puts "Updating: #{full_repo_name}"
        begin
            client.update_repository(full_repo_name, payload)
        rescue Octokit::UnprocessableEntity
            show_error('Error updating repo')
        else
            show_success("Repo #{full_repo_name} has been updated")
        end
    else
        puts "Creating: #{repo_name}"
        begin
            client.create_repository(repo_name, payload)
        rescue Octokit::UnprocessableEntity
            show_error('Error creating repo')
        else
            show_success("Repo #{repo_name} has been created")
        end
    end
end
delete_repository(client, full_repo_name) click to toggle source
# File lib/ghbulk.rb, line 91
def delete_repository(client, full_repo_name)
    if does_repo_exist(client, full_repo_name)
        client.delete_repository(full_repo_name)
        show_error("Repository #{full_repo_name} has been deleted")
    else
        show_warning("Cannot delete - #{full_repo_name} doesn't exist")
    end
end
does_repo_exist(client, repo_name) click to toggle source
# File lib/ghbulk.rb, line 65
def does_repo_exist(client, repo_name)
    return client.repository?(repo_name)
end
load_config(filename) click to toggle source
# File lib/ghbulk.rb, line 100
def load_config(filename)
    config = read_file(filename)

    return if config.nil?

    yaml_hash = YAML.safe_load(config)

    abort('Repos not located - check file symtax and try again') unless yaml_hash.key?('repos')

    return yaml_hash['repos']
end
read_file(filename) click to toggle source
# File lib/ghbulk.rb, line 28
def read_file(filename)
    contents = nil

    begin
        File.open(filename, 'r') do |f|
            contents = f.read
        end
    rescue SystemCallError
        abort_script("Error reading file: #{filename} - Aborting")
    end
    return contents
end
show_error(message) click to toggle source
# File lib/ghbulk.rb, line 16
def show_error(message)
    puts message.colorize(:light_red)
end
show_success(message) click to toggle source
# File lib/ghbulk.rb, line 24
def show_success(message)
    puts message.colorize(:light_green)
end
show_warning(message) click to toggle source
# File lib/ghbulk.rb, line 20
def show_warning(message)
    puts message.colorize(:light_yellow)
end