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

fedora-llvm-team / llvm-snapshots / 11269302141

10 Oct 2024 07:19AM UTC coverage: 61.451% (-8.7%) from 70.198%
11269302141

push

github

web-flow
Get CI green again (#774)

Changes in here check for chroots in a better way and don't allow unexpected names, versions or architectures.

Two tests (`test_create_or_get_todays_github_issue`, `test_flip_label`) that always fail because of missing token are now skipped until I've found a way to get them to work again.

* Add coverage python module
Needed for running `make test-snapshot-manager`.
* Properly handle chroots with regexes
* Fix tests
* Skip two github tests for now because of missing token

25 of 25 new or added lines in 3 files covered. (100.0%)

188 existing lines in 6 files now uncovered.

974 of 1585 relevant lines covered (61.45%)

0.61 hits per line

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

63.41
/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
        """
43
        self.__endpoint = endpoint
1✔
44
        self.__token = token
1✔
45
        self.__encoding = "utf-8"
1✔
46
        self.__raise_on_error = kwargs.get("raise_on_error", None)
1✔
47
        self.__session = Session()
1✔
48
        self.__session.headers.update(
1✔
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."""
UNCOV
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
        """
UNCOV
87
        with open(file=filename, encoding=self.encoding) as file_handle:
×
UNCOV
88
            query = file_handle.read()
×
UNCOV
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
        """
UNCOV
123
        req = self.__session.post(
×
124
            url=self.__endpoint, json={"query": query, "variables": variables}
125
        )
UNCOV
126
        req.raise_for_status()
×
UNCOV
127
        res = dict(req.json())
×
UNCOV
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
            )
UNCOV
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