class Saddler::Reporter::Github::Client

GitHub client wrapper

Public Class Methods

new(repo) click to toggle source

@param repo [Repository] git repository

# File lib/saddler/reporter/github/client.rb, line 7
def initialize(repo)
  @repo = repo
end

Public Instance Methods

access_token() click to toggle source

@return [String, nil] github access token

# File lib/saddler/reporter/github/client.rb, line 120
def access_token
  ENV['GITHUB_ACCESS_TOKEN']
end
client() click to toggle source

@return [Octokit::Client]

# File lib/saddler/reporter/github/client.rb, line 115
def client
  @client ||= Octokit::Client.new(access_token: access_token)
end
commit_comments(sha) click to toggle source

@param sha [String] target commit sha

@return [Array<Comment>] parse commit comments into Comment

# File lib/saddler/reporter/github/client.rb, line 14
def commit_comments(sha)
  client.commit_comments(slug, sha).map do |comment|
    Comment.new(sha, comment.body, comment.path, comment.position)
  end
end
commit_patches(sha) click to toggle source

@param sha [String] target commit sha

@return [Patches<Patch>] patches

# File lib/saddler/reporter/github/client.rb, line 33
def commit_patches(sha)
  patches = Patches.new
  client.commit(slug, sha).files.each do |file|
    patches << Patch.new(file.patch, file: file.filename, secure_hash: sha)
  end
  patches
end
create_commit_comment(comment) click to toggle source

@param comment [Comment] posting commit comment

@return [Sawyer::Resource] Commit comment

@see ::Octokit::Client::CommitComments.create_commit_comment

# File lib/saddler/reporter/github/client.rb, line 25
def create_commit_comment(comment)
  client.create_commit_comment(slug, comment.sha, comment.body,
                               comment.path, nil, comment.position)
end
create_issue_comment(comment) click to toggle source

@param comment [Comment] posting issue comment

@return [Sawyer::Resource] Comment

@see ::Octokit::Client::Issues.add_comment

# File lib/saddler/reporter/github/client.rb, line 54
def create_issue_comment(comment)
  client.add_comment(slug, pull_id, comment.body)
end
create_pull_request_review_comment(comment) click to toggle source

@param comment [Comment] posting pull request comment

@return [Sawyer::Resource] Hash representing the new comment

@see ::Octokit::Client::PullRequests.create_pull_request_comment

# File lib/saddler/reporter/github/client.rb, line 79
def create_pull_request_review_comment(comment)
  client.create_pull_request_comment(slug, pull_id, comment.body,
                                     comment.sha, comment.path, comment.position)
end
env_pull_id() click to toggle source

@return [Integer, nil] pull request id from environment variables

# File lib/saddler/reporter/github/client.rb, line 125
def env_pull_id
  @env_pull_id ||= EnvPullRequest.new.pull_request_id
end
issue_comments() click to toggle source

@return [Array<Comment>] parse issue comments into Comment

# File lib/saddler/reporter/github/client.rb, line 42
def issue_comments
  client.issue_comments(slug, pull_id).map do |comment|
    sha = nil
    Comment.new(sha, comment.body, comment.path, comment.position)
  end
end
pull_id() click to toggle source

@return [Integer, nil] pull request id

# File lib/saddler/reporter/github/client.rb, line 85
def pull_id
  return @pull_id unless @pull_id.nil?

  if env_pull_id
    @pull_id = env_pull_id
    return @pull_id
  end

  if @repo.current_branch
    pull = pull_requests.find { |pr| pr[:head][:ref] == @repo.current_branch }
    if pull
      @pull_id = pull[:number].to_i
      return @pull_id
    end
  end
end
pull_request_patches() click to toggle source

@return [Patches<Patch>] patches

# File lib/saddler/reporter/github/client.rb, line 66
def pull_request_patches
  patches = Patches.new
  client.pull_request_files(slug, pull_id).each do |file|
    patches << Patch.new(file.patch, file: file.filename, secure_hash: @repo.merging_sha)
  end
  patches
end
pull_request_review_comments() click to toggle source

@return [Array<Comment>] parse pull request comments into Comment

# File lib/saddler/reporter/github/client.rb, line 59
def pull_request_review_comments
  client.pull_request_comments(slug, pull_id).map do |comment|
    Comment.new(comment.commit_id, comment.body, comment.path, comment.position)
  end
end
pull_requests() click to toggle source

@return [Array<Sawyer::Resource>] Array of pulls

@see ::Octokit::Client::PullRequests.pull_requests

# File lib/saddler/reporter/github/client.rb, line 105
def pull_requests
  @pull_requests ||= client.pull_requests(slug)
end
slug() click to toggle source

@return [String, nil] repository's slug

# File lib/saddler/reporter/github/client.rb, line 110
def slug
  @slug ||= @repo.slug
end