Class: Gct::Command::Update::Tag

Inherits:
Gct::Command::Update show all
Defined in:
lib/gct/command/update/tag.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Gct::Command

#auto_add_tag, #check_branch_can_be_update, #config_gitlab, #current_branch, #file_contents, #get_project, #gitlab_error, run

Constructor Details

#initialize(argv) ⇒ Tag

Returns a new instance of Tag.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gct/command/update/tag.rb', line 25

def initialize(argv)
    config_gitlab
    @skip_tag = argv.flag?('skip-tag', false)
    @update_ci = argv.flag?('update-ci', false)
    @auto_tag = argv.flag?('auto-tag', false)
    @pre = argv.flag?('pre', false)
    @name = argv.shift_argument
    @tag = argv.shift_argument
    @pre_tag_suffix = Constant.PreHuffix
    @update_version_branch = @pre ? Constant.PreTagFromBranch : Constant.DefaultTagFromBranch
    @update_to_branch = @pre ? Constant.PreTagToBranch : Constant.DefaultTagToBranch
    @file = get_spec_file
    if @name.nil?
      @name = @spec.name
    end
    @project_id = "#{@group_name}/#{@name}"
    super
end

Class Method Details

.optionsObject



16
17
18
19
20
21
22
23
# File 'lib/gct/command/update/tag.rb', line 16

def self.options
  [
    ['--skip-tag', '是否忽略tag,直接触发ci'],
    ['--update-ci', '是否更新CI'],
    ['--auto-tag', '自动化'],
    ['--pre', '是否预发tag']
  ].concat(super)
end

Instance Method Details

#accept_merge_requestObject



198
199
200
201
202
203
# File 'lib/gct/command/update/tag.rb', line 198

def accept_merge_request
  mr_result = Gitlab.accept_merge_request("#{@project_id}", @id)
  if mr_result.state == 'merged'
    puts "成功合并到#{@update_to_branch}分支".green
  end
end

#check_ciObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/gct/command/update/tag.rb', line 81

def check_ci
  is_exist_ci = false
  res = Gitlab.tree("#{@group_name}/#{@name}")
  res.each do |g|
      if g["name"].eql?('.gitlab-ci.yml')
        is_exist_ci = true
        break
      end
  end
  is_exist_ci
end

#close_mrObject



205
206
207
# File 'lib/gct/command/update/tag.rb', line 205

def close_mr 
  Gitlab.update_merge_request("#{@project_id}", @id, {state_event: 'close'})
end

#create_file(file, content, commit_message) ⇒ Object



181
182
183
# File 'lib/gct/command/update/tag.rb', line 181

def create_file(file, content, commit_message)
  Gitlab.create_file(@project_id, file, @update_version_branch, content, commit_message)
end

#create_tagObject



220
221
222
223
224
225
226
# File 'lib/gct/command/update/tag.rb', line 220

def create_tag
  Gitlab.create_tag("#{@project_id}", "#{@new_tag}", "#{@update_to_branch}")
  db = Database::Data.new
  sql = "update tag t left join project p on t.project_version = p.version set t.is_tag = 1 where t.pod_name = '#{@name}' and t.is_tag != 1 and p.is_done = 0"
  db.query(sql)
  puts "开始上传podspec...  请在CI中查看结果".green
end

#edit_file(file, content, commit_message) ⇒ Object



177
178
179
# File 'lib/gct/command/update/tag.rb', line 177

def edit_file(file, content, commit_message)
  Gitlab.edit_file(@project_id, file, @update_version_branch, content, commit_message)
end

#get_spec_fileObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/gct/command/update/tag.rb', line 126

