Class: Txgh::GithubApi

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, repo_name) ⇒ GithubApi

Returns a new instance of GithubApi.



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

def initialize(client, repo_name)
  @client = client
  @repo_name = repo_name
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#repo_nameObject (readonly)

Returns the value of attribute repo_name.



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

def repo_name
  @repo_name
end

Class Method Details

.create_from_client(client, repo_name) ⇒ Object



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

def create_from_client(client, repo_name)
  new(client, repo_name)
end

.create_from_credentials(login, access_token, repo_name) ⇒ Object



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

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



29
30
31
# File 'lib/txgh/github_api.rb', line 29

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

#create_ref(branch, sha) ⇒ Object



33
34
35
# File 'lib/txgh/github_api.rb', line 33

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

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



88
89
90
# File 'lib/txgh/github_api.rb', line 88

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

#download(path, branch) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/txgh/github_api.rb', line 74

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



66
67
68
# File 'lib/txgh/github_api.rb', line 66

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

#get_ref(ref) ⇒ Object



70
71
72
# File 'lib/txgh/github_api.rb', line 70

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

#tree(sha) ⇒ Object



25
26
27
# File 'lib/txgh/github_api.rb', line 25

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

#update_contents(branch, content_list, message) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/txgh/github_api.rb', line 37

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