Class: Fastlane::Actions::GitlabIncreateLineNotesAction

Inherits:
Action
  • Object
show all
Includes:
HTTParty
Defined in:
lib/fastlane/plugin/gitlab_increate_line_notes/actions/gitlab_increate_line_notes_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



167
168
169
# File 'lib/fastlane/plugin/gitlab_increate_line_notes/actions/gitlab_increate_line_notes_action.rb', line 167

def self.authors
  ["xiongzenghui"]
end

.available_optionsObject



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
213
# File 'lib/fastlane/plugin/gitlab_increate_line_notes/actions/gitlab_increate_line_notes_action.rb', line 185

def self.available_options
  [
    FastlaneCore::ConfigItem.new(
      key: :gitlab_host,
      description: "your gitlab host"
    ),
    FastlaneCore::ConfigItem.new(
      key: :gitlab_token,
      description: "your gitlab token"
    ),
    FastlaneCore::ConfigItem.new(
      key: :projectid,
      description: "your gitlab project id"
    ),
    FastlaneCore::ConfigItem.new(
      key: :mrid,
      description: "your gitlab merge request id"
    ),
    FastlaneCore::ConfigItem.new(
      key: :swiftlint_result_json,
      description: "swiftlint report json",
      is_string: false
    ),
    FastlaneCore::ConfigItem.new(
      key: :last_commit,
      description: "gitlab merge requst current newest/lastest commit hash",
    )
  ]
end

.descriptionObject



163
164
165
# File 'lib/fastlane/plugin/gitlab_increate_line_notes/actions/gitlab_increate_line_notes_action.rb', line 163

def self.description
  "filter gitlab merge request changes files & swiftlint json, last add line code with gitlab discussion"
end

.detailsObject



181
182
183
# File 'lib/fastlane/plugin/gitlab_increate_line_notes/actions/gitlab_increate_line_notes_action.rb', line 181

def self.details
  "filter gitlab merge request changes files & swiftlint json, last add line code with gitlab discussion"
end

.example_codeObject



215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/fastlane/plugin/gitlab_increate_line_notes/actions/gitlab_increate_line_notes_action.rb', line 215

def self.example_code
  [
    'gitlab_increate_line_notes(
      gitlab_host: "https://git.in.xxx.com/api/v4",
      gitlab_token: "xxxx",
      projectid: "16456",
      mrid: "33",
      swiftlint_result_json: JSON.parse(File.read("spec/swiftlint_result_json")),
      last_commit: "dc6b7b2f3875b338b4961eb40c878540be170bd1"
    )
    pp Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::GITLAB_LINT_ADD_DISCUSSIONS_LINE_NOTES]'
  ]
end

.gitlab_changes_files(mr_hash) ⇒ Object

MR commit 中【修改】过的【代码行】



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
114
115
116
117
118
119
120
# File 'lib/fastlane/plugin/gitlab_increate_line_notes/actions/gitlab_increate_line_notes_action.rb', line 81

def self.gitlab_changes_files(mr_hash)
  # CI 绝对路径 ==> 相对于 pod 组件仓库 中的路径
  #
  # gitlab merge request changes = [
  #   "AFNeworking/Classes/Views/Animators/CreationPanelV2TransitionAnimator.swift",
  #   "AFNeworking/Classes/Views/Animators/RecommendV2TransitionAnimator.swift"
  # ]
  #
  # swiftlint_result files = [
  #   "/Users/xiongzenghui/ci-jenkins/workspace/xxx-iOS-module/AFNeworking/AFNeworking/Classes/Views/Animators/CreationPanelV2TransitionAnimator.swift",
  #   "/Users/xiongzenghui/ci-jenkins/workspace/xxx-iOS-module/AFNeworking/AFNeworking/Classes/Views/Animators/RecommendV2TransitionAnimator.swift"
  # ]

  changes_hash = mr_hash['changes']

  # changes Hash => changes Model
  changes = []
  changes_hash.each do |c|
    # pp c
    #------------------------------------------------
    # {
    #   "old_path":"AFNeworking/Classes/Commons/AFNeworkingObjecs.m",
    #   "new_path":"AFNeworking/Classes/Commons/AFNeworkingObjecs.m",
    #   "a_mode":"100644",
    #   "b_mode":"100644",
    #   "new_file":false,
    #   "renamed_file":false,
    #   "deleted_file":false,
    #   "diff":"@@ -10,6 +10,8 @@ ... "
    # }
    #------------------------------------------------

    #=> 过滤掉 renamed 和 delete 文件,只保留 new 和 update 文件
    # next if c['renamed_file'] || c['deleted_file']

    git_diff_file = Fastlane::Helper::GitlabDiffFile.new(c['old_path'], c['new_path'], c['diff'])
    changes.push(git_diff_file)
  end
  changes
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


229
230
231
# File 'lib/fastlane/plugin/gitlab_increate_line_notes/actions/gitlab_increate_line_notes_action.rb', line 229

def self.is_supported?(platform)
  true
end

.merge_request_discussions(project_id, mr_iid, per_page = 100) ⇒ Object

