Class: Fbe::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/fbe/github_graph.rb

Overview

The GitHub GraphQL client

Defined Under Namespace

Classes: Fake, HTTP

Instance Method Summary collapse

Constructor Details

#initialize(token:, host: 'api.github.com') ⇒ Graph

Returns a new instance of Graph.



46
47
48
49
50
51
# File 'lib/fbe/github_graph.rb', line 46

def initialize(token:, host: 'api.github.com')
  @token = token
  http = HTTP.new(token, host)
  @client = GraphQL::Client.new(schema: GraphQL::Client.load_schema(http), execute: http)
  @client.allow_dynamic_queries = true
end

Instance Method Details

#query(query_string) ⇒ Object



53
54
55
56
# File 'lib/fbe/github_graph.rb', line 53

def query(query_string)
  result = @client.query(@client.parse(query_string))
  result.data
end

#resolved_conversations(owner, name, number) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/fbe/github_graph.rb', line 58

def resolved_conversations(owner, name, number)
  result = query(
    <<~GRAPHQL
      {
        repository(owner: "#{owner}", name: "#{name}") {
          pullRequest(number: #{number}) {
            reviewThreads(first: 100) {
              nodes {
                id
                isResolved
                comments(first: 100) {
                  nodes {
                    id
                    body
                    author {
                      login
                    }
                    createdAt
                  }
                }
              }
            }
          }
        }
      }
    GRAPHQL
  )
  result&.to_h&.dig('repository', 'pullRequest', 'reviewThreads', 'nodes')&.select do |thread|
    thread['isResolved']
  end || []
end

#total_commits(owner, name, branch) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/fbe/github_graph.rb', line 90

def total_commits(owner, name, branch)
  result = query(
    <<~GRAPHQL
      {
        repository(owner: "#{owner}", name: "#{name}") {
          ref(qualifiedName: "#{branch}") {
            target {
              ... on Commit {
                history {
                  totalCount
                }
              }
            }
          }
        }
      }
    GRAPHQL
  )
  result.repository.ref.target.history.total_count
end

#total_issues_and_pulls(owner, name) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/fbe/github_graph.rb', line 111

def total_issues_and_pulls(owner, name)
  result = query(
    <<~GRAPHQL
      {
        repository(owner: "#{owner}", name: "#{name}") {
          issues {
            totalCount
          }
          pullRequests {
            totalCount
          }
        }
      }
    GRAPHQL
  ).to_h
  {
    'issues' => result.dig('repository', 'issues', 'totalCount') || 0,
    'pulls' => result.dig('repository', 'pullRequests', 'totalCount') || 0
  }
end