Class: TechnicalDebt

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

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ TechnicalDebt

Returns a new instance of TechnicalDebt.



9
10
11
# File 'lib/technical_debt.rb', line 9

def initialize(dir)
  @project = dir
end

Instance Method Details

#check_connectionObject



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

def check_connection
  begin
    status = Timeout::timeout(2){
      Net::HTTP.get(URI.parse("http://google.com"))
      return true
    }
  rescue Exception
    return false
  end
end

#committed_lines_onlyObject

DEBT



50
51
52
# File 'lib/technical_debt.rb', line 50

def committed_lines_only
  split_commit.reject { |commited_line| !(commited_line =~ /^\+[^\+]/)}
end

#connection_exists?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/technical_debt.rb', line 85

def connection_exists?
  check_connection ? true : false
end

#debt_lines_onlyObject



54
55
56
# File 'lib/technical_debt.rb', line 54

def debt_lines_only
  committed_lines_only.reject{ |committed_line| !(committed_line =~ regex_debt_matcher)}
end

#debt_lines_with_loggedObject



58
59
60
61
62
63
64
65
66
# File 'lib/technical_debt.rb', line 58

def debt_lines_with_logged
  if log_file_exists?
    debt = debt_lines_only + read_logged_debt
    File.delete(log_file)
  else
    debt = debt_lines_only
  end
  debt
end

#debtifyObject



128
129
130
131
132
133
134
135
# File 'lib/technical_debt.rb', line 128

def debtify
  path = File.expand_path(project.to_s)
  command = "#/bin/sh\ncd #{path} && senddebt ."
  post_commit_file = "#{path}/.git/hooks/post-commit"
  File.open(post_commit_file,'w'){ |f| f.write(command) }
  File.chmod(0755, post_commit_file)
  puts "Your project is now debtified."
end

#get_git_tokenObject



33
34
35
# File 'lib/technical_debt.rb', line 33

def get_git_token
  `git config --global technicaldebt.token`.strip
end

#git_token_exists?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/technical_debt.rb', line 37

def git_token_exists?
  !get_git_token.blank?
end

#git_versionObject



17
18
19
# File 'lib/technical_debt.rb', line 17

def git_version
  `git --version`.gsub(/git version/, "").strip
end

#last_commitObject



29
30
31
# File 'lib/technical_debt.rb', line 29

def last_commit
  `git diff master~1 head`
end

#last_commit_shaObject



21
22
23
# File 'lib/technical_debt.rb', line 21

def last_commit_sha
  `git rev-parse head`.strip
end

#log_debtObject



98
99
100
101
# File 'lib/technical_debt.rb', line 98

def log_debt
  File.open(log_file, 'a') { |f| f.write(debt_lines_only.join(",") + ",")}
  no_token_message unless git_token_exists?
end

#log_fileObject

DEBT 1h Just a sanity check maybe a reality check



116
117
118
# File 'lib/technical_debt.rb', line 116

def log_file
  "#{project}/.git/technical_debt"
end

#log_file_exists?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/technical_debt.rb', line 107

def log_file_exists?
  File.exists?("#{project}/.git/technical_debt")
end

#minimum_git_version?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/technical_debt.rb', line 25

def minimum_git_version?
  git_version >= "1.6"
end

#no_token_messageObject



103
104
105
# File 'lib/technical_debt.rb', line 103

def no_token_message
  puts "You don't have a token for Technical Debt access. To install it get your token from the website and:\ngit config --global technicaldebt.token your_token_here\nVisit http://technicaldebt.r09.railsrumble.com/account to obtain your token\n"
end

#projectObject



13
14
15
# File 'lib/technical_debt.rb', line 13

def project
  @project
end

#push_to_technical_debtObject



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

def push_to_technical_debt
  debt_lines_with_logged.each do |debt_line|
    send_to_server(debt_line)
  end
end

#read_logged_debtObject



111
112
113
# File 'lib/technical_debt.rb', line 111

def read_logged_debt
  File.open(log_file,'r') { |f| f.read.split(",")}
end

#regex_debt_matcherObject



45
46
47
# File 'lib/technical_debt.rb', line 45

def regex_debt_matcher
  /^\+\s*#\s*[dD][eE][bB][tT]\s+(.*)$*/
end

#register_debtObject



89
90
91
92
93
94
95
# File 'lib/technical_debt.rb', line 89

def register_debt
  if git_token_exists? && connection_exists? && !debt_lines_with_logged.blank?
    push_to_technical_debt
  else
    log_debt
  end
end

#send_to_server(debt) ⇒ Object



120
121
122
# File 'lib/technical_debt.rb', line 120

def send_to_server(debt)
  Net::HTTP.post_form(URI.parse("http://technicaldebt.r09.railsrumble.com/transactions"),  { 'transaction[message]' => stripped_debt_line(debt), 'transaction[sha]' => last_commit_sha, 'git_token' => get_git_token, 'kind' => 'debt' })
end

#split_commitObject



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

def split_commit
  last_commit.split("\n")
end

#stripped_debt_line(debt_line) ⇒ Object



124
125
126
# File 'lib/technical_debt.rb', line 124

def stripped_debt_line(debt_line)
  debt_line[/^\+\s*#\s*[dD][eE][bB][tT]\s+(.*)$/, 1]
end