Class: Gergich::Commit

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Commit.



32
33
34
35
# File 'lib/gergich.rb', line 32

def initialize(ref = ENV.fetch("GERRIT_PATCHSET_REVISION", "HEAD"), revision_number = nil)
  @ref = ref
  @revision_number = revision_number
end

Instance Attribute Details

#refObject (readonly)

Returns the value of attribute ref.



30
31
32
# File 'lib/gergich.rb', line 30

def ref
  @ref
end

Instance Method Details

#change_idObject



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

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



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gergich.rb', line 55

def files
  @files ||= begin
    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)
      Base64.decode64(raw)
        .scan(%r{^diff --git a/.*? b/(.*?)$})
        .flatten
    end
  end
end

#infoObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/gergich.rb', line 37

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



68
69
70
# File 'lib/gergich.rb', line 68

def revision_id
  info[:revision_id]
end

#revision_numberObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/gergich.rb', line 72

def revision_number
  @revision_number ||= begin
    patchset_number = ENV["GERRIT_PATCHSET_NUMBER"]
    return patchset_number unless patchset_number.nil?

    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