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

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

Overview

GitHub client wrapper

Instance Method Summary collapse

Constructor Details

#initialize(repo) ⇒ Client

Returns a new instance of Client.

Parameters:



7
8
9
# File 'lib/saddler/reporter/github/client.rb', line 7

def initialize(repo)
  @repo = repo
end

Instance Method Details

#access_tokenString?

Returns github access token.

Returns:

  • (String, nil)

    github access token



120
121
122
# File 'lib/saddler/reporter/github/client.rb', line 120

def access_token
  ENV['GITHUB_ACCESS_TOKEN']
end

#clientOctokit::Client

Returns:

  • (Octokit::Client)


115
116
117
# File 'lib/saddler/reporter/github/client.rb', line 115

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

#commit_comments(sha) ⇒ Array<Comment>

Returns parse commit comments into Comment.

Parameters:

  • sha (String)

    target commit sha

Returns:

  • (Array<Comment>)

    parse commit comments into Comment



14
15
16
17
18
# 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) ⇒ Patches<Patch>

Returns patches.

Parameters:

  • sha (String)

    target commit sha

Returns:



33
34
35
36
37
38
39
# 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) ⇒ Sawyer::Resource

Returns Commit comment.

Parameters:

  • comment (Comment)

    posting commit comment

Returns:

  • (Sawyer::Resource)

    Commit comment

See Also:

  • Octokit::Client::CommitComments.create_commit_comment


25
26
27
28
# 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) ⇒ Sawyer::Resource

Returns Comment.

Parameters:

  • comment (Comment)

    posting issue comment

Returns:

  • (Sawyer::Resource)

    Comment

See Also:

  • Octokit::Client::Issues.add_comment


54
55
56
# 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) ⇒ Sawyer::Resource

Returns Hash representing the new comment.

Parameters:

  • comment (Comment)

    posting pull request comment

Returns:

  • (Sawyer::Resource)

    Hash representing the new comment

See Also:

  • Octokit::Client::PullRequests.create_pull_request_comment


79
80
81
82
# 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_idInteger?

Returns pull request id from environment variables.

Returns:

  • (Integer, nil)

    pull request id from environment variables



125
126
127
# File 'lib/saddler/reporter/github/client.rb', line 125

def env_pull_id
  @env_pull_id ||= EnvPullRequest.new.pull_request_id
end

#issue_commentsArray<Comment>

Returns parse issue comments into Comment.

Returns:

  • (Array<Comment>)

    parse issue comments into Comment



42
43
44
45
46
47
# 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_idInteger?

Returns pull request id.

Returns:

  • (Integer, nil)

    pull request id



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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_patchesPatches<Patch>

Returns patches.

Returns:



66
67
68
69
70
71
72
# 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_commentsArray<Comment>

Returns parse pull request comments into Comment.

Returns:

  • (Array<Comment>)

    parse pull request comments into Comment



59
60
61
62
63
# 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_requestsArray<Sawyer::Resource>

Returns Array of pulls.

Returns:

  • (Array<Sawyer::Resource>)

    Array of pulls

See Also:

  • Octokit::Client::PullRequests.pull_requests


105
106
107
# File 'lib/saddler/reporter/github/client.rb', line 105

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

#slugString?

Returns repository’s slug.

Returns:

  • (String, nil)

    repository’s slug



110
111
112
# File 'lib/saddler/reporter/github/client.rb', line 110

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