shipit-engine 0.32.0 → 0.33.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (176) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/images/magic-solid.svg +1 -0
  3. data/app/assets/javascripts/shipit/repositories_search.js.coffee +60 -0
  4. data/app/assets/javascripts/shipit/{search.js.coffee → stack_search.js.coffee} +0 -0
  5. data/app/assets/stylesheets/_pages/_repositories.scss +148 -0
  6. data/app/assets/stylesheets/_pages/_stacks.scss +19 -0
  7. data/app/assets/stylesheets/shipit.scss +1 -0
  8. data/app/controllers/shipit/api/{pull_requests_controller.rb → merge_requests_controller.rb} +8 -8
  9. data/app/controllers/shipit/api/stacks_controller.rb +14 -1
  10. data/app/controllers/shipit/deploys_controller.rb +2 -2
  11. data/app/controllers/shipit/merge_requests_controller.rb +31 -0
  12. data/app/controllers/shipit/merge_status_controller.rb +15 -15
  13. data/app/controllers/shipit/repositories_controller.rb +74 -0
  14. data/app/controllers/shipit/tasks_controller.rb +4 -4
  15. data/app/helpers/shipit/chunks_helper.rb +2 -2
  16. data/app/helpers/shipit/github_url_helper.rb +8 -0
  17. data/app/helpers/shipit/stacks_helper.rb +4 -0
  18. data/app/jobs/shipit/create_on_github_job.rb +1 -0
  19. data/app/jobs/shipit/destroy_repository_job.rb +24 -0
  20. data/app/jobs/shipit/destroy_stack_job.rb +2 -2
  21. data/app/jobs/shipit/perform_task_job.rb +4 -98
  22. data/app/jobs/shipit/process_merge_requests_job.rb +32 -0
  23. data/app/jobs/shipit/refresh_merge_request_job.rb +11 -0
  24. data/app/models/shipit/anonymous_user.rb +4 -0
  25. data/app/models/shipit/check_run.rb +2 -2
  26. data/app/models/shipit/command_line_user.rb +4 -0
  27. data/app/models/shipit/commit.rb +11 -11
  28. data/app/models/shipit/commit_checks.rb +1 -0
  29. data/app/models/shipit/deploy.rb +1 -0
  30. data/app/models/shipit/deploy_spec.rb +16 -4
  31. data/app/models/shipit/deploy_spec/file_system.rb +11 -5
  32. data/app/models/shipit/hook.rb +2 -0
  33. data/app/models/shipit/merge_request.rb +302 -0
  34. data/app/models/shipit/provisioning_handler.rb +32 -0
  35. data/app/models/shipit/provisioning_handler/base.rb +30 -0
  36. data/app/models/shipit/provisioning_handler/unregistered_provisioning_handler.rb +35 -0
  37. data/app/models/shipit/pull_request.rb +25 -264
  38. data/app/models/shipit/pull_request_assignment.rb +10 -0
  39. data/app/models/shipit/repository.rb +54 -0
  40. data/app/models/shipit/review_stack.rb +116 -0
  41. data/app/models/shipit/review_stack_provisioning_queue.rb +39 -0
  42. data/app/models/shipit/stack.rb +22 -8
  43. data/app/models/shipit/task.rb +56 -7
  44. data/app/models/shipit/task_execution_strategy/base.rb +20 -0
  45. data/app/models/shipit/task_execution_strategy/default.rb +110 -0
  46. data/app/models/shipit/user.rb +6 -1
  47. data/app/models/shipit/webhooks.rb +10 -0
  48. data/app/models/shipit/webhooks/handlers/pull_request/assigned_handler.rb +74 -0
  49. data/app/models/shipit/webhooks/handlers/pull_request/closed_handler.rb +68 -0
  50. data/app/models/shipit/webhooks/handlers/pull_request/edited_handler.rb +74 -0
  51. data/app/models/shipit/webhooks/handlers/pull_request/label_capturing_handler.rb +127 -0
  52. data/app/models/shipit/webhooks/handlers/pull_request/labeled_handler.rb +106 -0
  53. data/app/models/shipit/webhooks/handlers/pull_request/opened_handler.rb +83 -0
  54. data/app/models/shipit/webhooks/handlers/pull_request/reopened_handler.rb +88 -0
  55. data/app/models/shipit/webhooks/handlers/pull_request/review_stack_adapter.rb +103 -0
  56. data/app/models/shipit/webhooks/handlers/pull_request/unlabeled_handler.rb +107 -0
  57. data/app/serializers/shipit/deploy_serializer.rb +6 -0
  58. data/app/serializers/shipit/merge_request_serializer.rb +21 -0
  59. data/app/serializers/shipit/pull_request_serializer.rb +5 -8
  60. data/app/serializers/shipit/review_stack_serializer.rb +7 -0
  61. data/app/serializers/shipit/stack_serializer.rb +7 -6
  62. data/app/serializers/shipit/tail_task_serializer.rb +10 -2
  63. data/app/serializers/shipit/task_serializer.rb +1 -1
  64. data/app/views/shipit/merge_requests/_merge_request.html.erb +29 -0
  65. data/app/views/shipit/{pull_requests → merge_requests}/index.html.erb +2 -2
  66. data/app/views/shipit/merge_requests/merge_requests/_pull_request.html.erb +29 -0
  67. data/app/views/shipit/merge_requests/merge_requests/index.html.erb +20 -0
  68. data/app/views/shipit/merge_status/_merge_queue_button.html.erb +3 -3
  69. data/app/views/shipit/merge_status/backlogged.html.erb +1 -1
  70. data/app/views/shipit/merge_status/failure.html.erb +1 -1
  71. data/app/views/shipit/merge_status/locked.html.erb +1 -1
  72. data/app/views/shipit/merge_status/success.html.erb +2 -2
  73. data/app/views/shipit/repositories/_header.html.erb +19 -0
  74. data/app/views/shipit/repositories/index.html.erb +31 -0
  75. data/app/views/shipit/repositories/new.html.erb +23 -0
  76. data/app/views/shipit/repositories/settings.html.erb +53 -0
  77. data/app/views/shipit/repositories/show.html.erb +30 -0
  78. data/app/views/shipit/stacks/_banners.html.erb +13 -0
  79. data/app/views/shipit/stacks/_header.html.erb +5 -2
  80. data/app/views/shipit/stacks/_stack.html.erb +8 -0
  81. data/app/views/shipit/stacks/index.html.erb +2 -1
  82. data/app/views/shipit/stacks/settings.html.erb +5 -5
  83. data/app/views/shipit/stacks/show.html.erb +1 -1
  84. data/app/views/shipit/tasks/_task_output.html.erb +1 -1
  85. data/config/routes.rb +15 -5
  86. data/db/migrate/20200706145406_add_review_stacks.rb +12 -0
  87. data/db/migrate/20200804144639_rename_pull_request_to_merge_request.rb +7 -0
  88. data/db/migrate/20200804161512_rename_commits_pull_request_id_to_merge_request_id.rb +5 -0
  89. data/db/migrate/20200813134712_recreate_shipit_pull_requests.rb +22 -0
  90. data/db/migrate/20200813194056_create_pull_request_assignments.rb +8 -0
  91. data/db/migrate/20201001125502_add_provision_pr_stacks_flag_to_repositories.rb +7 -0
  92. data/db/migrate/20201008145809_add_retry_attempt_to_tasks.rb +5 -0
  93. data/db/migrate/20201008152744_add_max_retries_to_tasks.rb +5 -0
  94. data/lib/shipit.rb +11 -1
  95. data/lib/shipit/github_app.rb +1 -1
  96. data/lib/shipit/review_stack_commands.rb +8 -0
  97. data/lib/shipit/stack_commands.rb +6 -1
  98. data/lib/shipit/task_commands.rb +1 -0
  99. data/lib/shipit/version.rb +1 -1
  100. data/lib/tasks/cron.rake +11 -2
  101. data/test/controllers/api/{pull_requests_controller_test.rb → merge_requests_controller_test.rb} +12 -12
  102. data/test/controllers/api/outputs_controller_test.rb +1 -0
  103. data/test/controllers/api/rollback_controller_test.rb +1 -1
  104. data/test/controllers/api/stacks_controller_test.rb +21 -1
  105. data/test/controllers/{pull_requests_controller_test.rb → merge_requests_controller_test.rb} +6 -6
  106. data/test/controllers/repositories_controller_test.rb +71 -0
  107. data/test/controllers/stacks_controller_test.rb +9 -1
  108. data/test/controllers/tasks_controller_test.rb +14 -2
  109. data/test/controllers/webhooks_controller_test.rb +1 -1
  110. data/test/dummy/config/application.rb +6 -1
  111. data/test/dummy/config/environments/development.rb +0 -3
  112. data/test/dummy/config/environments/test.rb +0 -5
  113. data/test/dummy/db/schema.rb +52 -14
  114. data/test/dummy/db/seeds.rb +1 -1
  115. data/test/fixtures/payloads/check_suite_master.json +2 -2
  116. data/test/fixtures/payloads/invalid_pull_request.json +117 -0
  117. data/test/fixtures/payloads/provision_disabled_pull_request.json +454 -0
  118. data/test/fixtures/payloads/pull_request_assigned.json +480 -0
  119. data/test/fixtures/payloads/pull_request_closed.json +454 -0
  120. data/test/fixtures/payloads/pull_request_labeled.json +461 -0
  121. data/test/fixtures/payloads/pull_request_opened.json +454 -0
  122. data/test/fixtures/payloads/pull_request_reopened.json +454 -0
  123. data/test/fixtures/payloads/pull_request_unlabeled.json +454 -0
  124. data/test/fixtures/payloads/pull_request_with_no_repo.json +454 -0
  125. data/test/fixtures/shipit/commits.yml +15 -2
  126. data/test/fixtures/shipit/merge_requests.yml +141 -0
  127. data/test/fixtures/shipit/pull_request_assignments.yml +3 -0
  128. data/test/fixtures/shipit/pull_requests.yml +10 -131
  129. data/test/fixtures/shipit/repositories.yml +1 -0
  130. data/test/fixtures/shipit/stacks.yml +145 -0
  131. data/test/fixtures/shipit/statuses.yml +9 -0
  132. data/test/fixtures/shipit/tasks.yml +3 -0
  133. data/test/fixtures/shipit/users.yml +7 -0
  134. data/test/helpers/payloads_helper.rb +4 -0
  135. data/test/jobs/chunk_rollup_job_test.rb +15 -1
  136. data/test/jobs/destroy_repository_job_test.rb +27 -0
  137. data/test/jobs/perform_task_job_test.rb +8 -8
  138. data/test/jobs/{merge_pull_requests_job_test.rb → process_merge_requests_job_test.rb} +18 -18
  139. data/test/lib/shipit/deploy_commands_test.rb +16 -0
  140. data/test/lib/shipit/task_commands_test.rb +17 -0
  141. data/test/models/commits_test.rb +22 -13
  142. data/test/models/deploy_spec_test.rb +57 -24
  143. data/test/models/deploys_test.rb +148 -14
  144. data/test/models/{pull_request_test.rb → merge_request_test.rb} +30 -30
  145. data/test/models/pull_request_assignment_test.rb +16 -0
  146. data/test/models/shipit/provisioning_handler/base_test.rb +33 -0
  147. data/test/models/shipit/provisioning_handler/unregistered_provisioning_handler_test.rb +49 -0
  148. data/test/models/shipit/provisioning_handler_test.rb +64 -0
  149. data/test/models/shipit/pull_request_test.rb +52 -0
  150. data/test/models/shipit/repository_test.rb +5 -1
  151. data/test/models/shipit/review_stack_provision_status_test.rb +77 -0
  152. data/test/models/shipit/review_stack_provisioning_queue_test.rb +63 -0
  153. data/test/models/shipit/review_stack_test.rb +59 -0
  154. data/test/models/{stacks_test.rb → shipit/stacks_test.rb} +10 -4
  155. data/test/models/shipit/webhooks/handlers/pull_request/assigned_handler_test.rb +45 -0
  156. data/test/models/shipit/webhooks/handlers/pull_request/closed_handler_test.rb +192 -0
  157. data/test/models/shipit/webhooks/handlers/pull_request/edited_handler_test.rb +47 -0
  158. data/test/models/shipit/webhooks/handlers/pull_request/label_capturing_handler_test.rb +209 -0
  159. data/test/models/shipit/webhooks/handlers/pull_request/labeled_handler_test.rb +332 -0
  160. data/test/models/shipit/webhooks/handlers/pull_request/opened_handler_test.rb +238 -0
  161. data/test/models/shipit/webhooks/handlers/pull_request/reopened_handler_test.rb +282 -0
  162. data/test/models/shipit/webhooks/handlers/pull_request/review_stack_adapter_test.rb +83 -0
  163. data/test/models/shipit/webhooks/handlers/pull_request/unlabeled_handler_test.rb +324 -0
  164. data/test/models/shipit/{wehbooks → webhooks}/handlers_test.rb +0 -0
  165. data/test/models/tasks_test.rb +44 -3
  166. data/test/serializers/shipit/pull_request_serializer_test.rb +29 -0
  167. data/test/unit/command_test.rb +3 -3
  168. data/test/unit/github_url_helper_test.rb +5 -0
  169. data/test/unit/shipit_task_execution_strategy_test.rb +47 -0
  170. metadata +260 -154
  171. data/app/controllers/shipit/pull_requests_controller.rb +0 -31
  172. data/app/jobs/shipit/merge_pull_requests_job.rb +0 -32
  173. data/app/jobs/shipit/refresh_pull_request_job.rb +0 -11
  174. data/app/views/shipit/pull_requests/_pull_request.html.erb +0 -29
  175. data/test/fixtures/shipit/output_chunks.yml +0 -47
  176. data/test/models/output_chunk_test.rb +0 -21
