Class: Prima

Inherits:
Object
  • Object
show all
Defined in:
lib/prima_twig.rb

Constant Summary collapse

CONFIG_KEYS =
['github', 'cloudflare_email', 'cloudflare_apikey']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePrima

Returns a new instance of Prima.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/prima_twig.rb', line 14

def initialize
  @twig = Twig.new(:read_options => true, :max_days_old => 30)
  unless has_config?
    if File.exist?('../prima/twig.yml')
      `cp ../prima/twig.yml .`
    elsif File.exist?('/tmp/twig.yml')
      `cp /tmp/twig.yml .`
    else
      generate_config
    end
    if File.exist?('./projects/prima')
       `git submodule init && git submodule update`
    end
  end
  unless has_latest_config?
    update_config
  end
  @config = YAML.load_file 'twig.yml'
  @rugged = Rugged::Repository.new('.')
  create_clients
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object

proxy di tutti i metodi a rugged



142
143
144
# File 'lib/prima_twig.rb', line 142

def method_missing(method)
  @rugged.send method
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#ghObject (readonly)

Returns the value of attribute gh.



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

def gh
  @gh
end

#ruggedObject (readonly)

Returns the value of attribute rugged.



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

def rugged
  @rugged
end

#twigObject (readonly)

Returns the value of attribute twig.



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

def twig
  @twig
end

Instance Method Details

#assign_issue_to_me(issue) ⇒ Object



211
212
213
214
215
216
217
218
# File 'lib/prima_twig.rb', line 211

def assign_issue_to_me(issue)
  detailed_issue = get_issue issue
  assignees = []
  detailed_issue.assignees.each do |assignee|
    assignees << assignee.
  end
  @gh.update_issue('primait/board', issue, :assignees => assignees)
end

#assign_pull_request_to_me(pr_number) ⇒ Object



220
221
222
# File 'lib/prima_twig.rb', line 220

def assign_pull_request_to_me(pr_number)
  @gh.update_pull_request(repo_name, pr_number, :assignee => )
end

#clean_branch_name(branch_name) ⇒ Object



123
124
125
126
127
# File 'lib/prima_twig.rb', line 123

def clean_branch_name(branch_name)
  branch_name.gsub! /[^a-zA-Z0-9\-_]/, '_'
  branch_name.gsub! /_+/, '_'
  branch_name.downcase
end

#close_issue(issue_number) ⇒ Object



257
258
259
# File 'lib/prima_twig.rb', line 257

def close_issue(issue_number)
  @gh.close_issue(repo_name, issue_number)
end

#create_clientsObject



79
80
81
82
83
84
# File 'lib/prima_twig.rb', line 79

def create_clients
  @gh = Octokit::Client.new(:access_token => @config['github'])
  Aws.config.update({
    region: 'eu-west-1'
  })
end

#create_pull_request(base_branch, head, title, body = '') ⇒ Object



247
248
249
# File 'lib/prima_twig.rb', line 247

def create_pull_request(base_branch, head, title, body = '')
  @gh.create_pull_request repo_name, base_branch, head, title, body
end

#create_release(tag_name, options = {}) ⇒ Object



265
266
267
# File 'lib/prima_twig.rb', line 265

def create_release(tag_name, options = {})
  @gh.create_release repo_name, tag_name, options
end

#current_branch_nameObject



146
147
148
# File 'lib/prima_twig.rb', line 146

def current_branch_name
  @twig.current_branch_name
end

#current_branch_refObject



118
119
120
121
# File 'lib/prima_twig.rb', line 118

def current_branch_ref
  repo = repo_user
  "#{repo}:#{current_branch_name}"
end

#delete_label_from_issue(issue_number, label) ⇒ Object



200
201
202
203
204
# File 'lib/prima_twig.rb', line 200

def delete_label_from_issue(issue_number, label)
  if issue_has_label?(issue_number, label)
    @gh.remove_label 'primait/board', issue_number, label
  end