def get_spec_file
  spec_file = nil
  full_path = nil
  if @auto_tag 
    raise "podspec名不能为空!!!".red if spec_file.nil? if @name.nil?
    Dir.chdir(FileBase.tmp_path) do
      spec_file = Pathname.glob("#{@name}.podspec").first
      full_path = spec_file.realpath
      raise "#{Dir.pwd} 目录下找不到podspec文件".red if spec_file.nil?
    end
  else
    spec_file = Pathname.glob('*.podspec').first
    full_path = spec_file
    raise "#{Dir.pwd} 目录下找不到podspec文件".red if spec_file.nil?
  end
  @spec = Pod::Specification.from_file(full_path)
  # 取出组名
  source = @spec.source[:git]
  left1 = "https://gi-dev.ccrgt.com/"
  left2 = "http://gi-dev.ccrgt.com/"
  right = "/"
  source_regex = /(?<=#{left1}|#{left2}).*(?=#{right})/
  @group_name = source_regex.match(source).to_s
  if @group_name.include?("/")
    @group_name = @group_name.sub!("/", "")
  end
  raise "gitlab中找不到#{@spec.name}所在的group".red if @group_name.nil? || @group_name.empty?
  spec_file
end

#mr_to_masterObject



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/gct/command/update/tag.rb', line 185

def mr_to_master
  puts "正在创建MR请求将#{@update_version_branch}分支合并到#{@update_to_branch}分支!".green
  mr = Gitlab.create_merge_request("#{@group_name}/#{@name}", "mr", { source_branch: "#{@update_version_branch}", target_branch: "#{@update_to_branch}" })
  @id = mr.iid
  puts mr.merge_status
  isEmptyMR = mr.diff_refs.base_sha.eql?(mr.diff_refs.head_sha)
  close_mr if isEmptyMR
  if mr.merge_status == 'cannot_be_merged' && !isEmptyMR
    raise "Merge有冲突,请前往 https://gi-dev.ccrgt.com/#{@group_name}/#{@name}/merge_requests/#{@id} 解决".red
  end
  isEmptyMR
end

#mr_to_master_if_needObject



119
120
121
122
123
124
# File 'lib/gct/command/update/tag.rb', line 119

def mr_to_master_if_need 
  isEmptyMR = mr_to_master
  if !isEmptyMR
    accept_merge_request
  end
end

#remove_and_create_tagObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/gct/command/update/tag.rb', line 61

def remove_and_create_tag
  @new_tag = @spec.version
  puts "new tag"
  puts @new_tag
  if @pre
    @new_tag = "#{@new_tag}" + @pre_tag_suffix
  end
  puts "tag == #{@new_tag}"
  # remove tag
  command = `git rev-parse --is-inside-work-tree`
  if command.eql?("true")
    `git tag -d #{@new_tag}`
    `git push origin :refs/tags/#{@new_tag}`  
  else
    remove_tag
  end
  # create tag
  create_tag
end

#remove_tagObject



209
210
211
212
213
214
215
216
217
218
# File 'lib/gct/command/update/tag.rb', line 209

def remove_tag
  tags = Gitlab.tags("#{@project_id}")
  puts "remove tag is " + "#{@new_tag}"
  tags.each do |t|
    if t["name"].eql?("#{@new_tag}")
      Gitlab.delete_tag("#{@project_id}", "#{@new_tag}")
      break
    end
  end
end

#runObject



44
45
46
47
48
49
50
51
# File 'lib/gct/command/update/tag.rb', line 44

def run
  update_ci_method if @update_ci
  if @skip_tag 
    skip_tag_method
  else
    update_podspec_version
  end
end

#skip_tag_methodObject



53
54
55
56
57
58
59
# File 'lib/gct/command/update/tag.rb', line 53

def skip_tag_method 
  isEmptyMR = mr_to_master
  if !isEmptyMR
    accept_merge_request
  end
  remove_and_create_tag
end

#update_ci_methodObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/gct/command/update/tag.rb', line 93

def update_ci_method
  puts "正在检查CI文件".green
  temp_local_file = TempLocalFile.new()
  path = File.expand_path("../../init", __FILE__)
  ci_content = temp_local_file.read_path_file(path, "#{@group_name}-ci.rb")
  if check_ci
    # 检查 ci 文件是否有变化
    remote_content = file_contents("#{@group_name}/#{@name}", '.gitlab-ci.yml', @update_version_branch)
    remote_content.force_encoding('UTF-8')
    strip_remote_content = remote_content.gsub(/\s/,'')
    ci_content.force_encoding('UTF-8')
    strip_ci_content = ci_content.gsub(/\s/,'')

    if strip_ci_content.eql?strip_remote_content
      puts "无需更新CI文件".green
    else 
      edit_file('.gitlab-ci.yml', ci_content, 'update ci')
      mr_to_master_if_need
    end
  else 
    create_file('.gitlab-ci.yml', ci_content, 'init ci')
    mr_to_master_if_need
  end
  
end

#update_podspec_versionObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/gct/command/update/tag.rb', line 156

def update_podspec_version 
  puts "开始读取podspec文件...".green
  podspec_content = file_contents(@project_id, @file, @update_version_branch)
  version_regex = /.*version.*/
  version_match = version_regex.match(podspec_content).to_s
  tag_regex = /(?<="|')\w.*(?="|')/
  tag = tag_regex.match(version_match)
  @now_tag = tag
  @new_tag = @tag || auto_add_tag(tag.to_s)
  if @pre
    @new_tag = "#{@new_tag}" + @pre_tag_suffix
  end
  replace_string = version_match.gsub(tag_regex, @new_tag)
  updated_podspec_content = podspec_content.gsub(version_match, replace_string)
  puts "#{@name} 更新版本号为:#{@new_tag}".green
  edit_file(@file, updated_podspec_content, "@config 更新版本号:#{@new_tag}")
  mr_to_master
  accept_merge_request
  create_tag
end