• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

fedora-llvm-team / llvm-snapshots / 13137779807

04 Feb 2025 02:32PM UTC coverage: 38.173% (-0.1%) from 38.322%
13137779807

Pull #1063

github

web-flow
Merge 0af3ff7dd into f5421dcb3
Pull Request #1063: Skip tests that require access to the token

15 of 17 new or added lines in 3 files covered. (88.24%)

46 existing lines in 5 files now uncovered.

9726 of 25479 relevant lines covered (38.17%)

0.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

48.78
/snapshot_manager/snapshot_manager/github_graphql.py
1
"""
2
GithubGraphQL
3
"""
4

5
import logging
1✔
6
import os
1✔
7
import pathlib
1✔
8
from typing import Any, Union
1✔
9

10
import fnc
1✔
11
from requests import Session
1✔
12
from requests.structures import CaseInsensitiveDict
1✔
13

14

15
class GithubGraphQL:
1✔
16
    """A lightweight Github GraphQL API client.
17

18
    In order to properly close the session, use this class as a context manager:
19

20
        with GithubGraphQL(token="<GITHUB_API_TOKEN>") as g:
21
            g.query_from_file(filename="query.graphql", variables=None)
22

23
    or call the close() method manually
24

25
        g = GithubGraphQL(token="<GITHUB_API_TOKEN>")
26
        g.close()
27
    """
28

29
    def __init__(
1✔
30
        self,
31
        token: str = "",
32
        endpoint: str = "https://api.github.com/graphql",
33
        **kwargs,
34
    ):
35
        """
36
        Creates a session with the given bearer `token` and `endpoint`.
37

38
        Args:
39
            token (str): Your personal access token in Github (see https://github.com/settings/tokens)
40
            endpoint (str): The endpoint to query GraphQL from
41
            **kwargs: key-value pairs (e.g. {raise_on_error=True})
42
        """
UNCOV
43
        self.__endpoint = endpoint
×
UNCOV
44
        self.__token = token
×
UNCOV
45
        self.__encoding = "utf-8"
×
UNCOV
46
        self.__raise_on_error = kwargs.get("raise_on_error", None)
×
UNCOV
47
        self.__session = Session()
×
UNCOV
48
        self.__session.headers.update(
×
49
            {
50
                "Authorization": f"Bearer {self.__token}",
51
                # See https://graphql.org/learn/best-practices/#json-with-gzip
52
                "Accept-Encoding": "gzip",
53
                # See #
54
                # https://github.blog/2021-11-16-graphql-global-id-migration-update/
55
                "X-Github-Next-Global-ID": "1",
56
            }
57
        )
58

59
    @property
1✔
60
    def token(self) -> str:
1✔
61
        """Returns the bearer token."""
62
        return self.__token
×
63

64
    @property
1✔
65
    def encoding(self) -> str:
1✔
66
        """Returns the default encoding to be expected from query files."""
67
        return self.__encoding
×
68

69
    def run_from_file(
1✔
70
        self, filename: str, variables: dict[str, str | int] = None, **kwargs
71
    ) -> Any:
72
        """
73
        Read the query/mutation from the given file and execute it with the variables
74
        applied. If not requested otherwise the plain result is returned. If you
75
        want to raise an exception in case of an error you can set
76
        `raise_on_error` to `True`.
77

78
        See also:
79
        https://docs.github.com/en/graphql/guides/forming-calls-with-graphql
80
        https://docs.github.com/en/graphql/overview/explorer
81

82
        Args:
83
            filename (str): The filename of the query/mutation file.
84
            variables (dict): The variables to be applied to the query/mutation.
85
            **kwargs: key-value pairs (e.g. {raise_on_error=True})
86
        """
87
        with open(file=filename, encoding=self.encoding) as file_handle:
×
88
            query = file_handle.read()
×
89
        return self.run(query, variables, **kwargs)
×
90

91
    def __enter__(self):
1✔
92
        return self
×
93

94
    @property
1✔
95
    def session_headers(self) -> CaseInsensitiveDict:
1✔
96
        """Returns the HTTP headers used for the session."""
97
        return self.__session.headers
×
98

99
    def __exit__(self, exc_type, exc_value, traceback):
1✔
100
        self.close()
×
101

102
    def close(self):
1✔
103
        """Closes the session."""
104
        self.__session.close()
×
105

106
    def run(self, query: str, variables: dict[str, str | int] = None, **kwargs) -> dict:
1✔
107
        """
108
        Execute the query with the variables applied. If not requested otherwise
109
        the plain result is returned. If you want to raise an exception in case
110
        of an error you can set `raise_on_error` to `True`.
111

112
        Args:
113
            query (str): The GraphQL query.
114
            variables (dict): The variables to be applied to the query.
115
            **kwargs: key-value pairs (e.g. {raise_on_error=True})
116

117
        Raises:
118
            RuntimeError: In case of an error when `raise` is `True`.
119

120
        Returns:
121
            Result: The result of the query. Inspect the result for errors!
122
        """
123
        req = self.__session.post(
×
124
            url=self.__endpoint, json={"query": query, "variables": variables}
125
        )
126
        req.raise_for_status()
×
127
        res = dict(req.json())
×
128
        if "errors" in res and kwargs.get("raise_on_error", self.__raise_on_error):
×
129
            raise RuntimeError(
×
130
                str(fnc.get("errors[0].message", res, default="GraphQL Error"))
131
            )
132
        return res
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc