Class: Github::TreeClient

Inherits:
Object
  • Object
show all
Includes:
Checks
Defined in:
app/github/tree_client.rb

Overview

Wraps a Github::Client to provide tree-specific features.

Constant Summary collapse

DEFAULT_MAX_TREE_CACHE_AGE_SECONDS =

5 minutes

60 * 5
DEFAULT_MAX_FILE_CACHE_AGE_SECONDS =

1 year

60 * 60 * 24 * 365

Class Attribute Summary collapse

Instance Method Summary collapse

Methods included from Checks

#check_boolean, #check_enumerable_of, #check_is_a, #check_non_empty_string, #check_non_nil, #check_positive_integer

Constructor Details

#initialize(access_token:, cache:, logger: Logger.new($stdout)) ⇒ TreeClient

Returns a new instance of TreeClient.



30
31
32
33
34
35
36
# File 'app/github/tree_client.rb', line 30

def initialize(access_token:, cache:, logger: Logger.new($stdout))
  check_non_empty_string(access_token: access_token)
  check_is_a(cache: [Cache, cache])

  @client = Github::CachingClient.new(Github::Client.new(access_token: access_token, logger: logger), cache)
  @logger = check_non_nil(logger: logger)
end

Class Attribute Details

.thread_poolObject (readonly)

Returns the value of attribute thread_pool.



24
25
26
# File 'app/github/tree_client.rb', line 24

def thread_pool
  @thread_pool
end

Instance Method Details

#create_commit_with_file(owner:, repo:, base_sha:, branch:, path:, content:, author_name:, author_email:) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/github/tree_client.rb', line 69

def create_commit_with_file(owner:, repo:, base_sha:, branch:, path:, content:, author_name:, author_email:)
  check_non_empty_string(
    owner: owner,
    repo: repo,
    base_sha: base_sha,
    branch: branch,
    path: path,
    content: content,
    author_name: author_name,
    author_email: author_email
  )

  begin
    @client.get_without_caching("https://api.github.com/repos/#{owner}/#{repo}/git/ref/#{branch}")
    raise "branch already exists: #{owner}/#{repo}/#{branch}"
  rescue RestClient::NotFound
    # ignored
  end

  blob_response = @client.post(
    "https://api.github.com/repos/#{owner}/#{repo}/git/blobs",
    {
      content: Base64.encode64(content),
      encoding: 'base64'
    }
  )
  tree_response = @client.post(
    "https://api.github.com/repos/#{owner}/#{repo}/git/trees",
    {
      base_tree: base_sha,
      tree: [
        {
          path: path,
          mode: '100644',
          type: 'blob',
          sha: check_non_empty_string(sha: blob_response['sha'])
        }
      ]
    }
  )
  basename = ::File.basename(path)
  message = basename.size <= 45 ? "Edit #{basename}" : "Edit #{basename[0...44]}…"
  description = basename.size <= 45 ? nil : "…#{basename[45..]}"
  commit_response = @client.post(
    "https://api.github.com/repos/#{owner}/#{repo}/git/commits",
    {
      message: message,
      description: description,
      author: {
        name: author_name,
        email: author_email,
        date: Time.now.iso8601
      },
      parents: [
        base_sha
      ],
      tree: check_non_empty_string(sha: tree_response['sha'])
    }
  )
  @client.post(
    "https://api.github.com/repos/#{owner}/#{repo}/git/refs",
    {
      ref: "refs/heads/#{branch}",
      sha: check_non_empty_string(sha: commit_response['sha'])
    }
  )
end

#get_tree(owner:, repo:, ref:, regex:, cache: true) ⇒ Object



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
65
66
67
# File 'app/github/tree_client.rb', line 38

def get_tree(owner:, repo:, ref:, regex:, cache: true)
  check_non_empty_string(owner: owner, repo: repo, ref: ref)
  check_is_a(regex: [Regexp, regex])

  response = if cache
               @client.get_with_caching(
                 "https://api.github.com/repos/#{owner}/#{repo}/git/trees/#{ref}?recursive=true",
                 cache_for: DEFAULT_MAX_TREE_CACHE_AGE_SECONDS
               )
             else
               @client.get_without_caching("https://api.github.com/repos/#{owner}/#{repo}/git/trees/#{ref}?recursive=true")
             end

  response['tree'] = response['tree'].select { |blob| regex.match?(blob['path']) }
  tree_size = response['tree'].size
  latch = CountDownLatch.new(tree_size)
  response['tree'].each do |blob|
    TreeClient.thread_pool.post do
      blob_response = @client.get_with_caching(blob['url'], cache_for: DEFAULT_MAX_FILE_CACHE_AGE_SECONDS)
      blob['content'] = Base64.decode64(blob_response['content'])
    ensure
      latch.count_down
    end
  end

  latch.await(timeout: 10 + tree_size)
  raise 'failed to load saved files' unless response['tree'].all? { |blob| blob['content'] }

  Tree.for(owner: owner, repo: repo, ref: ref, tree_response: response)
end