class GitHubBasecampExtractor

Constants

DEFAULT_CONFIG_FILE
DEFAULT_END_TAG
MAX_PR_BODY_LENGTH

Public Class Methods

new(params) click to toggle source
# File lib/gitbc.rb, line 16
def initialize(params)
  @params = params
  @github_client = Octokit::Client.new(access_token: @params[:github_access_token])
end

Public Instance Methods

get_basecamp_todos(pull_requests) { |pull_request_id, memo| ... } click to toggle source
# 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_logs() click to toggle source
# 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_branch() click to toggle source
# 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) click to toggle source
# File lib/gitbc.rb, line 43
def git_branch_exists?(branch)
  Open3.popen3('git branch') { |_, stdout, _, _| stdout.read } =~ /#{branch}/
end
git_repository() click to toggle source
# File lib/gitbc.rb, line 47
def git_repository
  Open3.popen3('git remote -v') { |_, stdout, _, _| stdout.read }[/git@github.com:(.+).git/,1]
end
is_git_installed?() click to toggle source
# 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?() click to toggle source
# 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?() click to toggle source
# 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) click to toggle source
# File lib/gitbc.rb, line 55
def remote_matches?(repo)
  Open3.popen3('git remote -v') { |_, stdout, _, _| stdout.read } =~ /#{repo}/
end
repo_exists?(repo) click to toggle source
# File lib/gitbc.rb, line 51
def repo_exists?(repo)
  @github_client.repository?(repo)
end
revision_exists?(rev) click to toggle source
# 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) click to toggle source
# File lib/gitbc.rb, line 72
def update_options(options)
  @params.merge(options)
end