<commit_before>class Issue < ActiveRecord::Base
  include EmptyArrayRemovable

  store_accessor :meta, :github_issue_id, :github_issue_number, :bitbucket_issue_id,
    :github_issue_comments_count, :github_issue_html_url, :github_labels, :bitbucket_issue_comment_count,
    :gitlab_issue_id

  has_many :users, :through => :user_to_issue_connections

  has_many :user_to_issue_connections, :dependent => :destroy

  belongs_to :project, :counter_cache => true

  has_many :boards, :through => :issue_to_section_connections

  has_many :sections, :through => :issue_to_section_connections

  has_many :columns, :through => :issue_to_section_connections

  has_many :issue_to_section_connections, :dependent => :destroy

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

  validates :body, :length => { :maximum => Settings.max_text_field_size }, :allow_blank => true

  validates :project_id, :presence => true

  validates :state, :presence => true, :inclusion => %w(closed open)

  after_save :assign_issue_to_section_connections

  def assign_issue_to_section_connections
    Section.where(:board_id => project.boards).where('ARRAY[?]::varchar[] && tags', tags).find_each do |section|
      build_section_connection(section)
    end

    Section.where(:board_id => project.boards).where(:include_all => true).find_each do |section|
      build_section_connection(section)
    end
  end

  def parse_tags(source_column_id, target_column_id)
    tags - (Column.find_by(:id => source_column_id.to_i).try(:tags).to_a & tags) +
      Column.find_by(:id => target_column_id.to_i).try(:tags).to_a
  end

  Settings.issues_providers.each do |provider|
    define_method "sync_with_#{ provider }" do |user_id|
      user = User.find_by(:id => user_id)

      return unless user.present?

      client = user.send("#{ provider }_client")

      return unless client.present?

      send("create_or_update_issue_to_#{ provider }", client)
    end
  end

  def create_or_update_issue_to_github(client)
    if github_issue_number.present?
      github_update_issue(client)
    else
      result = github_create_issue(client)

      update_attributes!(:github_issue_id => result.try(:id),
        :github_issue_number => result.try(:number),
        :github_issue_comments_count => result.try(:comments),
        :github_issue_html_url => result.try(:html_url),
        :github_labels => result.try(:labels))
    end
  end

  def create_or_update_issue_to_bitbucket(client)
    if bitbucket_issue_id.present?
      bitbucket_update_issue(client)
    else
      result = bitbucket_create_issue(client)

      update_attributes!(:bitbucket_issue_id => result.id)
    end
  end

  def create_or_update_issue_to_gitlab(client)
    if gitlab_issue_id.present?
      gitlab_update_issue(client)
    else
      result = gitlab_create_issue(client)

      update_attributes!(:gitlab_issue_id => result.id) if result.present? && result.id.present?
    end
  end

  def gitlab_update_issue(client)
    client.edit_issue(project.gitlab_repository_id, gitlab_issue_id, :title => title,
      :description => body, :labels => tags.join(','))
  end

  def gitlab_create_issue(client)
    client.create_issue(project.gitlab_repository_id, title, :description => body, :labels => tags.join(','))
  end

  def github_create_issue(client)
    client.create_issue(project.github_full_name, title, body, :labels => tags)
  end

  def github_update_issue(client)
    client.update_issue(project.github_full_name, github_issue_number,
      :title => title, :body => body, :labels => tags)
  end

  def bitbucket_create_issue(client)
    client.issues.create(project.bitbucket_owner, project.bitbucket_slug, 'title' => title)
  rescue BitBucket::Error::NotFound
    Rails.logger.info "BitBucket::Error::NotFound on sync bitbucket issues with project id #{ project.id }"

    false
  end
<commit_msg>Fix bitbucket create/update issue sync<commit_after>class Issue < ActiveRecord::Base
  include EmptyArrayRemovable

  store_accessor :meta, :github_issue_id, :github_issue_number, :bitbucket_issue_id,
    :github_issue_comments_count, :github_issue_html_url, :github_labels, :bitbucket_issue_comment_count,
    :gitlab_issue_id

  has_many :users, :through => :user_to_issue_connections

  has_many :user_to_issue_connections, :dependent => :destroy

  belongs_to :project, :counter_cache => true

  has_many :boards, :through => :issue_to_section_connections

  has_many :sections, :through => :issue_to_section_connections

  has_many :columns, :through => :issue_to_section_connections

  has_many :issue_to_section_connections, :dependent => :destroy

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

  validates :body, :length => { :maximum => Settings.max_text_field_size }, :allow_blank => true

  validates :project_id, :presence => true

  validates :state, :presence => true, :inclusion => %w(closed open)

  after_save :assign_issue_to_section_connections

  def assign_issue_to_section_connections
    Section.where(:board_id => project.boards).where('ARRAY[?]::varchar[] && tags', tags).find_each do |section|
      build_section_connection(section)
    end

    Section.where(:board_id => project.boards).where(:include_all => true).find_each do |section|
      build_section_connection(section)
    end
  end

  def parse_tags(source_column_id, target_column_id)
    tags - (Column.find_by(:id => source_column_id.to_i).try(:tags).to_a & tags) +
      Column.find_by(:id => target_column_id.to_i).try(:tags).to_a
  end

  Settings.issues_providers.each do |provider|
    define_method "sync_with_#{ provider }" do |user_id|
      user = User.find_by(:id => user_id)

      return unless user.present?

      client = user.send("#{ provider }_client")

      return unless client.present?

      send("create_or_update_issue_to_#{ provider }", client)
    end
  end

  def create_or_update_issue_to_github(client)
    if github_issue_number.present?
      github_update_issue(client)
    else
      result = github_create_issue(client)

      update_attributes!(:github_issue_id => result.try(:id),
        :github_issue_number => result.try(:number),
        :github_issue_comments_count => result.try(:comments),
        :github_issue_html_url => result.try(:html_url),
        :github_labels => result.try(:labels))
    end
  end

  def create_or_update_issue_to_bitbucket(client)
    if bitbucket_issue_id.present?
      bitbucket_update_issue(client)
    else
      result = bitbucket_create_issue(client)

      update_attributes!(:bitbucket_issue_id => result.id) if result.present? && result.id.present?
    end
  end

  def create_or_update_issue_to_gitlab(client)
    if gitlab_issue_id.present?
      gitlab_update_issue(client)
    else
      result = gitlab_create_issue(client)

      update_attributes!(:gitlab_issue_id => result.id)
    end
  end

  def gitlab_update_issue(client)
    client.edit_issue(project.gitlab_repository_id, gitlab_issue_id, :title => title,
      :description => body, :labels => tags.join(','))
  end

  def gitlab_create_issue(client)
    client.create_issue(project.gitlab_repository_id, title, :description => body, :labels => tags.join(','))
  end

  def github_create_issue(client)
    client.create_issue(project.github_full_name, title, body, :labels => tags)
  end

  def github_update_issue(client)
    client.update_issue(project.github_full_name, github_issue_number,
      :title => title, :body => body, :labels => tags)
  end

  def bitbucket_create_issue(client)
    client.issues.create(project.bitbucket_owner, project.bitbucket_slug, 'title' => title)
  rescue BitBucket::Error::NotFound
    Rails.logger.info "BitBucket::Error::NotFound on sync bitbucket issues with project id #{ project.id }"

    false
  end

{'fraction_non_alphanumeric': 0.10839383518023182, 'fraction_numerical': 0.0, 'mean_word_length': 4.61659513590844, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 81, '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': '1158575', 'n_tokens_mistral': 2574, 'n_tokens_neox': 2483, 'n_words': 464}