danger-dangermattic 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. checksums.yaml +7 -0
  2. data/.buildkite/gem-push.sh +15 -0
  3. data/.buildkite/pipeline.yml +69 -0
  4. data/.bundle/config +2 -0
  5. data/.github/workflows/reusable-check-labels-on-issues.yml +91 -0
  6. data/.github/workflows/reusable-run-danger.yml +54 -0
  7. data/.gitignore +30 -0
  8. data/.rubocop.yml +67 -0
  9. data/.ruby-version +1 -0
  10. data/.yardopts +7 -0
  11. data/CHANGELOG.md +7 -0
  12. data/Gemfile +5 -0
  13. data/Gemfile.lock +191 -0
  14. data/Guardfile +21 -0
  15. data/LICENSE +373 -0
  16. data/README.md +68 -0
  17. data/Rakefile +24 -0
  18. data/danger-dangermattic.gemspec +58 -0
  19. data/lib/danger_dangermattic.rb +3 -0
  20. data/lib/danger_plugin.rb +4 -0
  21. data/lib/dangermattic/gem_version.rb +5 -0
  22. data/lib/dangermattic/plugins/android_release_checker.rb +50 -0
  23. data/lib/dangermattic/plugins/android_strings_checker.rb +31 -0
  24. data/lib/dangermattic/plugins/android_unit_test_checker.rb +187 -0
  25. data/lib/dangermattic/plugins/common/common_release_checker.rb +113 -0
  26. data/lib/dangermattic/plugins/common/git_utils.rb +166 -0
  27. data/lib/dangermattic/plugins/common/github_utils.rb +68 -0
  28. data/lib/dangermattic/plugins/common/reporter.rb +38 -0
  29. data/lib/dangermattic/plugins/ios_release_checker.rb +106 -0
  30. data/lib/dangermattic/plugins/labels_checker.rb +74 -0
  31. data/lib/dangermattic/plugins/manifest_pr_checker.rb +106 -0
  32. data/lib/dangermattic/plugins/milestone_checker.rb +98 -0
  33. data/lib/dangermattic/plugins/podfile_checker.rb +122 -0
  34. data/lib/dangermattic/plugins/pr_size_checker.rb +125 -0
  35. data/lib/dangermattic/plugins/tracks_checker.rb +72 -0
  36. data/lib/dangermattic/plugins/view_changes_checker.rb +46 -0
  37. data/spec/android_release_checker_spec.rb +93 -0
  38. data/spec/android_strings_checker_spec.rb +185 -0
  39. data/spec/android_unit_test_checker_spec.rb +343 -0
  40. data/spec/common_release_checker_spec.rb +70 -0
  41. data/spec/fixtures/android_unit_test_checker/Abc.java +7 -0
  42. data/spec/fixtures/android_unit_test_checker/AbcFeatureConfig.java +7 -0
  43. data/spec/fixtures/android_unit_test_checker/Abcdef.kt +5 -0
  44. data/spec/fixtures/android_unit_test_checker/AbcdefgViewHelper.java +7 -0
  45. data/spec/fixtures/android_unit_test_checker/AnotherViewHelper.kt +7 -0
  46. data/spec/fixtures/android_unit_test_checker/MyNewClass.java +7 -0
  47. data/spec/fixtures/android_unit_test_checker/Polygon.kt +3 -0
  48. data/spec/fixtures/android_unit_test_checker/Shape.kt +10 -0
  49. data/spec/fixtures/android_unit_test_checker/TestsINeedThem.java +5 -0
  50. data/spec/fixtures/android_unit_test_checker/TestsINeedThem.kt +7 -0
  51. data/spec/fixtures/android_unit_test_checker/TestsINeedThem2.kt +12 -0
  52. data/spec/fixtures/android_unit_test_checker/src/android/java/org/activities/MyActivity.kt +7 -0
  53. data/spec/fixtures/android_unit_test_checker/src/android/java/org/activities/MyJavaActivity.java +7 -0
  54. data/spec/fixtures/android_unit_test_checker/src/android/java/org/fragments/MyFragment.kt +6 -0
  55. data/spec/fixtures/android_unit_test_checker/src/android/java/org/fragments/MyNewJavaFragment.java +7 -0
  56. data/spec/fixtures/android_unit_test_checker/src/android/java/org/module/MyModule.java +13 -0
  57. data/spec/fixtures/android_unit_test_checker/src/android/java/org/view/ActionCardViewHolder.kt +22 -0
  58. data/spec/fixtures/android_unit_test_checker/src/android/java/org/view/MyRecyclerView.java +7 -0
  59. data/spec/fixtures/android_unit_test_checker/src/androidTest/java/org/test/AbcTests.java +5 -0
  60. data/spec/fixtures/android_unit_test_checker/src/androidTest/java/org/test/AnotherTestClass.java +7 -0
  61. data/spec/fixtures/android_unit_test_checker/src/androidTest/java/org/test/PolygonTest.kt +4 -0
  62. data/spec/fixtures/android_unit_test_checker/src/androidTest/java/org/test/TestMyNewClass.java +9 -0
  63. data/spec/fixtures/android_unit_test_checker/src/androidTest/java/org/test/ToolTest.kt +5 -0
  64. data/spec/fixtures/android_unit_test_checker/src/main/java/org/wordpress/android/widgets/NestedWebView.kt +32 -0
  65. data/spec/fixtures/android_unit_test_checker/src/main/java/org/wordpress/util/config/BloggingPromptsFeatureConfig.kt +23 -0
  66. data/spec/fixtures/android_unit_test_checker/src/test/java/org/test/TestsINeedThem.java +9 -0
  67. data/spec/github_utils_spec.rb +110 -0
  68. data/spec/ios_release_checker_spec.rb +191 -0
  69. data/spec/labels_checker_spec.rb +169 -0
  70. data/spec/manifest_pr_checker_spec.rb +140 -0
  71. data/spec/milestone_checker_spec.rb +222 -0
  72. data/spec/podfile_checker_spec.rb +595 -0
  73. data/spec/pr_size_checker_spec.rb +250 -0
  74. data/spec/spec_helper.rb +115 -0
  75. data/spec/tracks_checker_spec.rb +156 -0
  76. data/spec/view_changes_checker_spec.rb +168 -0
  77. metadata +341 -0
