Class: Fbe::Graph
- Inherits:
-
Object
- Object
- Fbe::Graph
- Defined in:
- lib/fbe/github_graph.rb
Overview
A client to GitHub GraphQL.
- Author
Yegor Bugayenko (yegor256@gmail.com)
- Copyright
Copyright (c) 2024-2026 Zerocracy
- License
MIT
Defined Under Namespace
Instance Method Summary collapse
-
#initialize(token:, host: 'api.github.com') ⇒ Graph
constructor
rubocop:disable Metrics/ClassLength.
-
#issue_type_event(node_id) ⇒ Hash
Get info about issue type event.
-
#pull_request_reviews(owner, name, pulls: []) ⇒ Hash
Get reviews by pull numbers.
-
#pull_requests_with_reviews(owner, name, since, cursor: nil) ⇒ Hash
Get pulls id and number with review from since.
-
#query(qry) ⇒ GraphQL::Client::Response
Executes a GraphQL query against the GitHub API.
-
#resolved_conversations(owner, name, number) ⇒ Array<Hash>
Retrieves resolved conversation threads from a pull request.
-
#total_commits(owner = nil, name = nil, branch = nil, repos: nil) ⇒ Integer, Array<Hash>
Gets the total number of commits in a branch.
-
#total_commits_pushed(owner, name, since) ⇒ Hash
Get total commits pushed to default branch.
-
#total_issues_and_pulls(owner, name) ⇒ Hash
Gets the total number of issues and pull requests in a repository.
-
#total_issues_created(owner, name, since) ⇒ Hash
Get total count issues and pulls created from the specified date.
-
#total_releases_published(owner, name, since) ⇒ Hash
Get total count releases from the specified date.
Constructor Details
#initialize(token:, host: 'api.github.com') ⇒ Graph
rubocop:disable Metrics/ClassLength
36 37 38 39 |
# File 'lib/fbe/github_graph.rb', line 36 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
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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/fbe/github_graph.rb', line 197 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') previous = 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' => previous, '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 |
#pull_request_reviews(owner, name, pulls: []) ⇒ Hash
Get reviews by pull numbers
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/fbe/github_graph.rb', line 356 def pull_request_reviews(owner, name, pulls: []) requests = pulls.map do |number, cursor| after = "after: \"#{cursor}\", " unless cursor.nil? <<~GRAPHQL pr_#{number}: pullRequest(number: #{number}) { id number reviews(#{after}first: 100) { nodes { id submittedAt } pageInfo { hasNextPage endCursor } } } GRAPHQL end result = query( <<~GRAPHQL { repository(owner: "#{owner}", name: "#{name}") { #{requests.join("\n")} } } GRAPHQL ).to_h result['repository'].map do |_k, v| { 'id' => v['id'], 'number' => v['number'], 'reviews' => v.dig('reviews', 'nodes').map do |r| { 'id' => r['id'], 'submitted_at' => Time.parse(r['submittedAt']) } end, 'reviews_has_next_page' => v.dig('reviews', 'pageInfo', 'hasNextPage'), 'reviews_next_cursor' => v.dig('reviews', 'pageInfo', 'endCursor') } end end |
#pull_requests_with_reviews(owner, name, since, cursor: nil) ⇒ Hash
Get pulls id and number with review from since
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/fbe/github_graph.rb', line 295 def pull_requests_with_reviews(owner, name, since, cursor: nil) after = "after: \"#{cursor}\", " unless cursor.nil? result = query( <<~GRAPHQL { repository(owner: "#{owner}", name: "#{name}") { pullRequests(#{after}first: 100) { nodes { id number timelineItems(first: 1, itemTypes: [PULL_REQUEST_REVIEW], since: "#{since.utc.iso8601}") { nodes { ... on PullRequestReview { id } } } } pageInfo { hasNextPage endCursor } } } } GRAPHQL ).to_h nodes = result.dig('repository', 'pullRequests', 'nodes') raise(Fbe::Error, "Repository '#{owner}/#{name}' not found") if nodes.nil? { 'pulls_with_reviews' => nodes.filter_map do |pull| next if pull.dig('timelineItems', 'nodes').empty? { 'id' => pull['id'], 'number' => pull['number'] } end, 'has_next_page' => result.dig('repository', 'pullRequests', 'pageInfo', 'hasNextPage'), 'next_cursor' => result.dig('repository', 'pullRequests', 'pageInfo', 'endCursor') } end |
#query(qry) ⇒ GraphQL::Client::Response
Executes a GraphQL query against the GitHub API.
49 50 51 52 53 54 55 |
# File 'lib/fbe/github_graph.rb', line 49 def query(qry) result = client.query(client.parse(qry)) unless result.errors.empty? raise(Fbe::Error, "GitHub GraphQL query failed: #{result.errors..values.flatten.join('; ')}") end result.data end |
#resolved_conversations(owner, name, number) ⇒ Array<Hash>
Retrieves resolved conversation threads from a pull request.
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 92 93 94 95 96 97 |
# File 'lib/fbe/github_graph.rb', line 67 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 ) nodes = result&.to_h&.dig('repository', 'pullRequest', 'reviewThreads', 'nodes') return [] if nodes.nil? nodes.select { |thread| thread['isResolved'] } end |
#total_commits(owner = nil, name = nil, branch = nil, repos: nil) ⇒ Integer, Array<Hash>
Gets the total number of commits in a branch.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/fbe/github_graph.rb', line 120 def total_commits(owner = nil, name = nil, branch = nil, repos: nil) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity raise(Fbe::Error, 'Need owner, name and branch or repos') if owner.nil? && name.nil? && branch.nil? && repos.nil? raise(Fbe::Error, 'Owner, name and branch is required') if (owner.nil? || name.nil? || branch.nil?) && repos.nil? raise(Fbe::Error, 'Repos list cannot be empty') if owner.nil? && name.nil? && branch.nil? && repos&.empty? if (!owner.nil? || !name.nil? || !branch.nil?) && !repos.nil? raise(Fbe::Error, 'Need only owner, name and branch or repos') end repos ||= [[owner, name, branch]] requests = repos.each_with_index.map do |(owner, name, branch), i| <<~GRAPHQL repo_#{i}: repository(owner: "#{owner}", name: "#{name}") { ref(qualifiedName: "#{branch}") { target { ... on Commit { history { totalCount } } } } } GRAPHQL end result = query("{\n#{requests.join("\n")}\n}") if owner && name && branch ref = result.repo_0&.ref raise(Fbe::Error, "Repository '#{owner}/#{name}' or branch '#{branch}' not found") unless ref&.target&.history ref.target.history.total_count else repos.each_with_index.map do |(owner, name, branch), i| ref = result.public_send(:"repo_#{i}")&.ref raise(Fbe::Error, "Repository '#{owner}/#{name}' or branch '#{branch}' not found") unless ref&.target&.history { 'owner' => owner, 'name' => name, 'branch' => branch, 'total_commits' => ref.target.history.total_count } end end end |
#total_commits_pushed(owner, name, since) ⇒ Hash
Get total commits pushed to default branch
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/fbe/github_graph.rb', line 408 def total_commits_pushed(owner, name, since) cursor = nil total = 0 hoc = 0 loop do after = "after: \"#{cursor}\", " unless cursor.nil? result = query( <<~GRAPHQL { repository(owner: "#{owner}", name: "#{name}") { defaultBranchRef { target { ... on Commit { history(#{after}first: 100, since: "#{since.utc.iso8601}") { totalCount nodes { oid parents { totalCount } additions deletions } pageInfo { endCursor hasNextPage } } } } } } } GRAPHQL ).to_h commits = result.dig('repository', 'defaultBranchRef', 'target', 'history', 'nodes') hoc += commits.nil? ? 0 : commits.sum { (_1['additions'] || 0) + (_1['deletions'] || 0) } total = result.dig('repository', 'defaultBranchRef', 'target', 'history', 'totalCount') || 0 break unless result.dig('repository', 'defaultBranchRef', 'target', 'history', 'pageInfo', 'hasNextPage') cursor = result.dig('repository', 'defaultBranchRef', 'target', 'history', 'pageInfo', 'endCursor') end { 'commits' => total, 'hoc' => hoc } end |
#total_issues_and_pulls(owner, name) ⇒ Hash
Gets the total number of issues and pull requests in a repository.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/fbe/github_graph.rb', line 172 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 |
#total_issues_created(owner, name, since) ⇒ Hash
Get total count issues and pulls created from the specified date
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
# File 'lib/fbe/github_graph.rb', line 461 def total_issues_created(owner, name, since) result = query( <<~GRAPHQL { issues: search( query: "repo:#{owner}/#{name} type:issue created:>#{since.utc.iso8601}", type: ISSUE ) { issueCount }, pulls: search( query: "repo:#{owner}/#{name} type:pr created:>#{since.utc.iso8601}", type: ISSUE ) { issueCount } } GRAPHQL ).to_h { 'issues' => result.dig('issues', 'issueCount') || 0, 'pulls' => result.dig('pulls', 'issueCount') || 0 } end |
#total_releases_published(owner, name, since) ⇒ Hash
Get total count releases from the specified date
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 |
# File 'lib/fbe/github_graph.rb', line 492 def total_releases_published(owner, name, since) total = 0 cursor = nil loop do after = "after: \"#{cursor}\", " unless cursor.nil? result = query( <<~GRAPHQL { repository(owner: "#{owner}", name: "#{name}") { releases(#{after}first: 25, orderBy: { field: CREATED_AT, direction: DESC }) { nodes { isDraft publishedAt } pageInfo { endCursor hasNextPage } } } } GRAPHQL ).to_h releases = result.dig('repository', 'releases', 'nodes') break if releases.nil? || releases.empty? total += releases.count { !_1['isDraft'] && _1['publishedAt'] && Time.parse(_1['publishedAt']) > since } break if releases.all? { _1['publishedAt'] && Time.parse(_1['publishedAt']) < since } break unless result.dig('repository', 'releases', 'pageInfo', 'hasNextPage') cursor = result.dig('repository', 'releases', 'pageInfo', 'endCursor') end { 'releases' => total } end |