summaryrefslogtreecommitdiff
path: root/lib/github/representation/branch.rb
blob: 7c65a948edeff5d58956b5c661c54afd92283c56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module Github
  module Representation
    class Branch < Representation::Base
      attr_reader :repository

      def initialize(repository, raw)
        @repository = repository
        @raw = raw
      end

      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?
        branch_exists? && commit_exists?
      end

      def valid?
        sha.present? && ref.present?
      end

      private

      def branch_exists?
        repository.branch_exists?(ref)
      end

      def commit_exists?
        repository.branch_names_contains(sha).include?(ref)
      end
    end
  end
end