<commit_before>require 'chef_zero'
require 'chef_zero/rest_base'

module ChefZero
  class DataNormalizer
    def self.normalize_acls(acls, requestor)
      %w(create read update delete grant).each do |perm|
        acls[perm] ||= {}
        acls[perm]['actors'] ||= [ requestor ]
        acls[perm]['groups'] ||= [ 'admins' ]
      end
      acls
    end

    def self.merge_container_acls(acls, container_acls)
      container_acls.each_pair do |perm, who|
        acls[perm] ||= {}
        acls[perm]['actors'] ||= container_acls[perm]['actors']
        acls[perm]['groups'] ||= container_acls[perm]['groups']
      end
      acls
    end

    def self.normalize_client(client, name)
      client['name'] ||= name
      client['admin'] ||= false
      client['admin'] = !!client['admin']
      client['public_key'] ||= PUBLIC_KEY
      client['validator'] ||= false
      client['validator'] = !!client['validator']
      client['json_class'] ||= "Chef::ApiClient"
      client['chef_type'] ||= "client"
      client
    end

    def self.normalize_container(container, name)
      container['containername'] ||= name
      container['containerpath'] ||= name
      container
    end

    def self.normalize_user(user, name, identity_key)
      user[identity_key] ||= name
      user['admin'] ||= false
      user['admin'] = !!user['admin']
      user['openid'] ||= nil
      user['public_key'] ||= PUBLIC_KEY
      user
    end

    def self.normalize_data_bag_item(data_bag_item, data_bag_name, id, method)
      if method == 'DELETE'
        # TODO SERIOUSLY, WHO DOES THIS MANY EXCEPTIONS IN THEIR INTERFACE
        if !(data_bag_item['json_class'] == 'Chef::DataBagItem' && data_bag_item['raw_data'])
          data_bag_item['id'] ||= id
          data_bag_item = { 'raw_data' => data_bag_item }
          data_bag_item['chef_type'] ||= 'data_bag_item'
          data_bag_item['json_class'] ||= 'Chef::DataBagItem'
          data_bag_item['data_bag'] ||= data_bag_name
          data_bag_item['name'] ||= "data_bag_item_#{data_bag_name}_#{id}"
        end
      else
        # If it's not already wrapped with raw_data, wrap it.
        if data_bag_item['json_class'] == 'Chef::DataBagItem' && data_bag_item['raw_data']
          data_bag_item = data_bag_item['raw_data']
        end
        # Argh.  We don't do this on GET, but we do on PUT and POST????
        if %w(PUT POST).include?(method)
          data_bag_item['chef_type'] ||= 'data_bag_item'
          data_bag_item['data_bag'] ||= data_bag_name
        end
        data_bag_item['id'] ||= id
      end
      data_bag_item
    end

    def self.normalize_cookbook(endpoint, org_prefix, cookbook, name, version, base_uri, method)
      # TODO I feel dirty
      if method != 'PUT'
        cookbook.each_pair do |key, value|
          if value.is_a?(Array)
            value.each do |file|
              if file.is_a?(Hash) && file.has_key?('checksum')
                file['url'] ||= endpoint.build_uri(base_uri, org_prefix + ['file_store', 'checksums', file['checksum']])
              end
            end
          end
        end
        cookbook['name'] ||= "#{name}-#{version}"
        # TODO this feels wrong, but the real chef server doesn't expand this default
  #      cookbook['version'] ||= version
        cookbook['cookbook_name'] ||= name
        cookbook['frozen?'] ||= false
        cookbook['metadata'] ||= {}
        cookbook['metadata']['version'] ||= version
        # Sad to not be expanding defaults just because Chef doesn't :(
#        cookbook['metadata']['name'] ||= name
#        cookbook['metadata']['description'] ||= "A fabulous new cookbook"
        cookbook['metadata']['long_description'] ||= ""
