/ doc / examples / Repository.rst
Repository.rst
  1  Repository
  2  ==========
  3  
  4  Get repository topics
  5  ---------------------
  6  
  7  .. code-block:: python
  8  
  9      >>> repo = g.get_repo("PyGithub/PyGithub")
 10      >>> repo.get_topics()
 11      [u'pygithub', u'python', u'github', u'github-api']
 12  
 13  Get count of stars
 14  ------------------
 15  
 16  .. code-block:: python
 17  
 18      >>> repo = g.get_repo("PyGithub/PyGithub")
 19      >>> repo.stargazers_count
 20      2086
 21  
 22  Get list of open issues
 23  --------------------------
 24  
 25  .. code-block:: python
 26  
 27      >>> repo = g.get_repo("PyGithub/PyGithub")
 28      >>> open_issues = repo.get_issues(state='open')
 29      >>> for issue in open_issues:
 30      ...     print(issue)
 31      ...
 32      Issue(title="How to get public events?", number=913)
 33      Issue(title="Prevent .netrc from overwriting Auth header", number=910)
 34      Issue(title="Cache fetch responses", number=901)
 35      Issue(title="Is suspended_users for github enterprise implemented in NamedUser?", number=900)
 36      Issue(title="Adding migration api wrapper", number=899)
 37  
 38  Get list of code scanning alerts
 39  --------------------------------
 40  
 41  .. code-block:: python
 42  
 43      >>> repo = g.get_repo("PyGithub/PyGithub")
 44      >>> codescan_alerts = repo.get_codescan_alerts()
 45      >>> for alert in codescan_alerts:
 46      ...     print(alert.number, alert.created_at, alert.dismissed_at)
 47      ...     print("  ", alert.tool.name, alert.tool.version, alert.tool.guid)
 48      ...     print("  ", alert.rule.name alert.rule.security_severity_level alert.rule.severity)
 49      ...     print("    ", alert.rule.description)
 50      ...     print("  ", alert.most_recent_instance.ref, alert.most_recent_instance.state)
 51      ...     print("    ", alert.most_recent_instance.location)
 52      ...     print("    ", alert.most_recent_instance.message['text'])
 53      ...
 54      3 1984-02-29 12:34:56 None
 55        CodeQL 2.6.1 None
 56        py/weak-sensitive-data-hashing high warning
 57          Use of a broken or weak cryptographic hashing algorithm on sensitive data
 58        refs/heads/master | open
 59          src/secrets/rats.py @ l42:c13-l42:c69
 60          Sensitive data (password) is used in a hashing algorithm (SHA1) that is insecure⤶
 61          for password hashing, since it is not a computationally expensive hash function.
 62  
 63  Get all the labels of the repository
 64  ------------------------------------
 65  
 66  .. code-block:: python
 67  
 68      >>> repo = g.get_repo("PyGithub/PyGithub")
 69      >>> labels = repo.get_labels()
 70      >>> for label in labels:
 71      ...     print(label)
 72      ...
 73      Label(name="Hacktoberfest")
 74      Label(name="WIP")
 75      Label(name="bug")
 76      Label(name="documentation")
 77  
 78  Get all of the contents of the root directory of the repository
 79  ---------------------------------------------------------------
 80  
 81  .. code-block:: python
 82  
 83      >>> repo = g.get_repo("PyGithub/PyGithub")
 84      >>> contents = repo.get_contents("")
 85      >>> for content_file in contents:
 86      ...     print(content_file)
 87      ...
 88      ContentFile(path=".github")
 89      ContentFile(path=".gitignore")
 90      ContentFile(path="CONTRIBUTING.md")
 91      ContentFile(path="COPYING")
 92      ContentFile(path="COPYING.LESSER")
 93      ContentFile(path="MAINTAINERS")
 94      ContentFile(path="MANIFEST.in")
 95      ContentFile(path="README.md")
 96      ContentFile(path="doc")
 97      ContentFile(path="github")
 98      ContentFile(path="manage.sh")
 99      ContentFile(path="requirements.txt")