@@ -0,0 +1,169 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'spec_helper'
4
+
5
+ module Danger
6
+ describe Danger::LabelsChecker do
7
+ it 'is a plugin' do
8
+ expect(described_class.new(nil)).to be_a Danger::Plugin
9
+ end
10
+
11
+ describe 'with Dangerfile' do
12
+ before do
13
+ @dangerfile = testing_dangerfile
14
+ @plugin = @dangerfile.labels_checker
15
+ end
16
+
17
+ context 'with required labels' do
18
+ it 'reports a custom error when a PR does not have at least one label' do
19
+ allow(@plugin.github).to receive(:pr_labels).and_return([])
20
+
21
+ error = 'PR is missing at least one label.'
22
+ @plugin.check(required_labels: [//], required_labels_error: error)
23
+
24
+ expect(@dangerfile).to report_errors([error])
25
+ end
26
+
27
+ it 'does nothing when a PR has at least one label' do
28
+ pr_labels = ['my-label']
29
+ allow(@plugin.github).to receive(:pr_labels).and_return(pr_labels)
30
+
31
+ @plugin.check(required_labels: [//])
32
+
33
+ expect(@dangerfile).to not_report
34
+ end
35
+
36
+ it 'reports an error containing the required labels when a PR does not meet the label requirements' do
37
+ pr_labels = ['random other label', '[feature] magic', 'wizard needed', 'type: fantasy']
38
+ allow(@plugin.github).to receive(:pr_labels).and_return(pr_labels)
39
+
40
+ @plugin.check(
41
+ required_labels: [/^\[feature\]/, /^\[type\]:/]
42
+ )
43
+
44
+ expect(@dangerfile).to report_errors(['PR is missing label(s) matching: `^\[type\]:`'])
45
+ end
46
+
47
+ it 'reports a custom error when a PR has custom label requirements' do
48
+ pr_labels = ['random label', 'feature: magic', 'wizard needed', 'another type: test']
49
+ allow(@plugin.github).to receive(:pr_labels).and_return(pr_labels)
50
+
51
+ custom_labels_error = 'Please add at least one of the required labels.'
52
+
53
+ @plugin.check(
54
+ required_labels: [/^feature:/, /^type:/],
55
+ required_labels_error: custom_labels_error
56
+ )
57
+
58
+ expect(@dangerfile).to report_errors([custom_labels_error])
59
+ end
60
+
61
+ it 'does nothing when custom required labels are correctly set in the PR' do
62
+ pr_labels = [
63
+ 'some other label',
64
+ 'type: fantasy',
65
+ 'milestone: 1.0',
66
+ 'feature: time travel',
67
+ 'wizard needed'
68
+ ]
69
+ allow(@plugin.github).to receive(:pr_labels).and_return(pr_labels)
70
+
71
+ @plugin.check(
72
+ required_labels: [/^feature:/, /^type:/]
73
+ )
74
+
75
+ expect(@dangerfile).to not_report
76
+ end
77
+ end
78
+
79
+ context 'with recommended labels' do
80
+ it 'reports a warning containing the recommended labels when a PR does not meet the label requirements' do
81
+ pr_labels = ['random other label', 'milestone: rc']
82
+ allow(@plugin.github).to receive(:pr_labels).and_return(pr_labels)
83
+
84
+ @plugin.check(
85
+ recommended_labels: [/^feature:/, /^milestone:/, /^\[type\]/]
86
+ )
87
+
88
+ expect(@dangerfile).to report_warnings(['PR is missing label(s) matching: `^feature:`, `^\[type\]`'])
89
+ end
90
+
91
+ it 'reports a custom warning when a PR has custom label requirements' do
92
+ pr_labels = ['random other label', 'feature: myFeature', 'Milestone: beta']
93
+ allow(@plugin.github).to receive(:pr_labels).and_return(pr_labels)
94
+
95
+ custom_labels_warning = 'Please add at least one feature and milestone label in the expected formats.'
96
+
97
+ @plugin.check(
98
+ recommended_labels: [/^feature:/, /^milestone:/],
99
+ recommended_labels_warning: custom_labels_warning
100
+ )
101
+
102
+ expect(@dangerfile).to report_warnings([custom_labels_warning])
103
+ end
104
+
105
+ it 'does nothing when custom recommended labels are correctly set in the PR' do
106
+ pr_labels = [
107
+ '[feature]: time travel',
108
+ '[type]: prototype',
109
+ '[milestone]: prototype',
110
+ 'another random label'
111
+ ]
112
+ allow(@plugin.github).to receive(:pr_labels).and_return(pr_labels)
113
+
114
+ @plugin.check(
115
+ recommended_labels: [/^\[feature\]:/, /^\[type\]:/]
116
+ )
117
+
118
+ expect(@dangerfile).to not_report
119
+ end
120
+ end
121
+
122
+ context 'with \'do not merge\' labels' do
123
+ it 'reports an error when a PR has a single \'do not merge\' label' do
124
+ pr_label = 'DO NOT MERGE'
125
+ allow(@plugin.github).to receive(:pr_labels).and_return([pr_label])
126
+
127
+ @plugin.check(do_not_merge_labels: [pr_label])
128
+
129
+ expect(@dangerfile).to report_errors(["This PR is tagged with `#{pr_label}` label(s)."])
130
+ end
131
+
132
+ it 'reports an error when a PR has a custom label for not merging' do
133
+ pr_label = 'please dont merge'
134
+ allow(@plugin.github).to receive(:pr_labels).and_return([pr_label])
135
+
136
+ labels = ['blocked', pr_label]
137
+ @plugin.check(
138
+ do_not_merge_labels: labels
139
+ )
140
+
141
+ expect(@dangerfile).to report_errors(["This PR is tagged with `#{pr_label}` label(s)."])
142
+ end
143
+ end
144
+
145
+ it 'reports the right errors and warning when combining the check parameters' do
146
+ do_not_merge_label = 'blocked'
147
+ pr_labels = [do_not_merge_label, 'type: test', 'milestone: alpha']
148
+ allow(@plugin.github).to receive(:pr_labels).and_return(pr_labels)
149
+
150
+ custom_labels_error = "Please add at least one label in the 'type: issueType' format."
151
+ custom_labels_warning = "Please add at least one label in the 'feature: myFeatureName' format."
152
+
153
+ @plugin.check(
154
+ do_not_merge_labels: [do_not_merge_label],
155
+ required_labels: [/^type:/, /^feature:/],
156
+ required_labels_error: custom_labels_error,
157
+ recommended_labels: [/^bug-triage$/],
158
+ recommended_labels_warning: custom_labels_warning
159
+ )
160
+
161
+ expect(@dangerfile.status_report[:warnings]).to eq([custom_labels_warning])
162
+ expect(@dangerfile.status_report[:errors]).to contain_exactly(
163
+ "This PR is tagged with `#{do_not_merge_label}` label(s).",
164
+ custom_labels_error
165
+ )
166
+ end
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,140 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'spec_helper'
4
+
5
+ module Danger
6
+ describe Danger::ManifestPRChecker do
7
+ it 'is a plugin' do
8
+ expect(described_class.new(nil)).to be_a Danger::Plugin
9
+ end
10
+
11
+ describe 'with Dangerfile' do
12
+ before do
13
+ @dangerfile = testing_dangerfile
14
+ @plugin = @dangerfile.manifest_pr_checker
15
+ end
16
+
17
+ describe 'Bundler' do
18
+ it 'reports a warning when a PR changed the Gemfile but not the Gemfile.lock' do
19
+ modified_files = ['Gemfile']
20
+ allow(@plugin.git).to receive(:modified_files).and_return(modified_files)
21
+
22
+ @plugin.check_gemfile_lock_updated
23
+
24
+ expected_warning = format(ManifestPRChecker::MESSAGE, 'Gemfile', 'Gemfile.lock', 'Please run `bundle install` or `bundle update <updated_gem>`')
25
+ expect(@dangerfile).to report_warnings([expected_warning])
26
+ end
27
+
28
+ it 'reports no warnings when both the Gemfile and the Gemfile.lock were updated' do
29
+ modified_files = ['Gemfile', 'Gemfile.lock']
30
+ allow(@plugin.git).to receive(:modified_files).and_return(modified_files)
31
+
32
+ @plugin.check_gemfile_lock_updated
33
+
34
+ expect(@dangerfile).to not_report
35
+ end
36
+
37
+ it 'reports no warnings when only the Gemfile.lock was updated' do
38
+ modified_files = ['Gemfile.lock']
39
+ allow(@plugin.git).to receive(:modified_files).and_return(modified_files)
40
+
41
+ @plugin.check_gemfile_lock_updated
42
+
43
+ expect(@dangerfile).to not_report
44
+ end
45
+ end
46
+
47
+ describe 'CocoaPods' do
48
+ it 'reports a warning when a PR changed the Podfile but not the Podfile.lock' do
49
+ modified_files = ['Podfile']
50
+ allow(@plugin.git).to receive(:modified_files).and_return(modified_files)
51
+
52
+ @plugin.check_podfile_lock_updated
53
+
54
+ expected_warning = format(ManifestPRChecker::MESSAGE, 'Podfile', 'Podfile.lock', 'Please run `bundle exec pod install`')
55
+ expect(@dangerfile).to report_warnings([expected_warning])
56
+ end
57
+
58
+ it 'reports no warnings when both the Podfile and the Podfile.lock were updated' do
59
+ modified_files = ['Podfile', 'Podfile.lock']
60
+ allow(@plugin.git).to receive(:modified_files).and_return(modified_files)
61
+
62
+ @plugin.check_podfile_lock_updated
63
+
64
+ expect(@dangerfile).to not_report
65
+ end
66
+
67
+ it 'reports a warning when a PR changed a custom located Podfile but not the corresponding Podfile.lock' do
68
+ modified_files = ['./path/to/Podfile', './my/Podfile.lock']
69
+ allow(@plugin.git).to receive(:modified_files).and_return(modified_files)
70
+
71
+ @plugin.check_podfile_lock_updated
72
+
73
+ expected_warning = format(ManifestPRChecker::MESSAGE, './path/to/Podfile', 'Podfile.lock', 'Please run `bundle exec pod install`')
74
+ expect(@dangerfile).to report_warnings([expected_warning])
75
+ end
76
+
77
+ it 'reports multiple warnings when a PR changed multiple custom located Podfiles but not the corresponding Podfile.lock' do
78
+ modified_files = ['./dir1/Podfile', './dir2/Podfile', './dir3/Podfile', './dir1/Podfile.lock']
79
+ allow(@plugin.git).to receive(:modified_files).and_return(modified_files)
80
+
81
+ @plugin.check_podfile_lock_updated
82
+
83
+ expected_warnings = [
84
+ format(ManifestPRChecker::MESSAGE, './dir2/Podfile', 'Podfile.lock', 'Please run `bundle exec pod install`'),
85
+ format(ManifestPRChecker::MESSAGE, './dir3/Podfile', 'Podfile.lock', 'Please run `bundle exec pod install`')
86
+ ]
87
+ expect(@dangerfile).to report_warnings(expected_warnings)
88
+ end
89
+
90
+ it 'reports no warnings when both custom located Podfile`s and their corresponding Podfile.lock were updated' do
91
+ modified_files = ['./my/path/to/Podfile', './another/path/to/Podfile', './my/path/to/Podfile.lock', './another/path/to/Podfile.lock']
92
+ allow(@plugin.git).to receive(:modified_files).and_return(modified_files)
93
+
94
+ @plugin.check_podfile_lock_updated
95
+
96
+ expect(@dangerfile).to not_report
97
+ end
98
+
99
+ it 'reports no warnings when only the Podfile.lock was updated' do
100
+ modified_files = ['Podfile.lock']
101
+ allow(@plugin.git).to receive(:modified_files).and_return(modified_files)
102
+
103
+ @plugin.check_podfile_lock_updated
104
+
105
+ expect(@dangerfile).to not_report
106
+ end
107
+ end
108
+
109
+ describe 'Swift Package Manager' do
110
+ it 'reports a warning when a PR changed the Package.swift but not the Package.resolved' do
111
+ modified_files = ['Package.swift']
112
+ allow(@plugin.git).to receive(:modified_files).and_return(modified_files)
113
+
114
+ @plugin.check_swift_package_resolved_updated
115
+
116
+ expected_warning = format(ManifestPRChecker::MESSAGE, 'Package.swift', 'Package.resolved', 'Please resolve the Swift packages in Xcode')
117
+ expect(@dangerfile).to report_warnings([expected_warning])
118
+ end
119
+
120
+ it 'reports no warnings when both the Package.swift and the Package.resolved were updated' do
121
+ modified_files = ['Package.swift', 'Package.resolved']
122
+ allow(@plugin.git).to receive(:modified_files).and_return(modified_files)
123
+
124
+ @plugin.check_swift_package_resolved_updated
125
+
126
+ expect(@dangerfile).to not_report
127
+ end
128
+
129
+ it 'reports no warnings when only the Package.resolved was updated' do
130
+ modified_files = ['Package.resolved']
131
+ allow(@plugin.git).to receive(:modified_files).and_return(modified_files)
132
+
133
+ @plugin.check_swift_package_resolved_updated
134
+
135
+ expect(@dangerfile).to not_report
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,222 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'spec_helper'
4
+
5
+ module Danger
6
+ describe Danger::MilestoneChecker do
7
+ it 'is a plugin' do
8
+ expect(described_class.new(nil)).to be_a Danger::Plugin
9
+ end
10
+
11
+ describe 'with Dangerfile' do
12
+ before do
13
+ @dangerfile = testing_dangerfile
14
+ @plugin = @dangerfile.milestone_checker
15
+ end
16
+
17
+ describe '#check_milestone_set' do
18
+ context 'when there is no milestone set' do
19
+ it "reports a warning when a PR doesn't have a milestone set" do
20
+ allow(@plugin.github).to receive(:pr_json).and_return({})
21
+
22
+ @plugin.check_milestone_set
23
+
24
+ expected_warning = ['PR is not assigned to a milestone.']
25
+ expect(@dangerfile).to report_warnings(expected_warning)
26
+ end
27
+
28
+ it "reports an error when a PR doesn't have a milestone set" do
29
+ allow(@plugin.github).to receive(:pr_json).and_return({})
30
+
31
+ @plugin.check_milestone_set(report_type: :error)
32
+
33
+ expected_error = ['PR is not assigned to a milestone.']
34
+ expect(@dangerfile).to report_errors(expected_error)
35
+ end
36
+ end
37
+
38
+ context 'when there is a milestone set' do
39
+ it 'does nothing when a PR has a milestone set' do
40
+ pr_json = {
41
+ 'milestone' => {
42
+ 'title' => 'Release Day'
43
+ }
44
+ }
45
+
46
+ allow(@plugin.github).to receive(:pr_json).and_return(pr_json)
47
+
48
+ @plugin.check_milestone_set
49
+
50
+ expect(@dangerfile).to not_report
51
+ end
52
+
53
+ it 'does nothing when an error is expected but the PR has a milestone set' do
54
+ pr_json = {
55
+ 'milestone' => {
56
+ 'title' => 'Release Day'
57
+ }
58
+ }
59
+
60
+ allow(@plugin.github).to receive(:pr_json).and_return(pr_json)
61
+
62
+ @plugin.check_milestone_set(report_type: :error)
63
+
64
+ expect(@dangerfile).to not_report
65
+ end
66
+ end
67
+ end
68
+
69
+ describe '#check_milestone_due_date' do
70
+ it 'reports a warning when a PR has a milestone with due date within the warning days threshold' do
71
+ pr_json = {
72
+ 'milestone' => {
73
+ 'title' => 'Release Day',
74
+ 'html_url' => 'https://wp.com',
75
+ 'due_on' => DateTime.parse('2023-06-30T23:59:01Z')
76
+ },
77
+ 'state' => 'open'
78
+ }
79
+
80
+ date_one_day_before_due = DateTime.parse('2023-06-29T23:59:01Z')
81
+
82
+ allow(@plugin.github).to receive(:pr_json).and_return(pr_json)
83
+ allow(DateTime).to receive(:now).and_return(date_one_day_before_due)
84
+
85
+ @plugin.check_milestone_due_date(days_before_due: 5)
86
+
87
+ expected_warning = ["This PR is assigned to the milestone [Release Day](https://wp.com). This milestone is due in less than 5 days.\nPlease make sure to get it merged by then or assign it to a milestone with a later deadline."]
88
+ expect(@dangerfile).to report_warnings(expected_warning)
89
+ end
90
+
91
+ it 'does nothing when a PR has a milestone before the warning days threshold' do
92
+ pr_json = {
93
+ 'milestone' => {
94
+ 'title' => 'Release Day',
95
+ 'html_url' => 'https://wp.com',
96
+ 'due_on' => DateTime.parse('2023-06-30T23:59:01Z')
97
+ },
98
+ 'state' => 'open'
99
+ }
100
+
101
+ more_than_five_days_before_due = DateTime.parse('2023-06-25T23:00:01Z')
102
+
103
+ allow(@plugin.github).to receive(:pr_json).and_return(pr_json)
104
+ allow(DateTime).to receive(:now).and_return(more_than_five_days_before_due)
105
+
106
+ @plugin.check_milestone_due_date(days_before_due: 5)
107
+
108
+ expect(@dangerfile).to not_report
109
+ end
110
+
111
+ it 'reports an error when a PR has a milestone with due date after the warning days threshold' do
112
+ pr_json = {
113
+ 'milestone' => {
114
+ 'title' => 'Release Day',
115
+ 'html_url' => 'https://wp.com',
116
+ 'due_on' => DateTime.parse('2023-06-30T23:59:01Z')
117
+ },
118
+ 'state' => 'open'
119
+ }
120
+
121
+ date_one_day_after_due = DateTime.parse('2023-07-01T23:00:01Z')
122
+
123
+ allow(@plugin.github).to receive(:pr_json).and_return(pr_json)
124
+ allow(DateTime).to receive(:now).and_return(date_one_day_after_due)
125
+
126
+ @plugin.check_milestone_due_date(days_before_due: 5, report_type: :error)
127
+
128
+ expected_warning = ["This PR is assigned to the milestone [Release Day](https://wp.com). The due date for this milestone has already passed.\nPlease assign it to a milestone with a later deadline or check whether the release for this milestone has already been finished."]
129
+ expect(@dangerfile).to report_errors(expected_warning)
130
+ end
131
+
132
+ it 'reports a warning when a PR has a milestone with due date within a custom warning days threshold' do
133
+ pr_json = {
134
+ 'milestone' => {
135
+ 'title' => 'Release Day',
136
+ 'html_url' => 'https://wp.com',
137
+ 'due_on' => DateTime.parse('2023-06-30T23:59:01Z')
138
+ },
139
+ 'state' => 'open'
140
+ }
141
+
142
+ date_nine_days_before_due = DateTime.parse('2023-06-21T23:59:01Z')
143
+
144
+ allow(@plugin.github).to receive(:pr_json).and_return(pr_json)
145
+ allow(DateTime).to receive(:now).and_return(date_nine_days_before_due)
146
+
147
+ @plugin.check_milestone_due_date(days_before_due: 10)
148
+
149
+ expected_warning = ["This PR is assigned to the milestone [Release Day](https://wp.com). This milestone is due in less than 10 days.\nPlease make sure to get it merged by then or assign it to a milestone with a later deadline."]
150
+ expect(@dangerfile).to report_warnings(expected_warning)
151
+ end
152
+
153
+ it 'does nothing when a PR has a milestone without a due date' do
154
+ pr_json = {
155
+ 'milestone' => {
156
+ 'title' => 'Release Day',
157
+ 'html_url' => 'https://wp.com'
158
+ },
159
+ 'state' => 'open'
160
+ }
161
+
162
+ allow(@plugin.github).to receive(:pr_json).and_return(pr_json)
163
+
164
+ @plugin.check_milestone_due_date(days_before_due: 5)
165
+
166
+ expect(@dangerfile).to not_report
167
+ end
168
+
169
+ it 'does nothing when a PR has a milestone but it has already passed the due date' do
170
+ pr_json = {
171
+ 'milestone' => {
172
+ 'title' => 'Release Day',
173
+ 'html_url' => 'https://wp.com',
174
+ 'due_on' => DateTime.parse('2023-06-30T23:59:01Z')
175
+ },
176
+ 'state' => 'closed'
177
+ }
178
+
179
+ allow(@plugin.github).to receive(:pr_json).and_return(pr_json)
180
+
181
+ @plugin.check_milestone_due_date(days_before_due: 5)
182
+
183
+ expect(@dangerfile).to not_report
184
+ end
185
+
186
+ it "reports a warning when asked to do so when a PR doesn't have a milestone set" do
187
+ allow(@plugin.github).to receive(:pr_json).and_return({})
188
+
189
+ @plugin.check_milestone_due_date(days_before_due: 5, report_type_if_no_milestone: :warning)
190
+
191
+ expected_warning = ['PR is not assigned to a milestone.']
192
+ expect(@dangerfile).to report_warnings(expected_warning)
193
+ end
194
+
195
+ it "reports an error when asked to do so when a PR doesn't have a milestone set" do
196
+ allow(@plugin.github).to receive(:pr_json).and_return({})
197
+
198
+ @plugin.check_milestone_due_date(days_before_due: 5, report_type_if_no_milestone: :error)
199
+
200
+ expected_error = ['PR is not assigned to a milestone.']
201
+ expect(@dangerfile).to report_errors(expected_error)
202
+ end
203
+
204
+ it "does nothing when asked to do so when a PR doesn't have a milestone set" do
205
+ allow(@plugin.github).to receive(:pr_json).and_return({})
206
+
207
+ @plugin.check_milestone_due_date(days_before_due: 5, report_type_if_no_milestone: :none)
208
+
209
+ expect(@dangerfile).to not_report
210
+ end
211
+
212
+ it "does nothing when nil is used and a PR doesn't have a milestone set" do
213
+ allow(@plugin.github).to receive(:pr_json).and_return({})
214
+
215
+ @plugin.check_milestone_due_date(days_before_due: 5, report_type_if_no_milestone: nil)
216
+
217
+ expect(@dangerfile).to not_report
218
+ end
219
+ end
220
+ end
221
+ end
222
+ end