@@ -5,6 +5,7 @@ module Shipit
5
5
  class DeploysTest < ActiveSupport::TestCase
6
6
  def setup
7
7
  @deploy = shipit_deploys(:shipit)
8
+ @deploy.write("dummy output")
8
9
  @deploy.pid = 42
9
10
  @stack = shipit_stacks(:shipit)
10
11
  @user = shipit_users(:walrus)
@@ -60,6 +61,146 @@ module Shipit
60
61
  assert_equal [], @deploy.commits
61
62
  end
62
63
 
64
+ test "deploys retry up to limit upon timeout when configured" do
65
+ runnable_deploy = shipit_deploys(:shipit_pending)
66
+ deploy_stack = runnable_deploy.stack
67
+
68
+ Shipit::Deploy.any_instance.expects(:acquire_git_cache_lock).twice
69
+ .raises(Shipit::Command::TimedOut, 'Deploy timed out')
70
+ .then.raises(Shipit::Command::Error, "Second command error failure")
71
+
72
+ perform_enqueued_jobs(only: Shipit::PerformTaskJob) do
73
+ runnable_deploy.enqueue
74
+ end
75
+ assert_performed_jobs 2
76
+
77
+ runnable_deploy.reload
78
+ assert_equal 'timedout', runnable_deploy.status
79
+
80
+ retried_deploy = deploy_stack.deploys.last
81
+ assert_not_equal runnable_deploy.id, retried_deploy.id
82
+ assert_equal runnable_deploy.since_commit, retried_deploy.since_commit
83
+ assert_equal runnable_deploy.until_commit, retried_deploy.until_commit
84
+ assert_equal 'failed', retried_deploy.status
85
+ assert_equal 1, retried_deploy.retry_attempt
86
+ end
87
+
88
+ test "deploys retry up to limit upon failure when configured" do
89
+ runnable_deploy = shipit_deploys(:shipit_pending)
90
+ deploy_stack = runnable_deploy.stack
91
+
92
+ Shipit::Deploy.any_instance.expects(:acquire_git_cache_lock).twice
93
+ .raises(Shipit::Command::Error, 'Deploy failed')
94
+ .then.raises(Shipit::Command::Error, "Second deploy failed")
95
+
96
+ perform_enqueued_jobs(only: Shipit::PerformTaskJob) do
97
+ runnable_deploy.enqueue
98
+ end
99
+ assert_performed_jobs 2
100
+
101
+ runnable_deploy.reload
102
+ assert_equal 'failed', runnable_deploy.status
103
+
104
+ retried_deploy = deploy_stack.deploys.last
105
+ assert_not_equal runnable_deploy.id, retried_deploy.id
106
+ assert_equal runnable_deploy.since_commit, retried_deploy.since_commit
107
+ assert_equal runnable_deploy.until_commit, retried_deploy.until_commit
108
+ assert_equal 'failed', retried_deploy.status
109
+ assert_equal 1, retried_deploy.retry_attempt
110
+ end
111
+
112
+ test "deploys retry up to limit upon error when configured" do
113
+ runnable_deploy = shipit_deploys(:shipit_pending)
114
+ deploy_stack = runnable_deploy.stack
115
+
116
+ Shipit::Deploy.any_instance.expects(:acquire_git_cache_lock).twice
117
+ .raises(StandardError, 'Deploy failed')
118
+ .then.raises(StandardError, "Second deploy failed")
119
+
120
+ perform_enqueued_jobs(only: Shipit::PerformTaskJob) do
121
+ runnable_deploy.enqueue
122
+ end
123
+ assert_performed_jobs 2
124
+
125
+ runnable_deploy.reload
126
+ assert_equal 'error', runnable_deploy.status
127
+
128
+ retried_deploy = deploy_stack.deploys.last
129
+ assert_not_equal runnable_deploy.id, retried_deploy.id
130
+ assert_equal runnable_deploy.since_commit, retried_deploy.since_commit
131
+ assert_equal runnable_deploy.until_commit, retried_deploy.until_commit
132
+ assert_equal 'error', retried_deploy.status
133
+ assert_equal 1, retried_deploy.retry_attempt
134
+ end
135
+
136
+ test "deploys do not retry upon timeout when not configured" do
137
+ runnable_deploy = shipit_deploys(:shipit_pending)
138
+ runnable_deploy.update!(retry_attempt: 0, max_retries: 0)
139
+
140
+ Shipit::Deploy.any_instance.expects(:acquire_git_cache_lock)
141
+ .raises(Shipit::Command::TimedOut, 'Deploy timed out')
142
+
143
+ perform_enqueued_jobs(only: Shipit::PerformTaskJob) do
144
+ runnable_deploy.enqueue
145
+ end
146
+ assert_performed_jobs 1
147
+
148
+ runnable_deploy.reload
149
+
150
+ assert_equal 'timedout', runnable_deploy.status
151
+ end
152
+
153
+ test "rollbacks retry on timeouts if configured" do
154
+ deploy = shipit_deploys(:shipit)
155
+ deploy_stack = deploy.stack
156
+
157
+ DeploySpec.any_instance.expects(:retries_on_rollback).returns(1)
158
+
159
+ Shipit::Command.any_instance.expects(:run).twice
160
+ .raises(Shipit::Command::TimedOut, 'Rollback timed out')
161
+ .then.raises(Shipit::Command::Error, "Second command error failure")
162
+
163
+ first_rollback = nil
164
+
165
+ perform_enqueued_jobs(only: Shipit::PerformTaskJob) do
166
+ first_rollback = deploy.trigger_rollback(@user, force: true)
167
+ end
168
+ assert_performed_jobs 2
169
+
170
+ first_rollback.reload
171
+
172
+ assert_equal 'timedout', first_rollback.status
173
+ retried_rollback = deploy_stack.deploys_and_rollbacks.last
174
+
175
+ assert_not_equal first_rollback.id, retried_rollback.id
176
+ assert_equal first_rollback.since_commit, retried_rollback.since_commit
177
+ assert_equal first_rollback.until_commit, retried_rollback.until_commit
178
+ assert_equal 'failed', retried_rollback.status
179
+ assert_equal 1, retried_rollback.max_retries
180
+ end
181
+
182
+ test "rollbacks do not retry if not configured" do
183
+ deploy_stack = @deploy.stack
184
+
185
+ DeploySpec.any_instance.expects(:retries_on_rollback).returns(0)
186
+
187
+ Shipit::Command.any_instance.expects(:run).once
188
+ .raises(Shipit::Command::TimedOut, 'Rollback timed out')
189
+ .then.raises(Shipit::Command::Error, "Second command error failure")
190
+
191
+ first_rollback = nil
192
+
193
+ perform_enqueued_jobs(only: Shipit::PerformTaskJob) do
194
+ first_rollback = @deploy.trigger_rollback(@user, force: true)
195
+ end
196
+ assert_performed_jobs 1
197
+
198
+ first_rollback.reload
199
+
200
+ assert_equal 'timedout', first_rollback.status
201
+ assert_equal first_rollback, deploy_stack.deploys_and_rollbacks.last
202
+ end
203
+
63
204
  test "additions and deletions are denormalized on before create" do
