gitsflow 0.2.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of gitsflow might be problematic. Click here for more details.

@@ -0,0 +1,127 @@
1
+ class GitLab::Issue
2
+ attr_accessor :title, :labels, :assignee_id, :description, :branch, :iid, :obj_gitlab, :status
3
+ @labels = []
4
+
5
+ def initialize(params = {})
6
+ @title = params[:title]
7
+ @labels = params[:labels] || []
8
+ @description = params[:description]
9
+ @branch = params[:branch]
10
+ @assignee_id = GitLab::User.me["id"]
11
+ end
12
+
13
+ def set_default_branch branch
14
+ @description = "* ~default_branch #{branch}\n" + @description
15
+ end
16
+
17
+ def create
18
+ params = {}
19
+ params.merge!(title: @title)
20
+ params.merge!(description: @description.to_s)
21
+ params.merge!(labels: @labels.join(','))
22
+ params.merge!(assignee_id: @assignee_id)
23
+
24
+ # label = params.fetch(:label) || ''
25
+ # assignee_id = params.fetch(:assignee_id) || ''
26
+ print "\nCreate new GitLab issue \n\n".yellow
27
+ url = "projects/#{$GITLAB_PROJECT_ID}/issues"
28
+ issue_json = GitLab.request_post(url, params)
29
+ @iid = issue_json["iid"]
30
+ print "Issue created with success!\n".green
31
+ print "URL: #{issue_json["web_url"]}\n\n"
32
+ end
33
+
34
+ def close
35
+ params = {}
36
+ params.merge!(title: @title)
37
+ params.merge!(state_event: 'close')
38
+ params.merge!(description: @description.to_s)
39
+ params.merge!(labels: @labels.join(','))
40
+
41
+ url = "projects/#{$GITLAB_PROJECT_ID}/issues/#{@iid}"
42
+ GitLab.request_put(url, params)
43
+ print "Issue '#{@title}' closed with success!\n".green
44
+ end
45
+
46
+ def update
47
+ params = {}
48
+ params.merge!(title: @title)
49
+ params.merge!(description: @description.to_s)
50
+ params.merge!(labels: @labels.join(','))
51
+ params.merge!(assignee_id: @assignee_id)
52
+
53
+ # label = params.fetch(:label) || ''
54
+ # assignee_id = params.fetch(:assignee_id) || ''
55
+ print "\nUpdate GitLab issue\n\n".yellow
56
+ url = "projects/#{$GITLAB_PROJECT_ID}/issues/#{@iid}"
57
+ GitLab.request_put(url, params)
58
+ print "Issue updated with success!\n".green
59
+ end
60
+
61
+ def self.find_by(search)
62
+ url = "issues?search=#{search.values[0]}&in=#{search.keys[0]}&state=opened"
63
+ issue_json = GitLab.request_get(url)[0]
64
+ if issue_json
65
+ issue = GitLab::Issue.new
66
+ issue.set_data issue_json
67
+ else
68
+ raise "Issue not found #{search.keys[0]}"
69
+ end
70
+ end
71
+
72
+ def self.find_by_branch(branch)
73
+ url = "issues?search=#{branch}"
74
+ issue_json = GitLab.request_get(url)[0]
75
+ if issue_json
76
+ issue = GitLab::Issue.new
77
+ issue.set_data issue_json
78
+ else
79
+ raise "Issue not found #{branch}"
80
+ end
81
+ end
82
+
83
+ def self.all
84
+ url = "issues?state=opened"
85
+ GitLab.request_get(url)
86
+ end
87
+
88
+
89
+ def self.from_list(list_name)
90
+ url = "issues?labels=#{list_name}&state=opened"
91
+ issues = []
92
+ issues_gitlab = GitLab.request_get(url)
93
+ issues_gitlab.each do |obj|
94
+ issue = self.new
95
+ issue.set_data(obj)
96
+
97
+ issues << issue
98
+ end
99
+ issues
100
+ end
101
+
102
+ def merge_requests
103
+ url = "projects/#{$GITLAB_PROJECT_ID}/issues/#{iid}/closed_by"
104
+ GitLab.request_get(url)
105
+ end
106
+
107
+ def msg_changelog
108
+ # a.description.match(/(\* \~changelog .*\n)+/).to_a
109
+ description.match(/\* \~changelog .*\n?/).to_s.gsub('* ~changelog ', '') rescue nil
110
+ end
111
+
112
+ def set_data obj
113
+ @iid = obj["iid"]
114
+ @title = obj["title"]
115
+ @labels = obj["labels"]
116
+ @description = obj["description"]
117
+ @assignee_id = obj["assignees"][0]["id"]
118
+ @branch = obj["description"].match(/\* \~default_branch .*\n?/).to_s.gsub('* ~default_branch ', '').chomp.strip rescue nil
119
+ @obj_gitlab = obj
120
+ self
121
+ end
122
+
123
+ def create_link_issue target_issue_iid
124
+ url = "projects/#{$GITLAB_PROJECT_ID}/issues/#{@iid}/links?target_project_id=#{$GITLAB_PROJECT_ID}&target_issue_iid=#{target_issue_iid}"
125
+ GitLab.request_post(url, params)
126
+ end
127
+ end
@@ -0,0 +1,82 @@
1
+ class GitLab::MergeRequest
2
+ @options = {}
3
+ attr_accessor :source_branch, :target_branch, :title, :options, :assignee_id, :labels, :issue_iid, :obj_gitlab, :type, :description
4
+ @labels = []
5
+ def initialize(params = {})
6
+ @source_branch = params[:source_branch]
7
+ @target_branch = params[:target_branch]
8
+ @title = params[:title]
9
+ @labels = params[:labels]
10
+ @issue_iid = params[:issue_iid]
11
+ @type = params[:type]
12
+ @description = params[:description]
13
+ @options = params[:options]
14
+ end
15
+
16
+ def create
17
+ print "Create Merge Request: ".yellow
18
+ print "#{@source_branch} into #{@target_branch}\n\n".green
19
+ assignee_id = GitLab::User.me["id"]
20
+ if type != 'hotfix'
21
+ users = GitLab::User.all
22
+ print "Users list:\n\n".yellow
23
+ print "----------------------------\n".blue
24
+ print "#{"0".ljust(10)} - Empty\n".blue
25
+ users.each do |user|
26
+ print "#{user['id'].to_s.ljust(10)} - #{user['name']}\n".blue
27
+ end
28
+ print "----------------------------\n".blue
29
+ print "Choice user ID for assignee:\n".yellow
30
+ assignee_id = STDIN.gets.chomp
31
+ print "\n#{assignee_id}, "
32
+ print "ok!\n".green
33
+ end
34
+
35
+ url = "projects/#{$GITLAB_PROJECT_ID}/merge_requests"
36
+
37
+ labels = ['merge_request']
38
+ labels << type if type
39
+ @obj_gitlab = GitLab.request_post(url, {
40
+ source_branch: @source_branch,
41
+ target_branch: @target_branch,
42
+ title: @title,
43
+ labels: labels.join(','),
44
+ description: @description,
45
+ assignee_id: assignee_id.to_i
46
+ })
47
+
48
+ print "Merge request created with success!\n\n".green
49
+ end
50
+
51
+
52
+ def create_code_review
53
+ print "Create merge request for code review: ".yellow
54
+ print "#{@source_branch} into #{@target_branch}\n\n".green
55
+ users = GitLab::User.all
56
+ print "Users list:\n\n".yellow
57
+ print "----------------------------\n".blue
58
+ print "#{"0".ljust(10)} - Empty\n".blue
59
+ users.each do |user|
60
+ print "#{user['id'].to_s.ljust(10)} - #{user['name']}\n".blue
61
+ end
62
+ print "----------------------------\n".blue
63
+ print "Choice user ID for assignee code review:\n".yellow
64
+ assignee_id = STDIN.gets.chomp
65
+ print "\n#{assignee_id}, "
66
+ print "ok!\n".green
67
+
68
+ url = "projects/#{$GITLAB_PROJECT_ID}/merge_requests"
69
+ title = "WIP: ##{@issue_iid} - Code review #{@source_branch}"
70
+ @obj_gitlab = GitLab.request_post(url, {
71
+ source_branch: @source_branch,
72
+ target_branch: @target_branch,
73
+ title: title,
74
+ labels: @labels,
75
+ assignee_id: assignee_id.to_i
76
+ })
77
+
78
+ print "Merge request for Code Review created with success!\n\n".green
79
+ end
80
+
81
+
82
+ end
@@ -0,0 +1,20 @@
1
+ class GitLab::User
2
+ attr_accessor :id, :email, :name
3
+
4
+ def initialize(params = {})
5
+ @name = params[:name]
6
+ end
7
+
8
+ def self.me
9
+ GitLab.request_get("projects/#{$GITLAB_PROJECT_ID}/users?search=#{$GITLAB_EMAIL}")[0]
10
+ end
11
+
12
+ def self.all
13
+ GitLab.request_get("projects/#{$GITLAB_PROJECT_ID}/users")
14
+ end
15
+
16
+ def to_s
17
+ end
18
+
19
+
20
+ end
@@ -0,0 +1,11 @@
1
+ require 'dotenv'
2
+ Dotenv.load(File.join( Dir.pwd, ".env"))
3
+ $GITLAB_PROJECT_ID = ENV['GITLAB_PROJECT_ID']
4
+ $GITLAB_TOKEN = ENV['GITLAB_TOKEN']
5
+ $GITLAB_URL_API = ENV['GITLAB_URL_API']
6
+ $GITLAB_EMAIL = ENV['GITLAB_EMAIL'] == "" ? Open3.popen3("git config --global user.email") { |i, o| o.read }.chomp : ENV['GITLAB_EMAIL']
7
+ $GITLAB_LISTS = ENV['GITLAB_LISTS'].split(',') rescue nil
8
+ $GITLAB_NEXT_RELEASE_LIST = ENV['GITLAB_NEXT_RELEASE_LIST']
9
+ $GIT_BRANCH_MASTER= ENV["GIT_BRANCH_MASTER"]
10
+ $GIT_BRANCH_DEVELOP= ENV["GIT_BRANCH_DEVELOP"]
11
+ $GIT_BRANCHES_STAGING= ENV["GIT_BRANCHES_STAGING"].split(',') rescue nil
@@ -0,0 +1,567 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #!/usr/bin/ruby
5
+ require 'pry'
6
+
7
+ require 'net/http'
8
+ require "open3"
9
+ require 'uri'
10
+ load 'config.rb'
11
+ load 'string.rb'
12
+ load 'GitLab/gitlab.rb'
13
+ load 'Git/git.rb'
14
+
15
+ # require './lib/gitlab/issue.rb'
16
+ # require './lib/gitlab/merge_request.rb'
17
+ class SFlow
18
+ VERSION = "0.2.0"
19
+ $TYPE = ARGV[0]
20
+ $ACTION = ARGV[1]
21
+ $PARAM1 = ARGV[2]
22
+ $PARAM2 = ARGV[3..-1]&.join(' ')
23
+
24
+ def self.call
25
+ begin
26
+ print "Loading...\n".yellow
27
+ validates if !['config_', 'help_'].include? ("#{$TYPE}_#{$ACTION}")
28
+ #
29
+ send("#{$TYPE}_#{$ACTION}")
30
+ rescue => e
31
+ set_error e
32
+
33
+ end
34
+ end
35
+ def self.feature_start
36
+ title = $PARAM2 == "" ? $PARAM1 : $PARAM2
37
+ issue = GitLab::Issue.new(title: title, labels: ['feature'])
38
+ issue.create
39
+ branch = "#{issue.iid}-feature/#{$PARAM1}"
40
+ self.start(branch, issue)
41
+ end
42
+
43
+ def self.bugfix_start
44
+ title = $PARAM2 == "" ? $PARAM1 : $PARAM2
45
+ issue = GitLab::Issue.new(title: title, labels: ['bugfix'])
46
+ issue.create
47
+ branch = "#{issue.iid}-bugfix/#{$PARAM1}"
48
+ self.start(branch, issue)
49
+ end
50
+
51
+ def self.hotfix_start
52
+ title = $PARAM2 == "" ? $PARAM1 : $PARAM2
53
+ issue = GitLab::Issue.new(title: title, labels: ['hotfix', 'production'])
54
+ issue.create
55
+ branch = "#{issue.iid}-hotfix/#{$PARAM1}"
56
+ self.start(branch, issue, "master")
57
+ end
58
+
59
+ def self.feature_finish
60
+ self.feature_reintegration
61
+ end
62
+
63
+ def self.feature_reintegration
64
+ if (!$PARAM1.match(/\-feature\//))
65
+ raise "This branch is not a feature"
66
+ end
67
+ self.reintegration 'feature'
68
+ end
69
+
70
+ def self.bugfix_reintegration
71
+ if (!$PARAM1.match(/\-bugfix\//))
72
+ raise "This branch is not a bugfix"
73
+ end
74
+ self.reintegration 'bugfix'
75
+ end
76
+
77
+ def self.bugfix_finish
78
+ self.bugfix_reintegration
79
+ end
80
+
81
+ def self.hotfix_reintegration
82
+ if (!$PARAM1.match(/\-hotfix\//))
83
+ raise "This branch is not a hotfix"
84
+ end
85
+ self.reintegration 'hotfix'
86
+ end
87
+
88
+ def self.hotfix_finish
89
+ self.hotfix_reintegration
90
+ end
91
+
92
+ def self.feature_code_review
93
+ if (!$PARAM1.match(/\-feature\//))
94
+ raise "This branch is not a feature"
95
+ end
96
+ self.code_review()
97
+ end
98
+
99
+ def self.bugfix_code_review
100
+ if (!$PARAM1.match(/\-bugfix\//))
101
+ raise "This branch is not a bugfix"
102
+ end
103
+ self.code_review()
104
+ end
105
+
106
+ def self.feature_staging
107
+ branch = $PARAM1
108
+ issue = GitLab::Issue.find_by_branch(branch)
109
+
110
+ print "Staging branches list:\n\n".yellow
111
+ print "----------------------------\n".blue
112
+ $GIT_BRANCHES_STAGING.each_with_index do |staging, index|
113
+ print "#{index} - #{staging}\n".blue
114
+ end
115
+ print "----------------------------\n".blue
116
+ print "Choice number of target branch:\n".yellow
117
+ target_branch_id = STDIN.gets.chomp
118
+ print "\n#{target_branch_id}, "
119
+ target_branch = $GIT_BRANCHES_STAGING[target_branch_id.to_i]
120
+ if !$GIT_BRANCHES_STAGING.include?(target_branch)
121
+ raise "option invalid!"
122
+ end
123
+ print "ok!\n".green
124
+
125
+
126
+ print "\nAttention: \n".yellow.bg_cyan
127
+ print "Do you want clean first the target branch or only merge?\n\n".yellow
128
+ print "----------------------------\n".blue
129
+ print "0 - Clean it first, then do merge #{branch} into #{target_branch}\n".blue
130
+ print "1 - Only Merge: Merge #{branch} into #{target_branch}\n".blue
131
+ print "----------------------------\n".blue
132
+ print "Choice number of target branch:\n".yellow
133
+ option_merge = STDIN.gets.chomp
134
+ print "\n#{option_merge}, "
135
+ print "ok!\n".green
136
+
137
+ if option_merge == "0"
138
+ Git.reset_hard branch, target_branch
139
+ elsif option_merge == "1"
140
+ Git.merge branch, target_branch
141
+ else
142
+ raise "Wrong choice"
143
+ end
144
+ new_labels = [target_branch]
145
+ remove_labels = $GITLAB_LISTS
146
+ old_labels = issue.obj_gitlab["labels"]
147
+ old_labels.delete_if{|label| remove_labels.include? label}
148
+ issue.labels = (old_labels + new_labels).uniq
149
+ issue.update
150
+
151
+ self.code_review
152
+ end
153
+
154
+ def self.release_start
155
+ version = $PARAM1
156
+ if !version
157
+ raise "param 'VERSION' not found"
158
+ end
159
+ issues = GitLab::Issue.from_list($GITLAB_NEXT_RELEASE_LIST).select{|i| !i.labels.include? 'ready_to_deploy'}
160
+ issues_total = issues.size
161
+
162
+ if issues_total == 0
163
+ raise "Not exist issues ready for start release version"
164
+ end
165
+
166
+ issues_urgent = issues.select{|i| i.labels.include? 'urgent'}
167
+ issues_urgent_total = issues_urgent.size
168
+ issue_title = "Release version #{version}\n"
169
+
170
+ issue_release = GitLab::Issue.find_by(title: issue_title) rescue nil
171
+
172
+ if issue_release
173
+ print "This issue already exists, do you want to continue using it? (y/n):".yellow.bg_red
174
+
175
+ print"\n If you choose 'n', a new issue will be created!\n"
176
+ print "\n"
177
+ option = STDIN.gets.chomp
178
+ else
179
+ option = 'n'
180
+ end
181
+
182
+ if option == 'n'
183
+ issue_release = GitLab::Issue.new(title: issue_title)
184
+ issue_release.create
185
+ end
186
+
187
+ new_labels = []
188
+ changelogs = []
189
+
190
+ release_branch = "#{issue_release.iid}-release/#{version}"
191
+ print "Creating release version #{version}\n"
192
+
193
+
194
+ begin
195
+
196
+ Git.delete_branch(release_branch)
197
+ Git.fetch 'develop'
198
+ Git.checkout 'develop'
199
+ Git.new_branch release_branch
200
+
201
+ print "Issue(s) title(s): \n".yellow
202
+ issues.each do |issue|
203
+ print " -> #{issue.title}\n"
204
+ end
205
+ print "\n"
206
+
207
+ # if issues_urgent_total > 0
208
+ print "Attention!".yellow.bg_red
209
+ print "\n\nChoose an option for merge:\n".yellow
210
+ print "----------------------------\n".blue
211
+ print "#{"0".ljust(10)} - Only #{issues_urgent_total} hotfix/urgent issues\n".blue if issues_urgent_total > 0
212
+ print "#{"1".ljust(10)} - All #{issues.size} issues\n".blue
213
+ print "----------------------------\n".blue
214
+ print "Choice a number:\n".yellow
215
+ option = STDIN.gets.chomp
216
+ # else
217
+ # option = "1"
218
+ # end
219
+
220
+ case option
221
+ when "0"
222
+ print "Issue(s) title(s): \n"
223
+ issues_urgent.each do |issue|
224
+ print " -> #{issue.title}\n"
225
+ end
226
+
227
+ issues_urgent.each do |issue|
228
+
229
+ Git.merge(issue.branch, release_branch)
230
+ changelogs << "* ~changelog #{issue.msg_changelog} \n"
231
+ new_labels << 'hotfix'
232
+ # mr_master = GitLab::MergeRequest.new(
233
+ # source_branch: issue.branch,
234
+ # target_branch: 'master',
235
+ # issue_iid: issue.iid,
236
+ # title: "##{issue.iid} - #{version} - Reintegration #{issue.branch} into master",
237
+ # type: 'hotfix'
238
+ # )
239
+ # mr_master.create
240
+ # mr_develop = GitLab::MergeRequest.new(
241
+ # source_branch: issue.branch,
242
+ # target_branch: 'develop',
243
+ # issue_iid: issue.iid,
244
+ # title: "##{issue.iid} - #{version} - Reintegration #{issue.branch} into develop",
245
+ # type: 'hotfix'
246
+ # )
247
+ # mr_develop.create
248
+
249
+ # new_labels << 'merge_request'
250
+ # remove_labels = $GIT_BRANCHES_STAGING + $GITLAB_LISTS
251
+ # new_labels << $GITLAB_NEXT_RELEASE_LIST
252
+ # old_labels = issue.obj_gitlab["labels"]
253
+ # old_labels.delete_if{|label| remove_labels.include? label}
254
+ # issue.labels = (old_labels + new_labels).uniq
255
+ # issue.update
256
+ end
257
+ issues = issues_urgent
258
+ when "1"
259
+ type = 'other'
260
+ print "Next release has total (#{issues_total}) issues.\n\n".yellow
261
+ print "Issue(s) title(s): \n".yellow
262
+ issues.each do |issue|
263
+ print " -> #{issue.title}\n"
264
+ end
265
+ issues.each do |issue|
266
+ Git.merge(issue.branch, release_branch)
267
+ changelogs << "* ~changelog #{issue.msg_changelog} \n"
268
+
269
+ # mr_develop = GitLab::MergeRequest.new(
270
+ # source_branch: issue.branch,
271
+ # target_branch: 'develop',
272
+ # issue_iid: issue.iid,
273
+ # title: "##{issue.iid} - #{version} - Reintegration #{issue.branch} into develop"
274
+ # )
275
+ # mr_develop.create
276
+
277
+ # new_labels << 'merge_request'
278
+ # remove_labels = $GIT_BRANCHES_STAGING + $GITLAB_LISTS
279
+ # new_labels << $GITLAB_NEXT_RELEASE_LIST
280
+ # old_labels = issue.obj_gitlab["labels"]
281
+ # old_labels.delete_if{|label| remove_labels.include? label}
282
+ # issue.labels = (old_labels + new_labels).uniq
283
+ # issue.update
284
+ end
285
+ else
286
+ raise "option invalid!"
287
+ end
288
+ print "Changelog messages:\n\n".yellow
289
+ print "Release version #{version}\n".blue
290
+ msgs_changelog = []
291
+ changelogs.each do |clog|
292
+ msg_changelog = "#{clog.strip.chomp.gsub('* ~changelog ', ' - ')}\n"
293
+ msgs_changelog << msg_changelog
294
+ print msg_changelog.light_blue
295
+ end
296
+ print "\nSetting changelog message in CHANGELOG.md\n".yellow
297
+ sleep 2
298
+ # option = STDIN.gets.chomp
299
+
300
+ # if option != "ok"
301
+ # raise "abort!"
302
+ # end
303
+
304
+ file_changelog = IO.read 'CHANGELOG.md'
305
+ IO.write 'CHANGELOG.md', msgs_changelog.join('') + file_changelog
306
+
307
+ system('git add CHANGELOG.md')
308
+ system("git commit -m 'update CHANGELOG version #{version}'")
309
+ Git.push issue_release.branch
310
+
311
+ issue_release.description = "#{changelogs.join("")}\n * #{issues.map{|i| "##{i.iid},"}.join(' ')}"
312
+
313
+ issue_release.labels = ['ready_to_deploy', 'Next Release']
314
+ issue_release.set_default_branch(release_branch)
315
+ issue_release.update
316
+ Git.push release_branch
317
+ issues.each do |issue|
318
+ issue.labels = (issue.labels + new_labels).uniq
319
+ issue.close
320
+ end
321
+ print "\nYou are on branch: #{release_branch}\n".yellow
322
+ print "\nRelease #{version} created with success!\n\n".yellow
323
+
324
+
325
+ rescue => exception
326
+ Git.delete_branch(release_branch)
327
+
328
+ raise exception.message
329
+ end
330
+
331
+
332
+
333
+ end
334
+
335
+ def self.release_finish
336
+ version = $PARAM1
337
+ if !version
338
+ raise "param 'VERSION' not found"
339
+ end
340
+ new_labels = []
341
+
342
+ release_branch = "-release/#{version}"
343
+ issue_release = GitLab::Issue.find_by_branch(release_branch)
344
+
345
+ type = issue_release.labels.include?('hotfix') ? 'hotfix' : nil
346
+ mr_master = GitLab::MergeRequest.new(
347
+ source_branch: issue_release.branch,
348
+ target_branch: 'master',
349
+ issue_iid: issue_release.iid,
350
+ title: "Closes ##{issue_release.iid} - #{version} - Reintegration #{issue_release.branch} into master",
351
+ description: "Closes ##{issue_release.iid}",
352
+ type: type
353
+ )
354
+ mr_master.create
355
+
356
+ # end
357
+ # mr_develop = GitLab::MergeRequest.new(
358
+ # source_branch: issue_release.branch,
359
+ # target_branch: 'develop',
360
+ # issue_iid: issue_release.iid,
361
+ # title: "##{issue_release.iid} - #{version} - Reintegration #{issue_release.branch} into develop",
362
+ # type: 'hotfix'
363
+ # )
364
+ # mr_develop.create
365
+
366
+ Git.merge issue_release.branch, 'develop'
367
+
368
+ # remove_labels = [$GITLAB_NEXT_RELEASE_LIST]
369
+ remove_labels = []
370
+ old_labels = issue_release.obj_gitlab["labels"] + ['merge_request']
371
+ old_labels.delete_if{|label| remove_labels.include? label}
372
+ issue_release.labels = (old_labels + new_labels).uniq
373
+ issue_release.update
374
+
375
+ end
376
+
377
+
378
+ def self.uninstall_
379
+ puts "\n\Uninstall git alias\n\n".yellow
380
+ print " \u{1F611} git sflow alias"
381
+ print " (removing...) \r".yellow
382
+ sleep 2
383
+ system('git config --local --unset alias.sflow')
384
+ print " \u{1F601}\ git sflow alias"
385
+ print " (removed) \u{2714} ".green
386
+ print "\n\n"
387
+ print "Bye Bye"
388
+ print "\n\n"
389
+
390
+ end
391
+
392
+ def self.install_
393
+ puts "\n\nInstalling git alias\n\n".yellow
394
+ print " \u{1F611} git sflow alias"
395
+ print " (instaling...) \r".yellow
396
+ sleep 2
397
+ system("git config --local alias.sflow '!sh -c \" sflow $1 $2 $3 $4\" - '")
398
+ print " \u{1F601}\ git sflow alias"
399
+ print " (instaled) \u{2714} ".green
400
+ print "\n\n"
401
+ print "git sflow help\n\n"
402
+ print "git sflow config\n\n"
403
+ print "Sucesso!\n\n".green
404
+ # self.help_
405
+ # self.config_
406
+
407
+ end
408
+
409
+ private
410
+
411
+ def self.config_
412
+ print "\n\---------- Configuration ---------- \n".light_blue
413
+ print "\nsflow config \nor\ngit sflow config \n\n".light_blue
414
+
415
+ print "\In your project create or update file .env with variables below:\n\n"
416
+ print "GITLAB_PROJECT_ID=\n".pink
417
+ print "GITLAB_TOKEN=\n".pink
418
+ print "GITLAB_URL_API=\n".pink
419
+ print "GITLAB_EMAIL=\n".pink
420
+ print "GITLAB_LISTS=Do\n".pink
421
+ print "GIT_BRANCH_MASTER=master\n".pink
422
+ print "GIT_BRANCH_DEVELOP=develop\n".pink
423
+ print "GIT_BRANCHES_STAGING=staging_1,staging_2\n".pink
424
+ end
425
+
426
+ def self.set_error(e)
427
+ print "\n\n"
428
+ print "Error!".yellow.bg_red
429
+ print "\n"
430
+ print "#{e.message}".yellow.bg_red
431
+ print "\n\n"
432
+ end
433
+
434
+ def self.validates
435
+ print "Running validations... \n\n".yellow
436
+
437
+ if !$GITLAB_PROJECT_ID || !$GITLAB_TOKEN || !$GITLAB_URL_API ||
438
+ !$GIT_BRANCH_MASTER || !$GIT_BRANCH_DEVELOP || !$GITLAB_LISTS || !$GITLAB_NEXT_RELEASE_LIST
439
+ print "Variables not configured\n".yellow
440
+ raise "Run `sflow config` for help"
441
+ end
442
+
443
+ if !$TYPE && !$ACTION
444
+ print "Command invalid!\n".yellow
445
+ raise "Run `sflow help` for help"
446
+ end
447
+ branchs_validations = $GIT_BRANCHES_STAGING + [$GIT_BRANCH_MASTER, $GIT_BRANCH_DEVELOP]
448
+ Git.exist_branch?(branchs_validations.join(' ')) rescue raise "You need to create branches #{branchs_validations.join(', ')}"
449
+
450
+ end
451
+
452
+ def self.help_
453
+ print "\n\n---------- Help ---------- \n".light_blue
454
+ print "\nsflow help\nor\ngit sflow help\n\n".light_blue
455
+ print "1 - git sflow feature start FEATURE DESCRIPTION \n".yellow
456
+ print "2 - git sflow feature [reintegration|finish] FEATURE_BRANCH\n".yellow
457
+ print "3 - git sflow feature codereview BRANCH\n".yellow
458
+ print "4 - git sflow feature staging SOURCE_BRANCH\n".yellow
459
+ print "5 - git sflow bugfix start BUGFIX DESCRIPTION\n".yellow
460
+ print "6 - git sflow bugfix [reintegration|finish] BUGFIX_BRANCH\n".yellow
461
+ print "7 - git sflow bugfix codereview BUGFIX_BRANCH\n".yellow
462
+ print "8 - git sflow hotfix start HOTFIX DESCRIPTION\n".yellow
463
+ print "9 - git sflow hotfix [reintegration|finish] HOTFIX_BRANCH\n".yellow
464
+ print "10 - git sflow release start RELEASE\n".yellow
465
+ print "11 - git sflow push BRANCH\n".yellow
466
+
467
+ choice = -1
468
+ question = "Choice a number for show a example or 0 for exit:\n\n".light_blue
469
+ print question
470
+ choice = STDIN.gets.chomp
471
+ print ""
472
+ case choice
473
+ when '1'
474
+ print "-> git sflow feature start Ticket#9999 'Ticket#9999 - Create new...'\n\n"
475
+ when '2'
476
+ print "-> git sflow feature reintegration 11-feature/Ticket#9999\n\n"
477
+ when '3'
478
+ print "-> git sflow feature codereview 11-feature/Ticket#9999\n\n"
479
+ when '4'
480
+ print "-> git sflow feature staging 11-feature/Ticket#9999\n\n"
481
+ when '5'
482
+ print "-> git sflow bugfix start Ticket#9999 'Ticket#9999 Bug ...'\n\n"
483
+ when '6'
484
+ print "-> git sflow bugfix finish 12-bugfix/Ticket#9999'\n\n"
485
+ when '7'
486
+ print "-> git sflow bugfix codereview 12-bugfix/Ticket#9999\n"
487
+ when '8'
488
+ print "-> git sflow hotfix start Ticket#9999 'Ticket#9999 Bug at production in...'\n\n"
489
+ when '9'
490
+ print "-> git sflow hotfix reintegration Ticket#9999'\n\n"
491
+ when '0'
492
+ else
493
+ end
494
+ print "See you soon!".green
495
+ print "\n\n"
496
+
497
+
498
+ end
499
+
500
+ def self.reintegration type = "feature"
501
+ # Git.fetch ref_branch
502
+ # Git.checkout ref_branch
503
+ # Git.pull ref_branch
504
+ source_branch = $PARAM1
505
+ issue = GitLab::Issue.find_by_branch(source_branch)
506
+ print "Title: #{issue.title}\n\n"
507
+ print "Changelog message:\n--> ".yellow
508
+ message_changelog = STDIN.gets.chomp
509
+ print "\n ok!\n\n".green
510
+ new_labels = []
511
+ if (type == 'hotfix')
512
+ !source_branch.match('hotfix') rescue raise "invalid branch!"
513
+ new_labels << 'hotfix'
514
+ new_labels << 'urgent'
515
+ else
516
+ (!source_branch.match('feature') && !source_branch.match('bugfix')) rescue raise "invalid branch!"
517
+ end
518
+ remove_labels = $GIT_BRANCHES_STAGING + $GITLAB_LISTS
519
+ new_labels << 'changelog'
520
+ new_labels << $GITLAB_NEXT_RELEASE_LIST
521
+ old_labels = issue.obj_gitlab["labels"]
522
+ old_labels.delete_if{|label| remove_labels.include? label}
523
+ issue.labels = (old_labels + new_labels).uniq
524
+ issue.description.gsub!(/\* \~changelog .*\n?/,'')
525
+ issue.description = "#{issue.description} \n* ~changelog #{message_changelog}"
526
+ print "Setting changelog: ".yellow
527
+ print "#{message_changelog}\n".green
528
+ print "Moving issue to list: ".yellow
529
+ print "#{$GITLAB_NEXT_RELEASE_LIST}\n".green
530
+ issue.update
531
+
532
+ end
533
+
534
+ def self.start branch, issue, ref_branch = "develop"
535
+ Git.fetch ref_branch
536
+ Git.checkout ref_branch
537
+ Git.pull ref_branch
538
+
539
+ description = "* ~default_branch #{branch}"
540
+
541
+ issue.description = description
542
+ issue.update
543
+
544
+ Git.new_branch branch
545
+ Git.push branch
546
+
547
+ print "\nYou are on branch: #{branch}\n\n".yellow
548
+ end
549
+
550
+ def self.code_review
551
+ Git.fetch "develop"
552
+ Git.checkout "develop"
553
+ Git.pull "develop"
554
+ source_branch = $PARAM1
555
+ issue = GitLab::Issue.find_by_branch(source_branch)
556
+ # issue.move
557
+ mr = GitLab::MergeRequest.new(
558
+ source_branch: source_branch,
559
+ target_branch: 'develop',
560
+ issue_iid: issue.iid
561
+ )
562
+ mr.create_code_review
563
+ issue.labels = (issue.obj_gitlab["labels"] + ['code_review']).uniq
564
+ issue.update
565
+ end
566
+ end
567
+