<commit_before># frozen_string_literal: true

require 'text'

namespace :org_cleanup do
  desc 'Merge the first org_id into the second org_id'
  task :merge_orgs, %i[loser winner commit] => [:environment] do |_t, args|
    commit_it = args[:commit].to_s == 'true'
    if args[:loser].present? && args[:winner].present?
      loser = Org.includes(:templates, :tracker, :annotations,
                           :departments, :token_permission_types, :funded_plans,
                           identifiers: [:identifier_scheme],
                           guidance_groups: [guidances: [:themes]],
                           users: [identifiers: [:identifier_scheme]])
                 .find_by(id: args[:loser])

      winner = Org.includes(:templates, :tracker, :annotations,
                            :departments, :token_permission_types, :funded_plans,
                            identifiers: [:identifier_scheme],
                            guidance_groups: [guidances: [:themes]],
                            users: [identifiers: [:identifier_scheme]])
                  .find_by(id: args[:winner])

      if loser.present? && winner.present?
        p 'Analyizing merge ... '
        p "    Org that will go away - id: #{loser.id}, name: #{loser.name}"
        p "        annotations that will be moved: #{loser.annotations&.length || 0}"
        p "        departments that will be moved: #{loser.departments&.length || 0}"
        p "        funded plans that will be moved: #{loser.funded_plans&.length || 0}"
        p "        identifiers that will be moved: #{loser.identifiers&.length || 0}"
        p "        guidance_groups that will be moved: #{loser.identifiers&.length || 0}"
        p "        templates that will be moved: #{loser.templates&.length || 0}"
        p "        tracker codes that will be moved: #{loser.tracker.present? ? 1 : 0}"
        p "        users that will be moved: #{loser.users&.length || 0}"

        p "   Org that will remain - id: #{winner.id}, name: #{winner.name}"
        p ''

        if commit_it
          if winner.merge!(to_be_merged: loser)
            p 'Merge complete'
          else
            p 'Something went wrong during thr merge. All changes have been rolled back. Check the logs for more details.'
          end
        else
          p "To commit these changes, please run `rails 'org_cleanup:merge_orgs[#{loser.id},#{winner.id},true]"
        end
      else
        p 'Unable to merge orgs!'
        p 'Unable to find org to merge (the one that will go away)' unless loser.present?
        p 'Unable to find org to merge to (the one that will remain)' unless winner.present?
      end
    else
      p 'Unable to merge orgs!'
      p 'Expected 2 org ids. The 1st representing the Org that will be merged (go away) and the 2nd being the Org that will remain.'
      p 'For example: rails "org_cleanup:merge_orgs[1,2,false]"'
    end
  end

  desc 'Detect duplicate Orgs (RegistryOrg prioritization)'
  task detect_dups: :environment do
    registry_orgs = RegistryOrg.all.map do |registry_org|
      name_parts = registry_org.name.split('(')
      {
        id: registry_org.id,
        org_id: registry_org.org_id,
        ror: registry_org.ror_id,
        fundref: registry_org.fundref_id,
        name: registry_org.name,
        cleansed: remove_stop_words(name: name_parts.first.strip.downcase),
        domain: name_parts.length > 1 ? name_parts.last.gsub(')', '') : nil,
        homepage: registry_org.home_page,
        acronyms: registry_org.acronyms,
        aliases: registry_org.aliases.map { |name| remove_stop_words(name: name) },<commit_msg>fixed rubocop<commit_after># frozen_string_literal: true

require 'text'

namespace :org_cleanup do
  desc 'Merge the first org_id into the second org_id'
  task :merge_orgs, %i[loser winner commit] => [:environment] do |_t, args|
    commit_it = args[:commit].to_s == 'true'
    if args[:loser].present? && args[:winner].present?
      loser = Org.includes(:templates, :tracker, :annotations,
                           :departments, :token_permission_types, :funded_plans,
                           identifiers: [:identifier_scheme],
                           guidance_groups: [guidances: [:themes]],
                           users: [identifiers: [:identifier_scheme]])
                 .find_by(id: args[:loser])

      winner = Org.includes(:templates, :tracker, :annotations,
                            :departments, :token_permission_types, :funded_plans,
                            identifiers: [:identifier_scheme],
                            guidance_groups: [guidances: [:themes]],
                            users: [identifiers: [:identifier_scheme]])
                  .find_by(id: args[:winner])

      if loser.present? && winner.present?
        p 'Analyizing merge ... '
        p "    Org that will go away - id: #{loser.id}, name: #{loser.name}"
        p "        annotations that will be moved: #{loser.annotations&.length || 0}"
        p "        departments that will be moved: #{loser.departments&.length || 0}"
        p "        funded plans that will be moved: #{loser.funded_plans&.length || 0}"
        p "        identifiers that will be moved: #{loser.identifiers&.length || 0}"
        p "        guidance_groups that will be moved: #{loser.identifiers&.length || 0}"
        p "        templates that will be moved: #{loser.templates&.length || 0}"
        p "        tracker codes that will be moved: #{loser.tracker.present? ? 1 : 0}"
        p "        users that will be moved: #{loser.users&.length || 0}"

        p "   Org that will remain - id: #{winner.id}, name: #{winner.name}"
        p ''

        if commit_it
          # rubocop:disable Metrics/BlockNesting
          if winner.merge!(to_be_merged: loser)
            p 'Merge complete'
          else
            # rubocop:disable Layout/LineLength
            p 'Something went wrong during thr merge. All changes have been rolled back. Check the logs for more details.'
            # rubocop:enable Layout/LineLength
          end
          # rubocop:enable Metrics/BlockNesting
        else
          p "To commit these changes, please run `rails 'org_cleanup:merge_orgs[#{loser.id},#{winner.id},true]"
        end
      else
        p 'Unable to merge orgs!'
        p 'Unable to find org to merge (the one that will go away)' unless loser.present?
        p 'Unable to find org to merge to (the one that will remain)' unless winner.present?
      end
    else
      p 'Unable to merge orgs!'
      # rubocop:disable Layout/LineLength
      p 'Expected 2 org ids. The 1st representing the Org that will be merged (go away) and the 2nd being the Org that will remain.'
      # rubocop:enable Layout/LineLength
      p 'For example: rails "org_cleanup:merge_orgs[1,2,false]"'
    end
  end

  desc 'Detect duplicate Orgs (RegistryOrg prioritization)'
  task detect_dups: :environment do
    registry_orgs = RegistryOrg.all.map do |registry_org|
      name_parts = registry_org.name.split('(')
      {
        id: registry_org.id,
        org_id: registry_org.org_id,
        ror: registry_org.ror_id,
        fundref: registry_org.fundref_id,
        name: registry_org.name,
        cleansed: remove_stop_words(name: name_parts.first.strip.downcase),
        domain: name_parts.length > 1 ? name_parts.last.gsub(')', '') : nil,
        homepage: registry_org.home_page,
        acronyms: registry_org.acronyms,
        aliases: registry_org.aliases.map { |name| remove_stop_words(name: name) },
{'fraction_non_alphanumeric': 0.10832337714058145, 'fraction_numerical': 0.00398247710075667, 'mean_word_length': 2.244616709732989, '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': '6697186', 'n_tokens_mistral': 2258, 'n_tokens_neox': 2185, 'n_words': 647}