<commit_before>module ManageIQ
  module API
    class BaseController
      module Blueprints
        def show_blueprints
          @additional_attributes = %w(content)
          show_generic
        end

        def create_resource_blueprints(_type, _id, data)
          attributes = data.except("bundle")
          blueprint = Blueprint.new(attributes)
          bundle = data["bundle"]
          create_bundle(blueprint, bundle) if bundle
          blueprint.save!
          blueprint
        end

        def edit_resource_blueprints(_type, id, data)
          attributes = data.except("bundle")
          blueprint = Blueprint.find(id)
          blueprint.update!(attributes)
          bundle = data["bundle"]
          update_bundle(blueprint, bundle) if bundle
          blueprint
        end

        private

        def create_bundle(blueprint, bundle)
          blueprint.create_bundle(deserialize_bundle(bundle))
        rescue => e
          raise BadRequestError, "Couldn't create the bundle - #{e}"
        end

        def update_bundle(blueprint, bundle)
          blueprint.update_bundle(deserialize_bundle(bundle))
        rescue => e
          raise BadRequestError, "Couldn't update the bundle - #{e}"
        end

        def deserialize_bundle(bundle)
          options = {}
          if bundle["service_catalog"]
            options["service_catalog"] =
              if bundle["service_catalog"].blank?
                nil
              else
                ServiceTemplateCatalog.find(parse_id(bundle["service_catalog"], :service_catalogs))
              end
          end
          if bundle["service_dialog"]
            options[:service_dialog] = Dialog.find(parse_id(bundle["service_dialog"], :service_dialogs))
          end
          options[:service_templates] = bundle.fetch("service_templates", []).collect do |st|
            ServiceTemplate.find(parse_id(st, :service_templates))
          end
          options[:entry_points] = bundle["automate_entrypoints"] if bundle["automate_entrypoints"]
          options
        end
      end
    end
  end
end<commit_msg>allow for a blank service_dialog<commit_after>module ManageIQ
  module API
    class BaseController
      module Blueprints
        def show_blueprints
          @additional_attributes = %w(content)
          show_generic
        end

        def create_resource_blueprints(_type, _id, data)
          attributes = data.except("bundle")
          blueprint = Blueprint.new(attributes)
          bundle = data["bundle"]
          create_bundle(blueprint, bundle) if bundle
          blueprint.save!
          blueprint
        end

        def edit_resource_blueprints(_type, id, data)
          attributes = data.except("bundle")
          blueprint = Blueprint.find(id)
          blueprint.update!(attributes)
          bundle = data["bundle"]
          update_bundle(blueprint, bundle) if bundle
          blueprint
        end

        private

        def create_bundle(blueprint, bundle)
          blueprint.create_bundle(deserialize_bundle(bundle))
        rescue => e
          raise BadRequestError, "Couldn't create the bundle - #{e}"
        end

        def update_bundle(blueprint, bundle)
          blueprint.update_bundle(deserialize_bundle(bundle))
        rescue => e
          raise BadRequestError, "Couldn't update the bundle - #{e}"
        end

        def deserialize_bundle(bundle)
          options = {}
          if bundle["service_catalog"]
            options["service_catalog"] = service_catalog_option(bundle)
          end
          if bundle["service_dialog"]
            options[:service_dialog] = service_dialog_option(bundle)
          end
          options[:service_templates] = bundle.fetch("service_templates", []).collect do |st|
            ServiceTemplate.find(parse_id(st, :service_templates))
          end
          options[:entry_points] = bundle["automate_entrypoints"] if bundle["automate_entrypoints"]
          options
        end

        def service_catalog_option(bundle)
          if bundle["service_catalog"].blank?
            nil
          else
            ServiceTemplateCatalog.find(parse_id(bundle["service_catalog"], :service_catalogs))
          end
        end

        def service_dialog_option(bundle)
          if bundle["service_dialog"].blank?
            nil
          else
            Dialog.find(parse_id(bundle["service_dialog"], :service_dialogs))
          end
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08275553567434578, 'fraction_numerical': 0.0, 'mean_word_length': 2.1034004163775157, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3016763', 'n_tokens_mistral': 1137, 'n_tokens_neox': 1122, 'n_words': 246}