获取 MR 当前所有 discussions



136
137
138
# File 'lib/fastlane/plugin/gitlab_increate_line_notes/actions/gitlab_increate_line_notes_action.rb', line 136

def self.merge_request_discussions(project_id, mr_iid, per_page = 100)
  self.get("/projects/#{project_id}/merge_requests/#{mr_iid}/discussions?per_page=#{per_page}")
end

.merge_request_lint_lines(swift_lint_lines, git_changes, last_commit) ⇒ Object

过滤得到 gitlab merge request 中需要添加 discussion 的 line 代码行

  • 1) swiftlint result json => files 1

  • 2) gitlab changes => files 2

计算 [swiftlint result json] - [gitlab changes] 差值 = 当前 MR commit swiftlint 存在问题的【所有代码行】



126
127
128
129
130
131
132
133
# File 'lib/fastlane/plugin/gitlab_increate_line_notes/actions/gitlab_increate_line_notes_action.rb', line 126

def self.merge_request_lint_lines(swift_lint_lines, git_changes, last_commit)
  lrp = Fastlane::Helper::GitlabLintLineParser.new(
    swift_lint_lines,
    git_changes,
    last_commit
  )
  lrp.parse
end

.merge_request_position_notes(discussions) ⇒ Object

从当前 lint results 中过滤掉【已经】存在的 line note



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/fastlane/plugin/gitlab_increate_line_notes/actions/gitlab_increate_line_notes_action.rb', line 141

def self.merge_request_position_notes(discussions)
  notes = []
  discussions.each_with_index { |discuss, idx|
    # puts "--------- discussion #{idx+1}" + '-' * 30
    ith = discuss.to_hash
    # pp ith

    ith['notes'].each_with_index { |note, iidx|
      # puts "------------------ note #{iidx+1}" + '-' * 30
      iith = note.to_hash
      # pp iith
      # pp iith['body']
      note_type = iith['type']
      note_position = iith['position']
      # pp note_position

      notes << note_position if note_type == 'DiffNote' && note_position
    }
  }
  notes
end

.outputObject



175
176
177
178
179
# File 'lib/fastlane/plugin/gitlab_increate_line_notes/actions/gitlab_increate_line_notes_action.rb', line 175

def self.output
  [
    ['GITLAB_INCREATE_LINE_NOTES_ACTION_NOTES', 'gitlab merge request changes swiftlint line notes']
  ]
end

.return_valueObject



171
172
173
# File 'lib/fastlane/plugin/gitlab_increate_line_notes/actions/gitlab_increate_line_notes_action.rb', line 171

def self.return_value
  "Array"
end

.run(params) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fastlane/plugin/gitlab_increate_line_notes/actions/gitlab_increate_line_notes_action.rb', line 16

def self.run(params)
  require 'pp'
  require 'json'
  require 'gitlab'

  gitlab_host = params[:gitlab_host]
  gitlab_token = params[:gitlab_token]
  projectid = params[:projectid]
  mrid = params[:mrid]
  swiftlint_result_json = params[:swiftlint_result_json]
  last_commit = params[:last_commit]

  base_uri(gitlab_host)
  headers('PRIVATE-TOKEN' => gitlab_token)
  gc = Gitlab.client(endpoint: gitlab_host, private_token: gitlab_token)
  mr_hash = gc.merge_request_changes(projectid, mrid).to_hash

  # 1. 当前 mr changes 执行 lint 存在问题的 所有代码行
  lint_lines = merge_request_lint_lines(
    swiftlint_result_json, 
    gitlab_changes_files(mr_hash), 
    last_commit
  )

  # 2. mr 所有带有 position 的 note 评论
  notes = merge_request_position_notes(merge_request_discussions(projectid, mrid))

  # 3. 如果 file - line 已经添加 评论,则不再重复添加
  lint_lines = lint_lines.reject do |ll|
    ll.line_in_positons?(notes)
  end

  # 4. return
  Actions.lane_context[SharedValues::GITLAB_INCREATE_LINE_NOTES_ACTION_NOTES] = lint_lines.map(&:to_hash)

  # 5. 添加 mr discussion notes
  return unless lint_lines
  return if lint_lines.empty?
  diff_refs = mr_hash['diff_refs']
  base_sha = diff_refs['base_sha']
  head_sha = diff_refs['head_sha']
  start_sha = diff_refs['start_sha']
  lint_lines.each { |ll|
    body = ll.to_discussion
    # 添加 代码行 discussion 评论
    # 需要通过 https://git.in.xxx.com/api/v4/projects/10701/merge_requests/832/notes 接口获取所有的评论
    gc.create_merge_request_discussion(
      projectid,
      mrid,
      body: body,
      position: {
        base_sha: base_sha,
        start_sha: start_sha,
        head_sha: head_sha,
        position_type: 'text',
        # old_line: ll[:old_line], # FIXME: 暂时不知道这个 old_line 干嘛的?
        new_line: ll.line,
        old_path: ll.old_path,
        new_path: ll.new_path
      }
    )
  }
end