64
205
  stack = shipit_stacks(:shipit)
65
206
  first = shipit_commits(:first)
@@ -200,7 +341,7 @@ module Shipit
200
341
  since_commit: shipit.commits.first,
201
342
  until_commit: shipit.commits.last,
202
343
  )
203
- deploy.stubs(:pull_request_head_for_commit).returns(nil)
344
+ deploy.stubs(:merge_request_head_for_commit).returns(nil)
204
345
 
205
346
  expect_event(deploy)
206
347
  deploy.save!
@@ -263,9 +404,9 @@ module Shipit
263
404
  assert_in_delta @deploy.ended_at, @stack.last_deployed_at, 2
264
405
  end
265
406
 
266
- test "transitioning to success schedule a MergePullRequests job" do
407
+ test "transitioning to success schedule a MergeMergeRequests job" do
267
408
  @deploy = shipit_deploys(:shipit_running)
268
- assert_enqueued_with(job: MergePullRequestsJob, args: [@deploy.stack]) do
409
+ assert_enqueued_with(job: ProcessMergeRequestsJob, args: [@deploy.stack]) do
269
410
  @deploy.complete!
270
411
  end
271
412
  end
@@ -393,7 +534,7 @@ module Shipit
393
534
  # Check that the next item in the series is 1 greater than the last.