end

#deployable_branchesObject



90
91
92
# File 'lib/prima_twig.rb', line 90

def deployable_branches
  %w(dev)
end

#disabled_branchesObject



86
87
88
# File 'lib/prima_twig.rb', line 86

def disabled_branches
  %w(master dev)
end

#generate_configObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/prima_twig.rb', line 49

def generate_config
  puts 'Progetto non inizializzato'.red
  puts 'Creazione del file di configurazione'.yellow
  conf = {}
  CONFIG_KEYS.each do |k|
    token = ask "#{k} token: "
    conf[k] = token
  end
  write_config conf
end

#get_issue(issue_number) ⇒ Object



239
240
241
# File 'lib/prima_twig.rb', line 239

def get_issue(issue_number)
  @gh.issue 'primait/board', issue_number
end

#get_prObject



159
160
161
162
163
164
165
166
# File 'lib/prima_twig.rb', line 159

def get_pr
  prs = @gh.pulls repo_name, { head: current_branch_ref }
  if prs.length > 0
    prs[0]
  else
    nil
  end
end

#get_pr_commits(pr_number) ⇒ Object



243
244
245
# File 'lib/prima_twig.rb', line 243

def get_pr_commits(pr_number)
  @gh.pull_request_commits repo_name, pr_number
end

#get_pr_urlObject



150
151
152
153
154
155
156
157
# File 'lib/prima_twig.rb', line 150

def get_pr_url
  pr = get_pr
  if pr.nil?
    nil
  else
    pr.html_url
  end
end

#get_property(name) ⇒ Object



186
187
188
# File 'lib/prima_twig.rb', line 186

def get_property(name)
  @twig.get_branch_property current_branch_name, name
end

#has_config?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/prima_twig.rb', line 36

def has_config?
  File.exist? 'twig.yml'
end

#has_latest_config?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
# File 'lib/prima_twig.rb', line 40

def has_latest_config?
  existing_config = YAML.load_file 'twig.yml'
  CONFIG_KEYS.each do |k|
    unless existing_config.has_key? k
      return false
    end
  end
end

#has_property?(name) ⇒ Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/prima_twig.rb', line 190

def has_property?(name)
  not @twig.get_branch_property(current_branch_name, name).nil?
end

#index_diffObject



102
103
104
# File 'lib/prima_twig.rb', line 102

def index_diff
  @rugged.index.diff
end

#is_a_deployable_branch?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/prima_twig.rb', line 98

def is_a_deployable_branch?
  deployable_branches.include? current_branch_name
end

#is_a_valid_branch?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/prima_twig.rb', line 94

def is_a_valid_branch?
  not disabled_branches.include? current_branch_name
end

#is_already_a_pr?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/prima_twig.rb', line 168

def is_already_a_pr?
  not get_pr_url.nil?
end

#is_clean?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/prima_twig.rb', line 106

def is_clean?
  index_diff.count === 0
end

#is_issue_in_review?(issue) ⇒ Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/prima_twig.rb', line 172

def is_issue_in_review?(issue)
  issue_has_label?(issue, Prima::LABEL_REVIEW)
end

#issue_has_label?(issue, label) ⇒ Boolean

Returns:

  • (Boolean)


176
177
178
179
# File 'lib/prima_twig.rb', line 176

def issue_has_label?(issue, label)
  labels = @gh.labels_for_issue 'primait/board', issue.to_i
  not labels.find_index{ |l| l.name == label }.nil?
end

#list_issuesObject



224
225
226
227
228
229
230
231
232
233
# File 'lib/prima_twig.rb', line 224

def list_issues
  results = @gh.list_issues 'primait/board', state: :open, :per_page => 20, :page => 1
  unless @gh.last_response.rels[:last].nil?
    number_of_pages = @gh.last_response.rels[:last].href.match(/page=(\d+)/)[1]
    for i in 2..number_of_pages.to_i
      results.concat @gh.list_issues('primait/board', state: :open, :per_page => 20, :page => i)
    end
  end
  results
