Skip to content

Commit ee2d7d3

Browse files
committed
put all gitutils github stuff into this repo
correct imports
1 parent 2a20d4c commit ee2d7d3

24 files changed

+702
-85
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ on: [push, pull_request]
44

55
jobs:
66

7-
lint:
7+
run:
88
runs-on: ubuntu-latest
99
steps:
1010
- uses: actions/checkout@v1
1111
- uses: actions/setup-python@v1
1212
with:
1313
python-version: 3.7
14-
- run: pip install -e .[tests]
14+
- run: pip install -e .[tests,lint]
1515
- run: flake8
1616
- run: mypy .
17+
- run: pytest -r a -v

AddGithubMembers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
https://developer.github.com/v3/repos/#oauth-scope-requirements
1111
"""
1212
import pandas
13-
from gitutils.github_base import repo_exists, check_api_limit, connect_github
13+
from pygithubutils.base import repo_exists, check_api_limit, connect_github
1414
from pathlib import Path
1515
import warnings
1616
from argparse import ArgumentParser

CopyFileByLanguage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from argparse import ArgumentParser
1515
import base64
1616
import github
17-
import gitutils.github_base as gb
17+
import pygithubutils.base as gb
1818

1919

2020
def main():

CountGithubForks.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python
2+
"""
3+
lists all repos for a Github user.
4+
Can use Oauth login.
5+
GitHub API is otherwise very limited for unauthenticated users.
6+
7+
The Oauth file should be in a secure place, NOT in a Git repo!
8+
Maybe encrypted and with permissions 600.
9+
The Oauth key should have no checkboxes, so that it's read only for public repos.
10+
"""
11+
import pandas as pd
12+
from argparse import ArgumentParser
13+
import pygithubutils.repo_stats as gu
14+
15+
16+
def main():
17+
p = ArgumentParser(description="list all Github repos for a particular user")
18+
p.add_argument("user", help="Github username")
19+
p.add_argument("oauth", help="Oauth filename", nargs="?")
20+
p.add_argument("-b", "--branch", help="branch to consider")
21+
p.add_argument("-stars", help="only get star count", action="store_true")
22+
p.add_argument("-v", "--verbose", action="store_true")
23+
p = p.parse_args()
24+
25+
dat, ahead = gu.repo_prober(p.user, p.oauth, p.branch, p.stars, p.verbose)
26+
27+
datnz = dat[~(dat == 0).all(axis=1)].drop_duplicates()
28+
# %% Stars and Forks
29+
pd.set_option("display.max_rows", 500)
30+
31+
print(f'\n{p.user} total stars received {datnz["stars"].sum()}')
32+
print(f'{p.user} total other users forked {datnz["forks"].sum()}\n')
33+
34+
print(datnz.sort_values(["stars", "forks"], ascending=False))
35+
# %% Forks ahead
36+
if ahead:
37+
print(f"\n{p.user} Forks that are ahead by N commits")
38+
for a in ahead:
39+
print(a)
40+
41+
42+
if __name__ == "__main__":
43+
main()

CountGithubStars.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python
2+
"""
3+
Totals up stars and fork count for all repos of a Github user.
4+
Can use Oauth login if desired.
5+
"""
6+
import pandas
7+
from argparse import ArgumentParser
8+
import pygithubutils.repo_stats as gu
9+
10+
11+
def main():
12+
p = ArgumentParser(description="Totals up stars and fork count for all repos of a Github user.")
13+
p.add_argument("user", help="Github username / organization name", nargs="+")
14+
p.add_argument("-i", "--oauth", help="Oauth filename", nargs="?")
15+
p = p.parse_args()
16+
17+
dat = pandas.DataFrame(None)
18+
19+
for u in p.user:
20+
counts = gu.repo_prober(u, p.oauth, None, True)[0]
21+
22+
d = pandas.DataFrame([c[1:] for c in counts], index=[c[0] for c in counts], columns=["forks", "stars"])
23+
dat = pandas.concat((dat, d))
24+
25+
datnz = dat[~(dat == 0).all(axis=1)].drop_duplicates()
26+
# %% Stars and Forks
27+
pandas.set_option("display.max_rows", 500)
28+
29+
print(f'\n{" ".join(p.user)} total stars received {datnz["stars"].sum()}')
30+
print(f'{" ".join(p.user)} total other users forked {datnz["forks"].sum()}\n')
31+
32+
print(datnz.sort_values(["stars", "forks"], ascending=False))
33+
34+
35+
if __name__ == "__main__":
36+
main()

CountStars.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python
2+
"""
3+
Count how many GitHub Stars a user has received, using GitHub API v4 GraphQL
4+
"""
5+
import requests
6+
from pathlib import Path
7+
import argparse
8+
9+
ENDPOINT = "https://api.github.com/graphql"
10+
11+
12+
def run_query(query: str, token_file: Path):
13+
request = requests.post(ENDPOINT, json={"query": query}, headers={"Authorization": token_file.read_text()})
14+
if request.status_code == 200:
15+
return request.json()
16+
else:
17+
raise ValueError(f"Query failed with code {request.status_code}")
18+
19+
20+
if __name__ == "__main__":
21+
p = argparse.ArgumentParser()
22+
p.add_argument("oauth", help="path to oauth key")
23+
p.add_argument("username", help="GitHub username(s) to count stars for", nargs="+")
24+
p = p.parse_args()
25+
26+
query = """
27+
query {
28+
search(type: REPOSITORY, user: %s query: "sort:stars stars:>1") {
29+
userCount
30+
edges {
31+
node {
32+
... on Repository {
33+
name
34+
description
35+
stargazers {
36+
totalCount
37+
}
38+
url
39+
}
40+
}
41+
}
42+
}
43+
}
44+
""" % (
45+
p.username
46+
)
47+
48+
token_file = Path(p.oauth).expanduser()
49+
50+
dat = run_query(query, token_file)

CreateGithubTeamRepos.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
https://developer.github.com/v3/repos/#oauth-scope-requirements
1111
"""
1212
import pandas
13-
from gitutils.github_base import connect_github, repo_exists, check_api_limit
13+
from pygithubutils.base import connect_github, repo_exists, check_api_limit
1414
from pathlib import Path
1515
from argparse import ArgumentParser
1616

