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) ⇒ GithubApi



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

def initialize(client)
  @client = client
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

Class Method Details

.create_from_client(client) ⇒ Object



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

def create_from_client(client)
  new(client)
end

.create_from_credentials(login, access_token) ⇒ Object



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

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

Instance Method Details

#blob(repo, sha) ⇒ Object



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

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

#commit(repo, branch, content_map, allow_empty = false) ⇒ Object



36
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
# File 'lib/txgh/github_api.rb', line 36

def commit(repo, branch, content_map, allow_empty = false)
  parent = client.ref(repo, branch)
  base_commit = get_commit(repo, parent[:object][:sha])

  tree_data = content_map.map do |path, content|
    blob = client.create_blob(repo, content)
    { path: path, mode: '100644', type: 'blob', sha: blob }
  end

  tree_options = { base_tree: base_commit[:commit][:tree][:sha] }

  tree = client.create_tree(repo, tree_data, tree_options)
  commit = client.create_commit(
    repo, "Updating translations for #{content_map.keys.join(", ")}",
    tree[:sha], parent[:object][:sha]
  )

  # don't update the ref if the commit introduced no new changes
  unless allow_empty
    diff = client.compare(repo, parent[:object][:sha], commit[:sha])
    return if diff[:files].empty?
  end

  # false means don't force push
  client.update_ref(repo, branch, commit[:sha], false)
end

#create_ref(repo, branch, sha) ⇒ Object



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

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

#download(repo, path, branch) ⇒ Object



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

def download(repo, path, branch)
  master = client.ref(repo, branch)
  commit = client.commit(repo, master[:object][:sha])
  tree = client.tree(repo, commit[:commit][:tree][:sha], recursive: 1)

  if found = tree[:tree].find { |t| t[:path] == path }
    b = blob(repo, found[:sha])
    b['encoding'] == 'utf-8' ? b['content'] : Base64.decode64(b['content'])
  end
end

#get_commit(repo, sha) ⇒ Object



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

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

#get_ref(repo, ref) ⇒ Object



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

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

#tree(repo, sha) ⇒ Object



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

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