Module: Github::Commit

Defined in:
lib/github/commit.rb

Constant Summary collapse

ENDPOINT =
URI.join(Github::ROOT_ENDPOINT, 'repos/', "#{Github::OWNER}/", "#{Github::REPO}/", 'git/', 'commits').to_s
AUTHOR_KEYS =
%w(
  name
  email
  date
)

Class Method Summary collapse

Class Method Details

.create(message, tree, parents, author = {}) ⇒ Object

Returns - commit sha.

Parameters:

  • -

    message: The commit message

  • -

    tree: The SHA of the tree object this commit points to

  • -

    parents: Array of strings. The SHAs of the commits that were the parents of this commit. If omitted or empty, the commit will be written as a root commit. For a single parent, an array of one SHA should be provided; for a merge commit, an array of more than one should be provided.

Returns:

    • commit sha



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/github/commit.rb', line 24

def self.create(message, tree, parents, author={})
  input = {
    'message' => message,
    'tree' => tree,
    'parents' => parents
  }
  author.slice!(*AUTHOR_KEYS)
  input.merge!({'author' => author}) if author.present?
  resp = Github.post(ENDPOINT, input)
  raise "Github commit POST failed with http code: #{resp.code}" if resp.code != '201'
  ActiveSupport::JSON.decode(resp.body)['sha']
end

.get(sha) ⇒ Object



11
12
13
14
15
16
# File 'lib/github/commit.rb', line 11

def self.get(sha)
  raise 'invalid sha #{sha} when retrieving github commit' unless Github.valid_sha?(sha)
  resp = Github.get("#{ENDPOINT}/#{sha}")
  raise "Github commit retrieve failed with http code: #{resp.code}" if resp.code != '200'
  ActiveSupport::JSON.decode(resp.body)
end