DuplicateGithubRepos.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
Assumes you have an SSH key loaded for git push --mirror step
1111
"""
1212
from argparse import ArgumentParser
13-
import gitutils.github_duplicator as gu
14-
import gitutils.github_base as gb
13+
import pygithubutils.duplicator as gu
14+
import pygithubutils.base as gb
1515

1616

1717
def main():

ListGithubCollab.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
from pathlib import Path
1616
import itertools
1717

18-
from gitedu.get import get_collabs
19-
from gitutils.github_base import check_api_limit, connect_github
18+
from pygithubutils.get import get_collabs
19+
from pygithubutils.base import check_api_limit, connect_github
2020

2121

2222
def main(P):

ListUserGithubRepos.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python
2+
"""
3+
List all GitHub repos of a user / organization.
4+
Optionally, open the settings or laerts page for each repo.
5+
6+
For organization private repos, you will need "repo" Oauth permission.
7+
Restricted "Third-party application access policy"
8+
from organization oauth_application_policy settings is OK.
9+
10+
Without Oauth, you will only see public repos
11+
"""
12+
from argparse import ArgumentParser
13+
import webbrowser
14+
import pygithubutils.base as gb
15+
16+
17+
def main():
18+
p = ArgumentParser(description="List user/organization repos")
19+
p.add_argument("user", help="GitHub username / organization name")
20+
p.add_argument("oauth", help="Oauth filename", nargs="?")
21+
p.add_argument("-p", "--pattern", help="only repos with name starting with this string")
22+
p.add_argument("-settings", help="open settings page for each repo", action="store_true")
23+
p.add_argument("-alerts", help="open alerts page for each repo", action="store_true")
24+
P = p.parse_args()
25+
26+
# %% authentication
27+
sess = gb.github_session(P.oauth)
28+
gb.check_api_limit(sess)
29+
# %% get user / organization handle
30+
userorg = gb.user_or_org(sess, P.user)
31+
# %% prepare to loop over repos
32+
repos = gb.get_repos(userorg)
33+
34+
if P.pattern:
35+
repos = (repo for repo in repos if repo.name.startswith(P.pattern))
36+
37+
for repo in repos:
38+
print(repo.full_name)
39+
if P.settings:
40+
webbrowser.open_new_tab("https://github.com/" + repo.full_name + "/settings")
41+
if P.alerts:
42+
webbrowser.open_new_tab("https://github.com/" + repo.full_name + "/network/alerts")
43+
44+
45+
if __name__ == "__main__":
46+
main()

0 commit comments

Comments
 (0)