<commit_before>require 'core_ext/array/flatten_once'
require 'core_ext/hash/compact'

class Build < ActiveRecord::Base
  belongs_to :repository
  belongs_to :parent, :class_name => 'Build', :foreign_key => :parent_id

  has_many :matrix, :class_name => 'Build', :foreign_key => :parent_id

  validates :repository_id, :presence => true

  serialize :config

  before_save :expand_matrix!, :if => :expand_matrix?
  after_save :denormalize_to_repository, :if => :denormalize_to_repository?

  class << self
    def create_from_github_payload(data)
      repository = Repository.find_or_create_by_url(data['repository']['url'])
      commit     = data['commits'].last
      author     = commit['author'] || {}
      committer  = commit['committer'] || author || {}

      repository.builds.create(
        :commit          => commit['id'],
        :message         => commit['message'],
        :number          => repository.builds.count + 1,
        :committed_at    => commit['timestamp'],
        :committer_name  => committer['name'],
        :committer_email => committer['email'],
        :author_name     => author['name'],
        :author_email    => author['email']
      )
    end

    def started
      where(arel_table[:started_at].not_eq(nil))
    end
  end

  attr_accessor :log_appended

  def log_appended?
    log_appended.present?
  end

  def append_log!(chars)
    self.log_appended = chars
    update_attributes!(:log => [self.log, chars].join)
  end

  def started?
    started_at.present?
  end

  def was_started?
    started? && @previously_changed.keys.include?('started_at')
  end

  def finished?
    finished_at.present?
  end

  def was_finished?
    finished? && @previously_changed.keys.include?('finished_at')
  end

  def pending?
    !finished?
  end

  def passed?
    status == 0
  end

  def color
    pending? ? '' : passed? ? 'green' : 'red'
  end

  def matrix?
    parent_id.blank? && matrix_config?
  end

  def matrix_expanded?
    Travis::Buildable::Config.matrix?(@previously_changed['config'][1]) rescue false # TODO how to use some public AR API?
  end

  all_attrs = [:id, :repository_id, :parent_id, :number, :commit, :message, :status, :log, :started_at, :committed_at,
    :committer_name, :committer_email, :author_name, :author_email, :config]

  JSON_ATTRS = {
    :default          => all_attrs,
    :job              => [:id, :commit, :config],
    :'build:queued'   => [:id, :number],
    :'build:started'  => all_attrs - [:status, :log],
    :'build:log'      => [:id],<commit_msg>re-add finished_at attr to default build json<commit_after>require 'core_ext/array/flatten_once'
require 'core_ext/hash/compact'

class Build < ActiveRecord::Base
  belongs_to :repository
  belongs_to :parent, :class_name => 'Build', :foreign_key => :parent_id

  has_many :matrix, :class_name => 'Build', :foreign_key => :parent_id

  validates :repository_id, :presence => true

  serialize :config

  before_save :expand_matrix!, :if => :expand_matrix?
  after_save :denormalize_to_repository, :if => :denormalize_to_repository?

  class << self
    def create_from_github_payload(data)
      repository = Repository.find_or_create_by_url(data['repository']['url'])
      commit     = data['commits'].last
      author     = commit['author'] || {}
      committer  = commit['committer'] || author || {}

      repository.builds.create(
        :commit          => commit['id'],
        :message         => commit['message'],
        :number          => repository.builds.count + 1,
        :committed_at    => commit['timestamp'],
        :committer_name  => committer['name'],
        :committer_email => committer['email'],
        :author_name     => author['name'],
        :author_email    => author['email']
      )
    end

    def started
      where(arel_table[:started_at].not_eq(nil))
    end
  end

  attr_accessor :log_appended

  def log_appended?
    log_appended.present?
  end

  def append_log!(chars)
    self.log_appended = chars
    update_attributes!(:log => [self.log, chars].join)
  end

  def started?
    started_at.present?
  end

  def was_started?
    started? && @previously_changed.keys.include?('started_at')
  end

  def finished?
    finished_at.present?
  end

  def was_finished?
    finished? && @previously_changed.keys.include?('finished_at')
  end

  def pending?
    !finished?
  end

  def passed?
    status == 0
  end

  def color
    pending? ? '' : passed? ? 'green' : 'red'
  end

  def matrix?
    parent_id.blank? && matrix_config?
  end

  def matrix_expanded?
    Travis::Buildable::Config.matrix?(@previously_changed['config'][1]) rescue false # TODO how to use some public AR API?
  end

  all_attrs = [:id, :repository_id, :parent_id, :number, :commit, :message, :status, :log, :started_at, :finished_at, :committed_at,
    :committer_name, :committer_email, :author_name, :author_email, :config]

  JSON_ATTRS = {
    :default          => all_attrs,
    :job              => [:id, :commit, :config],
    :'build:queued'   => [:id, :number],
    :'build:started'  => all_attrs - [:status, :log, :finished_at],
    :'build:log'      => [:id],
{'fraction_non_alphanumeric': 0.13804517133956387, 'fraction_numerical': 0.0011682242990654205, 'mean_word_length': 3.2245065789473686, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 45, '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': '4076188', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1662, 'n_words': 349}