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
@@ -0,0 +1,238 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ module Shipit
6
+ module Webhooks
7
+ module Handlers
8
+ module PullRequest
9
+ class OpenedHandlerTest < ActiveSupport::TestCase
10
+ test "validates payload" do
11
+ assert_raise(StandardError) { OpenedHandler.new(payload_parsed(:invalid_pull_request)) }
12
+ end
13
+
14
+ test "ignores irrelevant PR actions" do
15
+ assert_no_difference -> { Shipit::Stack.count } do
16
+ OpenedHandler.new(payload_parsed(:pull_request_opened).merge(action: "assigned")).process
17
+ end
18
+ end
19
+
20
+ test "does not create stacks for repos that are not tracked" do
21
+ assert_no_difference -> { Shipit::Stack.count } do
22
+ OpenedHandler.new(payload_parsed(:pull_request_with_no_repo)).process
23
+ end
24
+ end
25
+
26
+ test "does not create a stack when the Pull Request data can be saved" do
27
+ Shipit::PullRequest
28
+ .any_instance
29
+ .expects(:update!)
30
+ .raises(ActiveRecord::StatementInvalid)
31
+
32
+ assert_no_difference -> { Shipit::Stack.count } do
33
+ OpenedHandler.new(payload_parsed(:pull_request_opened)).process
34
+ rescue ActiveRecord::StatementInvalid # We expect this to be raised, so it shouldn't fail the test
35
+ end
36
+ end
37
+
38
+ test "creates stacks for repos that are tracked" do
39
+ assert_difference -> { Shipit::Stack.count } do
40
+ OpenedHandler.new(payload_parsed(:pull_request_opened)).process
41
+ end
42
+ end
43
+
44
+ test "creates Shipit::Users when they're not already present" do
45
+ github_user = stub(
46
+ id: 42,
47
+ name: "Somenew Userlogin",
48
+ login: "some-new-user-login",
49
+ email: "somenewuser@login.com",
50
+ avatar_url: "https://avatars.githubusercontent.com/u/42?v=3",
51
+ url: "https://api.github.com/user/some-new-user-login"
52
+ )
53
+ Shipit.github.api.expects(:user).with("some-new-user-login")
54
+ .returns(github_user)
55
+ payload = payload_parsed(:pull_request_opened)
56
+ payload["pull_request"]["user"]["login"] = github_user.login
57
+
58
+ OpenedHandler.new(payload).process
59
+
60
+ user = Shipit::User.find_by(login: "some-new-user-login")
61
+ assert_equal github_user.login, user.login
62
+ assert_equal github_user.name, user.name
63
+ assert_equal github_user.email, user.email
64
+ assert_equal github_user.url, user.api_url
65
+ assert_equal github_user.avatar_url, user.avatar_url
66
+ end
67
+
68
+ test "does not create Shipit::Users when they're already present" do
69
+ payload = payload_parsed(:pull_request_opened)
70
+ user_login = payload["pull_request"]["user"]["login"]
71
+
72
+ assert_no_difference -> { Shipit::User.where(login: user_login).count } do
73
+ OpenedHandler.new(payload).process
74
+ end
75
+ end
76
+
77
+ test "auto-created stack should have default configuration values" do
78
+ payload = payload_parsed(:pull_request_opened)
79
+ OpenedHandler.new(payload).process
80
+ stack = shipit_repositories(:shipit).stacks.last
81
+ assert_equal stack.environment, "pr2"
82
+ assert_equal stack.continuous_deployment, false
83
+ assert_equal stack.ignore_ci, false
84
+ assert_equal stack.branch, payload_parsed(:pull_request_opened)["pull_request"]["head"]["ref"]
85
+ assert_pending_provision(stack)
86
+ end
87
+
88
+ test "auto-created stack should have pull request assigned" do
89
+ payload = payload_parsed(:pull_request_opened)
90
+
91
+ assert_difference -> { Shipit::PullRequest.count } do
92
+ OpenedHandler.new(payload).process
93
+ end
94
+ end
95
+
96
+ test "only provision stacks for repos with auto-provisioning enabled" do
97
+ repository = shipit_repositories(:shipit)
98
+ configure_provisioning_behavior(
99
+ repository: repository,
100
+ provisioning_enabled: false,
101
+ behavior: :allow_all
102
+ )
103
+
104
+ assert_no_difference -> { Shipit::Stack.count } do
105
+ OpenedHandler.new(payload_parsed(:provision_disabled_pull_request)).process
106
+ end
107
+ end
108
+
109
+ test "ignored duplicate deliveries" do
110
+ OpenedHandler.new(payload_parsed(:pull_request_opened)).process
111
+ assert_no_difference -> { Shipit::Stack.count } do
112
+ OpenedHandler.new(payload_parsed(:pull_request_opened)).process
113
+ end
114
+ end
115
+
116
+ test "creates stacks for repos that allow_all" do
117
+ repository = shipit_repositories(:shipit)
118
+ configure_provisioning_behavior(
119
+ repository: repository,
120
+ behavior: :allow_all,
121
+ label: "pull-requests-label"
122
+ )
123
+
124
+ assert_difference -> { Shipit::Stack.count } do
125
+ OpenedHandler.new(payload_parsed(:pull_request_opened)).process
126
+ end
127
+ end
128
+
129
+ test "creates stacks for repos that allow_with_label when label is present" do
130
+ repository = shipit_repositories(:shipit)
131
+ configure_provisioning_behavior(
132
+ repository: repository,
133
+ behavior: :allow_with_label,
134
+ label: "pull-requests-label"
135
+ )
136
+ payload = payload_parsed(:pull_request_opened)
137
+ payload["pull_request"]["labels"] << { "name" => "pull-requests-label" }
138
+
139
+ assert_difference -> { Shipit::Stack.count } do
140
+ OpenedHandler.new(payload).process
141
+ end
142
+ end
143
+
144
+ test "does not create stacks for repos that allow_with_label when label is absent" do
145
+ repository = shipit_repositories(:shipit)
146
+ configure_provisioning_behavior(
147
+ repository: repository,
148
+ behavior: :allow_with_label,
149
+ label: "pull-requests-label"
150
+ )
151
+ payload = payload_parsed(:pull_request_opened)
152
+ payload["pull_request"]["labels"] = []
153
+
154
+ assert_no_difference -> { Shipit::Stack.count } do
155
+ OpenedHandler.new(payload).process
156
+ end
157
+ end
158
+
159
+ test "create stacks for repos what prevent_with_label when label is absent" do
160
+ repository = shipit_repositories(:shipit)
161
+ configure_provisioning_behavior(
162
+ repository: repository,
163
+ behavior: :prevent_with_label,
164
+ label: "pull-requests-label"
165
+ )
166
+ payload = payload_parsed(:pull_request_opened)
167
+ payload["pull_request"]["labels"] = []
168
+
169
+ assert_difference -> { Shipit::Stack.count } do
170
+ OpenedHandler.new(payload).process
171
+ end
172
+ end
173
+
174
+ test "does not create stacks for repos what prevent_with_label when label is present" do
175
+ repository = shipit_repositories(:shipit)
176
+ configure_provisioning_behavior(
177
+ repository: repository,
178
+ behavior: :prevent_with_label,
179
+ label: "pull-requests-label"
180
+ )
181
+ payload = payload_parsed(:pull_request_opened)
182
+ payload["pull_request"]["labels"] << { "name" => "pull-requests-label" }
183
+
184
+ assert_no_difference -> { Shipit::Stack.count } do
185
+ OpenedHandler.new(payload).process
186
+ end
187
+ end
188
+
189
+ def configure_provisioning_behavior(repository:, provisioning_enabled: true, behavior: :allow_all, label: nil)
190
+ repository.review_stacks_enabled = provisioning_enabled
191
+ repository.provisioning_behavior = behavior
192
+ repository.provisioning_label_name = label
193
+ repository.save!
194
+
195
+ repository
196
+ end
197
+
198
+ def assert_pending_provision(stack)
199
+ stack.reload
200
+
201
+ assert(stack.awaiting_provision?, "Stack #{stack.environment} should be in the provisioning queue")
202
+ assert(stack.deprovisioned?, "Stack #{stack.environment} should be pending provision")
203
+ end
204
+
205
+ setup do
206
+ Shipit.github.api.stubs(:commit)
207
+ .with("shopify/shipit-engine", "ec26c3e57ca3a959ca5aad62de7213c562f8c821")
208
+ .returns(
209
+ resource(
210
+ {
211
+ sha: "ec26c3e57ca3a959ca5aad62de7213c562f8c821",
212
+ commit: {
213
+ author: {
214
+ name: "Codertocat",
215
+ email: "21031067+Codertocat@users.noreply.github.com",
216
+ date: "2019-05-15 15:20:30",
217
+ },
218
+ committer: {
219
+ name: "Codertocat",
220
+ email: "21031067+Codertocat@users.noreply.github.com",
221
+ date: "2019-05-15 15:20:30",
222
+ },
223
+ message: "Update README.md",
224
+ },
225
+ stats: {
226
+ total: 2,
227
+ additions: 1,
228
+ deletions: 1,
229
+ },
230
+ }
231
+ )
232
+ )
233
+ end
234
+ end
235
+ end
236
+ end
237
+ end
238
+ end
@@ -0,0 +1,282 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ module Shipit
6
+ module Webhooks
7
+ module Handlers
8
+ module PullRequest
9
+ class ReopenedHandlerTest < ActiveSupport::TestCase
10
+ test "validates payload" do
11
+ assert_raise(StandardError) { Shipit::Webhooks::Handlers::PullRequest::ReopenedHandler.new(payload_parsed(:invalid_pull_request)) }
12
+ end
13
+
14
+ test "ignores irrelevant PR actions" do
15
+ assert_no_difference -> { Shipit::Stack.count } do
16
+ Shipit::Webhooks::Handlers::PullRequest::ReopenedHandler.new(payload_parsed(:pull_request_opened).merge(action: "assigned")).process
17
+ end
18
+ end
19
+
20
+ test "does not error for repos that are not tracked" do
21
+ assert_no_difference -> { Shipit::Stack.count } do
22
+ Shipit::Webhooks::Handlers::PullRequest::ReopenedHandler.new(payload_parsed(:pull_request_with_no_repo)).process
23
+ end
24
+ end
25
+
26
+ test "de-archives stacks that were previously archived" do
27
+ create_archived_stack
28
+
29
+ assert_no_difference -> { Shipit::Stack.count } do
30
+ assert_difference -> { Shipit::Stack.not_archived.count } do
31
+ Shipit::Webhooks::Handlers::PullRequest::ReopenedHandler.new(payload_parsed(:pull_request_reopened)).process
32
+ end
33
+ end
34
+ end
35
+
36
+ test "ignored duplicate deliveries" do
37
+ stack = create_archived_stack
38
+ Shipit::Webhooks::Handlers::PullRequest::ReopenedHandler.new(payload_parsed(:pull_request_reopened)).process
39
+ complete_active_tasks(stack)
40
+
41
+ assert_no_difference -> { Shipit::Stack.not_archived.count } do
42
+ Shipit::Webhooks::Handlers::PullRequest::ReopenedHandler.new(payload_parsed(:pull_request_reopened)).process
43
+ end
44
+ end
45
+
46
+ test "unarchives stacks for repos that allow_all" do
47
+ stack = create_archived_stack
48
+ repository = shipit_repositories(:shipit)
49
+ configure_provisioning_behavior(
50
+ repository: repository,
51
+ behavior: :allow_all
52
+ )
53
+
54
+ Shipit::Webhooks::Handlers::PullRequest::ReopenedHandler.new(payload_parsed(:pull_request_reopened)).process
55
+
56
+ assert_not stack.reload.archived?, "Expected stack to NOT be archived"
57
+ assert_pending_provision(stack)
58
+ end
59
+
60
+ test "provisions missing stacks for repos that allow_all" do
61
+ repository = shipit_repositories(:shipit)
62
+ configure_provisioning_behavior(
63
+ repository: repository,
64
+ behavior: :allow_all
65
+ )
66
+ payload = payload_parsed(:pull_request_reopened)
67
+
68
+ Shipit::Webhooks::Handlers::PullRequest::ReopenedHandler.new(payload).process
69
+
70
+ stack = shipit_repositories(:shipit).stacks.last
71
+ assert_equal stack.environment, "pr#{payload['number']}"
72
+ assert_equal stack.continuous_deployment, false
73
+ assert_equal stack.ignore_ci, false
74
+ assert_equal stack.branch, payload["pull_request"]["head"]["ref"]
75
+ assert_not stack.archived?, "Expected stack to be NOT be archived"
76
+ assert_pending_provision(stack)
77
+ end
78
+
79
+ test "auto-created stack should have pull request assigned" do
80
+ repository = shipit_repositories(:shipit)
81
+ configure_provisioning_behavior(
82
+ repository: repository,
83
+ behavior: :allow_all
84
+ )
85
+ payload = payload_parsed(:pull_request_reopened)
86
+
87
+ assert_difference -> { Shipit::PullRequest.count } do
88
+ Shipit::Webhooks::Handlers::PullRequest::ReopenedHandler.new(payload).process
89
+ end
90
+ end
91
+
92
+ test "unarchives stacks for repos that allow_with_label when label is present" do
93
+ stack = create_archived_stack
94
+ repository = shipit_repositories(:shipit)
95
+ configure_provisioning_behavior(
96
+ repository: repository,
97
+ behavior: :allow_with_label,
98
+ label: "pull-requests-label"
99
+ )
100
+ payload = payload_parsed(:pull_request_reopened)
101
+ payload["pull_request"]["labels"] << { "name" => "pull-requests-label" }
102
+
103
+ Shipit::Webhooks::Handlers::PullRequest::ReopenedHandler.new(payload).process
104
+
105
+ assert_not stack.reload.archived?, "Expected stack to be NOT be archived"
106
+ assert_pending_provision(stack)
107
+ end
108
+
109
+ test "provisions missing stacks for repos that allow_with_label when label is present" do
110
+ repository = shipit_repositories(:shipit)
111
+ configure_provisioning_behavior(
112
+ repository: repository,
113
+ behavior: :allow_with_label,
114
+ label: "pull-requests-label"
115
+ )
116
+ payload = payload_parsed(:pull_request_reopened)
117
+ payload["pull_request"]["labels"] << { "name" => "pull-requests-label" }
118
+
119
+ Shipit::Webhooks::Handlers::PullRequest::ReopenedHandler.new(payload).process
120
+
121
+ stack = shipit_repositories(:shipit).stacks.last
122
+ assert_equal stack.environment, "pr#{payload['number']}"
123
+ assert_equal stack.continuous_deployment, false
124
+ assert_equal stack.ignore_ci, false
125
+ assert_equal stack.branch, payload["pull_request"]["head"]["ref"]
126
+ assert_not stack.archived?, "Expected stack to be NOT be archived"
127
+ assert_pending_provision(stack)
128
+ end
129
+
130
+ test "does not unarchive stacks for repos that allow_with_label when label is absent" do
131
+ stack = create_archived_stack
132
+ repository = shipit_repositories(:shipit)
133
+ configure_provisioning_behavior(
134
+ repository: repository,
135
+ behavior: :allow_with_label,
136
+ label: "pull-requests-label"
137
+ )
138
+ payload = payload_parsed(:pull_request_reopened)
139
+ payload["pull_request"]["labels"] = []
140
+
141
+ Shipit::Webhooks::Handlers::PullRequest::ReopenedHandler.new(payload).process
142
+
143
+ assert stack.reload.archived?, "Expected stack to be archived"
144
+ end
145
+
146
+ test "unarchives stacks for repos that prevent_with_label when label is absent" do
147
+ stack = create_archived_stack
148
+ repository = shipit_repositories(:shipit)
149
+ configure_provisioning_behavior(
150
+ repository: repository,
151
+ behavior: :prevent_with_label,
152
+ label: "pull-requests-label"
153
+ )
154
+ payload = payload_parsed(:pull_request_reopened)
155
+ payload["pull_request"]["labels"] = []
156
+
157
+ Shipit::Webhooks::Handlers::PullRequest::ReopenedHandler.new(payload).process
158
+
159
+ assert_not stack.reload.archived?, "Expected stack to be NOT be archived"
160
+ assert_pending_provision(stack)
161
+ end
162
+
163
+ test "provisions missing stacks for repos that prevent_with_label when label is absent" do
164
+ repository = shipit_repositories(:shipit)
165
+ configure_provisioning_behavior(
166
+ repository: repository,
167
+ behavior: :prevent_with_label,
168
+ label: "pull-requests-label"
169
+ )
170
+ payload = payload_parsed(:pull_request_reopened)
171
+ payload["pull_request"]["labels"] = []
172
+
173
+ Shipit::Webhooks::Handlers::PullRequest::ReopenedHandler.new(payload).process
174
+
175
+ stack = shipit_repositories(:shipit).stacks.last
176
+ assert_equal stack.environment, "pr#{payload['number']}"
177
+ assert_equal stack.continuous_deployment, false
178
+ assert_equal stack.ignore_ci, false
179
+ assert_equal stack.branch, payload["pull_request"]["head"]["ref"]
180
+ assert_not stack.archived?, "Expected stack to be NOT be archived"
181
+ assert_pending_provision(stack)
182
+ end
183
+
184
+ test "does not unarchive stacks for repos that prevent_with_label when label is present" do
185
+ stack = create_archived_stack
186
+ repository = shipit_repositories(:shipit)
187
+ configure_provisioning_behavior(
188
+ repository: repository,
189
+ behavior: :prevent_with_label,
190
+ label: "pull-requests-label"
191
+ )
192
+ payload = payload_parsed(:pull_request_reopened)
193
+ payload["pull_request"]["labels"] << { "name" => "pull-requests-label" }
194
+
195
+ Shipit::Webhooks::Handlers::PullRequest::ReopenedHandler.new(payload).process
196
+
197
+ assert stack.reload.archived?, "Expected stack to be archived"
198
+ end
199
+
200
+ def configure_provisioning_behavior(repository:, provisioning_enabled: true, behavior: :allow_all, label: nil)
201
+ repository.review_stacks_enabled = provisioning_enabled
202
+ repository.provisioning_behavior = behavior
203
+ repository.provisioning_label_name = label
204
+ repository.save!
205
+
206
+ repository
207
+ end
208
+
209
+ def create_archived_stack
210
+ stack = create_stack
211
+ stack.archive!(shipit_users(:codertocat))
212
+
213
+ stack
214
+ end
215
+
216
+ def create_stack
217
+ repository = shipit_repositories(:shipit)
218
+ repository.provisioning_behavior = :allow_all
219
+ repository.save!
220
+
221
+ payload = payload_parsed(:pull_request_labeled)
222
+ payload["action"] = "opened"
223
+
224
+ OpenedHandler.new(payload).process
225
+
226
+ stack = repository.stacks.last
227
+ complete_active_tasks(stack)
228
+
229
+ stack
230
+ end
231
+
232
+ def complete_active_tasks(stack)
233
+ active_tasks = stack
234
+ .tasks
235
+ .active
236
+
237
+ active_tasks.map(&:run)
238
+ active_tasks.reload
239
+ active_tasks.map(&:complete)
240
+ end
241
+
242
+ def assert_pending_provision(stack)
243
+ stack.reload
244
+
245
+ assert(stack.awaiting_provision?, "Stack #{stack.environment} should be in the provisioning queue")
246
+ assert(stack.deprovisioned?, "Stack #{stack.environment} should be pending provision")
247
+ end
248
+
249
+ setup do
250
+ Shipit.github.api.stubs(:commit)
251
+ .with("shopify/shipit-engine", "ec26c3e57ca3a959ca5aad62de7213c562f8c821")
252
+ .returns(
253
+ resource(
254
+ {
255
+ sha: "ec26c3e57ca3a959ca5aad62de7213c562f8c821",
256
+ commit: {
257
+ author: {
258
+ name: "Codertocat",
259
+ email: "21031067+Codertocat@users.noreply.github.com",
260
+ date: "2019-05-15 15:20:30",
261
+ },
262
+ committer: {
263
+ name: "Codertocat",
264
+ email: "21031067+Codertocat@users.noreply.github.com",
265
+ date: "2019-05-15 15:20:30",
266
+ },
267
+ message: "Update README.md",
268
+ },
269
+ stats: {
270
+ total: 2,
271
+ additions: 1,
272
+ deletions: 1,
273
+ },
274
+ }
275
+ )
276
+ )
277
+ end
278
+ end
279
+ end
280
+ end
281
+ end
282
+ end