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
|
|
|
|
|
2017-10-05 04:57:34 -04:00
|
|
|
def repo?
|
|
|
|
raw['repo'].present?
|
|
|
|
end
|
|
|
|
|
2017-04-11 21:34:59 -04:00
|
|
|
def repo
|
2017-10-05 04:57:34 -04:00
|
|
|
return unless repo?
|
2017-04-11 21:34:59 -04:00
|
|
|
|
2017-10-05 04:57:34 -04:00
|
|
|
@repo ||= Github::Representation::Repo.new(raw['repo'])
|
2017-04-11 21:34:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def ref
|
|
|
|
raw['ref']
|
|
|
|
end
|
|
|
|
|
|
|
|
def sha
|
|
|
|
raw['sha']
|
|
|
|
end
|
|
|
|
|
|
|
|
def short_sha
|
|
|
|
Commit.truncate_sha(sha)
|
|
|
|
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
|