Class: GitTracking

Inherits:
Object
  • Object
show all
Defined in:
lib/git_tracking.rb,
lib/git_tracking/config.rb,
lib/git_tracking/detect.rb,
lib/git_tracking/version.rb

Defined Under Namespace

Classes: Config

Constant Summary collapse

VERSION =
"0.1.3"

Class Method Summary collapse

Class Method Details

.api_keyObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/git_tracking.rb', line 132

def api_key
  return @api_key if @api_key
  message, retry_count = nil, 0
  email = highline.choose(*config.emails) do |menu|
    menu.header = "Pivotal Tracker email (default is: #{config.last_email})"
    menu.default = config.last_email
    menu.choice("Enter new") { highline.ask("New Email: ") }
  end
  unless @api_key = config.key_for_email(email.to_s)
    begin
      highline.say message if message
      password = highline.ask("Enter your PivotalTracker password: ") {|q| q.echo = "x" }
      @api_key = PivotalTracker::Client.token(email, password)
      config.key_for_email(email, @api_key)
    rescue RestClient::Request::Unauthorized
      retry_count += 1
      message = "401 Unauthorized. Please try again."
      if retry_count < 3
        retry
      else
        highline.say("Unable to authenticate to Pivotal Tracker. Exiting...")
        raise RestClient::Request::Unauthorized
      end
    end
  end

  @api_key
end

.authorObject



122
123
124
125
126
127
128
129
130
# File 'lib/git_tracking.rb', line 122

def author
  return @author if @author
  @author = highline.choose(*config.authors) do |menu|
    menu.header = "Git Author (default will be: #{config.author})"
    menu.default = config.author
    menu.choice("Enter new") { highline.ask("New git author: ") }
  end
  config.author = @author
end

.branchObject



109
110
111
# File 'lib/git_tracking.rb', line 109

def branch
  `git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`.chomp.gsub("* ", "")
end

.check_story_id(id) ⇒ Object



161
162
163
# File 'lib/git_tracking.rb', line 161

def check_story_id(id)
  return true if get_story(id)
end

.commit_messageObject



36
37
38
# File 'lib/git_tracking.rb', line 36

def commit_message
  @commit_message ||= File.read(ARGV[0])
end

.configObject



32
33
34
# File 'lib/git_tracking.rb', line 32

def config
  @config ||= Config.new
end

.detect_debuggersObject



3
4
5
6
7
8
9
10
11
12
# File 'lib/git_tracking/detect.rb', line 3

def detect_debuggers
  file_names = `git diff-index --cached -Sdebugger --name-only HEAD`
  file_names = file_names.gsub(".git_tracking",'').strip
  if file_names != ""
    highline.say highline.color("The following files have 'debugger' statements in them: ", :red)
    highline.say file_names
    raise DebuggerException,
      "Please remove debuggers prior to committing" if config.raise_on_debugger
  end
end

.detect_incomplete_mergesObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/git_tracking/detect.rb', line 14

def detect_incomplete_merges
  file_names = `git diff-index --cached -S'<<<<<<<' --name-only HEAD`.chomp.split
  file_names += `git diff-index --cached -S'>>>>>>>' --name-only HEAD`.chomp.split
  file_names = file_names.uniq.join("\n")
  if file_names != ""
    highline.say highline.color("The following files have incomplete merges: ", :red)
    highline.say file_names
    raise IncompleteMergeException,
      "Please complete your merge prior to committing" if config.raise_on_incomplete_merge
  end
end

.extract_story_id(string) ⇒ Object



117
118
119
120
# File 'lib/git_tracking.rb', line 117

def extract_story_id(string)
  the_story_id = string.match(/\d{5,}/)[0] if string.match(/\d{5,}/)
  return the_story_id if check_story_id(the_story_id)
end

.get_story(id) ⇒ Object



165
166
167
# File 'lib/git_tracking.rb', line 165

def get_story(id)
  pivotal_project.stories.find(id.to_i) rescue nil
end

.highlineObject



28
29
30
# File 'lib/git_tracking.rb', line 28

def highline
  @highline ||= HighLine.new($stdin.reopen("/dev/tty", "a+"), $stdout)
end

.pivotal_projectObject



69
70
71
72
73
# File 'lib/git_tracking.rb', line 69

def pivotal_project
  return @pivotal_project if @pivotal_project
  PivotalTracker::Client.token = api_key
  @pivotal_project = PivotalTracker::Project.find(project_id)
end

.post_commitObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/git_tracking.rb', line 56

def post_commit
  @api_key = config.last_api_key
  if config.last_story_completed?
    completed = GitTracking.highline.ask("Does this commit complete the story?", ["yes", "no"]) do |q|
      q.default = "yes"
    end
    if completed == "yes"
      story = get_story(config.last_story_id)
      story.notes.create(:text => config.commits_for_last_story)
    end
  end
end

.pre_commitObject



40
41
42
43
# File 'lib/git_tracking.rb', line 40

def pre_commit
  detect_debuggers
  detect_incomplete_merges
end

.prepare_commit_msgObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/git_tracking.rb', line 45

def prepare_commit_msg
  unless commit_message.include?("--no-gt")
    author
    File.open(ARGV[0], "w") do |f|
      f.puts 
      f.puts
      f.puts "  - #{commit_message}"
    end
  end
end

.project_idObject



75
76
77
78
79
80
81
82
83
84
# File 'lib/git_tracking.rb', line 75

def project_id
  return config.project_id if config.project_id
  id = highline.ask("Please enter the PivotalTracker project id for this project") do |q|
    q.validate = lambda do |a|
      PivotalTracker::Client.token = api_key
      PivotalTracker::Project.find(a) rescue false
    end
  end
  config.project_id(id)
end

.storyObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/git_tracking.rb', line 90

def story
  return @story if @story

  if story_id && story = get_story(story_id)
    highline.say("Found a valid story id in your branch or commit: #{story.id} - #{story.name}")
    @story = highline.ask("Hit enter to confirm story id #{story.id}, or enter some other story id: ", lambda{|a| get_story(a)}) do |q|
      q.default =  story.id
      q.validate = lambda{|a| check_story_id(a)}
    end
  else
    @story = highline.ask("Please enter a valid Pivotal Tracker story id: ", lambda{|a| get_story(a)}) do |q|
      q.validate = lambda{|a| check_story_id(a)}
    end
  end
  config.last_story_id = @story.id

  @story
end

.story_idObject



113
114
115
# File 'lib/git_tracking.rb', line 113

def story_id
  @story_id ||= (extract_story_id(commit_message) || extract_story_id(branch) || config.last_story_id)
end

.story_infoObject



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

def 
  "[##{story.id}] #{story.name}"
end