Class: Ask::GitHub::Content

Inherits:
Object
  • Object
show all
Defined in:
lib/ask/github/client.rb

Overview

The repo-content helpers: the raw pieces a docs-as-code connector needs (tree listing, raw file reads, the default branch, and the repo's license). Explicit methods, so connectors never poke at Octokit's full surface.

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Content

Returns a new instance of Content.



56
57
58
# File 'lib/ask/github/client.rb', line 56

def initialize(client)
  @client = client
end

Instance Method Details

#default_branch(repo) ⇒ Object

The repository's default branch name (e.g. "main").



61
62
63
# File 'lib/ask/github/client.rb', line 61

def default_branch(repo)
  call { @client.repo(repo).default_branch }
end

#file(repo, path, branch: nil) ⇒ Object

A file's raw content at a path, or nil when the path doesn't exist.



75
76
77
78
79
80
81
82
# File 'lib/ask/github/client.rb', line 75

def file(repo, path, branch: nil)
  call do
    @client.contents(repo, path: path, ref: branch || default_branch(repo),
      accept: "application/vnd.github.raw")
  end
rescue ::Octokit::NotFound
  nil
end

#license(repo) ⇒ Object

The repo's SPDX license id (e.g. "mit"), or nil when unlicensed.



85
86
87
88
89
# File 'lib/ask/github/client.rb', line 85

def license(repo)
  call { @client.license(repo) }&.license&.spdx_id
rescue ::Octokit::NotFound
  nil
end

#tree(repo, branch: nil) ⇒ Object

The recursive file tree for a branch: [type:] entries. GitHub truncates enormous trees; callers bound what they consume.



67
68
69
70
71
72
# File 'lib/ask/github/client.rb', line 67

def tree(repo, branch: nil)
  sha = branch || default_branch(repo)
  call { @client.tree(repo, sha, recursive: true) }.tree.map do |entry|
    {path: entry.path, type: entry.type}
  end
end