Class: Worklog::Github::Client

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

Overview

Client to interact with GitHub API

Defined Under Namespace

Classes: GithubAPIError

Constant Summary collapse

EVENT_FILTER =
Set.new(%w[
  PullRequestEvent
  PullRequestReviewEvent
]).freeze

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Client

Returns a new instance of Client.



21
22
23
24
25
26
# File 'lib/github/client.rb', line 21

def initialize(configuration)
  @configuration = configuration

  # Cache for events to avoid redundant API calls
  @event_cache = {}
end

Instance Method Details

#fetch_eventsArray<PullRequestEvent, PullRequestReviewEvent>

Fetch events for a given user from Github API PullRequestEvent and PullRequestReviewEvent objects

Returns:



31
32
33
34
35
36
37
38
39
40
# File 'lib/github/client.rb', line 31

def fetch_events
  verify_token!

  # Clear the cache before fetching new events
  @event_cache.clear

  WorkLogger.debug 'Fetching most recent GitHub events...'
  responses = fetch_event_pages
  responses.filter_map { |event| create_event(event) }
end

#pull_request_comments(repo, pr_number) ⇒ Object



48
49
50
51
52
# File 'lib/github/client.rb', line 48

def pull_request_comments(repo, pr_number)
  response = HTTParty.get("https://api.github.com/repos/#{repo}/pulls/#{pr_number}/comments",
                          headers: { 'Authorization' => "token #{TOKEN}" })
  response.parsed_response
end

#pull_request_data(repo, pr_number) ⇒ Object



42
43
44
45
46
# File 'lib/github/client.rb', line 42

def pull_request_data(repo, pr_number)
  response = HTTParty.get("https://api.github.com/repos/#{repo}/pulls/#{pr_number}",
                          headers: { 'Authorization' => "token #{TOKEN}" })
  response.parsed_response
end