Class: Amitree::GitClient

Inherits:
Object
  • Object
show all
Defined in:
lib/amitree/git_client.rb,
lib/amitree/git_client/range.rb,
lib/amitree/git_client/not_found_error.rb

Defined Under Namespace

Classes: EmptyRangeError, NotFoundError, Range

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository, username, password, options = {}) ⇒ GitClient

Returns a new instance of GitClient.



13
14
15
16
17
18
19
20
21
# File 'lib/amitree/git_client.rb', line 13

def initialize(repository, username, password, options={})
  @repository = repository
  @client = Octokit::Client.new \
    login: username,
    password: password,
    middleware: (self.class.verbose_middleware if options[:verbose]),
    connection_options: Octokit::Default.options[:connection_options].merge(request: {timeout: 60, open_timeout: 60})
  @max_commit_range = options[:max_commit_range] || 1000
end

Class Method Details

.verbose_middlewareObject



7
8
9
10
11
# File 'lib/amitree/git_client.rb', line 7

def self.verbose_middleware
  Octokit::Default::MIDDLEWARE.dup.tap do |middleware|
    middleware.response :logger, nil, bodies: true
  end
end

Instance Method Details

#commits_since(rev) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/amitree/git_client.rb', line 23

def commits_since(rev)
  result = []

  @client.commits(@repository, per_page: 100)
  response = @client.last_response

  loop do
    if response.data.length == 0
      raise "Empty response received from GitHub!"
    end

    result.concat response.data

    if index = result.index{|commit| commit.sha.start_with?(rev)}
      return result[0...index].reverse
    end
    if result.length >= @max_commit_range
      raise NotFoundError, "Failed to find #{rev} in the most recent #{@max_commit_range} commits. Consider increasing max_commit_range."
    end
    unless page = response.rels[:next]
      raise NotFoundError, "Failed to find #{rev} in entire commit history."
    end

    response = page.get
  end
end


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

def link_to(rev)
  "https://github.com/#{@repository}/commit/#{rev}"
end

#range_since(rev) ⇒ Object



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

def range_since(rev)
  Range.new(commits_since(rev))
end