#        cookbook['metadata']['maintainer'] ||= "YOUR_COMPANY_NAME"
#        cookbook['metadata']['maintainer_email'] ||= "YOUR_EMAIL"
#        cookbook['metadata']['license'] ||= "none"
        cookbook['metadata']['dependencies'] ||= {}
        cookbook['metadata']['attributes'] ||= {}
        cookbook['metadata']['recipes'] ||= {}
      end
      cookbook['json_class'] ||= 'Chef::CookbookVersion'
      cookbook['chef_type'] ||= 'cookbook_version'
      if method == 'MIN'
        cookbook['metadata'].delete('attributes')
        cookbook['metadata'].delete('long_description')
      end
      cookbook
    end

    def self.normalize_environment(environment, name)
      environment['name'] ||= name
      environment['description'] ||= ''
      environment['cookbook_versions'] ||= {}
      environment['json_class'] ||= "Chef::Environment"
      environment['chef_type'] ||= "environment"
      environment['default_attributes'] ||= {}
      environment['override_attributes'] ||= {}
      environment
    end

    def self.normalize_group(group, name, orgname)
      group['users'] ||= []
      group['clients'] ||= []
      group['actors'] ||= (group['clients'] + group['users'])
      group['groups'] ||= []
      group['orgname'] ||= orgname if orgname
      group['name'] ||= name
      group['groupname'] ||= name
      group
    end

    def self.normalize_node(node, name)
      node['name'] ||= name
      node['json_class'] ||= 'Chef::Node'
      node['chef_type'] ||= 'node'
      node['chef_environment'] ||= '_default'
      node['override'] ||= {}
      node['normal'] ||= {}
      node['default'] ||= {}
      node['automatic'] ||= {}
      node['run_list'] ||= []
      node['run_list'] = normalize_run_list(node['run_list'])
      node
    end

    def self.normalize_organization(org, name)
      org['name'] ||= name
      org['full_name'] ||= name
      org['org_type'] ||= 'Business'
      org['clientname'] ||= "#{name}-validator"
      org['billing_plan'] ||= 'platform-free'<commit_msg>Support alternate syntax for groups.actors<commit_after>require 'chef_zero'
require 'chef_zero/rest_base'

module ChefZero
  class DataNormalizer
    def self.normalize_acls(acls, requestor)
      %w(create read update delete grant).each do |perm|
        acls[perm] ||= {}
        acls[perm]['actors'] ||= [ requestor ]
        acls[perm]['groups'] ||= [ 'admins' ]
      end
      acls
    end

    def self.merge_container_acls(acls, container_acls)
      container_acls.each_pair do |perm, who|
        acls[perm] ||= {}
        acls[perm]['actors'] ||= container_acls[perm]['actors']
        acls[perm]['groups'] ||= container_acls[perm]['groups']
      end
      acls
    end

    def self.normalize_client(client, name)
      client['name'] ||= name
      client['admin'] ||= false
      client['admin'] = !!client['admin']
      client['public_key'] ||= PUBLIC_KEY
      client['validator'] ||= false
      client['validator'] = !!client['validator']
      client['json_class'] ||= "Chef::ApiClient"
      client['chef_type'] ||= "client"
      client
    end

    def self.normalize_container(container, name)
      container['containername'] ||= name
      container['containerpath'] ||= name
      container
    end

    def self.normalize_user(user, name, identity_key)
      user[identity_key] ||= name
      user['admin'] ||= false
      user['admin'] = !!user['admin']
      user['openid'] ||= nil
      user['public_key'] ||= PUBLIC_KEY
      user
    end

    def self.normalize_data_bag_item(data_bag_item, data_bag_name, id, method)
      if method == 'DELETE'
        # TODO SERIOUSLY, WHO DOES THIS MANY EXCEPTIONS IN THEIR INTERFACE
        if !(data_bag_item['json_class'] == 'Chef::DataBagItem' && data_bag_item['raw_data'])
          data_bag_item['id'] ||= id
          data_bag_item = { 'raw_data' => data_bag_item }
          data_bag_item['chef_type'] ||= 'data_bag_item'
          data_bag_item['json_class'] ||= 'Chef::DataBagItem'
          data_bag_item['data_bag'] ||= data_bag_name
          data_bag_item['name'] ||= "data_bag_item_#{data_bag_name}_#{id}"
        end
      else
        # If it's not already wrapped with raw_data, wrap it.
        if data_bag_item['json_class'] == 'Chef::DataBagItem' && data_bag_item['raw_data']
          data_bag_item = data_bag_item['raw_data']
        end
        # Argh.  We don't do this on GET, but we do on PUT and POST????
        if %w(PUT POST).include?(method)
          data_bag_item['chef_type'] ||= 'data_bag_item'
          data_bag_item['data_bag'] ||= data_bag_name
        end
        data_bag_item['id'] ||= id
      end
      data_bag_item
    end

    def self.normalize_cookbook(endpoint, org_prefix, cookbook, name, version, base_uri, method)
      # TODO I feel dirty
      if method != 'PUT'
        cookbook.each_pair do |key, value|
          if value.is_a?(Array)
            value.each do |file|
              if file.is_a?(Hash) && file.has_key?('checksum')
                file['url'] ||= endpoint.build_uri(base_uri, org_prefix + ['file_store', 'checksums', file['checksum']])
              end
            end
          end
        end
        cookbook['name'] ||= "#{name}-#{version}"
        # TODO this feels wrong, but the real chef server doesn't expand this default
  #      cookbook['version'] ||= version
        cookbook['cookbook_name'] ||= name
        cookbook['frozen?'] ||= false
        cookbook['metadata'] ||= {}
        cookbook['metadata']['version'] ||= version
        # Sad to not be expanding defaults just because Chef doesn't :(
