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



81
82
83
# File 'lib/txgh/github_api.rb', line 81

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

#download(path, branch) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/txgh/github_api.rb', line 67

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



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

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

#get_ref(ref) ⇒ Object



63
64
65
# File 'lib/txgh/github_api.rb', line 63

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_map, message) ⇒ Object



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

def update_contents(branch, content_map, message)
  content_map.each do |path, new_contents|
    branch = Utils.relative_branch(branch)

    file = begin
      client.contents(repo_name, { path: path, ref: branch })
    rescue Octokit::NotFound
      nil
    end

    current_sha = file ? 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