Class: GHBulk

Inherits:
Object
  • Object
show all
Defined in:
lib/ghbulk.rb,
lib/ghbulk/version.rb

Overview

The class definition

Constant Summary collapse

VERSION =
'0.1.0'.freeze

Instance Method Summary collapse

Instance Method Details

#abort_script(message) ⇒ Object



11
12
13
14
# File 'lib/ghbulk.rb', line 11

def abort_script(message)
    puts message.colorize(:light_red)
    abort
end

#connect_client(gh_token) ⇒ Object



112
113
114
# File 'lib/ghbulk.rb', line 112

def connect_client(gh_token)
    return Octokit::Client.new(:access_token => gh_token)
end

#create_payload(repo) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# 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.
    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) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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) ⇒ Object



91
92
93
94
95
96
97
98
# 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) ⇒ Object



65
66
67
# File 'lib/ghbulk.rb', line 65

def does_repo_exist(client, repo_name)
    return client.repository?(repo_name)
end

#load_config(filename) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# 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) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# 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) ⇒ Object



16
17
18
# File 'lib/ghbulk.rb', line 16

def show_error(message)
    puts message.colorize(:light_red)
end

#show_success(message) ⇒ Object



24
25
26
# File 'lib/ghbulk.rb', line 24

def show_success(message)
    puts message.colorize(:light_green)
end

#show_warning(message) ⇒ Object



20
21
22
# File 'lib/ghbulk.rb', line 20

def show_warning(message)
    puts message.colorize(:light_yellow)
end