<commit_before>#!/usr/bin/env ruby
require 'rubygems'
gem 'soap4r'
require 'jira_lib/defaultDriver.rb'

class JiraInterface
  ENDPOINT_URL = JIRA_URL + "/issues/rpc/soap/jirasoapservice-v2"

  MEMBERS = "all"
  LOGIN = 'forgeadmin'
  PASSWORD = '<PASSWORD>'

  ADMIN_GROUP_PERMISSIONS = {
   23 => :administer_projects,
   30 => :modify_reporter,
   16 => :delete_issues,
   32 => :manage_watcher_list,
   36 => :delete_all_comments,
   38 => :delete_all_attachments,
   43 => :delete_all_worklogs
  }

  USER_GROUP_PERMISSIONS = {

   29 => :view_version_control,
   10 => :browse_projects, 
   11 => :create_issues,
   12 => :edit_issues,
   28 => :schedule_issues,
   25 => :move_issues,
   13 => :assign_issues,
   17 => :assignable_user, 
   14 => :resolve_issues,
   18 => :close_issues,
   21 => :link_issues,
   31 => :view_voters_and_watchers,
   15 => :add_comments,
   34 => :edit_all_comments,
   35 => :edit_own_comments,
   37 => :delete_own_comments,
   19 => :create_attachments,
   39 => :delete_own_attachments,
   20 => :work_on_issues,
   40 => :edit_own_worklogs,
   41 => :edit_all_worklogs,
   42 => :delete_own_worklogs
  }

  UNASSIGNED_PERMISSIONS = {
    26 => :set_issue_security
  }

  def initialize
    @jira_soap_service = JiraSoapService.new(ENDPOINT_URL)

#      if ENDPOINT_URL =~ /forge\.fusesource\./
#      relm = "http://forge.fusesource.com/" 
#      username = "fuseforge" 
#      password = "<PASSWORD>" 
#      @jira_soap_service.options["protocol.http.basic_auth"] << [relm, username, password]    
#    elsif ENDPOINT_URL =~ /forge\.fusesourcedev\./
#      relm = "http://forge.fusesourcedev.com/" 
#      username = "fuseforge" 
#      password = "<PASSWORD>" 
#      @jira_soap_service.options["protocol.http.basic_auth"] << [relm, username, password]    
#    end

    @ctx = login
  end
  
  def login
    @jira_soap_service.login(LOGIN, PASSWORD)
  end  
  
  #if private_project is true then make it a private project from non-private project
  #if private_project is false then make it a non-private project from private project  
  def update_project(project_key , private_project)

  
    proj_id = @jira_soap_service.getProjectByKey(@ctx,project_key).id
    project = @jira_soap_service.getProjectWithSchemesById(@ctx,proj_id)
    old_perm_scheme_name = project.permissionScheme.name

    #if the permission scheme is Default Permission Scheme(a scenario where the user  changed the permission scheme
    #using the JIRA UI), don't delete the default permission scheme, but go ahead and create anothe permission scheme
    #
    @jira_soap_service.deletePermissionScheme(@ctx,old_perm_scheme_name) unless old_perm_scheme_name == 'Default Permission Scheme'
    
    new_perm_scheme = @jira_soap_service.createPermissionScheme(@ctx, "#{project_key}-scheme","Permission scheme for #{project_key}")

    proj_groups = ApplicationHelper.get_project_groups(project_key)
    forge_admin =  @jira_soap_service.getGroup(@ctx,ApplicationHelper.get_forge_admins_group)
    forge_users_grp = @jira_soap_service.getGroup(@ctx,ApplicationHelper.get_forge_jira_group)


    assign_permissions_to_perm_scheme(new_perm_scheme,forge_admin,proj_groups,forge_users_grp,private_project)
    
    project.permissionScheme = new_perm_scheme
    @jira_soap_service.updateProject(@ctx,project)
    @jira_soap_service.logout(@ctx)
  end
  
  def assign_permissions_to_perm_scheme(new_perm_scheme,forge_admin,proj_groups,forge_users_grp,private_project)
    admin_grp = @jira_soap_service.getGroup(@ctx,proj_groups[:admins_grp].downcase)
    user_grp =  @jira_soap_service.getGroup(@ctx,proj_groups[:membrs_grp].downcase)
    
    @jira_soap_service.getAllPermissions(@ctx).each do |perm| 
      @jira_soap_service.addPermissionTo(@ctx,new_perm_scheme,perm,forge_admin)
      
      if ADMIN_GROUP_PERMISSIONS.include?(perm.permission)
        @jira_soap_service.addPermissionTo(@ctx, new_perm_scheme, perm,admin_grp)
      elsif USER_GROUP_PERMISSIONS.include?(perm.permission)
        if private_project == true
          @jira_soap_service.addPermissionTo(@ctx,new_perm_scheme,perm,user_grp)
        else 
          @jira_soap_service.addPermissionTo(@ctx,new_perm_scheme,perm,forge_users_grp)                      
        end
      end
    end
  end
  
  def make_existing_project_private(project_key, admin_group_name, user_group_name)
    admin_group = @jira_soap_service.getGroup(@ctx, admin_group_name.downcase)
    user_group =  @jira_soap_service.getGroup(@ctx, user_group_name.downcase)
    project_id = @jira_soap_service.getProjectByKey(@ctx, project_key).id
    project = @jira_soap_service.getProjectWithSchemesById(@ctx, project_id)
    perm_scheme = project.permissionScheme

    raise "Cannot make project with Default Permission Scheme private!" if perm_scheme.name == 'Default Permission Scheme'
    
    @jira_soap_service.getAllPermissions(@ctx).each do |perm| 
