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', 'aws_key_id', 'aws_key_secret']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePrima

Returns a new instance of Prima.



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

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



133
134
135
# File 'lib/prima_twig.rb', line 133

def method_missing(method)
  @rugged.send method
end

Instance Attribute Details

#awsObject (readonly)

Returns the value of attribute aws.



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

def aws
  @aws
end

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#ghObject (readonly)

Returns the value of attribute gh.



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

def gh
  @gh
end

#ruggedObject (readonly)

Returns the value of attribute rugged.



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

def rugged
  @rugged
end

#twigObject (readonly)

Returns the value of attribute twig.



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

def twig
  @twig
end

Instance Method Details

#assign_issue_to_me(issue) ⇒ Object



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

def assign_issue_to_me(issue)
  @gh.update_issue(repo_name, issue, :assignee => )
end

#assign_pull_request_to_me(pr_number) ⇒ Object



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

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



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

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

#create_clientsObject



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

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

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



233
234
235
# File 'lib/prima_twig.rb', line 233

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



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

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

#current_branch_nameObject



137
138
139
# File 'lib/prima_twig.rb', line 137

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



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

def delete_label_from_issue(issue_number, label)
  if issue_has_label?(issue_number, label)
    @gh.remove_label repo_name, 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



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

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



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

def get_issue(issue_number)
  @gh.issue repo_name, issue_number
end

#get_prObject



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

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



229
230
231
# File 'lib/prima_twig.rb', line 229

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

#get_pr_urlObject



141
142
143
144
145
146
147
148
# File 'lib/prima_twig.rb', line 141

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

#get_property(name) ⇒ Object



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

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

#has_config?Boolean

Returns:

  • (Boolean)


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

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

#has_latest_config?Boolean

Returns:

  • (Boolean)


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

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)


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

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)


159
160
161
# File 'lib/prima_twig.rb', line 159

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)


163
164
165
# File 'lib/prima_twig.rb', line 163

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

#issue_has_label?(issue, label) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#list_issuesObject



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

def list_issues
  results = @gh.list_issues repo_name, 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(repo_name, state: :open, :per_page => 20, :page => i)
    end
  end
  results
end

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



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

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



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

def put_issue_in_review(issue)
  update_issue_with_label(issue, Prima::LABEL_REVIEW)
  gh.remove_label repo_name, issue, Prima::LABEL_WIP
end

#reduce_size(str, len) ⇒ Object



266
267
268
269
270
271
272
273
# File 'lib/prima_twig.rb', line 266

def reduce_size(str, len)
  out_str = str
  if str.size > len
    out_str.slice!(0, len)
    out_str << '...'
  end
  out_str
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



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

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



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

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



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

def update_issue_with_label(issue, label)
  unless issue_has_label?(issue, label)
    gh.add_labels_to_an_issue repo_name, issue, [ label ]
  end
end

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



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

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

#user_loginObject



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

def 
  user = @gh.user
  user.
end

#write_config(conf = {}) ⇒ Object



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

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



255
256
257
258
259
260
261
262
263
264
# File 'lib/prima_twig.rb', line 255

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