394
535
  def assert_generated_record_ids_are_sequential(record_id_series)
395
536
  record_id_series[0..-2].each_with_index do |id_element, index|
396
- assert_equal id_element + 1, record_id_series[index + 1]
537
+ assert_equal(id_element + 1, record_id_series[index + 1])
397
538
  end
398
539
  end
399
540
 
@@ -699,20 +840,13 @@ module Shipit
699
840
  assert_predicate @deploy, :error?
700
841
  end
701
842
 
702
- test "destroy deletes the related output chunks" do
703
- assert_difference -> { @deploy.chunks.count }, -@deploy.chunks.count do
704
- @deploy.destroy
705
- end
706
- end
707
-
708
- test "#chunk_output joins all chunk test if logs not rolled up" do
709
- assert_equal @deploy.chunks.count, @deploy.chunks.count
710
- assert_equal @deploy.chunks.pluck(:text).join, @deploy.chunk_output
843
+ test "#chunk_output fetches from Redis if logs not rolled up" do
844
+ assert_equal Shipit.redis.get(@deploy.send(:output_key)), @deploy.chunk_output
711
845
  refute @deploy.rolled_up
712
846
  end
713
847
 
714
848
  test "#chunk_output returns logs from records if rolled up" do
715
- expected_output = @deploy.chunks.pluck(:text).join
849
+ expected_output = Shipit.redis.get(@deploy.send(:output_key))
716
850
  @deploy.rollup_chunks