100      ContentFile(path="scripts")
101      ContentFile(path="setup.py")
102  
103  Get all of the contents of the repository recursively
104  -----------------------------------------------------
105  
106  .. code-block:: python
107  
108      >>> repo = g.get_repo("PyGithub/PyGithub")
109      >>> contents = repo.get_contents("")
110      >>> while contents:
111      ...     file_content = contents.pop(0)
112      ...     if file_content.type == "dir":
113      ...         contents.extend(repo.get_contents(file_content.path))
114      ...     else:
115      ...         print(file_content)
116      ...
117      ContentFile(path=".gitignore")
118      ContentFile(path="CONTRIBUTING.md")
119      ...
120      ContentFile(path="github/tests/ReplayData/Team.testRepoPermission.txt")
121      ContentFile(path="github/tests/ReplayData/Team.testRepos.txt")
122      ContentFile(path="github/tests/ReplayData/UserKey.setUp.txt")
123  
124  Get a specific content file
125  ---------------------------
126  
127  .. code-block:: python
128  
129      >>> repo = g.get_repo("PyGithub/PyGithub")
130      >>> contents = repo.get_contents("README.md")
131      >>> print(contents)
132      ...
133      ContentFile(path="README.md")
134  
135  Create a new file in the repository
136  -----------------------------------
137  
138  .. code-block:: python
139  
140      >>> repo = g.get_repo("PyGithub/PyGithub")
141      >>> repo.create_file("test.txt", "test", "test", branch="test")
142      {'content': ContentFile(path="test.txt"), 'commit': Commit(sha="5b584cf6d32d960bb7bee8ce94f161d939aec377")}
143  
144  Update a file in the repository
145  -------------------------------
146  
147  .. code-block:: python
148  
149      >>> repo = g.get_repo("PyGithub/PyGithub")
150      >>> contents = repo.get_contents("test.txt", ref="test")
151      >>> repo.update_file(contents.path, "more tests", "more tests", contents.sha, branch="test")
152      {'commit': Commit(sha="b06e05400afd6baee13fff74e38553d135dca7dc"), 'content': ContentFile(path="test.txt")}
153  
154  Delete a file in the repository
155  -------------------------------
156  
157  .. code-block:: python
158  
159      >>> repo = g.get_repo("PyGithub/PyGithub")
160      >>> contents = repo.get_contents("test.txt", ref="test")
161      >>> repo.delete_file(contents.path, "remove test", contents.sha, branch="test")
162      {'commit': Commit(sha="0f40b0b4f31f62454f1758d7e6b384795e48fd96"), 'content': NotSet}
163  
164  Get the top 10 referrers over the last 14 days
165  ----------------------------------------------
166  
167  .. code-block:: python
168  
169      >>> repo = g.get_repo("PyGithub/PyGithub")
170      >>> contents = repo.get_top_referrers()
171      >>> print(contents)
172      [
173        Referrer(referrer="Google", count=4, uniques=3),
174        Referrer(referrer="stackoverflow.com", count=2, uniques=2),
175        Referrer(referrer="eggsonbread.com", count=1, uniques=1),
176        Referrer(referrer="yandex.ru", count=1, uniques=1)
177      ]
178  
179  Get the top 10 popular contents over the last 14 days
180  -----------------------------------------------------
181  
182  .. code-block:: python
183  
184      >>> repo = g.get_repo("PyGithub/PyGithub")
185      >>> contents = repo.get_top_paths()
186      >>> print(contents)
187      [
188        Path(path="/github/hubot", title="github/hubot: A customizable life embetterment robot.", count=3542, uniques=2225),
189        Path(path="/github/hubot/blob/master/docs/scripting.md", title="hubot/scripting.md at master · github/hubot · GitHub", count=1707, uniques=804),
190        Path(path="/github/hubot/tree/master/docs", title="hubot/docs at master · github/hubot · GitHub", count=685, uniques=435),
191        Path(path="/github/hubot/tree/master/src", title="hubot/src at master · github/hubot · GitHub", count=577, uniques=347),
192        Path(path="/github/hubot/blob/master/docs/index.md", title="hubot/index.md at master · github/hubot · GitHub", count=379, uniques=259),
193        Path(path="/github/hubot/blob/master/docs/adapters.md", title="hubot/adapters.md at master · github/hubot · GitHub", count=354, uniques=201),
194        Path(path="/github/hubot/tree/master/examples", title="hubot/examples at master · github/hubot · GitHub", count=340, uniques=260),
195        Path(path="/github/hubot/blob/master/docs/deploying/heroku.md", title="hubot/heroku.md at master · github/hubot · GitHub", count=324, uniques=217),
196        Path(path="/github/hubot/blob/master/src/robot.coffee", title="hubot/robot.coffee at master · github/hubot · GitHub", count=293, uniques=191),
197        Path(path="/github/hubot/blob/master/LICENSE.md", title="hubot/LICENSE.md at master · github/hubot · GitHub", count=281, uniques=222)
198      ]
199  
200  Get number of clones and breakdown for the last 14 days
201  -------------------------------------------------------
202  
203  .. code-block:: python
204  
205      >>> repo = g.get_repo("PyGithub/PyGithub")
206      >>> contents = repo.get_clones_traffic()
207      >>> contents = repo.get_clones_traffic(per="week")
208      >>> print(contents)
209      {
210        "count": 173,
211        "uniques": 128,
212        "clones": [
213          Clones(timestamp=2016-10-10 00:00:00, count=2, uniques=1),
214          Clones(timestamp=2016-10-11 00:00:00, count=17, uniques=16),
215          Clones(timestamp=2016-10-12 00:00:00, count=21, uniques=15),
216          Clones(timestamp=2016-10-13 00:00:00, count=8, uniques=7),
217          Clones(timestamp=2016-10-14 00:00:00, count=5, uniques=5),
218          Clones(timestamp=2016-10-15 00:00:00, count=2, uniques=2),
219          Clones(timestamp=2016-10-16 00:00:00, count=8, uniques=7),
220          Clones(timestamp=2016-10-17 00:00:00, count=26, uniques=15),
221          Clones(timestamp=2016-10-18 00:00:00, count=19, uniques=17),
222          Clones(timestamp=2016-10-19 00:00:00, count=19, uniques=14),
223          Clones(timestamp=2016-10-20 00:00:00, count=19, uniques=15),
224          Clones(timestamp=2016-10-21 00:00:00, count=9, uniques=7),
225          Clones(timestamp=2016-10-22 00:00:00, count=5, uniques=5),
226          Clones(timestamp=2016-10-23 00:00:00, count=6, uniques=5),
227          Clones(timestamp=2016-10-24 00:00:00, count=7, uniques=5)
228        ]
229      }
230  
231  Get number of views and breakdown for the last 14 days
232  ------------------------------------------------------
233  
234  .. code-block:: python
235  
236      >>> repo = g.get_repo("PyGithub/PyGithub")
237      >>> contents = repo.get_views_traffic()
238      >>> contents = repo.get_views_traffic(per="week")
239      >>> print(contents)
240      {
241        "count": 14850,
242        "uniques": 3782,
243        "views": [
244          View(timestamp=2016-10-10 00:00:00, count=440, uniques=143),
245          View(timestamp=2016-10-11 00:00:00, count=1308, uniques=414),
246          View(timestamp=2016-10-12 00:00:00, count=1486, uniques=452),
247          View(timestamp=2016-10-13 00:00:00, count=1170, uniques=401),
248          View(timestamp=2016-10-14 00:00:00, count=868, uniques=266),
249          View(timestamp=2016-10-15 00:00:00, count=495, uniques=157),
250          View(timestamp=2016-10-16 00:00:00, count=524, uniques=175),
251          View(timestamp=2016-10-17 00:00:00, count=1263, uniques=412),
252          View(timestamp=2016-10-18 00:00:00, count=1402, uniques=417),
253          View(timestamp=2016-10-19 00:00:00, count=1394, uniques=424),
254          View(timestamp=2016-10-20 00:00:00, count=1492, uniques=448),
255          View(timestamp=2016-10-21 00:00:00, count=1153, uniques=332),
256          View(timestamp=2016-10-22 00:00:00, count=566, uniques=168),
257          View(timestamp=2016-10-23 00:00:00, count=675, uniques=184),
258          View(timestamp=2016-10-24 00:00:00, count=614, uniques=237)
259        ]
260      }
261  
262  Mark the notifications of the repository as read
263  ------------------------------------------------
264  
265  .. code-block:: python
266  
267      >>> repo = g.get_repo("PyGithub/PyGithub")
268      >>> repo.mark_notifications_as_read()