Class: LocaleNinja::GithubApiService

Inherits:
Object
  • Object
show all
Defined in:
app/services/locale_ninja/github_api_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token:) ⇒ GithubApiService

Returns a new instance of GithubApiService.



8
9
10
# File 'app/services/locale_ninja/github_api_service.rb', line 8

def initialize(access_token:)
  @client = ::Octokit::Client.new(access_token:)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



12
13
14
# File 'app/services/locale_ninja/github_api_service.rb', line 12

def client
  @client
end

Instance Method Details

#branch?(branch_name) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'app/services/locale_ninja/github_api_service.rb', line 72

def branch?(branch_name)
  branch_names.include?(branch_name)
end

#branch_namesObject



88
89
90
# File 'app/services/locale_ninja/github_api_service.rb', line 88

def branch_names
  @branch_names ||= branches.map(&:name)
end

#branchesObject



84
85
86
# File 'app/services/locale_ninja/github_api_service.rb', line 84

def branches
  @branches ||= @client.branches(repository_fullname)
end

#create_branch(parent_branch, child_branch) ⇒ Object



66
67
68
69
70
# File 'app/services/locale_ninja/github_api_service.rb', line 66

def create_branch(parent_branch, child_branch)
  sha = @client.ref(repository_fullname, "heads/#{parent_branch}").dig(:object, :sha)
  @client.create_ref(repository_fullname, "heads/#{child_branch}", sha)
  @branches << child_branch
end

#create_file(file_path, content, branch: 'translations') ⇒ Object



62
63
64
# File 'app/services/locale_ninja/github_api_service.rb', line 62

def create_file(file_path, content, branch: 'translations')
  @client.create_contents(repository_fullname, file_path, "translations #{DateTime.current}", content, branch:)
end

#create_translation_branch(branch) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'app/services/locale_ninja/github_api_service.rb', line 40

def create_translation_branch(branch)
  return branch if branch.ends_with?('__translations')

  translation_branch = "#{branch}__translations"
  return translation_branch if branch?(translation_branch)

  create_branch(branch, translation_branch)
  translation_branch
end

#default_branchObject



92
93
94
# File 'app/services/locale_ninja/github_api_service.rb', line 92

def default_branch
  %w[main master].find { |branch_name| branch_names.include?(branch_name) } || branch_names.first
end

#locale_files_path(dir = 'config/locales', branch: 'translations') ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'app/services/locale_ninja/github_api_service.rb', line 22

def locale_files_path(dir = 'config/locales', branch: 'translations')
  branch = translation_branch(branch) if branch?(translation_branch(branch))
  @client.contents(repository_fullname, path: dir, ref: "heads/#{branch}").map do |file|
    if file.type == 'dir'
      locale_files_path(file.path, branch:)
    else
      file.path
    end
  end.flatten
end

#public_branch_namesObject



96
97
98
# File 'app/services/locale_ninja/github_api_service.rb', line 96

def public_branch_names
  branch_names.reject { |branch| branch.ends_with?('__translations') }
end

#pull(files = nil, branch: 'translations') ⇒ Object



33
34
35
36
37
38
# File 'app/services/locale_ninja/github_api_service.rb', line 33

def pull(files = nil, branch: 'translations')
  files ||= locale_files_path(branch:)
  branch = translation_branch(branch) if branch?(translation_branch(branch))

  files.index_with { |path| Base64.decode64(@client.contents(repository_fullname, path:, ref: "heads/#{branch}").content) }
end

#pull_request(branch_name) ⇒ Object



100
101
102
103
104
# File 'app/services/locale_ninja/github_api_service.rb', line 100

def pull_request(branch_name)
  @client.create_pull_request(repository_fullname, branch_name, "#{branch_name}__translations", "translations #{Time.current}")
rescue ::Octokit::UnprocessableEntity
  # If pull request already exists, do nothing
end

#push(file_path, content, branch: 'translations') ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'app/services/locale_ninja/github_api_service.rb', line 50

def push(file_path, content, branch: 'translations')
  branch = create_translation_branch(branch)

  begin
    sha = @client.content(repository_fullname, path: file_path, ref: "heads/#{branch}")[:sha]
  rescue ::Octokit::NotFound
    create_file(file_path, content, branch:)
    return
  end
  @client.update_contents(repository_fullname, file_path, "translations #{DateTime.current}", sha, content, branch:)
end

#repo_infomationObject



80
81
82
# File 'app/services/locale_ninja/github_api_service.rb', line 80

def repo_infomation
  @client.repository(repository_fullname).to_h.slice(:full_name, :html_url)
end

#repository_fullnameObject



76
77
78
# File 'app/services/locale_ninja/github_api_service.rb', line 76

def repository_fullname
  @repository_fullname ||= REPOSITORY_FULLNAME
end

#translation_branch(branch) ⇒ Object



18
19
20
# File 'app/services/locale_ninja/github_api_service.rb', line 18

def translation_branch(branch)
  branch.ends_with?('__translations') ? branch : "#{branch}__translations"
end

#userObject



14
15
16
# File 'app/services/locale_ninja/github_api_service.rb', line 14

def user
  @client.user
end