Class: KCommercial::KCPipeline::ChangeLogTask

Inherits:
Object
  • Object
show all
Defined in:
lib/KCommercialPipeline/core/change_log/change_log_task.rb

Instance Method Summary collapse

Instance Method Details

#create_docObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/KCommercialPipeline/core/change_log/change_log_task.rb', line 159

def create_doc
  unless @request_body_list.empty?
    puts "create_doc #{@access_token}"
    if @access_token.empty?
      puts "access_token 获取失败"
      exit(-1)
    end
    # url = "https://is-gateway.corp.kuaishou.com/merlot/e/api/docs/create"
    url = KimConfig.configs["change_log"]["create_doc_url"]
    puts url
    body = {
      "docTypeEn": "doc",
      "docName": @version,
      "docDesc": "#{@version} change log",
      "knowLink": 3,
      "parentViewStrId": $parent_view_id,
      "shareDetail": [
        {
          "uid": "liuxiaoliang",
          "accessLevelEn": "write"
        }
      ]
    }

    RestClient::Request.new({
                              method: :post,
                              url: url,
                              headers: get_net_header,
                              payload: body.to_json
                            }).execute do |response|
      puts "response==#{response}"
      case response.code
      when 400
        [:error, JSON.parse(response.to_str)]
      when 200
        json = JSON.parse(response.to_str)
        code = json["code"]
        if code == 0
          doc_id = json["result"]["docId"] || ""
          if doc_id.empty?
            [:error, JSON.parse(response.to_str)]
          else
            @doc_id = doc_id
            [:success, JSON.parse(response.to_str)]
          end
        else
          [:error, JSON.parse(response.to_str)]
        end
      else
        fail "Invalid response #{response.to_str} received."
      end
    end
  end
end

#doc_builderObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/KCommercialPipeline/core/change_log/change_log_task.rb', line 81

