File: Search.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 (301 lines) | stat: -rw-r--r-- 12,320 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
############################ Copyrights and license ############################
#                                                                              #
# Copyright 2014 Vincent Jacques <vincent@vincent-jacques.net>                 #
# Copyright 2016 Peter Buckley <dx-pbuckley@users.noreply.github.com>          #
# Copyright 2018 Agor Maxime <maxime.agor23@gmail.com>                         #
# Copyright 2018 Joel Koglin <JoelKoglin@gmail.com>                            #
# Copyright 2018 Shubham Singh <41840111+singh811@users.noreply.github.com>    #
# Copyright 2018 Steve Kowalik <steven@wedontsleep.org>                        #
# Copyright 2018 Wan Liuyang <tsfdye@gmail.com>                                #
# Copyright 2018 h.shi <10385628+AnYeMoWang@users.noreply.github.com>          #
# Copyright 2018 sfdye <tsfdye@gmail.com>                                      #
# Copyright 2019 Steve Kowalik <steven@wedontsleep.org>                        #
# Copyright 2019 TechnicalPirate <35609336+TechnicalPirate@users.noreply.github.com>#
# Copyright 2019 Wan Liuyang <tsfdye@gmail.com>                                #
# Copyright 2020 Steve Kowalik <steven@wedontsleep.org>                        #
# Copyright 2021 Steve Kowalik <steven@wedontsleep.org>                        #
# Copyright 2023 Enrico Minack <github@enrico.minack.dev>                      #
# Copyright 2023 Jirka Borovec <6035284+Borda@users.noreply.github.com>        #
# 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 . import Framework


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

    def testSearchUsers(self):
        users = self.g.search_users("vincent", sort="followers", order="desc")
        self.assertEqual(users.totalCount, 2781)

    def testPaginateSearchUsers(self):
        users = self.g.search_users("", location="Berlin")
        self.assertListKeyBegin(
            users,
            lambda u: u.login,
            [
                "cloudhead",
                "felixge",
                "sferik",
                "rkh",
                "jezdez",
                "janl",
                "marijnh",
                "nikic",
                "igorw",
                "froschi",
                "svenfuchs",
                "omz",
                "chad",
                "bergie",
                "roidrage",
                "pcalcado",
                "durran",
                "hukl",
                "mttkay",
                "aFarkas",
                "ole",
                "hagenburger",
                "jberkel",
                "naderman",
                "joshk",
                "pudo",
                "robb",
                "josephwilk",
                "hanshuebner",
                "txus",
                "paulasmuth",
                "splitbrain",
                "langalex",
                "bendiken",
                "stefanw",
            ],
        )
        self.assertEqual(users.totalCount, 6038)
        self.assertEqual(users[0].score, 1.0)

    def testGetPageOnSearchUsers(self):
        users = self.g.search_users("", location="Berlin")
        self.assertEqual(
            [u.login for u in users.get_page(7)],
            [
                "ursachec",
                "bitboxer",
                "fs111",
                "michenriksen",
                "witsch",
                "booo",
                "mortice",
                "r0man",
                "MikeBild",
                "mhagger",
                "bkw",
                "fwbrasil",
                "mschneider",
                "lydiapintscher",
                "asksven",
                "iamtimm",
                "sneak",
                "kr1sp1n",
                "Feh",
                "GordonLesti",
                "annismckenzie",
                "eskimoblood",
                "tsujigiri",
                "riethmayer",
                "lauritzthamsen",
                "scotchi",
                "peritor",
                "toto",
                "hwaxxer",
                "lukaszklis",
            ],
        )

    def testSearchRepos(self):
        repos = self.g.search_repositories("github", sort="stars", order="desc", language="Python")
        self.assertListKeyBegin(
            repos,
            lambda r: r.full_name,
            [
                "kennethreitz/legit",
                "RuudBurger/CouchPotatoV1",
                "gelstudios/gitfiti",
                "gpjt/webgl-lessons",
                "jacquev6/PyGithub",
                "aaasen/github_globe",
                "hmason/gitmarks",
                "dnerdy/factory_boy",
                "binaryage/drydrop",
                "bgreenlee/sublime-github",
                "karan/HackerNewsAPI",
                "mfenniak/pyPdf",
                "skazhy/github-decorator",
                "llvmpy/llvmpy",
                "lexrupy/gmate",
                "ask/python-github2",
                "audreyr/cookiecutter-pypackage",
                "tabo/django-treebeard",
                "dbr/tvdb_api",
                "jchris/couchapp",
                "joeyespo/grip",
                "nigelsmall/py2neo",
                "ask/chishop",
                "sigmavirus24/github3.py",
                "jsmits/github-cli",
                "lincolnloop/django-layout",
                "amccloud/django-project-skel",
                "Stiivi/brewery",
                "webpy/webpy.github.com",
                "dustin/py-github",
                "logsol/Github-Auto-Deploy",
                "cloudkick/libcloud",
                "berkerpeksag/github-badge",
                "bitprophet/ssh",
                "azavea/OpenTreeMap",
            ],
        )

    def testSearchReposWithNoResults(self):
        repos = self.g.search_repositories("doesnotexist")
        self.assertEqual(repos.totalCount, 0)

    def testSearchIssues(self):
        issues = self.g.search_issues("compile", sort="comments", order="desc", language="C++")
        self.assertListKeyBegin(
            issues,
            lambda i: i.id,
            [
                12068673,
                23250111,
                14371957,
                9423897,
                24277400,
                2408877,
                11338741,
                13980502,
                27697165,
                23102422,
            ],
        )
        self.assertEqual(issues[0].score, 0.08252439)

    def testSearchCommits(self):
        pages = self.g.search_commits(query="hash:5b0224e868cc9242c9450ef02efbe3097abd7ba2")
        commits = list(pages)
        self.assertEqual(pages.totalCount, 12)
        self.assertEqual(commits[0].commit.message, "Fix README instructions")
        self.assertEqual(commits[0].score, 1.0)
        self.assertEqual(commits[0].sha, "5b0224e868cc9242c9450ef02efbe3097abd7ba2")

    def testSearchCommitsOrder(self):
        pages = self.g.search_commits(
            query="hash:1265747e992ba7d34a469b6b2f527809f8bf7067",
            sort="author-date",
            order="asc",
            merge="false",
        )
        commits = list(pages)
        self.assertEqual(pages.totalCount, 4)
        self.assertEqual(len(commits[0].commit.message), 490)
        self.assertEqual(commits[0].score, 1.0)
        self.assertEqual(commits[0].sha, "1265747e992ba7d34a469b6b2f527809f8bf7067")

    def testSearchTopics(self):
        topics = self.g.search_topics("python", repositories=">950")
        self.assertListKeyBegin(
            topics,
            lambda r: r.name,
            ["python", "django", "flask", "ruby", "scikit-learn", "wagtail"],
        )

    def testPaginateSearchTopics(self):
        repos = self.g.search_topics("python", repositories=">950")
        self.assertEqual(repos.totalCount, 6)

    def testSearchCode(self):
        files = self.g.search_code("toto", sort="indexed", order="asc", user="jacquev6")
        self.assertListKeyEqual(
            files,
            lambda f: f.name,
            [
                "Commit.setUp.txt",
                "PullRequest.testGetFiles.txt",
                "NamedUser.testGetEvents.txt",
                "PullRequest.testCreateComment.txt",
                "PullRequestFile.setUp.txt",
                "Repository.testGetIssuesWithWildcards.txt",
                "Repository.testGetIssuesWithArguments.txt",
                "test_ebnf.cpp",
                "test_abnf.cpp",
                "PullRequestFile.py",
                "SystemCalls.py",
                "tests.py",
                "LexerTestCase.py",
                "ParserTestCase.py",
            ],
        )
        self.assertEqual(files[0].score, 0.31651077)
        self.assertEqual(files[0].repository.full_name, "jacquev6/PyGithub")
        content = files[0].decoded_content
        if isinstance(content, bytes):
            content = content.decode("utf-8")
        self.assertEqual(content[:30], "https\nGET\napi.github.com\nNone\n")

    def testSearchHighlightingCode(self):
        files = self.g.search_code("toto", sort="indexed", order="asc", user="jacquev6", highlight=True)
        self.assertEqual(files[0].score, 14.030813)
        self.assertEqual(
            files[0].text_matches,
            [
                {
                    "fragment": ".assertEqual(\n"
                    "            self.recorded.instance_method(42, 43, 44, 45, "
                    "toto=46, tutu=47",
                    "matches": [{"indices": [72, 76], "text": "toto"}],
                    "object_type": "FileContent",
                    "object_url": "https://api.github.com/repositories/6430524/contents/MockMockMock/tests/record_replay.py?ref=562a55542f55426f6853f3013309c85f402c359e",
                    "property": "content",
                },
                {
                    "fragment": "),\n"
                    "            \"(42, 43, (44, 45), [('toto', 46), ('tutu', "
                    '47)])"\n'
                    "        )\n"
                    "        self.assertEqual",
                    "matches": [{"indices": [38, 42], "text": "toto"}],
                    "object_type": "FileContent",
                    "object_url": "https://api.github.com/repositories/6430524/contents/MockMockMock/tests/record_replay.py?ref=562a55542f55426f6853f3013309c85f402c359e",
                    "property": "content",
                },
            ],
        )

    def testUrlquotingOfQualifiers(self):
        # Example taken from #236
        issues = self.g.search_issues("repo:saltstack/salt-api type:Issues", updated=">2014-03-04T18:28:11Z")
        self.assertEqual(issues[0].id, 29138794)

    def testUrlquotingOfQuery(self):
        # Example taken from #236
        issues = self.g.search_issues("repo:saltstack/salt-api type:Issues updated:>2014-03-04T18:28:11Z")
        self.assertEqual(issues[0].id, 29138794)