Class: GitHubBasecampExtractor

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

Constant Summary collapse

DEFAULT_CONFIG_FILE =
File.expand_path('~/.gitbc')
DEFAULT_END_TAG =
'HEAD'
MAX_PR_BODY_LENGTH =
235

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ GitHubBasecampExtractor

Returns a new instance of GitHubBasecampExtractor.



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

def initialize(params)
  @params = params
  @github_client = Octokit::Client.new(access_token: @params[:github_access_token])
end

Instance Method Details

#get_basecamp_todos(pull_requests) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/gitbc.rb', line 76

def get_basecamp_todos(pull_requests)
  basecamp_todos = pull_requests.inject({}) do |memo, pull_request_id|
    pr = @github_client.pull_request(@params[:repository], pull_request_id)
    if pr.attrs[:body]
      basecamp_lines = pr.attrs[:body].split("\r\n").grep(/.*https\:\/\/basecamp\.com.*/)
      memo[pull_request_id] = {body: pr.attrs[:body][0..MAX_PR_BODY_LENGTH].strip, lines: []}
    else
      basecamp_lines = []
      memo[pull_request_id] = {body: '', lines: []}
    end
    if basecamp_lines.count > 0
      memo[pull_request_id][:lines] = basecamp_lines.map { |line| {url: line[/.*(https\:\/\/basecamp\.com[^!?#:;,.\s]*)/, 1]} }.uniq
      if @params[:basecamp_content]
        memo[pull_request_id][:lines].each do |line|
          line[:url].match /(https\:\/\/basecamp\.com\/\d+\/)(.*)/ do |match|
            begin
              ret = HTTParty.get "#{match[1]}api/v1/#{match[2]}.json", {basic_auth: {username: @params[:basecamp_user], password: @params[:basecamp_password]}, headers: {'Content-Type' => 'application/json', 'User-Agent' => "gitbc API (#{@params[:basecamp_user]})"}}
              line[:content] = JSON.parse(ret.body)['content'] if ret != nil && ret.body.length > 0
            rescue
            end
          end
        end
      end
    end
    yield(pull_request_id, memo[pull_request_id]) if block_given?
    memo
  end
  basecamp_todos
end

#get_pull_requests_from_git_logsObject



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

def get_pull_requests_from_git_logs
  lines = `git --no-pager log #{@params[:branch]} #{@params[:start_tag]}..#{@params[:end_tag]} --merges | grep 'Merge pull request #'`.split("\n")
  lines.map {|l| l[/.*#([0-9]+)/,1].to_i}
end

#git_branchObject



39
40
41
# File 'lib/gitbc.rb', line 39

def git_branch
  Open3.popen3('git status') { |_, stdout, _, _| stdout.read }[/^On branch ([^\n]+)/,1]
end

#git_branch_exists?(branch) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/gitbc.rb', line 43

def git_branch_exists?(branch)
  Open3.popen3('git branch') { |_, stdout, _, _| stdout.read } =~ /#{branch}/
end

#git_repositoryObject



47
48
49
# File 'lib/gitbc.rb', line 47

def git_repository
  Open3.popen3('git remote -v') { |_, stdout, _, _| stdout.read }[/[email protected]:(.+).git/,1]
end

#is_git_installed?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
# File 'lib/gitbc.rb', line 26

def is_git_installed?
  begin
    Open3.popen3('git --version')
    return true
  rescue Errno::ENOENT
    return false
  end
end

#is_git_repo?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/gitbc.rb', line 35

def is_git_repo?
  Open3.popen3('git status') { |_, _, stderr, _| stderr.read } !~ /Not a git repository/
end

#is_github_alive?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
# File 'lib/gitbc.rb', line 63

def is_github_alive?
  begin
    raise if @github_client.github_status_last_message.attrs[:status] != 'good'
  rescue
    return false
  end
  return true
end

#remote_matches?(repo) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/gitbc.rb', line 55

def remote_matches?(repo)
  Open3.popen3('git remote -v') { |_, stdout, _, _| stdout.read } =~ /#{repo}/
end

#repo_exists?(repo) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/gitbc.rb', line 51

def repo_exists?(repo)
  @github_client.repository?(repo)
end

#revision_exists?(rev) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/gitbc.rb', line 59

def revision_exists?(rev)
  Open3.popen3("git cat-file -t #{rev}") { |_, stdout, _, _| stdout.read } =~ /commit/
end

#update_options(options) ⇒ Object



72
73
74
# File 'lib/gitbc.rb', line 72

def update_options(options)
  @params.merge(options)
end