Class: Codeowners::Git::Contributors

Inherits:
Object
  • Object
show all
Defined in:
lib/codeowners/git/contributors.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Contributors

Returns a new instance of Contributors.



72
73
74
75
76
# File 'lib/codeowners/git/contributors.rb', line 72

def initialize(data)
  @contributors = data.map do |email, stats|
    Contributor.new(email, *stats.values)
  end
end

Class Method Details

.calculate_stats(stats) ⇒ Object



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

def self.calculate_stats(stats)
  stats.each_with_object([0, 0]) do |stat, result|
    stat = stat.split(/[[:space:]]+/)

    insertions, deletions, = *stat
    result[0] += Integer(insertions)
    result[1] += Integer(deletions)
  end
end

.call(file, output) ⇒ Object

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/codeowners/git/contributors.rb', line 11

def self.call(file, output)
  lines = output.split($INPUT_RECORD_SEPARATOR)

  result = {}
  each_commit(lines) do |authors, insertions, deletions|
    authors.each do |author|
      author_email = author.fetch("email")
      author_name  = author.fetch("name")

      result[author_email] ||= {}
      result[author_email]["name"] = author_name
      result[author_email]["file"] = file
      result[author_email]["insertions"] ||= 0
      result[author_email]["deletions"] ||= 0
      result[author_email]["insertions"] += insertions
      result[author_email]["deletions"] += deletions
    end
  end

  new(result)
end

.each_commit(lines) ⇒ Object

rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize



35
36
37
38
39
40
41
# File 'lib/codeowners/git/contributors.rb', line 35

def self.each_commit(lines)
  while lines.any?
    commit = lines.take_while { |line| line != "" }
    yield parse(commit.dup) unless commit.empty?
    lines.shift(commit.size + 1)
  end
end

.extract_authors(authors) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/codeowners/git/contributors.rb', line 49

def self.extract_authors(authors)
  authors.map do |author|
    {
      "name" => scan(author, /author:(.*)email:/).chop,
      "email" => scan(author, /email:(.*)/)
    }
  end.uniq
end

.parse(commit) ⇒ Object



43
44
45
46
47
# File 'lib/codeowners/git/contributors.rb', line 43

def self.parse(commit)
  authors, stats = commit.partition { |line| line.match?(/author:/) }

  [extract_authors(authors), *calculate_stats(stats)]
end

.scan(string, pattern) ⇒ Object



68
69
70
# File 'lib/codeowners/git/contributors.rb', line 68

def self.scan(string, pattern)
  string.scan(pattern).flatten.first
end

Instance Method Details

#each(&blk) ⇒ Object



78
79
80
81
82
# File 'lib/codeowners/git/contributors.rb', line 78

def each(&blk)
  return enum_for(:each) unless block_given?

  @contributors.each(&blk)
end

#empty?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/codeowners/git/contributors.rb', line 84

def empty?
  @contributors.empty?
end