Sha256: 5cb9b21e2552780333d17d083f0b575bc36248710bda17cb2750bb4e18aea673

Contents?: true

Size: 911 Bytes

Versions: 5

Compression:

Stored size: 911 Bytes

Contents

class Project < ApplicationRecord
  belongs_to :team, optional: true
  belongs_to :user, optional: true
  has_many :pipelines
  has_many :jobs, through: :pipelines

  def self.add_project(user, name)
    repo = user.client.repo(name)

    found = Project.find_by(repo_id: repo[:id])
    return found if found

    project = {
        name: repo[:name],
        repo_provider: user.provider,
        repo_owner: repo[:owner],
        repo_name: repo[:name],
        repo_id: repo[:id],
    }

    team = user.teams.find_by(provider_id: repo[:owner_id])
    if team
      project[:team] = team
    else
      project[:user] = user
    end

    create!(project)
  end

  def status
    jobs.where(branch: 'master').last.try(:status)
  end

  def branches
    jobs.group(:branch).limit(20).pluck(:branch).map do |branch|
      jobs.where(branch: branch).first
    end
  end

  def owner
    user || team
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
jobs-api-0.2.1 app/models/project.rb
jobs-api-0.2.0 app/models/project.rb
jobs-api-0.1.2 app/models/project.rb
jobs-api-0.1.1 app/models/project.rb
jobs-api-0.1.0 app/models/project.rb