<commit_before>---
platform: linux
image_resource:
  type: docker-image
  source:
    repository: ruby
    tag: 2.5-slim
inputs:
  - name: paas-trusted-people
run:
  path: ruby
  args:
    - -e
    - |
      # frozen_string_literal: true

      # rubocop:disable Lint/MissingCopEnableDirective
      # rubocop:disable Layout/MultilineMethodCallIndentation
      # rubocop:disable Metrics/MethodLength
      # rubocop:disable Metrics/BlockLength
      # rubocop:disable Metrics/AbcSize
      # rubocop:disable Style/Documentation

      require 'json'
      require 'net/http'
      require 'openssl'
      require 'securerandom'
      require 'set'
      require 'uri'
      require 'yaml'

      class String
        def colour(colour_code)
          "\e[#{colour_code}m#{self}\e[0m"
        end

        def yellow
          colour(33)
        end

        def green
          colour(32)
        end
      end

      def grafana_api(method:, url:, password:, body: nil)
        klass = Net::HTTP.const_get method.capitalize
        uri = URI(url)

        resp = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
          req = klass.new(uri)
          req.basic_auth '<PASSWORD>', password
          req['Content-Type'] = 'application/json'
          req.body = body.to_json unless body.nil?
          http.request(req)
        end

        abort "Error #{resp.body}" unless resp.code == '200'
        puts resp.body

        JSON.parse(resp.body)
      end

      def grafana_role(roles)
        role_names = roles.map { |r| r['role'] }
        return 'Admin'  if role_names.include? 'grafana-admin'
        return 'Editor' if role_names.include? 'grafana-editor'
        return 'Viewer' if role_names.include? 'grafana-viewer'
      end

      system_domain = ENV.fetch('SYSTEM_DNS_ZONE_NAME')
      slim          = ENV.fetch('SLIM_DEV_DEPLOYMENT')
      azs           = slim == 'true' ? 1 : 2
      grafana_pass  = ENV.fetch('GRAFANA_PASS')
      aws_account   = ENV.fetch('AWS_ACCOUNT')
      grafana_roles = %w[viewer editor admin'].map { |r| "grafana-#{r}" }

      users = YAML
        .load_file("#{Dir.pwd}/paas-trusted-people/users.yml")
        .fetch('users')

      user_roles = users
        .reject { |u| u['email'].nil? }
        .reject { |u| u.dig('roles', aws_account).nil? }
        .select { |u| u.dig('roles', aws_account) & grafana_roles }
        .map { |u| [u['email'], grafana_role(u.dig('roles', aws_account))] }
        .to_h

      users_that_should_exist = user_roles.keys

      puts 'Users that should exist:'.yellow<commit_msg>Grafana supports multiple roles for same email<commit_after>---
platform: linux
image_resource:
  type: docker-image
  source:
    repository: ruby
    tag: 2.5-slim
inputs:
  - name: paas-trusted-people
run:
  path: ruby
  args:
    - -e
    - |
      # frozen_string_literal: true

      # rubocop:disable Lint/MissingCopEnableDirective
      # rubocop:disable Layout/MultilineMethodCallIndentation
      # rubocop:disable Metrics/MethodLength
      # rubocop:disable Metrics/BlockLength
      # rubocop:disable Metrics/AbcSize
      # rubocop:disable Style/Documentation

      require 'json'
      require 'net/http'
      require 'openssl'
      require 'securerandom'
      require 'set'
      require 'uri'
      require 'yaml'

      class String
        def colour(colour_code)
          "\e[#{colour_code}m#{self}\e[0m"
        end

        def yellow
          colour(33)
        end

        def green
          colour(32)
        end
      end

      def grafana_api(method:, url:, password:, body: nil)
        klass = Net::HTTP.const_get method.capitalize
        uri = URI(url)

        resp = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
          req = klass.new(uri)
          req.basic_auth '<PASSWORD>', password
          req['Content-Type'] = 'application/json'
          req.body = body.to_json unless body.nil?
          http.request(req)
        end

        abort "Error #{resp.body}" unless resp.code == '200'
        puts resp.body

        JSON.parse(resp.body)
      end

      def grafana_role(roles)
        role_names = roles.map { |r| r['role'] }
        return 'Admin'  if role_names.include? 'grafana-admin'
        return 'Editor' if role_names.include? 'grafana-editor'
        return 'Viewer' if role_names.include? 'grafana-viewer'
      end

      system_domain = ENV.fetch('SYSTEM_DNS_ZONE_NAME')
      slim          = ENV.fetch('SLIM_DEV_DEPLOYMENT')
      azs           = slim == 'true' ? 1 : 2
      grafana_pass  = ENV.fetch('GRAFANA_PASS')
      aws_account   = ENV.fetch('AWS_ACCOUNT')
      grafana_roles = %w[viewer editor admin'].map { |r| "grafana-#{r}" }

      users = YAML
        .load_file("#{Dir.pwd}/paas-trusted-people/users.yml")
        .fetch('users')

      # a user might be an admin user and an auditor for different uaa origins
      # we should union the roles with group_by
      user_roles = users
        .reject { |u| u['email'].nil? }
        .reject { |u| u.dig('roles', aws_account).nil? }
        .select { |u| u.dig('roles', aws_account) & grafana_roles }
        .group_by { |u| u['email'] }
        .transform_values { |usrs| usrs.map { |u| u.dig('roles', aws_account) } }
        .transform_values(&:flatten)
        .transform_values { |roles| grafana_role(roles) }

      users_that_should_exist = user_roles.keys

      puts 'Users that should exist:'.yellow
{'fraction_non_alphanumeric': 0.12000736241487207, 'fraction_numerical': 0.004417448923246825, 'mean_word_length': 2.2230130486358246, 'pattern_counts': {'":': 0, '<': 5, '<?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': '655237', 'n_tokens_mistral': 1865, 'n_tokens_neox': 1815, 'n_words': 383}