logger.info '--------------------------------------------------------'
logger.info perm.inspect      
      begin
        @jira_soap_service.deletePermissionFrom(@ctx, perm_scheme, perm, admin_group)
        @jira_soap_service.deletePermissionFrom(@ctx, perm_scheme, perm, user_group)
      rescue => e
logger.info e.inspect        
      end
logger.info '---------------------------------------------------------'      
    end
  end
  
  def project_name_exists?(project_name)
    @jira_soap_service.getProjectsNoSchemes(@ctx).any? { |remote_project| remote_project.name == project_name }
  end

  def project_key_exists?(project_key)
    @jira_soap_service.getProjectsNoSchemes(@ctx).any? { |remote_project| remote_project.key == project_key }
  end

  def remove_project(project_id)
    @jira_soap_service.deleteProject(@ctx,project_id)
  end
  
  def is_project_name_key_unique?(project_name, project_key)
    <commit_msg>added code to inactivate a project<commit_after>#!/usr/bin/env ruby
require 'rubygems'
gem 'soap4r'
require 'jira_lib/defaultDriver.rb'

class JiraInterface
  ENDPOINT_URL = JIRA_URL + "/issues/rpc/soap/jirasoapservice-v2"

  MEMBERS = "all"
  LOGIN = 'forgeadmin'
  PASSWORD = '<PASSWORD>'

  ADMIN_GROUP_PERMISSIONS = {
   23 => :administer_projects,
   30 => :modify_reporter,
   16 => :delete_issues,
   32 => :manage_watcher_list,
   36 => :delete_all_comments,
   38 => :delete_all_attachments,
   43 => :delete_all_worklogs
  }

  USER_GROUP_PERMISSIONS = {

   29 => :view_version_control,
   10 => :browse_projects, 
   11 => :create_issues,
   12 => :edit_issues,
   28 => :schedule_issues,
   25 => :move_issues,
   13 => :assign_issues,
   17 => :assignable_user, 
   14 => :resolve_issues,
   18 => :close_issues,
   21 => :link_issues,
   31 => :view_voters_and_watchers,
   15 => :add_comments,
   34 => :edit_all_comments,
   35 => :edit_own_comments,
   37 => :delete_own_comments,
   19 => :create_attachments,
   39 => :delete_own_attachments,
   20 => :work_on_issues,
   40 => :edit_own_worklogs,
   41 => :edit_all_worklogs,
   42 => :delete_own_worklogs
  }

  UNASSIGNED_PERMISSIONS = {
    26 => :set_issue_security
  }

  def initialize
    @jira_soap_service = JiraSoapService.new(ENDPOINT_URL)