def doc_builder
  diffs = KCBranchDiffs.new(@new_branch, @old_branch, "tag").diffs
  puts diffs
  puts '---------------------'
  #读取配置正则
  regular = KimConfig.configs["change_log"]["commit_regular"]
  puts "#{regular}"
  unless regular.size > 0
    regular = "^(\\[[a-zA-Z0-9]+\\])?feat(\\(.*\\))?:.*"
  end
  diffs = diffs.find_all { |c| c.message =~ /#{regular}/ }
  puts diffs
  @request_body_list = []
  unless diffs.empty?
    index = diffs.size
    diffs.each do |c|
      doc_commit_author(c)
      doc_commit_id(c)
      doc_commit_data(c)
      doc_commit_msg(c, index)
      index = index - 1
    end

    title_doc = DocBuilder.build_doc
    title_list_element = DocBuilder.build_paragraph_element_list
    title_list_element["paragraphElementList"].push(DocBuilder.build_doc_version(@version))
    title_list_element["paragraphElementList"].push(DocBuilder.build_doc_paragraph)
    # title_list_element["paragraphElementList"].push(DocBuilder.build_doc_paragraph)
    puts title_list_element.to_json
    title_doc["editRequestList"].push(title_list_element.to_json)
    @request_body_list.push(title_doc)
  end
end

#doc_commit_author(c) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/KCommercialPipeline/core/change_log/change_log_task.rb', line 115

def doc_commit_author(c)
  content_doc = DocBuilder.build_doc
  content_list_element = DocBuilder.build_paragraph_element_list
  content_list_element["paragraphElementList"].push(DocBuilder.build_doc_changes("author : #{c.author.author || ""}"))
  # content_list_element["paragraphElementList"].push(DocBuilder.build_doc_paragraph)
  content_doc["editRequestList"].push(content_list_element.to_json)
  @request_body_list.push(content_doc)
end

#doc_commit_data(c) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/KCommercialPipeline/core/change_log/change_log_task.rb', line 133

def doc_commit_data(c)
  content_doc = DocBuilder.build_doc
  content_list_element = DocBuilder.build_paragraph_element_list
  content_list_element["paragraphElementList"].push(DocBuilder.build_doc_changes("date:#{c.date || ""}"))
  # content_list_element["paragraphElementList"].push(DocBuilder.build_doc_paragraph)
  content_doc["editRequestList"].push(content_list_element.to_json)
  @request_body_list.push(content_doc)
end

#doc_commit_id(c) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/KCommercialPipeline/core/change_log/change_log_task.rb', line 124

def doc_commit_id(c)
  content_doc = DocBuilder.build_doc
  content_list_element = DocBuilder.build_paragraph_element_list
  content_list_element["paragraphElementList"].push(DocBuilder.build_doc_changes("commit:#{c.ori_commit_id || ""}"))
  # content_list_element["paragraphElementList"].push(DocBuilder.build_doc_paragraph)
  content_doc["editRequestList"].push(content_list_element.to_json)
  @request_body_list.push(content_doc)
end

#doc_commit_msg(c, index) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/KCommercialPipeline/core/change_log/change_log_task.rb', line 142

def doc_commit_msg(c, index)
  content_doc = DocBuilder.build_doc
  content_list_element = DocBuilder.build_paragraph_element_list
  content_list_element["paragraphElementList"].push(DocBuilder.build_doc_title("#{index}#{c.message.split("\n")[0] || ""}"))
  # content_list_element["paragraphElementList"].push(DocBuilder.build_doc_paragraph)
  content_doc["editRequestList"].push(content_list_element.to_json)
  @request_body_list.push(content_doc)
end

#find_diff_branchObject



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/KCommercialPipeline/core/change_log/change_log_task.rb', line 286

def find_diff_branch
  reg = Regexp.new('^oversea/V\d(\d)?.\d(\d)?.\d(\d)?$')
  all_b = KCGit::git.lib.tags.find_all { |b| b =~ reg }
  all_tag = all_b.map do |b|
    if b.is_a? Array
      ReleaseBranch.new(b[0])
    elsif b.is_a? String
      ReleaseBranch.new(b)
    end
  end
  branch_list = all_tag.sort_by { |v|
    version = v.name.gsub("oversea/V", "").split(".")
    version[0] * 1000 + version[1] * 100 + version[2]
  }
  if branch_list.size >= 2
    last_branch = branch_list[-1]
    new_branch = last_branch.name || ""
    old_branch = branch_list[branch_list.size - 2].name || ""
    if new_branch != "" || old_branch != ""
      puts "最新tag: #{new_branch}"
      puts "上次tag: #{old_branch}"
      @new_branch = new_branch
      @old_branch = old_branch
      @version = new_branch.gsub("oversea/V", "")
    else
      exit -1
    end
  end
end

#get_access_tokenObject



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/KCommercialPipeline/core/change_log/change_log_task.rb', line 252

def get_access_token
  puts "get_access_token"
  # url = "https://is-gateway.corp.kuaishou.com/token/get?appKey=#{$app_key}&secretKey=#{$secret_key}"
  url = "#{KimConfig.configs["change_log"]["get_access_token_url"]}?appKey=#{$app_key}&secretKey=#{$secret_key}"
  puts url
  RestClient::Request.new({
                            method: :get,
                            url: url,
                          }).execute do |response|
    puts "response==#{response}"
    case response.code
    when 400
      [:error, JSON.parse(response.to_str)]
    when 200
      json = JSON.parse(response.to_str)
      code = json["code"]
      if code == 0
        access_token = json["result"]["accessToken"]
        if access_token.empty?
          [:error, JSON.parse(response.to_str)]
        else
          @access_token = access_token
          [:success, JSON.parse(response.to_str)]

        end
      else
        [:error, JSON.parse(response.to_str)]
      end
    else
      fail "Invalid response #{response.to_str} received."
    end
  end
end

#get_doc_versionObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/KCommercialPipeline/core/change_log/change_log_task.rb', line 214

def get_doc_version
  puts "get_doc_version docId: #{@doc_id}, access_token: #{@access_token}"
  if @doc_id.empty?
    return
  end
  # url = "https://is-gateway.corp.kuaishou.com/merlot/e/api/docs/get-rich-content?docId=#{@doc_id}&version=-1"
  url = "#{KimConfig.configs["change_log"]["get_doc_version_url"]}?docId=#{@doc_id}&version=-1"
  puts url
  RestClient::Request.new({
                            method: :get,
                            url: url,
                            headers: get_net_header
                          }).execute do |response|
    puts "response==#{response}"
    case response.code
    when 400
      [:error, JSON.parse(response.to_str)]
    when 200
      json = JSON.parse(response.to_str)
      code = json["code"]
      if code == 0
        doc_version = json["result"]["version"] || -1
        if doc_version < 0
          [:error, JSON.parse(response.to_str)]
        else
          @doc_version = doc_version
          puts "docId : #{@doc_id}, docVersion : #{@doc_version}"
          [:success, JSON.parse(response.to_str)]
        end
      else
        [:error, JSON.parse(response.to_str)]
      end
    else
      fail "Invalid response #{response.to_str} received."
    end
  end
end

#get_net_headerObject



74
75
76
77
78
79
# File 'lib/KCommercialPipeline/core/change_log/change_log_task.rb', line 74

def get_net_header
  { "Content-Type" => "application/json",
    "appKey" => $app_key,
    "secretKey" => $secret_key,
    "Authorization" => "Bearer #{@access_token}" }
end

#initObject



12
13
14
15
16
17
18
19
# File 'lib/KCommercialPipeline/core/change_log/change_log_task.rb', line 12

def init
  # 文档父视图 https://docs.corp.kuaishou.com/k/home/VEH16BgYm-i8/fcACQuPoTP6FiHYcHBONdL0MF
  $parent_view_id = KimConfig.configs["change_log"]["doc_parent_view_id"]
  $app_key = KimConfig.configs["change_log"]["app_key"]
  $secret_key = KimConfig.configs["change_log"]["secret_key"]
  @access_token = ""
  @doc_version = -1
end

#prepareObject



152
153
154
155
156
157
# File 'lib/KCommercialPipeline/core/change_log/change_log_task.rb', line 152

def prepare
  find_diff_branch
  doc_builder
  get_access_token
  create_doc
end

#runObject



21
22
23
24
25
# File 'lib/KCommercialPipeline/core/change_log/change_log_task.rb', line 21

def run
  init
  prepare
  send
end

#sendObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/KCommercialPipeline/core/change_log/change_log_task.rb', line 27

def send
  index = 1
  @request_body_list.each { |i|
    begin
      t = Thread.new {
        @access_token = ""
        @doc_version = -1
        get_access_token
        get_doc_version
        if @access_token.empty? || @doc_version < 0
          exit(-1)
        else
          puts "send_change_log #{@doc_id}, #{@access_token}, #{@doc_version}"
          send_change_log(i)
          # sleep(0.05)
          index += 1
        end
      }
      t.join
    end
  }
end

#send_change_log(doc) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/KCommercialPipeline/core/change_log/change_log_task.rb', line 50

def send_change_log(doc)
  doc["docId"] = @doc_id
  doc["version"] = @doc_version
  # url = "https://is-gateway.corp.kuaishou.com/merlot/e/api/docs/edit"
  url = KimConfig.configs["change_log"]["edit_doc_url"]
  RestClient::Request.new({
                            method: :post,
                            url: url,
                            headers: get_net_header,
                            payload: doc.to_json
                          }).execute do |response|
    puts "response==#{response}"
    puts "doc==#{doc.to_json}"
    case response.code
    when 400
      [:error, JSON.parse(response.to_str)]
    when 200
      [:success, JSON.parse(response.to_str)]
    else
      fail "Invalid response #{response.to_str} received."
    end
  end
end