end

#merge_pull_request(pr, commit_message = '') ⇒ Object



251
252
253
254
255
# File 'lib/prima_twig.rb', line 251

def merge_pull_request(pr, commit_message='')
  raise "Invalid Pull Request" unless pr
  pr_number = pr[:number]
  @gh.merge_pull_request(repo_name, pr_number, commit_message)
end

#put_issue_in_review(issue) ⇒ Object



181
182
183
184
# File 'lib/prima_twig.rb', line 181

def put_issue_in_review(issue)
  update_issue_with_label(issue, Prima::LABEL_REVIEW)
  gh.remove_label 'primait/board', issue, Prima::LABEL_WIP
end

#reduce_size(str, len) ⇒ Object



280
281
282
283
284
285
286
287
# File 'lib/prima_twig.rb', line 280

def reduce_size(str, len)
  out_str = str
  if str.size > len
    out_str = str[0..(len-1)]
    out_str << '...'
  end
  out_str
end

#repo_has_modified_files?Boolean

Returns:

  • (Boolean)


129
130
131
132
133
134
135
136
137
138
139
# File 'lib/prima_twig.rb', line 129

def repo_has_modified_files?
  modified = false
  @rugged.status do |file, status_data|
    if status_data.first != :ignored
      puts "#{file}: #{status_data.inspect}"
      modified = true
      break
    end
  end
  modified
end

#repo_nameObject



110
111
112
# File 'lib/prima_twig.rb', line 110

def repo_name
  `git config --get remote.origin.url`.split(':').last.chomp.chomp('.git')
end

#repo_urlObject



261
262
263
# File 'lib/prima_twig.rb', line 261

def repo_url
  `git config remote.origin.url`
end

#repo_userObject



114
115
116
# File 'lib/prima_twig.rb', line 114

def repo_user
  repo_url.chomp.split(':').last.split('/').first
end

#update_configObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/prima_twig.rb', line 60

def update_config
  puts 'Mancano alcuni parametri nella tua configurazione'.yellow
  existing_config = YAML.load_file 'twig.yml'
  CONFIG_KEYS.each do |k|
    unless existing_config.has_key? k
      token = ask "#{k} token: "
      existing_config[k] = token
    end
  end
  write_config existing_config
end

#update_issue_with_label(issue, label) ⇒ Object



194
195
196
197
198
# File 'lib/prima_twig.rb', line 194

def update_issue_with_label(issue, label)
  unless issue_has_label?(issue, label)
    gh.add_labels_to_an_issue 'primait/board', issue, [ label ]
  end
end

#update_pr(pr_number, options = {}) ⇒ Object



235
236
237
# File 'lib/prima_twig.rb', line 235

def update_pr(pr_number, options = {})
  @gh.update_pull_request repo_name, pr_number, options
end

#user_loginObject



206
207
208
209
# File 'lib/prima_twig.rb', line 206

def 
  user = @gh.user
  user.
end

#write_config(conf = {}) ⇒ Object



72
73
74
75
76
77
# File 'lib/prima_twig.rb', line 72

def write_config(conf = {})
  File.open('twig.yml', 'w') { |file|
    file.write conf.to_yaml
    puts 'File di configurazione aggiornato'.green
  }
end

#yesno(prompt = 'Continue?', default = true) ⇒ Object



269
270
271
272
273
274
275
276
277
278
# File 'lib/prima_twig.rb', line 269

def yesno(prompt = 'Continue?', default = true)
  a = ''
  s = default ? '[Y/n]' : '[y/N]'
  d = default ? 'y' : 'n'
  until %w[y n].include? a
    a = ask("#{prompt} #{s} ") { |q| q.limit = 1; q.case = :downcase }
    a = d if a.length == 0
  end
  a == 'y'
end