Class: Saddler::Reporter::Github::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(repo) ⇒ Client

Returns a new instance of Client.



5
6
7
# File 'lib/saddler/reporter/github/client.rb', line 5

def initialize(repo)
  @repo = repo
end

Instance Method Details

#access_tokenObject



81
82
83
# File 'lib/saddler/reporter/github/client.rb', line 81

def access_token
  ENV['GITHUB_ACCESS_TOKEN']
end

#clientObject



77
78
79
# File 'lib/saddler/reporter/github/client.rb', line 77

def client
  @client ||= Octokit::Client.new(access_token: access_token)
end

#commit_comments(sha) ⇒ Object



9
10
11
12
13
# File 'lib/saddler/reporter/github/client.rb', line 9

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) ⇒ Object



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

def commit_patches(sha)
  patches = ::GitDiffParser::Patches[]
  client.commit(slug, sha).files.each do |file|
    patches << ::GitDiffParser::Patch.new(file.patch, file: file.filename, secure_hash: sha)
  end
  patches
end

#create_commit_comment(comment) ⇒ Object



15
16
17
18
# File 'lib/saddler/reporter/github/client.rb', line 15

def create_commit_comment(comment)
  client.create_commit_comment(slug, comment.sha, comment.body,
                               comment.path, nil, comment.position)
end

#create_issue_comment(comment) ⇒ Object



34
35
36
# File 'lib/saddler/reporter/github/client.rb', line 34

def create_issue_comment(comment)
  client.add_comment(slug, pull_id, comment.body)
end

#create_pull_request_review_comment(comment) ⇒ Object



52
53
54
55
# File 'lib/saddler/reporter/github/client.rb', line 52

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_idObject



85
86
87
88
89
90
91
92
93
# File 'lib/saddler/reporter/github/client.rb', line 85

def env_pull_id
  if ENV['PULL_REQUEST_ID']
    ENV['PULL_REQUEST_ID'].to_i
  elsif ENV['TRAVIS_PULL_REQUEST'] && ENV['TRAVIS_PULL_REQUEST'] != 'false'
    ENV['TRAVIS_PULL_REQUEST'].to_i
  elsif ENV['CIRCLE_PR_NUMBER']
    ENV['CIRCLE_PR_NUMBER'].to_i
  end
end

#issue_commentsObject



28
29
30
31
32
# File 'lib/saddler/reporter/github/client.rb', line 28

def issue_comments
  client.issue_comments(slug, pull_id).map do |comment|
    Comment.new(sha = nil, comment.body, comment.path, comment.position)
  end
end

#pull_idObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/saddler/reporter/github/client.rb', line 57

def pull_id
  @pull_id ||= begin
    pull_id = env_pull_id
    if pull_id
      pull_id.to_i
    elsif @repo.current_branch
      pull = pull_requests.find { |pr| pr[:head][:ref] == @repo.current_branch }
      pull[:number].to_i if pull
    end
  end
end

#pull_request_patchesObject



44
45
46
47
48
49
50
# File 'lib/saddler/reporter/github/client.rb', line 44

def pull_request_patches
  patches = ::GitDiffParser::Patches[]
  client.pull_request_files(slug, pull_id).each do |file|
    patches << ::GitDiffParser::Patch.new(file.patch, file: file.filename, secure_hash: @repo.merging_sha)
  end
  patches
end

#pull_request_review_commentsObject



38
39
40
41
42
# File 'lib/saddler/reporter/github/client.rb', line 38

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_requestsObject



69
70
71
# File 'lib/saddler/reporter/github/client.rb', line 69

def pull_requests
  @pull_requests ||= client.pull_requests(slug)
end

#slugObject



73
74
75
# File 'lib/saddler/reporter/github/client.rb', line 73

def slug
  @slug ||= @repo.slug
end