File: GraphQl.py

package info (click to toggle)
pygithub 2.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 33,476 kB
  • sloc: python: 38,409; sh: 7; makefile: 6
file content (358 lines) | stat: -rw-r--r-- 14,536 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
############################ Copyrights and license ############################
#                                                                              #
# Copyright 2024 Enrico Minack <github@enrico.minack.dev>                      #
# Copyright 2025 Enrico Minack <github@enrico.minack.dev>                      #
#                                                                              #
# This file is part of PyGithub.                                               #
# http://pygithub.readthedocs.io/                                              #
#                                                                              #
# PyGithub is free software: you can redistribute it and/or modify it under    #
# the terms of the GNU Lesser General Public License as published by the Free  #
# Software Foundation, either version 3 of the License, or (at your option)    #
# any later version.                                                           #
#                                                                              #
# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY  #
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    #
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more #
# details.                                                                     #
#                                                                              #
# You should have received a copy of the GNU Lesser General Public License     #
# along with PyGithub. If not, see <http://www.gnu.org/licenses/>.             #
#                                                                              #
################################################################################

from typing import Any, Dict

import github
import github.GithubException
import github.Organization
import github.Repository
import github.RepositoryDiscussion
import github.RepositoryDiscussionComment
import github.Requester
from github import Github

from . import Framework


