Module: Brainiac::Plugins::Github::AppClient

Defined in:
lib/brainiac/plugins/github/app_client.rb

Overview

HTTP client that authenticates as a GitHub App (installation).

When app credentials are configured (app_id + private_key_path + installation_id), API calls are made as the App's bot user — so PR comments, reactions, etc. appear with the App's identity rather than a personal user.

Falls back to gh CLI when app credentials are not configured.

Constant Summary collapse

GITHUB_API =
"https://api.github.com"
TOKEN_EXPIRY_BUFFER =

refresh token 60s before expiry

60

Class Method Summary collapse

Class Method Details

.configured?Boolean

Returns true if GitHub App credentials are fully configured.

Returns:

  • (Boolean)


30
31
32
# File 'lib/brainiac/plugins/github/app_client.rb', line 30

def configured?
  !!(Config.app_id && Config.private_key_path && Config.installation_id)
end

.create_comment(repo, pr_number, body) ⇒ Hash

POST a comment on an issue or PR.

Parameters:

  • repo (String)

    "owner/repo"

  • pr_number (Integer)
  • body (String)

    comment markdown

Returns:

  • (Hash)

    parsed response



40
41
42
# File 'lib/brainiac/plugins/github/app_client.rb', line 40

def create_comment(repo, pr_number, body)
  post("/repos/#{repo}/issues/#{pr_number}/comments", { body: body })
end

.create_comment_reaction(repo, comment_id, reaction) ⇒ Hash

POST a reaction on an issue comment.

Parameters:

  • repo (String)

    "owner/repo"

  • comment_id (Integer)
  • reaction (String)

    e.g. "eyes", "+1", "rocket"

Returns:

  • (Hash)

    parsed response



50
51
52
# File 'lib/brainiac/plugins/github/app_client.rb', line 50

def create_comment_reaction(repo, comment_id, reaction)
  post("/repos/#{repo}/issues/comments/#{comment_id}/reactions", { content: reaction })
end

.create_review_reaction(repo, review_id, reaction) ⇒ Hash

POST a reaction on a PR review.

Parameters:

  • repo (String)

    "owner/repo"

  • review_id (Integer)
  • reaction (String)

Returns:

  • (Hash)

    parsed response



60
61
62
# File 'lib/brainiac/plugins/github/app_client.rb', line 60

def create_review_reaction(repo, review_id, reaction)
  post("/repos/#{repo}/pulls/reviews/#{review_id}/reactions", { content: reaction })
end

.get(path) ⇒ Hash

GET request to GitHub API.

Parameters:

  • path (String)

    API path (e.g. "/repos/owner/repo/pulls/1")

Returns:

  • (Hash)

    parsed response



68
69
70
# File 'lib/brainiac/plugins/github/app_client.rb', line 68

def get(path)
  request(:get, path)
end

.post(path, body) ⇒ Hash

POST request to GitHub API.

Parameters:

  • path (String)

    API path

  • body (Hash)

    request body

Returns:

  • (Hash)

    parsed response



77
78
79
# File 'lib/brainiac/plugins/github/app_client.rb', line 77

def post(path, body)
  request(:post, path, body)
end

.reset!Object

Reset cached token (useful for testing or when credentials change).



82
83
84
85
86
87
# File 'lib/brainiac/plugins/github/app_client.rb', line 82

def reset!
  @mutex.synchronize do
    @token = nil
    @token_expires_at = nil
  end
end