717
851
 
718
852
  assert_no_queries do
@@ -2,24 +2,24 @@
2
2
  require 'test_helper'
3
3
 
4
4
  module Shipit
5
- class PullRequestTest < ActiveSupport::TestCase
5
+ class MergeRequestTest < ActiveSupport::TestCase
6
6
  setup do
7
7
  @stack = shipit_stacks(:shipit)
8
- @pr = shipit_pull_requests(:shipit_pending)
8
+ @pr = shipit_merge_requests(:shipit_pending)
9
9
  @user = shipit_users(:walrus)
10
10
  end
11
11
 
12
12
  test ".request_merge! creates a record and schedule a refresh" do
13
- pull_request = nil
14
- assert_enqueued_with(job: RefreshPullRequestJob) do
15
- pull_request = PullRequest.request_merge!(@stack, 64, @user)
13
+ merge_request = nil
14
+ assert_enqueued_with(job: RefreshMergeRequestJob) do
15
+ merge_request = MergeRequest.request_merge!(@stack, 64, @user)
16
16
  end
17
- assert_predicate pull_request, :persisted?
17
+ assert_predicate merge_request, :persisted?
18
18
  end
19
19
 
20
20
  test ".request_merge! only track pull requests once" do
21
- assert_difference -> { PullRequest.count }, +1 do
22
- 5.times { PullRequest.request_merge!(@stack, 999, @user) }
21
+ assert_difference -> { MergeRequest.count }, +1 do
22
+ 5.times { MergeRequest.request_merge!(@stack, 999, @user) }
23
23
  end
