<commit_before>module Utils
  module LitaPuppet
    # Utility methods for working with PuppetDB
    module PuppetDB
      def db_connect
        ::PuppetDB::Client.new({
                                 server: config.puppetdb_url,
                                 pem: {
                                   'key' => config.puppetdb_key,
                                   'cert'    => config.puppetdb_cert,
                                   'ca_file' => config.puppetdb_ca_cert
                                 }
                               }, config.puppetdb_api_vers)
      end

      def class_nodes(classname)
        q = db_connect.request(
          'resources',
          [
            :and,
            [:'=', 'type', 'Class'],
            [:'=', 'title', classname.to_s]
          ]
        )

        q.data.map { |node| node['certname'] }
      end

      def query_fact(node, fact)
        q = db_connect.request(
          "facts/#{fact}",
          [:'=', 'certname', node]
        )
        begin
          raise 'invalid query' if q.data.empty?
          q.data.last['value']
        rescue
          nil
        end
      end

      def node_info(node)
        q = db_connect.request(
          'nodes',
          [:'=', 'certname', node]
        )
        begin
          raise 'invalid node' if q.data.empty?
          q.data.last.to_yaml
        rescue
          nil
        end
      end

      # rubocop:disable AbcSize, MethodLength
      def node_roles_and_profiles(what, nodename)
        # TODO: validate url and nodename
        ::PuppetDB::Client.new({
                                 server: config.puppetdb_url,
                                 pem: {
                                   'key' => config.puppetdb_key,
                                   'cert'    => config.puppetdb_cert,
                                   'ca_file' => config.puppetdb_ca_cert
                                 }
                               }, config.puppetdb_api_vers) # this is weird but required
        d = ::PuppetDB::Client.get("/catalogs/#{nodename}")
        return d['error'] if d['error']

        tags = []
        if d['resources'].is_a?(Array)
          d['resources'].each { |r| tags.concat(r['tags']) }
        else
          d['resources']['data'].each { |r| tags.concat(r['tags']) }
        end

        # return all the tags related to profile:: or role::
        case what
        when 'profiles'
          tags.sort.uniq.select { |t| t.match(/^profile::/) }
        when 'roles'
          tags.sort.uniq.select { |t| t.match(/^role::/) }
        when 'r&p', 'p&r', 'roles and profiles'
          tags.sort.uniq.select { |t| t.match(/^(profile|role)::/) }
        end
      end
    end
  end
end
<commit_msg>More rubocop fixes<commit_after>module Utils
  module LitaPuppet
    # Utility methods for working with PuppetDB
    module PuppetDB
      def db_connect
        ::PuppetDB::Client.new({
                                 server: config.puppetdb_url,
                                 pem: {
                                   'key' => config.puppetdb_key,
                                   'cert'    => config.puppetdb_cert,
                                   'ca_file' => config.puppetdb_ca_cert
                                 }
                               }, config.puppetdb_api_vers)
      end

      def class_nodes(classname)
        q = db_connect.request(
          'resources',
          [
            :and,
            [:'=', 'type', 'Class'],
            [:'=', 'title', classname.to_s]
          ]
        )

        q.data.map { |node| node['certname'] }
      end

      def query_fact(node, fact)
        q = db_connect.request(
          "facts/#{fact}",
          [:'=', 'certname', node]
        )
        begin
          raise 'invalid query' if q.data.empty?
          q.data.last['value']
        rescue
          nil
        end
      end

      def node_info(node)
        q = db_connect.request(
          'nodes',
          [:'=', 'certname', node]
        )
        begin
          raise 'invalid node' if q.data.empty?
          q.data.last.to_yaml
        rescue
          nil
        end
      end

      # rubocop:disable AbcSize
      def node_roles_and_profiles(what, nodename)
        # TODO: validate url and nodename
        ::PuppetDB::Client.new({
                                 server: config.puppetdb_url,
                                 pem: {
                                   'key' => config.puppetdb_key,
                                   'cert'    => config.puppetdb_cert,
                                   'ca_file' => config.puppetdb_ca_cert
                                 }
                               }, config.puppetdb_api_vers) # this is weird but required
        d = ::PuppetDB::Client.get("/catalogs/#{nodename}")
        return d['error'] if d['error']

        tags = []
        if d['resources'].is_a?(Array)
          d['resources'].each { |r| tags.concat(r['tags']) }
        else
          d['resources']['data'].each { |r| tags.concat(r['tags']) }
        end

        # return all the tags related to profile:: or role::
        case what
        when 'profiles'
          tags.sort.uniq.select { |t| t.match(/^profile::/) }
        when 'roles'
          tags.sort.uniq.select { |t| t.match(/^role::/) }
        when 'r&p', 'p&r', 'roles and profiles'
          tags.sort.uniq.select { |t| t.match(/^(profile|role)::/) }
        end
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.11909538573773482, 'fraction_numerical': 0.0, 'mean_word_length': 1.1761904761904762, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2210857', 'n_tokens_mistral': 1613, 'n_tokens_neox': 1579, 'n_words': 313}