2017-08-17 22:00:37 +05:30
|
|
|
module Github
|
|
|
|
module Representation
|
|
|
|
class Branch < Representation::Base
|
|
|
|
attr_reader :repository
|
|
|
|
|
|
|
|
def user
|
|
|
|
raw.dig('user', 'login') || 'unknown'
|
|
|
|
end
|
|
|
|
|
|
|
|
def repo
|
|
|
|
return @repo if defined?(@repo)
|
|
|
|
|
|
|
|
@repo = Github::Representation::Repo.new(raw['repo']) if raw['repo'].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def ref
|
|
|
|
raw['ref']
|
|
|
|
end
|
|
|
|
|
|
|
|
def sha
|
|
|
|
raw['sha']
|
|
|
|
end
|
|
|
|
|
|
|
|
def short_sha
|
|
|
|
Commit.truncate_sha(sha)
|
|
|
|
end
|
|
|
|
|
|
|
|
def exists?
|
2017-09-10 17:25:29 +05:30
|
|
|
@exists ||= branch_exists? && commit_exists?
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def valid?
|
|
|
|
sha.present? && ref.present?
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def restore!(name)
|
|
|
|
repository.create_branch(name, sha)
|
|
|
|
rescue Gitlab::Git::Repository::InvalidRef => e
|
|
|
|
Rails.logger.error("#{self.class.name}: Could not restore branch #{name}: #{e}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove!(name)
|
|
|
|
repository.delete_branch(name)
|
|
|
|
rescue Rugged::ReferenceError => e
|
|
|
|
Rails.logger.error("#{self.class.name}: Could not remove branch #{name}: #{e}")
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def branch_exists?
|
|
|
|
repository.branch_exists?(ref)
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_exists?
|
|
|
|
repository.branch_names_contains(sha).include?(ref)
|
|
|
|
end
|
|
|
|
|
|
|
|
def repository
|
|
|
|
@repository ||= options.fetch(:repository)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|