24
24
  end
25
25
 
@@ -27,7 +27,7 @@ module Shipit
27
27
  original_merge_requested_at = @pr.merge_requested_at
28
28
  @pr.cancel!
29
29
  assert_predicate @pr, :canceled?
30
- PullRequest.request_merge!(@stack, @pr.number, @user)
30
+ MergeRequest.request_merge!(@stack, @pr.number, @user)
31
31
  assert_predicate @pr.reload, :pending?
32
32
  assert_not_equal original_merge_requested_at, @pr.merge_requested_at
33
33
  assert_in_delta Time.now.utc, @pr.merge_requested_at, 2
@@ -37,7 +37,7 @@ module Shipit
37
37
  original_merge_requested_at = @pr.merge_requested_at
38
38
  @pr.reject!('merge_conflict')
39
39
  assert_predicate @pr, :rejected?
40
- PullRequest.request_merge!(@stack, @pr.number, @user)
40
+ MergeRequest.request_merge!(@stack, @pr.number, @user)
41
41
  assert_predicate @pr.reload, :pending?
42
42
  assert_not_equal original_merge_requested_at, @pr.merge_requested_at
43
43
  assert_in_delta Time.now.utc, @pr.merge_requested_at, 2
@@ -48,29 +48,29 @@ module Shipit
48
48
  original_merge_requested_at = @pr.merge_requested_at
49
49
  @pr.revalidate!
50
50
  assert_predicate @pr, :revalidating?
51
- PullRequest.request_merge!(@stack, @pr.number, @user)
51
+ MergeRequest.request_merge!(@stack, @pr.number, @user)
52
52
  assert_predicate @pr.reload, :pending?
53
53
  assert_equal original_merge_requested_at, @pr.merge_requested_at
54
54
  end
55
55
 
56
56
  test ".extract_number can get a pull request number from different formats" do
57
- assert_equal 42, PullRequest.extract_number(@stack, '42')
58
- assert_equal 42, PullRequest.extract_number(@stack, '#42')
59
- assert_equal 42, PullRequest.extract_number(@stack, 'https://github.com/Shopify/shipit-engine/pull/42')
57
+ assert_equal 42, MergeRequest.extract_number(@stack, '42')
58
+ assert_equal 42, MergeRequest.extract_number(@stack, '#42')
59
+ assert_equal 42, MergeRequest.extract_number(@stack, 'https://github.com/Shopify/shipit-engine/pull/42')
60
60
 
