Class: Pronto::Github

Inherits:
Client
  • Object
show all
Defined in:
lib/pronto/github_resolver/github_client_ext.rb

Overview

extend stock pronto github client wrapper

Constant Summary collapse

GET_REVIEW_THREADS_QUERY =
"query getReviewThreadIds($owner: String!, $name: String!, $pull_num: Int!) {\n  repository(owner: $owner, name: $name) {\n    pullRequest: issueOrPullRequest(number: $pull_num) {\n      ... on PullRequest {\n        reviewThreads(last:100) {\n            totalCount\n            nodes {\n                id\n                comments(last: 10) {\n                    nodes {\n                        viewerDidAuthor\n                        path position body\n                    }\n                }\n            }\n        }\n      }\n    }\n  }\n}\n"

Instance Method Summary collapse

Instance Method Details

#approve_pull_request(message = "") ⇒ Object



27
28
29
30
31
32
# File 'lib/pronto/github_resolver/github_client_ext.rb', line 27

def approve_pull_request(message = "")
  client.create_pull_request_review(
    slug, pull_id,
    { event: "APPROVE", body: message, accept: "application/vnd.github.v3.diff+json" }
  )
end

#create_pull_request_review(comments, event: nil) ⇒ Object

original, but with event param



16
17
18
19
20
21
22
23
24
25
# File 'lib/pronto/github_resolver/github_client_ext.rb', line 16

def create_pull_request_review(comments, event: nil)
  options = {
    event: event || @config.github_review_type,
    accept: "application/vnd.github.v3.diff+json", # https://developer.github.com/v3/pulls/reviews/#create-a-pull-request-review
    comments: comments.map do |comment|
      { path: comment.path, position: comment.position, body: comment.body }
    end
  }
  client.create_pull_request_review(slug, pull_id, options)
end

#existing_pull_request_reviewsObject



34
35
36
# File 'lib/pronto/github_resolver/github_client_ext.rb', line 34

def existing_pull_request_reviews
  client.pull_request_reviews(slug, pull_id)
end

#fetch_review_threadsObject

rubocop:disable Metrics/MethodLength



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pronto/github_resolver/github_client_ext.rb', line 61

def fetch_review_threads # rubocop:disable Metrics/MethodLength
  owner, repo_name = slug.split("/")
  res = client.post :graphql, {
    query: GET_REVIEW_THREADS_QUERY,
    variables: { owner: owner, name: repo_name, pull_num: pull_id }
  }.to_json

  return [] if res.errors || !res.data # TODO: handle errors

  res.data.repository.pullRequest.reviewThreads.nodes.to_h do |node|
    [
      node.id,
      node.comments.nodes.map do |comment|
        { authored: comment.viewerDidAuthor, path: comment.path, position: comment.position, body: comment.body }
      end
    ]
  end
end

#publish_pull_request_comments(comments, event: nil) ⇒ Object

original, but with event param



7
8
9
10
11
12
13
# File 'lib/pronto/github_resolver/github_client_ext.rb', line 7

def publish_pull_request_comments(comments, event: nil)
  comments_left = comments.clone
  while comments_left.any?
    comments_to_publish = comments_left.slice!(0, warnings_per_review)
    create_pull_request_review(comments_to_publish, event: event)
  end
end

#resolve_review_threads(node_ids) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pronto/github_resolver/github_client_ext.rb', line 80

def resolve_review_threads(node_ids)
  return unless node_ids.any?

  query = "    mutation {\n      \#{node_ids.each_with_index.map do |id, index|\n          \"q\#{index}: resolveReviewThread(input: { threadId: \\\"\#{id}\\\" }){ thread { id } } \"\n        end.join(\"\\n\")}\n    }\n  GQL\n  client.post :graphql, { query: query }.to_json\nend\n"