gitlab-dangerfiles 0.9.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/gitlab-dangerfiles.gemspec +1 -1
- data/lib/danger/helper.rb +71 -17
- data/lib/gitlab/dangerfiles/spec_helper.rb +51 -5
- data/lib/gitlab/dangerfiles/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f9f8e727ceb4fd8d6a8163b5c2637cb117426e30d3002ae5772b7031c83bc98a
|
4
|
+
data.tar.gz: 650de232d49a5cb7a022ed53e4607ddc8941ba0615e5cc73214da44d2e838967
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe6821b98e683ead18db025365f4efe6821ca92773f8baacfec88ab12131ab1597d735a1262d2cf65928d24b9b72182a1db2d2cb3435edd6c52248eb029ecd87
|
7
|
+
data.tar.gz: e984610303236d5d6f964ed04c61f924560cf2e29b04c1b485434f0755dc9dd676d486b879972f02b4463da4d499fd070e2a8629bfb570cee93e88b0e4eaf531
|
data/gitlab-dangerfiles.gemspec
CHANGED
@@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
28
|
spec.require_paths = ["lib"]
|
29
29
|
|
30
|
-
spec.add_dependency "danger"
|
30
|
+
spec.add_dependency "danger-gitlab"
|
31
31
|
|
32
32
|
spec.add_development_dependency "rspec", "~> 3.0"
|
33
33
|
spec.add_development_dependency "rspec-parameterized"
|
data/lib/danger/helper.rb
CHANGED
@@ -51,8 +51,62 @@ module Danger
|
|
51
51
|
JSON.parse(rsp.body)
|
52
52
|
end
|
53
53
|
|
54
|
+
def added_files
|
55
|
+
@added_files ||= if changes_from_api
|
56
|
+
changes_from_api.select { |file| file["new_file"] }.map { |file| file["new_path"] }
|
57
|
+
else
|
58
|
+
git.added_files.to_a
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def modified_files
|
63
|
+
@modified_files ||= if changes_from_api
|
64
|
+
changes_from_api.select { |file| !file["new_file"] && !file["deleted_file"] && !file["renamed_file"] }.map { |file| file["new_path"] }
|
65
|
+
else
|
66
|
+
git.modified_files.to_a
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def renamed_files
|
71
|
+
@renamed_files ||= if changes_from_api
|
72
|
+
changes_from_api.select { |file| file["renamed_file"] }.each_with_object([]) do |file, memo|
|
73
|
+
memo << { before: file["old_path"], after: file["new_path"] }
|
74
|
+
end
|
75
|
+
else
|
76
|
+
git.renamed_files.to_a
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def deleted_files
|
81
|
+
@deleted_files ||= if changes_from_api
|
82
|
+
changes_from_api.select { |file| file["deleted_file"] }.map { |file| file["new_path"] }
|
83
|
+
else
|
84
|
+
git.deleted_files.to_a
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def diff_for_file(filename)
|
89
|
+
if changes_from_api
|
90
|
+
changes_hash = changes_from_api.find { |file| file["new_path"] == filename }
|
91
|
+
changes_hash["diff"] if changes_hash
|
92
|
+
else
|
93
|
+
git.diff_for_file(filename)&.patch
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def changes_from_api
|
98
|
+
return nil unless ci?
|
99
|
+
return nil if defined?(@force_changes_from_git)
|
100
|
+
|
101
|
+
@changes_from_api ||= gitlab_helper.api.merge_request_changes(gitlab_helper.mr_json["project_id"], gitlab_helper.mr_json["iid"]).to_h["changes"]
|
102
|
+
rescue
|
103
|
+
# Fallback to the Git strategy in any case
|
104
|
+
@force_changes_from_git = true
|
105
|
+
nil
|
106
|
+
end
|
107
|
+
|
54
108
|
# Returns a list of all files that have been added, modified or renamed.
|
55
|
-
# `
|
109
|
+
# `modified_files` might contain paths that already have been renamed,
|
56
110
|
# so we need to remove them from the list.
|
57
111
|
#
|
58
112
|
# Considering these changes:
|
@@ -70,10 +124,10 @@ module Danger
|
|
70
124
|
# @return [Array<String>]
|
71
125
|
def all_changed_files
|
72
126
|
Set.new
|
73
|
-
.merge(
|
74
|
-
.merge(
|
75
|
-
.merge(
|
76
|
-
.subtract(
|
127
|
+
.merge(added_files)
|
128
|
+
.merge(modified_files)
|
129
|
+
.merge(renamed_files.map { |x| x[:after] })
|
130
|
+
.subtract(renamed_files.map { |x| x[:before] })
|
77
131
|
.to_a
|
78
132
|
.sort
|
79
133
|
end
|
@@ -87,10 +141,10 @@ module Danger
|
|
87
141
|
# "+ # Test change",
|
88
142
|
# "- # Old change" ]
|
89
143
|
def changed_lines(changed_file)
|
90
|
-
diff =
|
144
|
+
diff = diff_for_file(changed_file)
|
91
145
|
return [] unless diff
|
92
146
|
|
93
|
-
diff.
|
147
|
+
diff.split("\n").select { |line| %r{^[+-]}.match?(line) }
|
94
148
|
end
|
95
149
|
|
96
150
|
def release_automation?
|
@@ -117,23 +171,23 @@ module Danger
|
|
117
171
|
# @return [Gitlab::Dangerfiles::Changes]
|
118
172
|
def changes(categories)
|
119
173
|
Gitlab::Dangerfiles::Changes.new([]).tap do |changes|
|
120
|
-
|
174
|
+
added_files.each do |file|
|
121
175
|
categories_for_file(file, categories).each { |category| changes << Gitlab::Dangerfiles::Change.new(file, :added, category) }
|
122
176
|
end
|
123
177
|
|
124
|
-
|
178
|
+
modified_files.each do |file|
|
125
179
|
categories_for_file(file, categories).each { |category| changes << Gitlab::Dangerfiles::Change.new(file, :modified, category) }
|
126
180
|
end
|
127
181
|
|
128
|
-
|
182
|
+
deleted_files.each do |file|
|
129
183
|
categories_for_file(file, categories).each { |category| changes << Gitlab::Dangerfiles::Change.new(file, :deleted, category) }
|
130
184
|
end
|
131
185
|
|
132
|
-
|
186
|
+
renamed_files.map { |x| x[:before] }.each do |file|
|
133
187
|
categories_for_file(file, categories).each { |category| changes << Gitlab::Dangerfiles::Change.new(file, :renamed_before, category) }
|
134
188
|
end
|
135
189
|
|
136
|
-
|
190
|
+
renamed_files.map { |x| x[:after] }.each do |file|
|
137
191
|
categories_for_file(file, categories).each { |category| changes << Gitlab::Dangerfiles::Change.new(file, :renamed_after, category) }
|
138
192
|
end
|
139
193
|
end
|
@@ -169,31 +223,31 @@ module Danger
|
|
169
223
|
end
|
170
224
|
|
171
225
|
def mr_iid
|
172
|
-
return "" unless
|
226
|
+
return "" unless ci?
|
173
227
|
|
174
228
|
gitlab_helper.mr_json["iid"]
|
175
229
|
end
|
176
230
|
|
177
231
|
def mr_title
|
178
|
-
return "" unless
|
232
|
+
return "" unless ci?
|
179
233
|
|
180
234
|
gitlab_helper.mr_json["title"]
|
181
235
|
end
|
182
236
|
|
183
237
|
def mr_web_url
|
184
|
-
return "" unless
|
238
|
+
return "" unless ci?
|
185
239
|
|
186
240
|
gitlab_helper.mr_json["web_url"]
|
187
241
|
end
|
188
242
|
|
189
243
|
def mr_labels
|
190
|
-
return [] unless
|
244
|
+
return [] unless ci?
|
191
245
|
|
192
246
|
gitlab_helper.mr_labels
|
193
247
|
end
|
194
248
|
|
195
249
|
def mr_target_branch
|
196
|
-
return "" unless
|
250
|
+
return "" unless ci?
|
197
251
|
|
198
252
|
gitlab_helper.mr_json["target_branch"]
|
199
253
|
end
|
@@ -45,17 +45,63 @@ end
|
|
45
45
|
|
46
46
|
RSpec.shared_context "with dangerfile" do
|
47
47
|
let(:dangerfile) { DangerSpecHelper.testing_dangerfile }
|
48
|
-
let(:added_files) { %w[
|
49
|
-
let(:modified_files) { %w[
|
50
|
-
let(:deleted_files) { %w[
|
51
|
-
let(:renamed_before_file) { "renamed_before" }
|
52
|
-
let(:renamed_after_file) { "renamed_after" }
|
48
|
+
let(:added_files) { %w[added-from-git] }
|
49
|
+
let(:modified_files) { %w[modified-from-git] }
|
50
|
+
let(:deleted_files) { %w[deleted-from-git] }
|
51
|
+
let(:renamed_before_file) { "renamed_before-from-git" }
|
52
|
+
let(:renamed_after_file) { "renamed_after-from-git" }
|
53
53
|
let(:renamed_files) { [{ before: renamed_before_file, after: renamed_after_file }] }
|
54
54
|
let(:change_class) { Gitlab::Dangerfiles::Change }
|
55
55
|
let(:changes_class) { Gitlab::Dangerfiles::Changes }
|
56
56
|
let(:changes) { changes_class.new([]) }
|
57
57
|
let(:mr_title) { "Fake Title" }
|
58
58
|
let(:mr_labels) { [] }
|
59
|
+
let(:mr_changes_from_api) do
|
60
|
+
{
|
61
|
+
"changes" => [
|
62
|
+
{
|
63
|
+
"old_path" => "added-from-api",
|
64
|
+
"new_path" => "added-from-api",
|
65
|
+
"a_mode" => "100644",
|
66
|
+
"b_mode" => "100644",
|
67
|
+
"new_file" => true,
|
68
|
+
"renamed_file" => false,
|
69
|
+
"deleted_file" => false,
|
70
|
+
"diff" => "@@ -49,6 +49,14 @@\n- vendor/ruby/\n policy: pull\n \n+.danger-review-cache:\n",
|
71
|
+
},
|
72
|
+
{
|
73
|
+
"old_path" => "modified-from-api",
|
74
|
+
"new_path" => "modified-from-api",
|
75
|
+
"a_mode" => "100644",
|
76
|
+
"b_mode" => "100644",
|
77
|
+
"new_file" => false,
|
78
|
+
"renamed_file" => false,
|
79
|
+
"deleted_file" => false,
|
80
|
+
"diff" => "@@ -49,6 +49,14 @@\n- vendor/ruby/\n policy: pull\n \n+.danger-review-cache:\n",
|
81
|
+
},
|
82
|
+
{
|
83
|
+
"old_path" => "renamed_before-from-api",
|
84
|
+
"new_path" => "renamed_after-from-api",
|
85
|
+
"a_mode" => "100644",
|
86
|
+
"b_mode" => "100644",
|
87
|
+
"new_file" => false,
|
88
|
+
"renamed_file" => true,
|
89
|
+
"deleted_file" => false,
|
90
|
+
"diff" => "@@ -49,6 +49,14 @@\n- vendor/ruby/\n policy: pull\n \n+.danger-review-cache:\n",
|
91
|
+
},
|
92
|
+
{
|
93
|
+
"old_path" => "deleted-from-api",
|
94
|
+
"new_path" => "deleted-from-api",
|
95
|
+
"a_mode" => "100644",
|
96
|
+
"b_mode" => "100644",
|
97
|
+
"new_file" => false,
|
98
|
+
"renamed_file" => false,
|
99
|
+
"deleted_file" => true,
|
100
|
+
"diff" => "@@ -49,6 +49,14 @@\n- vendor/ruby/\n policy: pull\n \n+.danger-review-cache:\n",
|
101
|
+
},
|
102
|
+
],
|
103
|
+
}
|
104
|
+
end
|
59
105
|
|
60
106
|
let(:fake_git) { double("fake-git", added_files: added_files, modified_files: modified_files, deleted_files: deleted_files, renamed_files: renamed_files) }
|
61
107
|
let(:fake_helper) { double("fake-helper", changes: changes, mr_iid: 1234, mr_title: mr_title, mr_labels: mr_labels) }
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gitlab-dangerfiles
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rémy Coutable
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-03-
|
11
|
+
date: 2021-03-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name: danger
|
14
|
+
name: danger-gitlab
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|