|
| 1 | +require 'pact_broker/api/resources/base_resource' |
| 2 | +require 'pact_broker/api/decorators/webhook_decorator' |
| 3 | +require 'pact_broker/api/decorators/webhooks_decorator' |
| 4 | +require 'pact_broker/api/contracts/webhook_contract' |
| 5 | + |
| 6 | +module PactBroker |
| 7 | + module Api |
| 8 | + module Resources |
| 9 | + class Webhooks < BaseResource |
| 10 | + |
| 11 | + def allowed_methods |
| 12 | + ["POST", "GET"] |
| 13 | + end |
| 14 | + |
| 15 | + def content_types_provided |
| 16 | + [["application/hal+json", :to_json]] |
| 17 | + end |
| 18 | + |
| 19 | + def content_types_accepted |
| 20 | + [["application/json", :from_json]] |
| 21 | + end |
| 22 | + |
| 23 | + def resource_exists? |
| 24 | + (identifier_from_path[:consumer_name].nil? || consumer) && |
| 25 | + (identifier_from_path[:provider_name].nil? || provider) |
| 26 | + end |
| 27 | + |
| 28 | + def malformed_request? |
| 29 | + if request.post? |
| 30 | + return invalid_json? || validation_errors?(webhook) |
| 31 | + end |
| 32 | + false |
| 33 | + end |
| 34 | + |
| 35 | + def validation_errors? webhook |
| 36 | + errors = webhook_service.errors(webhook) |
| 37 | + |
| 38 | + unless errors.empty? |
| 39 | + response.headers['Content-Type'] = 'application/json;charset=utf-8' |
| 40 | + response.body = { errors: errors.messages }.to_json |
| 41 | + end |
| 42 | + |
| 43 | + !errors.empty? |
| 44 | + end |
| 45 | + |
| 46 | + def create_path |
| 47 | + webhook_url next_uuid, base_url |
| 48 | + end |
| 49 | + |
| 50 | + def post_is_create? |
| 51 | + true |
| 52 | + end |
| 53 | + |
| 54 | + def from_json |
| 55 | + saved_webhook = webhook_service.create next_uuid, webhook, consumer, provider |
| 56 | + response.body = Decorators::WebhookDecorator.new(saved_webhook).to_json(user_options: { base_url: base_url }) |
| 57 | + end |
| 58 | + |
| 59 | + def to_json |
| 60 | + Decorators::WebhooksDecorator.new(webhooks).to_json(user_options: decorator_context(resource_title: 'Pact webhooks')) |
| 61 | + end |
| 62 | + |
| 63 | + private |
| 64 | + |
| 65 | + def webhooks |
| 66 | + webhook_service.find_by_consumer_and_provider consumer, provider |
| 67 | + end |
| 68 | + |
| 69 | + def webhook |
| 70 | + @webhook ||= Decorators::WebhookDecorator.new(PactBroker::Domain::Webhook.new).from_json(request_body) |
| 71 | + end |
| 72 | + |
| 73 | + def next_uuid |
| 74 | + @next_uuid ||= webhook_service.next_uuid |
| 75 | + end |
| 76 | + |
| 77 | + def consumer |
| 78 | + @consumer ||= identifier_from_path[:consumer_name] && find_pacticipant(identifier_from_path[:consumer_name], "consumer") |
| 79 | + end |
| 80 | + |
| 81 | + def provider |
| 82 | + @provider ||= identifier_from_path[:provider_name] && find_pacticipant(identifier_from_path[:provider_name], "provider") |
| 83 | + end |
| 84 | + |
| 85 | + def find_pacticipant name, role |
| 86 | + pacticipant_service.find_pacticipant_by_name(name).tap do | pacticipant | |
| 87 | + set_json_error_message("No #{role} with name '#{name}' found") if pacticipant.nil? |
| 88 | + end |
| 89 | + end |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | +end |
0 commit comments