Module: Github::Tree

Defined in:
lib/github/tree.rb

Constant Summary collapse

ENDPOINT =
URI.join(Github::ROOT_ENDPOINT, 'repos/', "#{Github::OWNER}/", "#{Github::REPO}/", 'git/', 'trees').to_s

Class Method Summary collapse

Class Method Details

.create(tree, base_tree) ⇒ Object

Parameters:

  • -

    tree: array of hashes. Objects (of path, mode, type, and sha) specifying a tree structure

  • -

    base_tree: The SHA1 of the tree you want to update with new data. If you don’t set this, the commit will be created on top of everything; however, it will only contain your change, the rest of your files will show up as deleted.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/github/tree.rb', line 17

def self.create(tree, base_tree)
  return unless tree.instance_of?(Array)
  params = {
    'base_tree' => base_tree,
    'tree' => tree
  }

  resp = Github.post(ENDPOINT, params)
  raise "Github tree POST failed with http code: #{resp.code}\nBody is: #{resp.body}" if resp.code != '201'
  ActiveSupport::JSON.decode(resp.body)['sha']
end

.get(sha, recursive = false) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/github/tree.rb', line 5

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