github_helpers.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """GitHub GraphQL helpers.
  2. https://developer.github.com/v4/explorer/ is very useful for building queries.
  3. """
  4. __copyright__ = """
  5. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  6. Exceptions. See /LICENSE for license information.
  7. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  8. """
  9. import os
  10. # https://pypi.org/project/gql/
  11. import gql
  12. import gql.transport.requests
  13. _ENV_TOKEN = "GITHUB_ACCESS_TOKEN"
  14. # Query elements for pagination.
  15. PAGINATION = """pageInfo {
  16. hasNextPage
  17. endCursor
  18. }
  19. totalCount"""
  20. def add_access_token_arg(parser, permissions):
  21. """Adds a flag to set the access token."""
  22. access_token = os.environ.get(_ENV_TOKEN, default=None)
  23. parser.add_argument(
  24. "--access-token",
  25. metavar="ACCESS_TOKEN",
  26. default=access_token,
  27. required=not access_token,
  28. help="The access token for use with GitHub. May also be specified in "
  29. "the environment as %s. The access token should have permissions: %s"
  30. % (_ENV_TOKEN, permissions),
  31. )
  32. class Client(object):
  33. """A GitHub GraphQL client."""
  34. def __init__(self, parsed_args):
  35. """Connects to GitHub."""
  36. transport = gql.transport.requests.RequestsHTTPTransport(
  37. url="https://api.github.com/graphql",
  38. headers={"Authorization": "bearer %s" % parsed_args.access_token},
  39. )
  40. self._client = gql.Client(
  41. transport=transport, fetch_schema_from_transport=True
  42. )
  43. def execute(self, query):
  44. """Runs a query."""
  45. return self._client.execute(gql.gql(query))
  46. def execute_and_paginate(self, query, path, first_page=None):
  47. """Runs a query with pagination.
  48. Arguments:
  49. query: The GraphQL query template, which must have both 'cursor' and
  50. 'pagination' fields to fill in. The cursor should be part of the
  51. location query (with 'first'), and the pagination should be at the
  52. same level as nodes.
  53. path: A list of strings indicating the path to the nodes in the
  54. result.
  55. first_page: An optional object for the first page of results, which
  56. will otherwise automatically be collected. This exists for callers
  57. to optimize by collecting other data with the first page.
  58. """
  59. format = {"cursor": "", "pagination": PAGINATION}
  60. count = 0
  61. exp_count = None
  62. while True:
  63. if first_page:
  64. result = first_page
  65. first_page = None
  66. else:
  67. result = self.execute(query % format)
  68. # Follow the path to the nodes being paginated.
  69. node_parent = result
  70. for entry in path:
  71. node_parent = node_parent[entry]
  72. # Store the total count of responses.
  73. if not exp_count:
  74. exp_count = node_parent["totalCount"]
  75. # Yield each node individually.
  76. for node in node_parent["nodes"]:
  77. yield node
  78. count += 1
  79. # Check for pagination, verifying the total count on exit.
  80. page_info = node_parent["pageInfo"]
  81. if not page_info["hasNextPage"]:
  82. assert exp_count == count, "exp %d != actual %d at path %s" % (
  83. exp_count,
  84. count,
  85. path,
  86. )
  87. return
  88. format["cursor"] = ' after: "%s"' % page_info["endCursor"]