#      if ENDPOINT_URL =~ /forge\.fusesource\./
#      relm = "http://forge.fusesource.com/" 
#      username = "fuseforge" 
#      password = "<PASSWORD>" 
#      @jira_soap_service.options["protocol.http.basic_auth"] << [relm, username, password]    
#    elsif ENDPOINT_URL =~ /forge\.fusesourcedev\./
#      relm = "http://forge.fusesourcedev.com/" 
#      username = "fuseforge" 
#      password = "<PASSWORD>" 
#      @jira_soap_service.options["protocol.http.basic_auth"] << [relm, username, password]    
#    end

    @ctx = login
  end
  
  def login
    @jira_soap_service.login(LOGIN, PASSWORD)
  end  
  
  #if private_project is true then make it a private project from non-private project
  #if private_project is false then make it a non-private project from private project  
  def update_project(project_key , private_project)

  
    proj_id = @jira_soap_service.getProjectByKey(@ctx,project_key).id
    project = @jira_soap_service.getProjectWithSchemesById(@ctx,proj_id)
    old_perm_scheme_name = project.permissionScheme.name

    #if the permission scheme is Default Permission Scheme(a scenario where the user  changed the permission scheme
    #using the JIRA UI), don't delete the default permission scheme, but go ahead and create anothe permission scheme
    #
    @jira_soap_service.deletePermissionScheme(@ctx,old_perm_scheme_name) unless old_perm_scheme_name == 'Default Permission Scheme'
    
    new_perm_scheme = @jira_soap_service.createPermissionScheme(@ctx, "#{project_key}-scheme","Permission scheme for #{project_key}")

    proj_groups = ApplicationHelper.get_project_groups(project_key)
    forge_admin =  @jira_soap_service.getGroup(@ctx,ApplicationHelper.get_forge_admins_group)
    forge_users_grp = @jira_soap_service.getGroup(@ctx,ApplicationHelper.get_forge_jira_group)


    assign_permissions_to_perm_scheme(new_perm_scheme,forge_admin,proj_groups,forge_users_grp,private_project)
    
    project.permissionScheme = new_perm_scheme
    @jira_soap_service.updateProject(@ctx,project)
    @jira_soap_service.logout(@ctx)
  end
  
  def assign_permissions_to_perm_scheme(new_perm_scheme,forge_admin,proj_groups,forge_users_grp,private_project)
    admin_grp = @jira_soap_service.getGroup(@ctx,proj_groups[:admins_grp].downcase)
    user_grp =  @jira_soap_service.getGroup(@ctx,proj_groups[:membrs_grp].downcase)
    
    @jira_soap_service.getAllPermissions(@ctx).each do |perm| 
      @jira_soap_service.addPermissionTo(@ctx,new_perm_scheme,perm,forge_admin)
      
      if ADMIN_GROUP_PERMISSIONS.include?(perm.permission)
        @jira_soap_service.addPermissionTo(@ctx, new_perm_scheme, perm,admin_grp)
      elsif USER_GROUP_PERMISSIONS.include?(perm.permission)
        if private_project == true
          @jira_soap_service.addPermissionTo(@ctx,new_perm_scheme,perm,user_grp)
        else 
          @jira_soap_service.addPermissionTo(@ctx,new_perm_scheme,perm,forge_users_grp)                      
        end
      end
    end
  end
  
  def make_existing_project_private(project_key, admin_group_name, user_group_name)
    admin_group = @jira_soap_service.getGroup(@ctx, admin_group_name.downcase)
    user_group =  @jira_soap_service.getGroup(@ctx, user_group_name.downcase)
    project_id = @jira_soap_service.getProjectByKey(@ctx, project_key).id
    project = @jira_soap_service.getProjectWithSchemesById(@ctx, project_id)
    perm_scheme = project.permissionScheme

    raise "Cannot make project with Default Permission Scheme private!" if perm_scheme.name == 'Default Permission Scheme'
    
    @jira_soap_service.getAllPermissions(@ctx).each do |perm| 
puts '--------------------------------------------------------'
puts perm.inspect      
      begin
        @jira_soap_service.deletePermissionFrom(@ctx, perm_scheme, perm, admin_group)
        @jira_soap_service.deletePermissionFrom(@ctx, perm_scheme, perm, user_group)
      rescue => e
puts e.inspect        
      end
puts '---------------------------------------------------------'      
    end
  end
  
  def project_name_exists?(project_name)
    @jira_soap_service.getProjectsNoSchemes(@ctx).any? { |remote_project| remote_project.name == project_name }
  end

  def project_key_exists?(project_key)
    @jira_soap_service.getProjectsNoSchemes(@ctx).any? { |remote_project| remote_project.key == project_key }
  end

  def remove_project(project_id)
    @jira_soap_service.deleteProject(@ctx,project_id)
  end
  
  def is_project_name_key_unique?(project_name, project_key)
    
{'fraction_non_alphanumeric': 0.11683083511777302, 'fraction_numerical': 0.010620985010706638, 'mean_word_length': 3.844813278008299, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 71, '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': '3805195', 'n_tokens_mistral': 4023, 'n_tokens_neox': 3858, 'n_words': 641}