Class: Gergich::Commit

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/gergich.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

log_level, logger, #logger

Constructor Details

#initialize(ref = ENV.fetch("GERRIT_PATCHSET_REVISION", "HEAD"), revision_number = nil) ⇒ Commit

Returns a new instance of Commit.



41
42
43
44
45
# File 'lib/gergich.rb', line 41

def initialize(ref = ENV.fetch("GERRIT_PATCHSET_REVISION", "HEAD"), revision_number = nil)
  @ref = ref
  @revision_number = revision_number
  logger.debug "Commit initialized with ref: #{ref}"
end

Instance Attribute Details

#refObject (readonly)

Returns the value of attribute ref.



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

def ref
  @ref
end

Instance Method Details

#change_idObject



92
93
94
95
96
97
98
# File 'lib/gergich.rb', line 92

def change_id
  if info[:project] && info[:branch]
    "#{info[:project]}~#{ERB::Util.url_encode info[:branch]}~#{info[:change_id]}"
  else
    info[:change_id]
  end
end

#filesObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/gergich.rb', line 65

def files
  @files ||= if Gergich.use_git?
    Gergich.git("diff-tree --no-commit-id --name-only -r #{ref}").split
  else
    raw = API.get("/changes/#{change_id}/revisions/#{revision_id}/patch", raw: true)
    raw.unpack1("m")
      .scan(%r{^diff --git a/.*? b/(.*?)$})
      .flatten
  end
end

#infoObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gergich.rb', line 47

def info
  @info ||= begin
    if Gergich.use_git?
      output = Gergich.git("log -1 #{ref}")
      /\Acommit (?<revision_id>[0-9a-f]+).*^\s*Change-Id: (?<change_id>\w+)/m =~ output
    else
      revision_id = ENV["GERRIT_PATCHSET_REVISION"] \
        || raise(GergichError, "No .git directory, and GERRIT_PATCHSET_REVISION not set")
      change_id = ENV["GERRIT_CHANGE_ID"] \
        || raise(GergichError, "No .git directory, and GERRIT_CHANGE_ID not set")
    end
    project = ENV["GERRIT_PROJECT"]
    branch = ENV["GERRIT_BRANCH"]

    { revision_id: revision_id, change_id: change_id, project: project, branch: branch }
  end
end

#revision_idObject



76
77
78
# File 'lib/gergich.rb', line 76

def revision_id
  info[:revision_id]
end

#revision_numberObject



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/gergich.rb', line 80

def revision_number
  @revision_number ||= begin
    patchset_number = ENV["GERRIT_PATCHSET_NUMBER"]
    return patchset_number unless patchset_number.nil? # rubocop:disable Lint/NoReturnInBeginEndBlocks

    gerrit_info = API.get("/changes/?q=#{change_id}&o=ALL_REVISIONS")[0]
    raise GergichError, "Gerrit patchset not found" unless gerrit_info

    gerrit_info["revisions"][revision_id]["_number"]
  end
end