gitsflow 0.2.1

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 = "projects/#{$GITLAB_PROJECT_ID}/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 = "projects/#{$GITLAB_PROJECT_ID}/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 = "projects/#{$GITLAB_PROJECT_ID}/issues?state=opened"
85
+ GitLab.request_get(url)
86
+ end
87
+
88
+
89
+ def self.from_list(list_name)
90
+ url = "projects/#{$GITLAB_PROJECT_ID}/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,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,546 @@
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.1"
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
+ Git.push release_branch
279
+ issues.each do |issue|
280
+ issue.labels = (issue.labels + new_labels).uniq
281
+ issue.close
282
+ end
283
+ print "\nYou are on branch: #{release_branch}\n".yellow
284
+ print "\nRelease #{version} created with success!\n\n".yellow
285
+
286
+
287
+ rescue => exception
288
+ Git.delete_branch(release_branch)
289
+
290
+ raise exception.message
291
+ end
292
+
293
+ end
294
+
295
+ def self.release_finish
296
+ version = $PARAM1
297
+ if !version
298
+ raise "param 'VERSION' not found"
299
+ end
300
+ new_labels = []
301
+
302
+ release_branch = "-release/#{version}"
303
+ issue_release = GitLab::Issue.find_by_branch(release_branch)
304
+
305
+ type = issue_release.labels.include?('hotfix') ? 'hotfix' : nil
306
+ mr_master = GitLab::MergeRequest.new(
307
+ source_branch: issue_release.branch,
308
+ target_branch: 'master',
309
+ issue_iid: issue_release.iid,
310
+ title: "Closes ##{issue_release.iid} - #{version} - Reintegration #{issue_release.branch} into master",
311
+ description: "Closes ##{issue_release.iid}",
312
+ type: type
313
+ )
314
+ mr_master.create
315
+
316
+ # end
317
+ # mr_develop = GitLab::MergeRequest.new(
318
+ # source_branch: issue_release.branch,
319
+ # target_branch: 'develop',
320
+ # issue_iid: issue_release.iid,
321
+ # title: "##{issue_release.iid} - #{version} - Reintegration #{issue_release.branch} into develop",
322
+ # type: 'hotfix'
323
+ # )
324
+ # mr_develop.create
325
+
326
+ Git.merge issue_release.branch, 'develop'
327
+
328
+ # remove_labels = [$GITLAB_NEXT_RELEASE_LIST]
329
+ remove_labels = []
330
+ old_labels = issue_release.obj_gitlab["labels"] + ['merge_request']
331
+ old_labels.delete_if{|label| remove_labels.include? label}
332
+ issue_release.labels = (old_labels + new_labels).uniq
333
+ issue_release.update
334
+
335
+ end
336
+
337
+
338
+ def self.uninstall_
339
+ puts "\n\Uninstall git alias\n\n".yellow
340
+ print " \u{1F611} git sflow alias"
341
+ print " (removing...) \r".yellow
342
+ sleep 2
343
+ system('git config --local --unset alias.sflow')
344
+ print " \u{1F601}\ git sflow alias"
345
+ print " (removed) \u{2714} ".green
346
+ print "\n\n"
347
+ print "Bye Bye"
348
+ print "\n\n"
349
+
350
+ end
351
+
352
+ def self.install_
353
+ puts "\n\nInstalling git alias\n\n".yellow
354
+ print " \u{1F611} git sflow alias"
355
+ print " (instaling...) \r".yellow
356
+ GitLab.create_labels
357
+ sleep 2
358
+ system("git config --local alias.sflow '!sh -c \" sflow $1 $2 $3 $4\" - '")
359
+ print " \u{1F601}\ git sflow alias"
360
+ print " (instaled) \u{2714} ".green
361
+ print "\n\n"
362
+ print "git sflow help\n\n"
363
+ print "git sflow config\n\n"
364
+ print "Sucesso!\n\n".green
365
+ # self.help_
366
+ # self.config_
367
+
368
+ end
369
+
370
+
371
+ def push_
372
+
373
+ end
374
+
375
+ private
376
+
377
+ def self.config_
378
+ print "\n\---------- Configuration ---------- \n".light_blue
379
+ print "\nsflow config \nor\ngit sflow config \n\n".light_blue
380
+
381
+ print "\In your project create or update file .env with variables below:\n\n"
382
+ print "GITLAB_PROJECT_ID=\n".pink
383
+ print "GITLAB_TOKEN=\n".pink
384
+ print "GITLAB_URL_API=\n".pink
385
+ print "GITLAB_EMAIL=\n".pink
386
+ print "GITLAB_LISTS=To Do,Doing,Next Release,Staging\n".pink
387
+ print "GITLAB_NEXT_RELEASE_LIST=Next Release\n".pink
388
+ print "GIT_BRANCH_MASTER=master\n".pink
389
+ print "GIT_BRANCH_DEVELOP=develop\n".pink
390
+ print "GIT_BRANCHES_STAGING=staging_1,staging_2\n".pink
391
+
392
+ end
393
+
394
+ def self.set_error(e)
395
+ print "\n\n"
396
+ print "Error!".yellow.bg_red
397
+ print "\n"
398
+ print "#{e.message}".yellow.bg_red
399
+ print "\n\n"
400
+ print "#{e.backtrace}".yellow.bg_red
401
+ print "\n\n"
402
+ end
403
+
404
+ def self.validates
405
+ print "Running validations... \n\n".yellow
406
+ if !$GITLAB_PROJECT_ID || !$GITLAB_TOKEN || !$GITLAB_URL_API ||
407
+ !$GIT_BRANCH_MASTER || !$GIT_BRANCH_DEVELOP || !$GITLAB_LISTS || !$GITLAB_NEXT_RELEASE_LIST
408
+ print "Variables not configured\n".yellow
409
+ raise "Run `sflow config` for help"
410
+ end
411
+
412
+ if !$TYPE && !$ACTION
413
+ print "Command invalid!\n".yellow
414
+ raise "Run `sflow help` for help"
415
+ end
416
+ branchs_validations = $GIT_BRANCHES_STAGING + [$GIT_BRANCH_MASTER, $GIT_BRANCH_DEVELOP]
417
+ Git.exist_branch?(branchs_validations.join(' ')) rescue raise "You need to create branches #{branchs_validations.join(', ')}"
418
+
419
+ end
420
+
421
+ def self.help_
422
+ print "\n\n---------- Help ---------- \n".light_blue
423
+ print "\nsflow help\nor\ngit sflow help\n\n".light_blue
424
+ print "1 - git sflow feature start FEATURE DESCRIPTION \n".yellow
425
+ print "2 - git sflow feature [reintegration|finish] FEATURE_BRANCH\n".yellow
426
+ print "3 - git sflow feature codereview BRANCH\n".yellow
427
+ print "4 - git sflow feature staging SOURCE_BRANCH\n".yellow
428
+ print "5 - git sflow bugfix start BUGFIX DESCRIPTION\n".yellow
429
+ print "6 - git sflow bugfix [reintegration|finish] BUGFIX_BRANCH\n".yellow
430
+ print "7 - git sflow bugfix codereview BUGFIX_BRANCH\n".yellow
431
+ print "8 - git sflow bugfix staging BUGFIX_BRANCH\n".yellow
432
+ print "9 - git sflow hotfix start HOTFIX DESCRIPTION\n".yellow
433
+ print "10 - git sflow hotfix [reintegration|finish] HOTFIX_BRANCH\n".yellow
434
+ print "11 - git sflow release start RELEASE\n".yellow
435
+ print "12 - git sflow release finish RELEASE\n".yellow
436
+ print "13 - git sflow push BRANCH\n".yellow
437
+
438
+ choice = -1
439
+ question = "Choice a number for show a example or 0 for exit:\n\n".light_blue
440
+ print question
441
+ choice = STDIN.gets.chomp
442
+ print ""
443
+ case choice
444
+ when '1'
445
+ print "-> git sflow feature start Ticket#9999 'Ticket#9999 - Create new...'\n\n"
446
+ when '2'
447
+ print "-> git sflow feature reintegration 11-feature/Ticket#9999\n\n"
448
+ when '3'
449
+ print "-> git sflow feature codereview 11-feature/Ticket#9999\n\n"
450
+ when '4'
451
+ print "-> git sflow feature staging 11-feature/Ticket#9999\n\n"
452
+ when '5'
453
+ print "-> git sflow bugfix start Ticket#9999 'Ticket#9999 Bug ...'\n\n"
454
+ when '6'
455
+ print "-> git sflow bugfix finish 12-bugfix/Ticket#9999'\n\n"
456
+ when '7'
457
+ print "-> git sflow bugfix codereview 12-bugfix/Ticket#9999\n"
458
+ when '8'
459
+ print "-> git sflow bugfix staging 12-bugfix/Ticket#9999\n"
460
+ when '9'
461
+ print "-> git sflow hotfix start Ticket#9999 'Ticket#9999 Bug at production in...'\n\n"
462
+ when '10'
463
+ print "-> git sflow hotfix reintegration Ticket#9999'\n\n"
464
+ when '11'
465
+ print "-> git sflow release start v5.5.99'\n\n"
466
+ when '12'
467
+ print "-> git sflow release finish v5.5.99'\n\n"
468
+ when '13'
469
+ print "-> git sflow push BRANCH\n\n"
470
+ when '0'
471
+ else
472
+ end
473
+ print "See you soon!".green
474
+ print "\n\n"
475
+
476
+
477
+ end
478
+
479
+ def self.reintegration type = "feature"
480
+ # Git.fetch ref_branch
481
+ # Git.checkout ref_branch
482
+ # Git.pull ref_branch
483
+ source_branch = $PARAM1
484
+ issue = GitLab::Issue.find_by_branch(source_branch)
485
+ print "Title: #{issue.title}\n\n"
486
+ print "Changelog message:\n--> ".yellow
487
+ message_changelog = STDIN.gets.chomp
488
+ print "\n ok!\n\n".green
489
+ new_labels = []
490
+ if (type == 'hotfix')
491
+ !source_branch.match('hotfix') rescue raise "invalid branch!"
492
+ new_labels << 'hotfix'
493
+ new_labels << 'urgent'
494
+ else
495
+ (!source_branch.match('feature') && !source_branch.match('bugfix')) rescue raise "invalid branch!"
496
+ end
497
+ remove_labels = $GIT_BRANCHES_STAGING + $GITLAB_LISTS + ['Staging']
498
+ new_labels << 'changelog'
499
+ new_labels << $GITLAB_NEXT_RELEASE_LIST
500
+ old_labels = issue.obj_gitlab["labels"]
501
+ old_labels.delete_if{|label| remove_labels.include? label}
502
+ issue.labels = (old_labels + new_labels).uniq
503
+ issue.description.gsub!(/\* \~changelog .*\n?/,'')
504
+ issue.description = "#{issue.description} \n* ~changelog #{message_changelog}"
505
+ print "Setting changelog: ".yellow
506
+ print "#{message_changelog}\n".green
507
+ print "Moving issue to list: ".yellow
508
+ print "#{$GITLAB_NEXT_RELEASE_LIST}\n".green
509
+ issue.update
510
+
511
+ end
512
+
513
+ def self.start branch, issue, ref_branch = "develop"
514
+ Git.fetch ref_branch
515
+ Git.checkout ref_branch
516
+ Git.pull ref_branch
517
+
518
+ description = "* ~default_branch #{branch}"
519
+
520
+ issue.description = description
521
+ issue.update
522
+
523
+ Git.new_branch branch
524
+ Git.push branch
525
+
526
+ print "\nYou are on branch: #{branch}\n\n".yellow
527
+ end
528
+
529
+ def self.codereview
530
+ Git.fetch "develop"
531
+ Git.checkout "develop"
532
+ Git.pull "develop"
533
+ source_branch = $PARAM1
534
+ issue = GitLab::Issue.find_by_branch(source_branch)
535
+ # issue.move
536
+ mr = GitLab::MergeRequest.new(
537
+ source_branch: source_branch,
538
+ target_branch: 'develop',
539
+ issue_iid: issue.iid
540
+ )
541
+ mr.create_code_review
542
+ issue.labels = (issue.obj_gitlab["labels"] + ['code_review']).uniq
543
+ issue.update
544
+ end
545
+ end
546
+