2017-04-11 21:34:59 -04:00
|
|
|
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-22 10:57:35 -04:00
|
|
|
return @exists if defined?(@exists)
|
|
|
|
|
|
|
|
@exists = repository.branch_exists?(ref)
|
2017-04-11 21:34:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def valid?
|
|
|
|
sha.present? && ref.present?
|
|
|
|
end
|
|
|
|
|
2017-06-20 20:39:51 -04:00
|
|
|
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)
|
2017-08-28 03:31:41 -04:00
|
|
|
rescue Gitlab::Git::Repository::DeleteBranchError => e
|
2017-06-20 20:39:51 -04:00
|
|
|
Rails.logger.error("#{self.class.name}: Could not remove branch #{name}: #{e}")
|
|
|
|
end
|
|
|
|
|
2017-04-11 21:34:59 -04:00
|
|
|
private
|
|
|
|
|
2017-04-24 20:13:51 -04:00
|
|
|
def repository
|
|
|
|
@repository ||= options.fetch(:repository)
|
|
|
|
end
|
2017-04-11 21:34:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|