Class: GithubCommit::CommitsFetcher

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

Instance Method Summary collapse

Constructor Details

#initialize(repo:, client: Octokit::Client.new) ⇒ CommitsFetcher

Returns a new instance of CommitsFetcher.



3
4
5
6
# File 'lib/github_commit/commits_fetcher.rb', line 3

def initialize(repo:, client: Octokit::Client.new)
  @client = client
  @repo = repo
end

Instance Method Details

#commits(sha: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/github_commit/commits_fetcher.rb', line 8

def commits(sha: nil)
  branch_commits = client.branches(repo).map do |branch|
    client.commits(repo, { sha: branch.commit.sha, per_page: 15 })
      .map do |c|
        {
          date: c.commit.committer.date,
          sha: c.sha,
        }
      end
  end

  commits = branch_commits.flatten
    .uniq { |c| c.dig(:sha) }
    .sort { |a, b| b.dig(:date) <=> a.dig(:date) }
    .map { |s| { ref: s.dig(:sha) } }

  return [commits.first] unless sha

  index = commits.index({ ref: sha })
  index = 1 if index == commits.count - 1
  output = commits[0..index]

  output.reverse
end