-
Notifications
You must be signed in to change notification settings - Fork 67
/
sample_basic.py
executable file
·77 lines (58 loc) · 2.07 KB
/
sample_basic.py
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
#!/usr/bin/env python3
# -*-coding: utf8 -*-
'''
Get user, repo info.
Export token first before run this script:
$ export GITHUB_TOKEN=ghp_xxx
'''
import os, json, time, datetime
from github import GitHub, ApiNotFoundError
def main():
# read token from env:
token = os.getenv('GITHUB_TOKEN')
if not token:
print('could not read token from env.')
exit(1)
gh = GitHub(token, debug=True)
user = 'michaelliao'
repo = 'githubpy-test'
# get a user: https://docs.github.com/rest/users/users#get-a-user
u = gh.users(user).get()
print_json('user:', u)
sleep()
# get repositories for a user: https://docs.github.com/rest/repos/repos#list-repositories-for-a-user
repos = gh.users(user).repos.get(sort='updated', page=1, per_page=6)
print_json('repos:', repos)
for rp in repos:
sleep()
# list repository contributors: https://docs.github.com/rest/repos/repos#list-repository-contributors
cs = gh.repos(user)(rp.name).contributors.get()
print_json(f'contributors of repo {rp.name}', cs)
if rp.name == repo:
# update a repository: https://docs.github.com/rest/repos/repos#update-a-repository
updated = gh.repos(user)(rp.name).patch(
{
'description': 'Test repo for githubpy, updated at ' + now()
}
)
print_json('updated repo:', updated)
# update a repository without permission: https://docs.github.com/rest/repos/repos#update-a-repository
try:
gh.repos('torvalds')('linux').patch(
{
'description': 'Update Linux kernel source tree'
}
)
except ApiNotFoundError as e:
print(e.code, e.url)
def print_json(msg, obj):
print(f'{msg}')
print('----------------------------------------')
print(json.dumps(obj, indent=2))
print('----------------------------------------')
def sleep():
time.sleep(1.5)
def now():
return datetime.datetime.now().strftime('%Y-%m-%d %H:%M')
if __name__ == '__main__':
main()