Class: Fbe::Graph

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

Overview

A client to GitHub GraphQL.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Zerocracy

License

MIT

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.



33
34
35
36
# File 'lib/fbe/github_graph.rb', line 33

def initialize(token:, host: 'api.github.com')
  @token = token
  @host = host
end

Instance Method Details

#issue_type_event(node_id) ⇒ Hash

Get info about issue type event

Parameters:

  • node_id (String)

    ID of the event object

Returns:

  • (Hash)

    A hash with issue type event



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/fbe/github_graph.rb', line 158

def issue_type_event(node_id)
  result = query(
    <<~GRAPHQL
      {
        node(id: "#{node_id}") {
          __typename
          ... on IssueTypeAddedEvent {
            id
            createdAt
            issueType { ...IssueTypeFragment }
            actor { ...ActorFragment }
          }
          ... on IssueTypeChangedEvent {
            id
            createdAt
            issueType { ...IssueTypeFragment }
            prevIssueType { ...IssueTypeFragment }
            actor { ...ActorFragment }
          }
          ... on IssueTypeRemovedEvent {
            id
            createdAt
            issueType { ...IssueTypeFragment }
            actor { ...ActorFragment }
          }
        }
      }
      fragment ActorFragment on Actor {
        __typename
        login
        ... on User { databaseId name email }
        ... on Bot { databaseId }
        ... on EnterpriseUserAccount { user { databaseId name email } }
        ... on Mannequin { claimant { databaseId name email } }
      }
      fragment IssueTypeFragment on IssueType {
        id
        name
        description
      }
    GRAPHQL
  ).to_h
  return unless result['node']
  type = result.dig('node', '__typename')
  prev_issue_type =
    if type == 'IssueTypeChangedEvent'
      {
        'id' => result.dig('node', 'prevIssueType', 'id'),
        'name' => result.dig('node', 'prevIssueType', 'name'),
        'description' => result.dig('node', 'prevIssueType', 'description')
      }
    end
  {
    'type' => type,
    'created_at' => Time.parse(result.dig('node', 'createdAt')),
    'issue_type' => {
      'id' => result.dig('node', 'issueType', 'id'),
      'name' => result.dig('node', 'issueType', 'name'),
      'description' => result.dig('node', 'issueType', 'description')
    },
    'prev_issue_type' => prev_issue_type,
    'actor' => {
      'login' => result.dig('node', 'actor', 'login'),
      'type' => result.dig('node', 'actor', '__typename'),
      'id' => result.dig('node', 'actor', 'databaseId') ||
        result.dig('node', 'actor', 'user', 'databaseId') ||
        result.dig('node', 'actor', 'claimant', 'databaseId'),
      'name' => result.dig('node', 'actor', 'name') ||
        result.dig('node', 'actor', 'user', 'name') ||
        result.dig('node', 'actor', 'claimant', 'name'),
      'email' => result.dig('node', 'actor', 'email') ||
        result.dig('node', 'actor', 'user', 'email') ||
        result.dig('node', 'actor', 'claimant', 'email')
    }
  }
end

#query(qry) ⇒ GraphQL::Client::Response

Executes a GraphQL query against the GitHub API.

Examples:

graph = Fbe::Graph.new(token: 'github_token')
result = graph.query('{viewer {login}}')
puts result.viewer. #=> "octocat"

Parameters:

  • qry (String)

    The GraphQL query to execute

Returns:

  • (GraphQL::Client::Response)

    The query result data



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

def query(qry)
  result = client.query(client.parse(qry))
  result.data
end

#resolved_conversations(owner, name, number) ⇒ Array<Hash>

Retrieves resolved conversation threads from a pull request.

Examples:

graph = Fbe::Graph.new(token: 'github_token')
threads = graph.resolved_conversations('octocat', 'Hello-World', 42)
threads.first['comments']['nodes'].first['body'] #=> "Great work!"

Parameters:

  • owner (String)

    The repository owner (username or organization)

  • name (String)

    The repository name

  • number (Integer)

    The pull request number

Returns:

  • (Array<Hash>)

    An array of resolved conversation threads with their comments



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
89
90
91
# File 'lib/fbe/github_graph.rb', line 61

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

Gets the total number of commits in a branch.

Examples:

graph = Fbe::Graph.new(token: 'github_token')
count = graph.total_commits('octocat', 'Hello-World', 'main')
puts count #=> 42

Parameters:

  • owner (String)

    The repository owner (username or organization)

  • name (String)

    The repository name

  • branch (String)

    The branch name (e.g., “master” or “main”)

Returns:

  • (Integer)

    The total number of commits in the branch



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/fbe/github_graph.rb', line 103

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

Gets the total number of issues and pull requests in a repository.

Examples:

graph = Fbe::Graph.new(token: 'github_token')
counts = graph.total_issues_and_pulls('octocat', 'Hello-World')
puts counts #=> {"issues"=>42, "pulls"=>17}

Parameters:

  • owner (String)

    The repository owner (username or organization)

  • name (String)

    The repository name

Returns:

  • (Hash)

    A hash with ‘issues’ and ‘pulls’ counts



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/fbe/github_graph.rb', line 133

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