61
- assert_nil PullRequest.extract_number(@stack, 'https://github.com/ACME/shipit-engine/pull/42')
61
+ assert_nil MergeRequest.extract_number(@stack, 'https://github.com/ACME/shipit-engine/pull/42')
62
62
 
63
63
  Shipit.github.expects(:domain).returns('github.acme.com').at_least_once
64
- assert_equal 42, PullRequest.extract_number(@stack, 'https://github.acme.com/Shopify/shipit-engine/pull/42')
65
- assert_nil PullRequest.extract_number(@stack, 'https://github.com/Shopify/shipit-engine/pull/42')
64
+ assert_equal 42, MergeRequest.extract_number(@stack, 'https://github.acme.com/Shopify/shipit-engine/pull/42')
65
+ assert_nil MergeRequest.extract_number(@stack, 'https://github.com/Shopify/shipit-engine/pull/42')
66
66
  end
67
67
 
68
68
  test "refresh! pulls state from GitHub" do
69
- pull_request = shipit_pull_requests(:shipit_fetching)
69
+ merge_request = shipit_merge_requests(:shipit_fetching)
70
70
 
71
71
  head_sha = '64b3833d39def7ec65b57b42f496eb27ab4980b6'
72
72
  base_sha = 'ba7ab50e02286f7d6c60c1ef75258133dd9ea763'
73
- Shipit.github.api.expects(:pull_request).with(@stack.github_repo_name, pull_request.number).returns(
73
+ Shipit.github.api.expects(:pull_request).with(@stack.github_repo_name, merge_request.number).returns(
74
74
  stub(
75
75
  id: 4_857_578,
76
76
  url: 'https://api.github.com/repos/Shopify/shipit-engine/pulls/64',
@@ -125,15 +125,15 @@ module Shipit
125
125
  created_at: 1.day.ago,
126
126
  )])
127
127
 
128
- pull_request.refresh!
128
+ merge_request.refresh!
129
129
 
130
- assert_predicate pull_request, :mergeable?
131
- assert_predicate pull_request, :pending?
132
- assert_equal 'super-branch', pull_request.branch
130
+ assert_predicate merge_request, :mergeable?
131
+ assert_predicate merge_request, :pending?
132
+ assert_equal 'super-branch', merge_request.branch
133
133
 
134
- assert_not_nil pull_request.head
135
- assert_predicate pull_request.head, :detached?
136
- assert_predicate pull_request.head, :success?
134
+ assert_not_nil merge_request.head
135
+ assert_predicate merge_request.head, :detached?
136
+ assert_predicate merge_request.head, :success?
137
137
  end
138
138
 
139
139
  test "#reject! records the reason" do
@@ -216,11 +216,11 @@ module Shipit
216
216
  assert_equal 'requires_rebase', @pr.rejection_reason
217
217
  end
218
218
 
219
- test "#merge! raises a PullRequest::NotReady if the PR isn't mergeable yet" do
219
+ test "#merge! raises a MergeRequest::NotReady if the PR isn't mergeable yet" do
220
220
  @pr.update!(mergeable: nil)
221
221
 
222
222
  assert_predicate @pr, :not_mergeable_yet?
223
- assert_raises PullRequest::NotReady do
223
+ assert_raises MergeRequest::NotReady do
224
224
  @pr.merge!
225
225
  end
226
226
  @pr.reload
@@ -234,8 +234,8 @@ module Shipit
234
234
  params = job.arguments.first
235
235
  assert_equal 'merge', params[:event]
236
236
  assert_json 'status', 'rejected', document: params[:payload]
237
- assert_json 'pull_request.rejection_reason', 'merge_conflict', document: params[:payload]
238
- assert_json 'pull_request.number', @pr.number, document: params[:payload]
237
+ assert_json 'merge_request.rejection_reason', 'merge_conflict', document: params[:payload]
238
+ assert_json 'merge_request.number', @pr.number, document: params[:payload]
239
239
  end
240
240
 
