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