-
Notifications
You must be signed in to change notification settings - Fork 3
/
agent.rb
128 lines (99 loc) · 2.8 KB
/
agent.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# frozen_string_literal: true
require 'colorize'
require 'highline'
require 'yaml'
require 'octokit'
class Agent
CLI = HighLine.new
MAX_RETRY = 3
LABELS = 'labels.yaml'.freeze
PROJECT = 'project.yaml'.freeze
def initialize(logger)
@logger = logger
@logger.formatter \
= proc { |severity, _, _, message| "#{severity}: #{message}\n".green }
end
def run
set_resource_folder
connect
choose_start_point
repo = if @start <= 1
create_private_repo
else
CLI.ask('repo for CTF (username/repo)? ')
end
setup_labels(repo) if @start <= 2
setup_project(repo) if @start <= 3
end
private
def set_resource_folder
@res = CLI.ask('resource folder? ') { |q| q.default = 'templates' }
end
def label_file
"#{@res}/#{LABELS}"
end
def project_file
"#{@res}/#{PROJECT}"
end
def connect
retries ||= 0
@username = CLI.ask 'username? '
@password = CLI.ask('password? ') { |q| q.echo = 'x' }
@client = Octokit::Client.new(login: @username, password: @password)
@client.user # Try to fetch
@logger.info "Logged in user #{@username}"
rescue Octokit::Unauthorized
@logger.error 'Wrong username or password'
retry if (retries += 1) < MAX_RETRY
raise
end
def choose_start_point
CLI.choose do |menu|
menu.header = 'Start with creating'
menu.choice(:repository) { @start = 1 }
menu.choice(:labels) { @start = 2 }
menu.choice(:project) { @start = 3 }
menu.prompt = 'from where? '
end
end
def create_private_repo
repo = CLI.ask 'new repo for CTF? '
opts = { private: true }
@logger.info "Creating #{repo}"
@client.create_repo(repo, opts)
"#{@username}/#{repo}"
end
def setup_labels(repo)
delete_old_labels! repo
labels = YAML.load_file label_file
add_labels(repo, labels)
end
def delete_old_labels!(repo)
@logger.info "Deleting old labels in #{repo}..."
@client.labels(repo).each do |label|
@logger.info "Deleting a label (#{label.name})..."
@client.delete_label!(repo, label.name)
end
end
def add_labels(repo, labels)
labels.each do |name, color|
@logger.info "Adding a new label (#{name}: #{color})..."
@client.add_label(repo, name.to_s, color)
end
end
def setup_project(repo)
project = YAML.load_file project_file
new_project = create_project(repo, project['name'])
add_project_columns(new_project.id, project['columns'])
end
def create_project(repo, name)
@logger.info "Creating a new project (#{name})..."
@client.create_project(repo, name)
end
def add_project_columns(id, columns)
columns.each do |_, column|
@logger.info "Adding a new column (#{column})..."
@client.create_project_column(id, column)
end
end
end