Skip to content

Commit a128f65

Browse files
authored
Merge pull request #1 from SAnCherepan/develop
v0.2.0
2 parents 42df55d + dbcc2a4 commit a128f65

14 files changed

Lines changed: 671 additions & 1 deletion

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 SAnCherepan
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
1-
# Test-code
1+
# Repo reader
2+
3+
This is a simple example package that can be used to get basic info on GitHub repository groups (a.k.a. organisations).
4+
5+
# Installation
6+
7+
Install the python package:
8+
```
9+
python -m pip install --index-url https://test.pypi.org/simple/ repo_reader_sancherepan
10+
```
11+
12+
13+
# Usage
14+
15+
In your code:
16+
```python
17+
from repo_reader_sancherepan.repo_reader import RepoReader
18+
19+
reader = RepoReader("<your_github_auth_token>")
20+
repos = reader.read_repos("<github_repo_group_name>")
21+
```
22+
23+
In command line:
24+
```commandline
25+
read-repos your_github_auth_token github_repo_group_name
26+
```

docker/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# ToDo: write Dockerfile

pyproject.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[build-system]
2+
requires = ["setuptools>=61.0"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "repo_reader_sancherepan"
7+
version = "0.2.0"
8+
authors = [
9+
{ name="SAnCherepan", email="sivkinpunk@gmail.com" },
10+
]
11+
description = "A small example package for reading basic GitHub repo data"
12+
license = {file = "LICENSE.md"}
13+
readme = "README.md"
14+
requires-python = ">=3.11"
15+
classifiers = [
16+
"Development Status :: 2 - Pre-Alpha",
17+
"Programming Language :: Python :: 3.11",
18+
"License :: OSI Approved :: MIT License",
19+
"Operating System :: OS Independent",
20+
]
21+
22+
[project.urls]
23+
"Homepage" = "https://github.com/sancherepan/test-code"
24+
25+
[project.scripts]
26+
read-repos = "repo_reader_sancherepan.print_repos_info:main"

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests

src/repo_reader_sancherepan/__init__.py

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Branch(object):
2+
def __init__(self, data):
3+
if "name" in data:
4+
self.name = data["name"]
5+
else:
6+
raise ValueError("bad repository data: expected name")
7+
8+
if "protected" in data:
9+
self.protected = data["protected"]
10+
else:
11+
raise ValueError("bad repository data: expected protected")
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import sys
2+
import argparse
3+
from .repo_reader import RepoReader
4+
5+
6+
def main(args=None):
7+
parser = argparse.ArgumentParser()
8+
parser.add_argument('auth_token', help='<your_github_auth_token>')
9+
parser.add_argument('org_name', help="<github_repo_group_name>")
10+
11+
if args is None:
12+
args = sys.argv[1:]
13+
14+
args = parser.parse_args(args)
15+
auth_token = args.auth_token
16+
org_name = args.org_name
17+
18+
reader = RepoReader(auth_token)
19+
repos = reader.read_repos(org_name)
20+
21+
for repo in repos:
22+
print(f"repo: {repo.name}, "
23+
f"size: {repo.size}, "
24+
f"open_issues: {repo.open_issues_count}, "
25+
f"branches: {len(repo.branches)}\n")
26+
27+
28+
if __name__ == '__main__':
29+
sys.exit(main(sys.argv))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import requests
2+
from .repository import Repository
3+
4+
5+
class RepoReader:
6+
7+
github_base_url = "https://api.github.com"
8+
9+
github_org_repos_url = "/orgs/{}/repos"
10+
11+
def __init__(self, auth_token):
12+
self.session = requests.Session()
13+
self.session.headers.update({"Authorization": f"Bearer {auth_token}",
14+
"Accept": "application/vnd.github+json"})
15+
16+
def read_repos(self, org_name: str):
17+
url = self.github_base_url + self.github_org_repos_url.format(org_name)
18+
19+
response = self.session.get(url)
20+
response.raise_for_status()
21+
22+
repos = [Repository(self.session, repo_data) for repo_data in response.json()]
23+
24+
return repos
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import requests
2+
from .branch import Branch
3+
4+
5+
class Repository(object):
6+
"""
7+
A GitHub repository with some basic properties
8+
"""
9+
session: requests.Session
10+
11+
def __init__(self, session, data: dict):
12+
self.session = session
13+
14+
if "name" in data:
15+
self.name = data["name"]
16+
else:
17+
raise ValueError("bad repository data: expected name")
18+
19+
if "size" in data:
20+
self.size = data["size"]
21+
else:
22+
raise ValueError("bad repository data: expected size")
23+
24+
if "open_issues_count" in data:
25+
self.open_issues_count = data["open_issues_count"]
26+
else:
27+
raise ValueError("bad repository data: expected open_issues_count")
28+
29+
if "url" in data:
30+
self.count_branches(data["url"])
31+
else:
32+
self.branches = None
33+
34+
def count_branches(self, repo_url):
35+
url = f"{repo_url}/branches"
36+
response = self.session.get(url)
37+
response.raise_for_status()
38+
39+
self.branches = [Branch(branch_data) for branch_data in response.json()]

0 commit comments

Comments
 (0)