class Gitgut::Branch

A local git branch

Public Class Methods

new(name) click to toggle source
# File lib/gitgut/branch.rb, line 4
def initialize(name)
  @raw_name = name
end

Public Instance Methods

action_suggestion() click to toggle source
# File lib/gitgut/branch.rb, line 95
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?() click to toggle source
# File lib/gitgut/branch.rb, line 65
def all_pull_requests_merged?
  pull_requests.all?(&:merged?)
end
color() click to toggle source
# File lib/gitgut/branch.rb, line 56
def color
  return :red unless valid?
  if has_pull_requests? && all_pull_requests_merged?
    :green
  else
    :white
  end
end
develop_is_ahead_of_staging?() click to toggle source
# File lib/gitgut/branch.rb, line 79
def develop_is_ahead_of_staging?
  to_develop > 0 && to_develop < to_staging
end
from_develop() click to toggle source
# File lib/gitgut/branch.rb, line 19
def from_develop
  Gitgut::Git.missing_commits_count(name, 'develop')
end
from_staging() click to toggle source
# File lib/gitgut/branch.rb, line 27
def from_staging
  Gitgut::Git.missing_commits_count(name, 'staging')
end
has_pull_requests?() click to toggle source
# File lib/gitgut/branch.rb, line 69
def has_pull_requests?
  !pull_requests.empty?
end
jira_ticket_number() click to toggle source
# 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?() click to toggle source
# File lib/gitgut/branch.rb, line 83
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?() click to toggle source
# File lib/gitgut/branch.rb, line 91
def merged_everywhere?
  to_staging.zero? && to_develop.zero?
end
name() click to toggle source

TODO: Sanitization should be done elsewhere

# 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!() click to toggle source
# File lib/gitgut/branch.rb, line 106
def preload!
  ticket
  pull_requests
  to_staging
  to_develop
  self
end
pull_requests() click to toggle source
# 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
ticket() click to toggle source
# 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
    if payload['errorMessages']
      puts "\nError while getting JIRA ticket ##{jira_ticket_number}: #{payload['errorMessages'].join('; ')}"
      return @ticket = nil
    end
    puts payload.inspect unless payload['issues']
    return @ticket = nil if payload['issues'].empty?
    @ticket = Gitgut::Jira::Ticket.new(payload['issues'].first)
  else
    @ticket = nil
  end
end
to_develop() click to toggle source
# File lib/gitgut/branch.rb, line 15
def to_develop
  Gitgut::Git.missing_commits_count('develop', name)
end
to_staging() click to toggle source
# File lib/gitgut/branch.rb, line 23
def to_staging
  Gitgut::Git.missing_commits_count('staging', name)
end
valid?() click to toggle source
# File lib/gitgut/branch.rb, line 73
def valid?
  return false if develop_is_ahead_of_staging?
  return false if merge_in_staging_required_by_ticket_status?
  true
end