Module: InchCI::Store

Defined in:
lib/inch_ci/store.rb

Overview

ActiveRecordStore provides a thin layer on top of ActiveRecord so we do not need to use it's API in our own code.

Defined Under Namespace

Classes: BuildCodeObject, CreateCodeObject, CreateRevisionDiff, DigestCodeObject

Constant Summary collapse

FindUser =
-> (service_name, user_name) { User.where(:provider => service_name, :user_name => user_name).first }
FindUserById =
-> (id) { User.find(id) }
UpdateLastProjectSync =
-> (user, time = Time.now) { user.update_attribute(:last_synced_projects_at, time) }
UpdateOrganizations =
-> (user, organization_names) { user.update_attribute(:organizations, organization_names) }
SaveUser =
-> (user) { user.save! }
FindProject =
-> (uid) { Project.find_by_uid(uid) }
FindAllProjects =
-> (user = nil) { user.nil? ? Project.all : user.projects }
CreateProject =
-> (uid, repo_url, origin = nil) { Project.create!(:uid => uid, :repo_url => repo_url, :origin => origin.to_s) }
SaveProject =
-> (project) { project.save! }
UpdateDefaultBranch =
-> (project, branch) { project.update_attribute(:default_branch, branch) }
EnsureProject =

this does not belong here

-> (url, origin = nil) {
  info = RepoURL.new(url)
  if info.project_uid
    Store::FindProject.call(info.project_uid) ||
      Store::CreateProject.call(info.project_uid, info.url, origin)
  end
}
EnsureProjectAndBranch =

this does not belong here

-> (url, branch_name) {
  project = Store::EnsureProject.call(url)

  Store::FindBranch.call(project, branch_name) ||
    Store::CreateBranch.call(project, branch_name)
}
FindBranch =
-> (project, name) { project.branches.where(:name => name).first }
CreateBranch =
-> (project, name) { project.branches.create!(:name => name) }
FindDefaultBranch =
-> (project) { project.default_branch || project.branches.first }
FindRevision =
-> (branch, uid) { branch.revisions.where('uid LIKE ?', "#{uid}%").first }
CreateRevision =
-> (branch, uid, tag_uid, message, author_name, author_email, badge_in_readme, authored_at) {
  attributes = {
    :uid => uid,
    :tag_uid => tag_uid,
    :badge_in_readme => badge_in_readme,
    :message => message,
    :author_name => author_name,
    :author_email => author_email,
    :authored_at => authored_at,
  }
  branch.revisions.create!(attributes)
}
FindLatestRevision =
-> (branch) { branch.latest_revision }
UpdateLatestRevision =
-> (branch, revision) { branch.update_attribute(:latest_revision, revision) }
FindRevisionDiffs =
-> (branch, count = 200) { branch.revision_diffs.includes(:code_object_diffs).includes(:after_revision).limit(count) }
STATUS_SCHEDULED =
'created'
FindBuild =
-> (id) { Build.find(id) }
FindBuilds =
-> (count = 200) { Build.order('id DESC').limit(count).includes(:revision).includes(:branch).includes(:project) }
FindBuildsInProject =
-> (project) { project.builds }
FindBuildsInBranch =
-> (branch, count = 200) { branch.to_model.builds.preload(:revision, :revision_diff).limit(count) }
FindScheduledBuildsInBranch =
-> (branch) { branch.to_model.builds.where(:status => STATUS_SCHEDULED) }
FindLatestBuildInProject =
-> (project) { project.builds.order('id DESC').first }
CreateBuild =
-> (branch, trigger, status = STATUS_SCHEDULED) do
  attributes = {
    :status => status,
    :trigger => trigger,
    :number => branch.project.builds.count + 1
  }
  branch.builds.create!(attributes)
end
RemoveBuild =
-> (build) { build.destroy }
UpdateFinishedBuild =
-> (build, revision, build_data) do
  attributes = {
    :revision => revision,
    :status => build_data.status,
    :stderr => build_data.stderr,
    :inch_version => build_data.inch_version,
    :finished_at => Time.now
  }
  build.update_attributes!(attributes)
end
UpdateBuildStatus =
-> (build, status, started_at = nil) do
  attributes = {:status => status}
  attributes[:started_at] = started_at if started_at
  build.update_attributes!(attributes)
end
FindCodeObject =
-> (id) { CodeObject.includes(:code_object_roles).find(id) }
FindCodeObjects =
-> (revision) { revision.code_objects }
FindRelevantCodeObjects =
-> (revision) do
  revision.code_objects.select { |object| object.priority >= Config::MIN_RELEVANT_PRIORITY }
end
CreateStats =
-> (name, value, date = Time.now.midnight) { Statistics.create!(:date => date, :name => name, :value => value) }
CreateOrUpdateStats =
-> (name, value, date = Time.now.midnight) {
  existing = Statistics.where(:date => date, :name => name).first
  if existing
    existing.update_attribute(:value, value)
  else
    Statistics.create!(:date => date, :name => name, :value => value)
  end
}
IncreaseStats =
-> (name, date = Time.now.midnight, init_value = 0) {
  stat = Statistics.where(:date => date, :name => name).first
  if stat
    stat.update_attribute(:value, stat.value + 1)
    stat
  else
    Statistics.create!(:date => date, :name => name, :value => init_value + 1)
  end
}

Class Method Summary collapse

Class Method Details

.transaction(&block) ⇒ Object


243
244
245
# File 'lib/inch_ci/store.rb', line 243

def self.transaction(&block)
  ActiveRecord::Base.transaction(&block)
end