Skip to content

Commit

Permalink
Provide 2 more events on creating status checks
Browse files Browse the repository at this point in the history
  • Loading branch information
coolo committed Nov 14, 2018
1 parent bea7f81 commit 6bf4ab9
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/api/app/controllers/status/checks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def update
end

if @check.save
create_event
render :show
else
render_error(status: 422, errorcode: 'invalid_check', message: "Could not save check: #{@check.errors.full_messages.to_sentence}")
Expand All @@ -26,6 +27,21 @@ def update

private

def create_event
if @project
Event::StatusCheckForPublished.create(check_notify_params)
else
Event::StatusCheckForRequest.create(check_notify_params)
end
end

def check_notify_params
params = { who: User.current.login, state: @check.state, name: @check.name }
params[:url] = @check.url if @check.url
params[:short_description] = @check.short_description if @check.short_description
params.merge(@status_report.notify_params)
end

def set_status_report
if params[:report_uuid]
@status_report = @checkable.status_reports.find_or_initialize_by(uuid: params[:report_uuid])
Expand Down
10 changes: 10 additions & 0 deletions src/api/app/models/event/status_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Event
class StatusCheck < Base
self.abstract_class = true
payload_keys :who, :name, :short_description, :state, :url

def originator
payload_address('who')
end
end
end
6 changes: 6 additions & 0 deletions src/api/app/models/event/status_check_for_published.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Event
class StatusCheckForPublished < StatusCheck
self.description = 'Status Check for Published Repository Created'
payload_keys :project, :repository, :uuid
end
end
5 changes: 5 additions & 0 deletions src/api/app/models/event/status_check_for_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Event
class StatusCheckForRequest < StatusCheck
payload_keys :number
end
end
11 changes: 11 additions & 0 deletions src/api/app/models/status/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ def projects
end
end

def notify_params
case checkable
when BsRequest
{ number: checkable.number }
when Repository
{ project: checkable.project.name, repository: checkable.name, uuid: uuid }
else
{}
end
end

#### Instance methods (public and then protected/private)

#### Alias of methods
Expand Down
12 changes: 12 additions & 0 deletions src/api/spec/controllers/status/checks_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
state: 'pending', url: 'http://checks.example.com/12345')).to exist
end
it { is_expected.to have_http_status(:success) }
it { expect(Event::StatusCheck.count).to eq(1) }
end

context 'with invalid XML' do
Expand All @@ -78,6 +79,7 @@

it { is_expected.to have_http_status(:unprocessable_entity) }
it { expect(status_report.checks).to be_empty }
it { expect(Event::StatusCheck.count).to eq(0) }
end

context 'with no permissions' do
Expand All @@ -91,6 +93,7 @@

it { is_expected.to have_http_status(:forbidden) }
it { expect(status_report.checks).to be_empty }
it { expect(Event::StatusCheck.count).to eq(0) }
end
end

Expand All @@ -100,6 +103,11 @@
end

include_context 'does create the check'

it 'creates the proper event' do
post :update, body: xml, params: params, format: :xml
expect(Event::StatusCheckForPublished.first.payload).to include('who' => user.login, 'project' => project.name, 'repository' => repository.name, 'state' => 'pending')
end
end

context 'for a repository architecture' do
Expand All @@ -121,6 +129,10 @@
end

include_context 'does create the check'
it 'creates the proper event' do
post :update, body: xml, params: params, format: :xml
expect(Event::StatusCheckForRequest.first.payload).to include('who' => user.login, 'number' => bs_request.number, 'state' => 'pending')
end
end
end

Expand Down

0 comments on commit 6bf4ab9

Please sign in to comment.