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
# 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 .`
    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



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

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



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

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



223
224
225
# File 'lib/prima_twig.rb', line 223

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

#clean_branch_name(branch_name) ⇒ Object



126
127
128
129
130
# File 'lib/prima_twig.rb', line 126

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



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

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

#create_clientsObject



82
83
84
85
86
87
# File 'lib/prima_twig.rb', line 82

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



250
251
252
# File 'lib/prima_twig.rb', line 250

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



268
269
270
# File 'lib/prima_twig.rb', line 268

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

#current_branch_nameObject



149
150
151
# File 'lib/prima_twig.rb', line 149

def current_branch_name
  @twig.current_branch_name
end

#current_branch_refObject



121
122
123
124
# File 'lib/prima_twig.rb', line 121

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

#delete_label_from_issue(issue_number, label) ⇒ Object



203
204
205
206
207
# File 'lib/prima_twig.rb', line 203

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



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

def deployable_branches
  %w(dev)
end

#disabled_branchesObject



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

def disabled_branches
  %w(master dev)
end

#generate_configObject



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

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



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

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

#get_prObject



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

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



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

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

#get_pr_urlObject



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

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

#get_property(name) ⇒ Object



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

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

#has_config?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/prima_twig.rb', line 39

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

#has_latest_config?Boolean

Returns:

  • (Boolean)


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

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)


193
194
195
# File 'lib/prima_twig.rb', line 193

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

#index_diffObject



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

def index_diff
  @rugged.index.diff
end

#is_a_deployable_branch?Boolean

Returns:

  • (Boolean)


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

def is_a_deployable_branch?
  deployable_branches.include? current_branch_name
end

#is_a_valid_branch?Boolean

Returns:

  • (Boolean)


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

def is_a_valid_branch?
  not disabled_branches.include? current_branch_name
end

#is_already_a_pr?Boolean

Returns:

  • (Boolean)


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

def is_already_a_pr?
  not get_pr_url.nil?
end

#is_clean?Boolean

Returns:

  • (Boolean)


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

def is_clean?
  index_diff.count === 0
end

#is_issue_in_review?(issue) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#issue_has_label?(issue, label) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
182
# File 'lib/prima_twig.rb', line 179

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



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

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



254
255
256
257
258
# File 'lib/prima_twig.rb', line 254

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



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

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



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

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)


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

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



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

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

#repo_urlObject



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

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

#repo_userObject



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

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

#update_configObject



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

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



197
198
199
200
201
# File 'lib/prima_twig.rb', line 197

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



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

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

#user_loginObject



209
210
211
212
# File 'lib/prima_twig.rb', line 209

def 
  user = @gh.user
  user.
end

#write_config(conf = {}) ⇒ Object



75
76
77
78
79
80
# File 'lib/prima_twig.rb', line 75

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



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

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