<commit_before>require 'json'

class MarketplacesController < ApplicationController
  respond_to :js
  include MarketplaceHelper
  include AppsHelper
  ##
  ## index page get all marketplace items from storage(we use riak) using megam_gateway
  ## and show the items in order of category
  ##
  def index
    if current_user_verify
      mkp = get_marketplaces
      @mkp_collection = mkp[:mkp_collection]
      if @mkp_collection.class == Megam::Error
        redirect_to main_dashboards_path, :gflash => { :warning => { :value => "API server may be down. Please contact #{ActionController::Base.helpers.link_to 'support !.', "http://support.megam.co/", :target => "_blank"}.", :sticky => false, :nodom_wrap => true } }
      else
        @categories=[]
        @order=[]
        @order = @mkp_collection.map {|c|
      c.name
      }
        @order = @order.sort_by {|elt| ary = elt.split("-").map(&:to_i); ary[0] + ary[1]}
        @categories = @mkp_collection.map {|c| c.appdetails[:category]}
        @categories = @categories.uniq

      end
    else
      redirect_to signin_path
    end
  end

  ##
  ## to show the selected marketplace item
  ##
  def show
    if current_user_verify
      @pro_name = params[:id].split("-")
      @apps = get_apps
      @mkp = GetMarketplaceApp.perform(force_api[:email], force_api[:api_key], params[:id])
      if @mkp.class == Megam::Error
        redirect_to main_dashboards_path, :gflash => { :warning => { :value => "API server may be down. Please contact #{ActionController::Base.helpers.link_to 'support !.', "http://support.megam.co/", :target => "_blank"}.", :sticky => false, :nodom_wrap => true } }
      else
        @mkp = @mkp.lookup(params[:id])
        @predef_name = get_predef_name(@pro_name[3].downcase)
        @deps_scm = get_deps_scm(@pro_name[3].downcase)
        @my_apps = []

        @type = get_type(@pro_name[3].downcase)
        @version_order=[]
        @version_order = @mkp.plans.map {|c| c["version"]}
        @version_order = @version_order.sort
        respond_to do |format|
          format.js {
            respond_with(@mkp, @version_order, @type, :layout => !request.xhr? )
          }
        end
      end
    else
      redirect_to signin_path
    end
  end

  ##
  ## get all assemblies(it means applications) for that user launched and
  ## list the applications in dashboard
  ##
  def get_apps
    apps = []
    if current_user_verify
      @user_id = current_user["email"]
      @assemblies = ListAssemblies.perform(force_api[:email],force_api[:api_key])
      @service_counter = 0
      @app_counter = 0
      if @assemblies != nil
        @assemblies.each do |asm|
          if asm.class != Megam:: Error
            asm.assemblies.each do |assembly|
              if assembly != nil
                if assembly[0].class != Megam::Error
                  assembly[0].components.each do |com|
                    if com != nil
                      com.each do |c|
                        com_type = c.tosca_type.split(".")
                        ctype = get_type(com_type[2])
                        if ctype == "APP" && com[0].related_components == ""
                          apps << {"name" => assembly[0].name + "." + assembly[0].components[0][0].inputs[:domain] + "/" + com[0].name, "aid" => assembly[0].id, "cid" => assembly[0].components[0][0].id }
                        end
                      end
                    end
                  end
                  assembly[0].components.each do |com|
                    if com != nil
                      com.each do |c|
                        com_type = c.tosca_type.split(".")
                        ctype = get_type(com_type[2])
                        if ctype == "APP"
                          @app_counter = @app_counter + 1
                        end
                      end
                    end
                  end
                end
              end
            end
          end
        end
      end
    else
      redirect_to signin_path
    end
    apps
  end

=begin
def authorize_scm
logger.debug "CloudBooks:authorize_scm, entry"
auth_token = request.env['omniauth.auth']['credentials']['token']
github = Github.new oauth_token: auth_token
git_array = github.repos.all.collect { |repo| repo.clone_url }
@repos = git_array
render :template => "apps/new", :locals => {:repos => @repos}

#session[:info] = request.env['omniauth.auth']['credentials']
auth_token = request.env['omniauth.auth']['credentials']['token']
github = Github.new oauth_token: auth_token
git_array = github.repos.all.collect { |repo| repo.clone_url }
@repos = git_array

# @repos
end

=end

  ##
  ## after finish the github authentication the callback url comes this method
  ## this function parse the request and get the github credentials
  ## and store that credentials to session
  ##
  def github_scm
    if current_user.nil?
      redirect_to :controller=>'sessions', :action=>'create'
    else
      @auth_token = request.env['omniauth.auth']['credentials']['token']
      session[:github] =  @auth_token
      session[:git_owner] = request.env['omniauth.auth']['extra']['raw_info']['login']
    end
  end

  ##
  ## this method collect all repositories for user using oauth token
  ##
  def github_sessions
    auth_id = params['id']
    github = Github.new oauth_token: auth_id
    git_array = github.repos.all.collect { |repo| repo.clone_url }
    @repos = git_array
    respond_to do |format|
      format.js {
        respond_with(@repos, :layout => !request.xhr? )
      }
    end
  end

  ##
  ## get session data and sends to UI
  ##
  def github_sessions_data
    @tokens_gh = session[:github]
    render :text => @tokens_gh
  end

  def gogs
  end

  ##
  ## gogswindow html page method
  ##
  def gogswindow
  end

  ##
  ## get the repositories from session
  ##
  def gogs_sessions
    @repos = session[:gogs_repos]
    respond_to do |format|
      format.js {
        respond_with(@repos, :layout => !request.xhr? )
      }
    end
  end

  ##
  ## this function get the gogs token using username and password
  ## then list the repositories using oauth tokens
  ##
  def gogs_return
    session[:gogs_owner] = params[:gogs_username]
    tokens = ListGogsTokens.perform(params[:gogs_username], params[:gogs_password])
    obj = JSON.parse(tokens)
    token = obj[0]["sha1"]
    session[:gogs_token] = token
    @gogs_repos = ListGogsRepo.perform(token)
    obj_repo = JSON.parse(@gogs_repos)
    @repos_arr = []
    obj_repo.each do |one_repo|
      @repos_arr << one_repo["clone_url"]
    end
    session[:gogs_repos] =  @repos_arr 
  end
<commit_msg>testing<commit_after>require 'json'

class MarketplacesController < ApplicationController
  respond_to :js
  include MarketplaceHelper
  include AppsHelper
  ##
  ## index page get all marketplace items from storage(we use riak) using megam_gateway
  ## and show the items in order of category
  ##
  def index
    if current_user_verify
      mkp = get_marketplaces
      @mkp_collection = mkp[:mkp_collection]
      if @mkp_collection.class == Megam::Error
        redirect_to main_dashboards_path, :gflash => { :warning => { :value => "API server may be down. Please contact #{ActionController::Base.helpers.link_to 'support !.', "http://support.megam.co/", :target => "_blank"}.", :sticky => false, :nodom_wrap => true } }
      else
        @categories=[]
        @order=[]
        @order = @mkp_collection.map {|c|
      c.name
      }
        @order = @order.sort_by {|elt| ary = elt.split("-").map(&:to_i); ary[0] + ary[1]}
        @categories = @mkp_collection.map {|c| c.appdetails[:category]}
        @categories = @categories.uniq

      end
    else
      redirect_to signin_path
    end
  end

  ##
  ## to show the selected marketplace item
  ##
  def show
    if current_user_verify
      @pro_name = params[:id].split("-")
      @apps = get_apps
      @mkp = GetMarketplaceApp.perform(force_api[:email], force_api[:api_key], params[:id])
      if @mkp.class == Megam::Error
        redirect_to main_dashboards_path, :gflash => { :warning => { :value => "API server may be down. Please contact #{ActionController::Base.helpers.link_to 'support !.', "http://support.megam.co/", :target => "_blank"}.", :sticky => false, :nodom_wrap => true } }
      else
        @mkp = @mkp.lookup(params[:id])
        @predef_name = get_predef_name(@pro_name[3].downcase)
        @deps_scm = get_deps_scm(@pro_name[3].downcase)
        @my_apps = []

        @type = get_type(@pro_name[3].downcase)
        @version_order=[]
        @version_order = @mkp.plans.map {|c| c["version"]}
        @version_order = @version_order.sort
        respond_to do |format|
          format.js {
            respond_with(@mkp, @version_order, @type, :layout => !request.xhr? )
          }
        end
      end
    else
      redirect_to signin_path
    end
  end

  ##
  ## get all assemblies(it means applications) for that user launched and
  ## list the applications in dashboard
  ##
  def get_apps
    apps = []
    if current_user_verify
      @user_id = current_user["email"]
      @assemblies = ListAssemblies.perform(force_api[:email],force_api[:api_key])
      @service_counter = 0
      @app_counter = 0
      if @assemblies != nil
        @assemblies.each do |asm|
          if asm.class != Megam:: Error
            asm.assemblies.each do |assembly|
              if assembly != nil
                if assembly[0].class != Megam::Error
                  assembly[0].components.each do |com|
                    if com != nil
                      com.each do |c|
                        com_type = c.tosca_type.split(".")
                        ctype = get_type(com_type[2])
                        if ctype == "APP" && com[0].related_components == ""
                          apps << {"name" => assembly[0].name + "." + assembly[0].components[0][0].inputs[:domain] + "/" + com[0].name, "aid" => assembly[0].id, "cid" => assembly[0].components[0][0].id }
                        end
                      end
                    end
                  end
                  assembly[0].components.each do |com|
                    if com != nil
                      com.each do |c|
                        com_type = c.tosca_type.split(".")
                        ctype = get_type(com_type[2])
                        if ctype == "APP"
                          @app_counter = @app_counter + 1
                        end
                      end
                    end
                  end
                end
              end
            end
          end
        end
      end
    else
      redirect_to signin_path
    end
    apps
  end

=begin
def authorize_scm
logger.debug "CloudBooks:authorize_scm, entry"
auth_token = request.env['omniauth.auth']['credentials']['token']
github = Github.new oauth_token: auth_token
git_array = github.repos.all.collect { |repo| repo.clone_url }
@repos = git_array
render :template => "apps/new", :locals => {:repos => @repos}

#session[:info] = request.env['omniauth.auth']['credentials']
auth_token = request.env['omniauth.auth']['credentials']['token']
github = Github.new oauth_token: auth_token
git_array = github.repos.all.collect { |repo| repo.clone_url }
@repos = git_array

# @repos
end

=end

  ##
  ## after finish the github authentication the callback url comes this method
  ## this function parse the request and get the github credentials
  ## and store that credentials to session
  ##
  def github_scm
    if current_user.nil?
      redirect_to :controller=>'sessions', :action=>'create'
    else
      @auth_token = request.env['omniauth.auth']['credentials']['token']
      session[:github] =  @auth_token
      session[:git_owner] = request.env['omniauth.auth']['extra']['raw_info']['login']
    end
  end

  ##
  ## this method collect all repositories for user using oauth token
  ##
  def github_sessions
    auth_id = params['id']
    github = Github.new oauth_token: auth_id
    git_array = github.repos.all.collect { |repo| repo.clone_url }
    @repos = git_array
    respond_to do |format|
      format.js {
        respond_with(@repos, :layout => !request.xhr? )
      }
    end
  end

  ##
  ## get session data and sends to UI
  ##
  def github_sessions_data
    @tokens_gh = session[:github]
    render :text => @tokens_gh
  end

  def gogs
  end

  ##
  ## gogswindow html page method
  ##
  def gogswindow
  end

  ##
  ## get the repositories from session
  ##
  def gogs_sessions
    @repos = session[:gogs_repos]
    respond_to do |format|
      format.js {
        respond_with(@repos, :layout => !request.xhr? )
      }
    end
  end

  ##
  ## this function get the gogs token using username and password
  ## then list the repositories using oauth tokens.
  ##
  def gogs_return
    session[:gogs_owner] = params[:gogs_username]
    tokens = ListGogsTokens.perform(params[:gogs_username], params[:gogs_password])
    obj = JSON.parse(tokens)
    token = obj[0]["sha1"]
    session[:gogs_token] = token
    @gogs_repos = ListGogsRepo.perform(token)
    obj_repo = JSON.parse(@gogs_repos)
    @repos_arr = []
    obj_repo.each do |one_repo|
      @repos_arr << one_repo["clone_url"]
    end
    session[:gogs_repos] =  @repos_arr 
  end

{'fraction_non_alphanumeric': 0.12319879290833648, 'fraction_numerical': 0.003772161448509996, 'mean_word_length': 2.3024414549078225, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 51, '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': '4217544', 'n_tokens_mistral': 4242, 'n_tokens_neox': 4067, 'n_words': 960}