Class: Gitgut::Branch

Inherits:
Object
  • Object
show all
Defined in:
lib/gitgut/branch.rb

Overview

A local git branch

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Branch

Returns a new instance of Branch.



4
5
6
# File 'lib/gitgut/branch.rb', line 4

def initialize(name)
  @raw_name = name
end

Instance Method Details

#action_suggestionObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/gitgut/branch.rb', line 90

def action_suggestion
  return 'Review the PR' if ticket && ticket.assigned_to_me? && ticket.in_review?
  return 'Merge into staging' if merge_in_staging_required_by_ticket_status?
  return 'Merge into staging or update your staging branch' if develop_is_ahead_of_staging?
  if ticket
    return 'Do some code' if ticket.assigned_to_me?
    return 'Assign the ticket to me (and start the development)' if ticket.assignee.nil? && !ticket.done?
  end
  return 'Delete the local branch' if (merged_everywhere? && (!ticket || ticket.done?)) || (ticket && ticket.closed?)
end

#all_pull_requests_merged?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/gitgut/branch.rb', line 60

def all_pull_requests_merged?
  pull_requests.all?(&:merged?)
end

#colorObject



51
52
53
54
55
56
57
58
# File 'lib/gitgut/branch.rb', line 51

def color
  return :red unless valid?
  if has_pull_requests? && all_pull_requests_merged?
    :green
  else
    :white
  end
end

#develop_is_ahead_of_staging?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/gitgut/branch.rb', line 74

def develop_is_ahead_of_staging?
  to_develop > 0 && to_develop < to_staging
end

#from_developObject



19
20
21
# File 'lib/gitgut/branch.rb', line 19

def from_develop
  Gitgut::Git.missing_commits_count(name, 'develop')
end

#from_stagingObject



27
28
29
# File 'lib/gitgut/branch.rb', line 27

def from_staging
  Gitgut::Git.missing_commits_count(name, 'staging')
end

#has_pull_requests?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/gitgut/branch.rb', line 64

def has_pull_requests?
  !pull_requests.empty?
end

#jira_ticket_numberObject



35
36
37
38
# File 'lib/gitgut/branch.rb', line 35

def jira_ticket_number
  matches = name.match(/^jt-(?<id>\d+)/i)
  return 'JT-' + matches[:id] if matches
end

#merge_in_staging_required_by_ticket_status?Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
# File 'lib/gitgut/branch.rb', line 78

def merge_in_staging_required_by_ticket_status?
  return false unless ticket
  if ['In Functional Review', 'In Review', 'Ready for Release'].include?(ticket.status) && to_staging > 0
    return true
  end
  false
end

#merged_everywhere?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/gitgut/branch.rb', line 86

def merged_everywhere?
  to_staging.zero? && to_develop.zero?
end

#nameObject

TODO: Sanitization should be done elsewhere



9
10
11
12
13
# File 'lib/gitgut/branch.rb', line 9

def name
  return @name if @name
  match_data = /[ *]+(?<branch>[^\s]*)/.match @raw_name
  @name = match_data[:branch]
end

#preload!Object



101
102
103
104
105
106
107
# File 'lib/gitgut/branch.rb', line 101

def preload!
  ticket
  pull_requests
  to_staging
  to_develop
  self
end

#pull_requestsObject



31
32
33
# File 'lib/gitgut/branch.rb', line 31

def pull_requests
  @pull_requests ||= Gitgut::Github.client.pull_requests("#{Settings.github.repo.owner}/#{Settings.github.repo.name}", state: 'all', head: "#{Settings.github.repo.owner}:#{name}").map { |pr| Gitgut::Github::PullRequest.new(pr) }
end

#ticketObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/gitgut/branch.rb', line 40

def ticket
  return @ticket if defined?(@ticket)
  if jira_ticket_number
    payload = Gitgut::Jira.request 'id=' + jira_ticket_number
    return @ticket = nil if payload['issues'].empty?
    @ticket = Gitgut::Jira::Ticket.new(payload['issues'].first)
  else
    @ticket = nil
  end
end

#to_developObject



15
16
17
# File 'lib/gitgut/branch.rb', line 15

def to_develop
  Gitgut::Git.missing_commits_count('develop', name)
end

#to_stagingObject



23
24
25
# File 'lib/gitgut/branch.rb', line 23

def to_staging
  Gitgut::Git.missing_commits_count('staging', name)
end

#valid?Boolean

Returns:

  • (Boolean)


68
69
70
71
72
# File 'lib/gitgut/branch.rb', line 68

def valid?
  return false if develop_is_ahead_of_staging?
  return false if merge_in_staging_required_by_ticket_status?
  true
end