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