<commit_before>class Project < ActiveRecord::Base
  store_accessor :meta, :github_repository_id, :github_name, :github_full_name,
    :is_github_repository, :is_bitbucket_repository, :bitbucket_name, :bitbucket_owner,
    :bitbucket_slug, :bitbucket_full_name, :github_secret_token_for_hook, :bitbucket_secret_token_for_hook,
    :github_url, :gitlab_url, :gitlab_repository_id, :gitlab_name, :gitlab_full_name, :is_gitlab_repository,
    :gitlab_secret_token_for_hook

  has_many :users, :through => :user_to_project_connections

  has_many :user_to_project_connections, :dependent => :destroy

  has_many :issues, :dependent => :destroy

  has_many :sections, :dependent => :destroy

  has_many :columns, :dependent => :destroy

  has_many :issue_to_section_connections

  has_many :boards, :through => :project_to_board_connections

  has_many :project_to_board_connections, :dependent => :destroy

  validates :name, :length => { :maximum => Settings.max_string_field_size }, :presence => true

  after_save :update_issues

  %w(gitlab github bitbucket).each do |provider|
    define_method "fetch_and_remove_hook_from_#{ provider }" do |client|
      hook = send("fetch_hook_from_#{ provider }", client)

      send("remove_hook_from_#{ provider }", client, hook) if hook.present?
    end

    define_method "create_#{ provider }_hook" do |client|
      unless send("#{ provider }_secret_token_for_hook").present?
        update_attributes("#{ provider }_secret_token_for_hook" => SecureRandom.hex(20))
      end

      save

      hook = send("fetch_hook_from_#{ provider }", client)

      send("create_hook_to_#{ provider }", client) unless hook.present?
    end
  end

  def remove_hook_from_bitbucket(authentication, hook)
    BitBucket::Repos::Webhooks.new(:oauth_token => authentication.token,
      :oauth_secret => authentication.secret).delete(bitbucket_owner,
        bitbucket_slug, hook.first.uuid[1...-1])
  end

  def remove_hook_from_gitlab(client, hook)
    client.delete_project_hook(gitlab_repository_id, hook.id)
  end

  def remove_hook_from_github(client, hook)
    client.remove_hook(github_full_name, hook.id)
  end

  def parse_issue_params_from_github_webhook(params)
    issue = issues.find_by("meta ->> 'github_issue_id' = '?'", params[:id].to_i)

    issue = issues.build.tap { |i| i.github_issue_id = params[:id].to_i } unless issue.present?

    issue.assign_attributes_from_github_hook(params)

    issue.save!
  end

  def parse_issue_params_from_bitbucket_webhook(params)
    issue = issues.find_by("meta ->> 'bitbucket_issue_id' = '?'", params[:id])

    issue = issues.build.tap { |i| i.github_issue_id = params[:id] } unless issue.present?

    issue.assign_attributes(
      :title => params[:title],
      :body => params[:content][:raw]
    )

    issue.save!
  end

  def parse_issue_params_from_gitlab_webhook(params)
    issue = issues.find_by("meta ->> 'gitlab_issue_id' = '?'", params[:id].to_i)

    issue = issues.build.tap { |i| i.gitlab_issue_id = params[:id].to_i } unless issue.present?

    issue.assign_attributes(:title => params[:title], :body => params[:description])

    issue.save!
  end

  def fetch_hook_from_github(client)
    client.hooks(github_full_name).select do |hook|
      hook.config[:url] == Rails.application.routes.url_helpers.
        payload_from_github_project_url(id, :host => Settings.webhook_host)
    end.first
  end

  def fetch_hook_from_bitbucket(authentication)
    result = list_bitbucket_hooks(authentication)

    result[:values].select { |h| h[:description] == 'kanbanonrails' }
  end

  def fetch_hook_from_gitlab(client)
    client.project_hooks(gitlab_repository_id).select do |hook|
      hook.url == Rails.application.routes.url_helpers.
        payload_from_gitlab_project_url(id, :secure_token => gitlab_secret_token_for_hook,
          :host => Settings.webhook_host)<commit_msg>Log not found hooks from github<commit_after>class Project < ActiveRecord::Base
  store_accessor :meta, :github_repository_id, :github_name, :github_full_name,
    :is_github_repository, :is_bitbucket_repository, :bitbucket_name, :bitbucket_owner,
    :bitbucket_slug, :bitbucket_full_name, :github_secret_token_for_hook, :bitbucket_secret_token_for_hook,
    :github_url, :gitlab_url, :gitlab_repository_id, :gitlab_name, :gitlab_full_name, :is_gitlab_repository,
    :gitlab_secret_token_for_hook

  has_many :users, :through => :user_to_project_connections

  has_many :user_to_project_connections, :dependent => :destroy

  has_many :issues, :dependent => :destroy

  has_many :sections, :dependent => :destroy

  has_many :columns, :dependent => :destroy

  has_many :issue_to_section_connections

  has_many :boards, :through => :project_to_board_connections

  has_many :project_to_board_connections, :dependent => :destroy

  validates :name, :length => { :maximum => Settings.max_string_field_size }, :presence => true

  after_save :update_issues

  %w(gitlab github bitbucket).each do |provider|
    define_method "fetch_and_remove_hook_from_#{ provider }" do |client|
      hook = send("fetch_hook_from_#{ provider }", client)

      send("remove_hook_from_#{ provider }", client, hook) if hook.present?
    end

    define_method "create_#{ provider }_hook" do |client|
      unless send("#{ provider }_secret_token_for_hook").present?
        update_attributes("#{ provider }_secret_token_for_hook" => SecureRandom.hex(20))
      end

      save

      hook = send("fetch_hook_from_#{ provider }", client)

      send("create_hook_to_#{ provider }", client) unless hook.present?
    end
  end

  def remove_hook_from_bitbucket(authentication, hook)
    BitBucket::Repos::Webhooks.new(:oauth_token => authentication.token,
      :oauth_secret => authentication.secret).delete(bitbucket_owner,
        bitbucket_slug, hook.first.uuid[1...-1])
  end

  def remove_hook_from_gitlab(client, hook)
    client.delete_project_hook(gitlab_repository_id, hook.id)
  end

  def remove_hook_from_github(client, hook)
    client.remove_hook(github_full_name, hook.id)
  end

  def parse_issue_params_from_github_webhook(params)
    issue = issues.find_by("meta ->> 'github_issue_id' = '?'", params[:id].to_i)

    issue = issues.build.tap { |i| i.github_issue_id = params[:id].to_i } unless issue.present?

    issue.assign_attributes_from_github_hook(params)

    issue.save!
  end

  def parse_issue_params_from_bitbucket_webhook(params)
    issue = issues.find_by("meta ->> 'bitbucket_issue_id' = '?'", params[:id])

    issue = issues.build.tap { |i| i.github_issue_id = params[:id] } unless issue.present?

    issue.assign_attributes(
      :title => params[:title],
      :body => params[:content][:raw]
    )

    issue.save!
  end

  def parse_issue_params_from_gitlab_webhook(params)
    issue = issues.find_by("meta ->> 'gitlab_issue_id' = '?'", params[:id].to_i)

    issue = issues.build.tap { |i| i.gitlab_issue_id = params[:id].to_i } unless issue.present?

    issue.assign_attributes(:title => params[:title], :body => params[:description])

    issue.save!
  end

  def fetch_hook_from_github(client)
    client.hooks(github_full_name).select do |hook|
      hook.config[:url] == Rails.application.routes.url_helpers.
        payload_from_github_project_url(id, :host => Settings.webhook_host)
    end.first
  rescue Octokit::NotFound
    Rails.logger.info "Octokit::NotFound on fetch hooks from project id #{ id }"
  end

  def fetch_hook_from_bitbucket(authentication)
    result = list_bitbucket_hooks(authentication)

    result[:values].select { |h| h[:description] == 'kanbanonrails' }
  end

  def fetch_hook_from_gitlab(client)
    client.project_hooks(gitlab_repository_id).select do |hook|
      hook.url == Rails.application.routes.url_helpers.
        payload_from_gitlab_project_url(id, :secure_token => gitlab_secret_token_for_hook,
          :host => Settings.webhook_host)
{'fraction_non_alphanumeric': 0.11675963488843813, 'fraction_numerical': 0.0010141987829614604, 'mean_word_length': 4.762600438276114, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, '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': '1031658', 'n_tokens_mistral': 2661, 'n_tokens_neox': 2585, 'n_words': 455}