Class: Txgh::GithubApi

Inherits:
GitApi
  • Object
show all
Defined in:
lib/txgh/github_api.rb

Instance Attribute Summary

Attributes inherited from GitApi

#client, #repo_name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GitApi

create_from_client, #initialize

Constructor Details

This class inherits a constructor from Txgh::GitApi

Class Method Details

.create_from_credentials(login, access_token, repo_name) ⇒ Object



6
7
8
9
10
# File 'lib/txgh/github_api.rb', line 6

def create_from_credentials(, access_token, repo_name)
  create_from_client(
    Octokit::Client.new(login: , access_token: access_token), repo_name
  )
end

Instance Method Details

#blob(sha) ⇒ Object



17
18
19
# File 'lib/txgh/github_api.rb', line 17

def blob(sha)
  client.blob(repo_name, sha)
end

#create_ref(branch, sha) ⇒ Object



21
22
23
# File 'lib/txgh/github_api.rb', line 21

def create_ref(branch, sha)
  client.create_ref(repo_name, branch, sha) rescue false
end

#create_status(sha, state, options = {}) ⇒ Object



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

def create_status(sha, state, options = {})
  client.create_status(repo_name, sha, state, options)
end

#download(path, branch) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/txgh/github_api.rb', line 62

def download(path, branch)
  file = client.contents(repo_name, { path: path, ref: branch }).to_h

  file[:content] = case file[:encoding]
    when 'base64'
      Base64.decode64(file[:content])
    else
      file[:content].force_encoding(file[:encoding])
  end

  file.delete(:encoding)
  file
end

#get_commit(sha) ⇒ Object



54
55
56
# File 'lib/txgh/github_api.rb', line 54

def get_commit(sha)
  client.commit(repo_name, sha)
end

#get_ref(ref) ⇒ Object



58
59
60
# File 'lib/txgh/github_api.rb', line 58

def get_ref(ref)
  client.ref(repo_name, ref)
end

#tree(sha) ⇒ Object



13
14
15
# File 'lib/txgh/github_api.rb', line 13

def tree(sha)
  client.tree(repo_name, sha, recursive: 1)
end

#update_contents(branch, content_list, message) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/txgh/github_api.rb', line 25

def update_contents(branch, content_list, message)
  content_list.each do |file_params|
    path = file_params.fetch(:path)
    new_contents = file_params.fetch(:contents)
    branch = Utils.relative_branch(branch)

    file_sha = file_params.fetch(:sha) do
      begin
        client.contents(repo_name, { path: path, ref: branch })[:sha]
      rescue Octokit::NotFound
        nil
      end
    end

    # If the file doesnt exist, then it isn't tracked by git and file_sha
    # will be nil. In git land, a SHA of all zeroes means create a new file
    # instead of updating an existing one.
    current_sha = file_sha || '0' * 40
    new_sha = Utils.git_hash_blob(new_contents)
    options = { branch: branch }

    if current_sha != new_sha
      client.update_contents(
        repo_name, path, message, current_sha, new_contents, options
      )
    end
  end
end