#        cookbook['metadata']['name'] ||= name
#        cookbook['metadata']['description'] ||= "A fabulous new cookbook"
        cookbook['metadata']['long_description'] ||= ""
#        cookbook['metadata']['maintainer'] ||= "YOUR_COMPANY_NAME"
#        cookbook['metadata']['maintainer_email'] ||= "YOUR_EMAIL"
#        cookbook['metadata']['license'] ||= "none"
        cookbook['metadata']['dependencies'] ||= {}
        cookbook['metadata']['attributes'] ||= {}
        cookbook['metadata']['recipes'] ||= {}
      end
      cookbook['json_class'] ||= 'Chef::CookbookVersion'
      cookbook['chef_type'] ||= 'cookbook_version'
      if method == 'MIN'
        cookbook['metadata'].delete('attributes')
        cookbook['metadata'].delete('long_description')
      end
      cookbook
    end

    def self.normalize_environment(environment, name)
      environment['name'] ||= name
      environment['description'] ||= ''
      environment['cookbook_versions'] ||= {}
      environment['json_class'] ||= "Chef::Environment"
      environment['chef_type'] ||= "environment"
      environment['default_attributes'] ||= {}
      environment['override_attributes'] ||= {}
      environment
    end

    def self.normalize_group(group, name, orgname)
      if group['actors'].is_a?(Hash)
        group['users'] ||= group['actors']['users']
        group['clients'] ||= group['actors']['clients']
        group['groups'] ||= group['actors']['groups']
        group['actors'] = nil
      end
      group['users'] ||= []
      group['clients'] ||= []
      group['actors'] ||= (group['clients'] + group['users'])
      group['groups'] ||= []
      group['orgname'] ||= orgname if orgname
      group['name'] ||= name
      group['groupname'] ||= name
      group
    end

    def self.normalize_node(node, name)
      node['name'] ||= name
      node['json_class'] ||= 'Chef::Node'
      node['chef_type'] ||= 'node'
      node['chef_environment'] ||= '_default'
      node['override'] ||= {}
      node['normal'] ||= {}
      node['default'] ||= {}
      node['automatic'] ||= {}
      node['run_list'] ||= []
      node['run_list'] = normalize_run_list(node['run_list'])
      node
    end

    def self.normalize_organization(org, name)
      org['name'] ||= name
      org['full_name'] ||= name
      org['org_type'] ||= 'Business'
      org['clientname'] ||= "#{name}-validator"
      org['billing_plan'] ||= 'platform-free'
{'fraction_non_alphanumeric': 0.16757819176209196, 'fraction_numerical': 0.0, 'mean_word_length': 2.908817635270541, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4894547', 'n_tokens_mistral': 3701, 'n_tokens_neox': 3627, 'n_words': 686}