class GraphQl(Framework.TestCase):
    def setUp(self):
        super().setUp()

    def expected(self, base_url: str = "https://github.com") -> Dict[Any, Any]:
        return {
            "actor": {
                "avatarUrl": "https://avatars.githubusercontent.com/u/14806300?u=786f9f8ef8782d45381b01580f7f7783cf9c7e37&v=4",
                "login": "heitorpolidoro",
                "resourcePath": "/heitorpolidoro",
                "url": f"{base_url}/heitorpolidoro",
            },
            "clientMutationId": None,
        }

    def testRequesterGraphQlPrefix(self):
        get_graphql_prefix = github.Requester.Requester.get_graphql_prefix
        assert "/graphql" == get_graphql_prefix(None)
        assert "/graphql" == get_graphql_prefix("")
        assert "/graphql" == get_graphql_prefix("/")
        assert "/api/graphql" == get_graphql_prefix("/api/v3")
        assert "/path/to/github/api/graphql" == get_graphql_prefix("/path/to/github/api/v3")
        assert "/path/to/github/graphql" == get_graphql_prefix("/path/to/github")

    def testDefaultUrl(self):
        pull = self.g.get_repo("PyGithub/PyGithub").get_pull(31)
        response = pull.disable_automerge()
        assert response == self.expected()

    def testOtherUrl(self):
        base_url = "https://my.enterprise.com/api/v3"
        gh = Github(base_url=base_url)
        pull = gh.get_repo("PyGithub/PyGithub").get_pull(31)
        response = pull.disable_automerge()
        assert response == self.expected(base_url)

    def testOtherPort(self):
        base_url = "https://my.enterprise.com:8080/api/v3"
        gh = Github(base_url=base_url)
        pull = gh.get_repo("PyGithub/PyGithub").get_pull(31)
        response = pull.disable_automerge()
        assert response == self.expected(base_url)

    def testNode(self):
        requester = self.g._Github__requester
        headers, data = requester.graphql_node("D_kwDOADYVqs4ATJZD", "title", "Discussion")
        self.assertTrue(headers)
        self.assertEqual(
            data.get("data", {}).get("node", {}).get("title"),
            "Is there a way to search if a string present in default branch?",
        )

        # non-existing node should throw a NOT FOUND exception
        with self.assertRaises(github.UnknownObjectException) as e:
            requester.graphql_node("D_abcdefgh", "title", "Discussion")
        self.assertEqual(e.exception.status, 404)
        self.assertEqual(
            e.exception.data,
            {
                "data": {"node": None},
                "errors": [
                    {
                        "type": "NOT_FOUND",
                        "path": ["node"],
                        "locations": [{"line": 3, "column": 15}],
                        "message": "Could not resolve to a node with the global id of 'D_abcdefgh'",
                    }
                ],
            },
        )
        self.assertEqual(e.exception.message, "Could not resolve to a node with the global id of 'D_abcdefgh'")

        # wrong type should throw an exception
        with self.assertRaises(github.GithubException) as e:
            requester.graphql_node("D_kwDOADYVqs4ATJZD", "login", "User")
        self.assertEqual(e.exception.message, "Retrieved User object is of different type: Discussion")
        self.assertEqual(e.exception.status, 400)
        self.assertEqual(e.exception.data, {"data": {"node": {"__typename": "Discussion"}}})

    def testNodeClass(self):
        requester = self.g._Github__requester
        discussion = requester.graphql_node_class(
            "D_kwDOADYVqs4ATJZD", "title", github.RepositoryDiscussion.RepositoryDiscussion, "Discussion"
        )
        self.assertEqual(discussion.title, "Is there a way to search if a string present in default branch?")

        # non-existing node should throw a NOT FOUND exception
        with self.assertRaises(github.UnknownObjectException) as e:
            requester.graphql_node_class(
                "D_abcdefgh", "title", github.RepositoryDiscussion.RepositoryDiscussion, "Discussion"
            )
        self.assertEqual(e.exception.status, 404)
        self.assertEqual(
            e.exception.data,
            {
                "data": {"node": None},
                "errors": [
                    {
                        "type": "NOT_FOUND",
                        "path": ["node"],
                        "locations": [{"line": 3, "column": 15}],
                        "message": "Could not resolve to a node with the global id of 'D_abcdefgh'",
                    }
                ],
            },
        )
        self.assertEqual(e.exception.message, "Could not resolve to a node with the global id of 'D_abcdefgh'")

        # wrong type should throw an exception
        with self.assertRaises(github.GithubException) as e:
            requester.graphql_node_class(
                "D_kwDOADYVqs4ATJZD", "login", github.RepositoryDiscussion.RepositoryDiscussion, "User"
            )
        self.assertEqual(e.exception.message, "Retrieved User object is of different type: Discussion")
        self.assertEqual(e.exception.status, 400)
        self.assertEqual(e.exception.data, {"data": {"node": {"__typename": "Discussion"}}})

    def testQuery(self):
        requester = self.g._Github__requester
        query = """
            query Q($owner: String!, $name: String!) {
              repository(owner: $owner, name: $name) { url }
            }"""
        variables = {"owner": "PyGithub", "name": "PyGithub"}
        header, data = requester.graphql_query(query, variables)
        self.assertTrue(header)
        self.assertEqual(data, {"data": {"repository": {"url": "https://github.com/PyGithub/PyGithub"}}})

    def testQueryRestClass(self):
        requester = self.g._Github__requester
        query = """
            query Q($owner: String!, $name: String!) {
              repository(owner: $owner, name: $name) { url }
            }"""
        variables = {"owner": "PyGithub", "name": "PyGithub"}
        repo = requester.graphql_query_class(query, variables, ["repository"], github.Repository.Repository)
        self.assertIsInstance(repo, github.Repository.Repository)
        self.assertEqual(repo.html_url, "https://github.com/PyGithub/PyGithub")

    def testQueryGraphQlClass(self):
        requester = self.g._Github__requester
        query = """
            query Q($id: ID!) {
              node(id: $id) { ... on DiscussionComment { url } }
            }"""
        variables = {"id": "DC_kwDOADYVqs4AU3Mg"}
        comment = requester.graphql_query_class(
            query, variables, ["node"], github.RepositoryDiscussionComment.RepositoryDiscussionComment
        )
        self.assertIsInstance(comment, github.RepositoryDiscussionComment.RepositoryDiscussionComment)
        self.assertEqual(
            comment.html_url, "https://github.com/PyGithub/PyGithub/discussions/2480#discussioncomment-5468960"
        )

    def testMutation(self):
        requester = self.g._Github__requester
        header, data = requester.graphql_named_mutation(
            "followOrganization", {"organizationId": "O_kgDOAKxBpA"}, "organization { name }"
        )
        self.assertTrue(header)
        self.assertEqual(data, {"organization": {"name": "PyGithub"}})

    def testMutationClass(self):
        requester = self.g._Github__requester
        org = requester.graphql_named_mutation_class(
            "followOrganization",
            {"organizationId": "O_kgDOAKxBpA"},
            "organization { name }",
            "organization",
            github.Organization.Organization,
        )
        self.assertEqual(org.name, "PyGithub")

    def testPaginationAndRestIntegration(self):
        repo = self.g.get_repo("PyGithub/PyGithub")
        discussion_schema = """
            id
            url
            number
            author {
              login
              avatarUrl
              url
            }
            repository {
              owner { login }
              name
              issues(first: 10) {
                totalCount
                pageInfo {
                  startCursor
                  endCursor
                  hasNextPage
                  hasPreviousPage
                }
                nodes {
                  databaseId
                  id
                  number
                  title
                }
              }
            }
            title
            createdAt
            comments(first: 10) {
              totalCount
              pageInfo {
                startCursor
                endCursor
                hasNextPage
                hasPreviousPage
              }
              nodes {
                id
                url
                createdAt
                author {
                  login
                  avatarUrl
                  url
                }
                isAnswer
                replies(first: 10) {
                  totalCount
                  pageInfo {
                    startCursor
                    endCursor
                    hasNextPage
                    hasPreviousPage
                  }
                  nodes {
                    id
                    url
                    createdAt
                    author {
                      login
                      avatarUrl
                      url
                    }
                  }
                }
              }
            }
            labels(first: 10) {
              totalCount
              pageInfo {
                startCursor
                endCursor
                hasNextPage
                hasPreviousPage
              }
              nodes {
                id
                name
                issues(first: 10) {
                  totalCount
                  pageInfo {
                    startCursor
                    endCursor
                    hasNextPage
                    hasPreviousPage
                  }
                  nodes {
                    databaseId
                    id
                    number
                    title
                  }
                }
              }
            }
          """
        discussions_pages = repo.get_discussions(discussion_schema)
        discussions = list(discussions_pages)
        # would perform an extra request if called before iterating discussions_pages
        self.assertEqual(discussions_pages.totalCount, 65)
        self.assertEqual(len(discussions), 65)
        self.assertEqual(discussions[0].number, 3044)
        self.assertEqual(discussions[-1].number, 1780)

        discussion = discussions[28]
        self.assertEqual(discussion.author.login, "arunanandhan")
        self.assertEqual(discussion.node_id, "D_kwDOADYVqs4ATJZD")
        self.assertEqual(
            discussion.author.avatar_url,
            "https://avatars.githubusercontent.com/u/48812131?u=571c345a5994a55100a16b45a9688f5d6d340730&v=4",
        )
        self.assertEqual(discussion.author.html_url, "https://github.com/arunanandhan")

        # inner page of GraphQL comments
        comments = discussion.get_comments("id")
        self.assertEqual(comments.totalCount, 1)  # does not perform an extra request
        comments = list(comments)
        self.assertEqual(len(comments), 1)
        comment = comments[0]
        self.assertEqual(comment.node_id, "DC_kwDOADYVqs4AU3Mg")
        self.assertEqual(
            comment.html_url, "https://github.com/PyGithub/PyGithub/discussions/2480#discussioncomment-5468960"
        )
        self.assertEqual(comment.author.login, "EnricoMi")

        # inner inner page of GraphQL replies
        replies = comment.get_replies()
        self.assertEqual(replies.totalCount, 5)  # does not perform an extra request
        self.assertEqual(replies[0].node_id, "DC_kwDOADYVqs4AU3Wg")

        # inner page of REST labels
        labels_pages = discussions[3].get_labels()
        self.assertEqual(labels_pages.totalCount, 1)
        label = labels_pages[0]
        self.assertEqual(label.name, "Call for Contribution")

        # inner REST repository
        repo = discussion.repository
        issues_pages = repo.get_issues()
        issue = issues_pages[0]
        # GraphQL retrieved 10 issues, but repo.get_issues() is not aware of that data
        # it calls the REST API
        self.assertEqual(issues_pages.totalCount, 341)
        self.assertEqual(issue.number, 3045)