241
241
  test "#merge! doesnt delete the branch if there are open PRs against it" do
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ module Shipit
6
+ class PullRequestAssignmentTest < ActiveSupport::TestCase
7
+ setup do
8
+ @assignment = shipit_pull_request_assignments(:walrus_shopify_developers)
9
+ end
10
+
11
+ test "no duplicates are accepted" do
12
+ assignment = PullRequestAssignment.new(user: @assignment.user, pull_request: @assignment.pull_request)
13
+ refute assignment.valid?
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ module Shipit
6
+ module ProvisioningHandler
7
+ class BaseTest < ActiveSupport::TestCase
8
+ test "provides a default #up handler" do
9
+ assert(
10
+ handler.respond_to?(:up),
11
+ "expected #{handler.class.name} to provide a default #up handler"
12
+ )
13
+ end
14
+
15
+ test "provides a default #down handler" do
16
+ assert(
17
+ handler.respond_to?(:down),
18
+ "expected #{handler.class.name} to provide a default #down handler"
19
+ )
20
+ end
21
+
22
+ test "provides a default #provision? handler" do
23
+ assert_equal true, handler.provision?
24
+ end
25
+
26
+ private
27
+
28
+ def handler
29
+ ProvisioningHandler::Base.new(mock("Stack"))
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ module Shipit
6
+ module ProvisioningHandler
7
+ class UnregisteredProvisioningHandlerTest < ActiveSupport::TestCase
8
+ test "#up stops transitions" do
9
+ stack = shipit_stacks(:shipit)
10
+ stack.update(provision_status: :deprovisioned)
11
+
12
+ assert_throws :halt do
13
+ UnregisteredProvisioningHandler.new(stack).up
14
+ end
15
+ end
16
+
17
+ test "#up locks the stack" do
18
+ stack = shipit_stacks(:shipit)
19
+ stack.update(provision_status: :deprovisioned)
20
+
21
+ assert_changes -> { stack.locked? }, from: false, to: true do
22
+ catch :halt do
23
+ UnregisteredProvisioningHandler.new(stack).up
24
+ end
25
+ end
26
+ end
27
+
28
+ test "#down stops transitions" do
29
+ stack = shipit_stacks(:shipit)
30
+ stack.update(provision_status: :deprovisioned)
31
+
32
+ assert_throws :halt do
33
+ UnregisteredProvisioningHandler.new(stack).down
34
+ end
35
+ end
36
+
37
+ test "#down prevents transitions" do
38
+ stack = shipit_stacks(:shipit)
39
+ stack.update(provision_status: :deprovisioned)
40
+
41
+ assert_changes -> { stack.locked? }, from: false, to: true do
42
+ catch :halt do
43
+ UnregisteredProvisioningHandler.new(stack).down
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ module Shipit
6
+ class StackProvisioningHandlerTest < ActiveSupport::TestCase
7
+ teardown do
8
+ Shipit::ProvisioningHandler.reset_registry!
9
+ end
10
+
11
+ test "uses the no-op handler as default when no default handler is registered" do
12
+ assert_equal Shipit::ProvisioningHandler::Base, Shipit::ProvisioningHandler.default
13
+ end
14
+
15
+ test "allows registration of a default handler" do
16
+ mock_handler = mock("Mock Provisioning Handler")
17
+
18
+ Shipit::ProvisioningHandler.default = mock_handler
19
+
20
+ assert_equal mock_handler, Shipit::ProvisioningHandler.default
21
+ end
22
+
23
+ test "UnregisteredProvisioningHandler is returned when an attempt to fetch an unregistered handler is made" do
24
+ unregistered_handler = mock("Mock Provisioning Handler")
25
+
26
+ assert_equal(
27
+ Shipit::ProvisioningHandler::UnregisteredProvisioningHandler,
28
+ Shipit::ProvisioningHandler.fetch(unregistered_handler)
29
+ )
30
+ end
31
+
32
+ test "registers handlers so they become fetchable" do
33
+ mock_handler = mock("Mock Provisioning Handler")
34
+
35
+ Shipit::ProvisioningHandler.register(mock_handler)
36
+
37
+ assert_equal mock_handler, Shipit::ProvisioningHandler.fetch(mock_handler.to_s)
38
+ end
39
+
40
+ test "handlers are called during provisioning" do
41
+ stack = shipit_stacks(:review_stack)
42
+ stack.update(
43
+ provision_status: :deprovisioned
44
+ )
45
+ handler = Shipit::ProvisioningHandler.default
46
+
47
+ handler.any_instance.expects(:up)
48
+
49
+ assert stack.provision!, "stack should have provisioned."
50
+ end
51
+
52
+ test "handlers are called during deprovisioning" do
53
+ stack = shipit_stacks(:review_stack)
54
+ stack.update(
55
+ provision_status: :provisioned
56
+ )
57
+ handler = Shipit::ProvisioningHandler.default
58
+
59
+ handler.any_instance.expects(:down)
60
+
61
+ assert stack.deprovision!, "stack should have deprovisioned."
62
+ end
63
+ end
64
+ end