Class: Prima

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

Constant Summary collapse

LABEL_REVIEW =
'review'
LABEL_WIP =
'wip'
LABEL_HOTFIX =
'hotfix'
LABEL_NEXT_RELEASE =
'next release'
LABEL_IN_STAGING =
'in staging'
CONFIG_KEYS =
['github', 'mandrill', 'sparkpost', 'cloudflare_email', 'cloudflare_apikey', 'aws_username', 'aws_password', 'prima_apikey']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePrima

Returns a new instance of Prima.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/prima_twig.rb', line 19

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



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

def method_missing(method)
  @rugged.send method
end

Instance Attribute Details

#awsObject (readonly)

Returns the value of attribute aws.



17
18
19
# File 'lib/prima_twig.rb', line 17

def aws
  @aws
end

#configObject (readonly)

Returns the value of attribute config.



17
18
19
# File 'lib/prima_twig.rb', line 17

def config
  @config
end

#ghObject (readonly)

Returns the value of attribute gh.



17
18
19
# File 'lib/prima_twig.rb', line 17

def gh
  @gh
end

#ruggedObject (readonly)

Returns the value of attribute rugged.



17
18
19
# File 'lib/prima_twig.rb', line 17

def rugged
  @rugged
end

#twigObject (readonly)

Returns the value of attribute twig.



17
18
19
# File 'lib/prima_twig.rb', line 17

def twig
  @twig
end

Instance Method Details

#assign_issue_to_me(issue) ⇒ Object



216
217
218
219
220
221
222
223
# File 'lib/prima_twig.rb', line 216

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



225
226
227
# File 'lib/prima_twig.rb', line 225

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

#clean_branch_name(branch_name) ⇒ Object



128
129
130
131
132
# File 'lib/prima_twig.rb', line 128

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



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

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

#create_clientsObject



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

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



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

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



270
271
272
# File 'lib/prima_twig.rb', line 270

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

#current_branch_nameObject



151
152
153
# File 'lib/prima_twig.rb', line 151

def current_branch_name
  @twig.current_branch_name
end

#current_branch_refObject



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

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

#delete_label_from_issue(issue_number, label) ⇒ Object



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

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



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

def deployable_branches
  %w(dev)
end

#disabled_branchesObject



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

def disabled_branches
  %w(master dev)
end

#generate_configObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/prima_twig.rb', line 54

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



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

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

#get_prObject



164
165
166
167
168
169
170
171
# File 'lib/prima_twig.rb', line 164

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



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

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

#get_pr_urlObject



155
156
157
158
159
160
161
162
# File 'lib/prima_twig.rb', line 155

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

#get_property(name) ⇒ Object



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

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

#has_config?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/prima_twig.rb', line 41

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

#has_latest_config?Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
# File 'lib/prima_twig.rb', line 45

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)


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

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

#index_diffObject



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

def index_diff
  @rugged.index.diff
end

#is_a_deployable_branch?Boolean

Returns:

  • (Boolean)


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

def is_a_deployable_branch?
  deployable_branches.include? current_branch_name
end

#is_a_valid_branch?Boolean

Returns:

  • (Boolean)


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

def is_a_valid_branch?
  not disabled_branches.include? current_branch_name
end

#is_already_a_pr?Boolean

Returns:

  • (Boolean)


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

def is_already_a_pr?
  not get_pr_url.nil?
end

#is_clean?Boolean

Returns:

  • (Boolean)


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

def is_clean?
  index_diff.count === 0
end

#is_issue_in_review?(issue) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#issue_has_label?(issue, label) ⇒ Boolean

Returns:

  • (Boolean)


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

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



229
230
231
232
233
234
235
236
237
238
# File 'lib/prima_twig.rb', line 229

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



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

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



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

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



285
286
287
288
289
290
291
292
# File 'lib/prima_twig.rb', line 285

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)


134
135
136
137
138
139
140
141
142
143
144
# File 'lib/prima_twig.rb', line 134

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



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

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

#repo_urlObject



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

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

#repo_userObject



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

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

#update_configObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/prima_twig.rb', line 65

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



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

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



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

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

#user_loginObject



211
212
213
214
# File 'lib/prima_twig.rb', line 211

def 
  user = @gh.user
  user.
end

#write_config(conf = {}) ⇒ Object



77
78
79
80
81
82
# File 'lib/prima_twig.rb', line 77

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



274
275
276
277
278
279
280
281
282
283
# File 'lib/prima_twig.rb', line 274

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