Skip to content

Commit 374ead8

Browse files
authored
Merge pull request #49 from lbrealdev/version-argparse
Add --version argparser command
2 parents 8144051 + 258d472 commit 374ead8

9 files changed

Lines changed: 118 additions & 68 deletions

File tree

.github/workflows/python-ci.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ jobs:
4141
- name: uv sync
4242
run: uv sync
4343

44-
- name: Debug CLI
44+
- name: Get CLI version
4545
run: |
4646
source .venv/bin/activate
47-
github-rest-cli -h
48-
env:
49-
GITHUB_AUTH_TOKEN: f@k3-t0k3n
47+
github-rest-cli --version

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
# GitHub REST API
22

3-
### Usage
3+
## Installation
4+
5+
Install using `pip`:
6+
```shell
7+
pip install github-rest-cli
8+
```
9+
10+
Install using `uv`:
11+
```shell
12+
uv pip install github-rest-cli
13+
```
14+
15+
## Usage
416

517
Set up python package dependencies in `pyproject.toml`:
618
```shell
@@ -24,7 +36,7 @@ export GITHUB_AUTH_TOKEN="<github-auth-token>"
2436

2537
Run cli:
2638
```shell
27-
github-rest-cli -h
39+
github-rest-cli -v
2840
```
2941

3042
### Dynaconf

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "github-rest-cli"
3-
version = "1.0.0"
3+
version = "1.0.1"
44
description = "GitHub REST API cli"
55
authors = [
66
{ name = "lbrealdev", email = "lbrealdeveloper@gmail.com" }

src/github_rest_cli/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
__version__ = "0.1.0"

src/github_rest_cli/api.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import requests
2-
from github_rest_cli.globals import GITHUB_URL, HEADERS
2+
from github_rest_cli.globals import GITHUB_URL, get_headers
33
from github_rest_cli.utils import rich_output, rprint
44

55

@@ -40,21 +40,34 @@ def build_url(*segments: str) -> str:
4040
return f"{base}/{path}"
4141

4242

43-
def fetch_user():
43+
def fetch_user() -> str:
44+
headers = get_headers()
4445
url = build_url("user")
45-
response = request_with_handling("GET", url, headers=HEADERS)
46+
response = request_with_handling("GET", url, headers=headers)
4647
if response:
4748
data = response.json()
4849
return data.get("login")
4950
return None
5051

5152

52-
def get_repository(owner: str, name: str, org: str = None):
53+
def get_repository(name: str, org: str = None):
54+
owner = fetch_user()
55+
headers = get_headers()
5356
url = build_url("repos", org or owner, name)
54-
response = request_with_handling("GET", url, headers=HEADERS)
57+
response = request_with_handling(
58+
"GET",
59+
url,
60+
headers=headers,
61+
error_msg={
62+
401: "Unauthorized access. Please check your token or credentials.",
63+
404: "The requested repository does not exist.",
64+
},
65+
)
66+
5567
if response:
5668
data = response.json()
5769
rprint(data)
70+
return None
5871

5972

6073
def create_repository(owner: str, name: str, visibility: str, org: str = None):
@@ -67,12 +80,13 @@ def create_repository(owner: str, name: str, visibility: str, org: str = None):
6780
if visibility == "private":
6881
data["private"] = True
6982

83+
headers = get_headers()
7084
url = build_url("orgs", org, "repos") if org else build_url("user", "repos")
7185

7286
return request_with_handling(
7387
"POST",
7488
url,
75-
headers=HEADERS,
89+
headers=headers,
7690
json=data,
7791
success_msg=f"Repository successfully created in {owner or org }/{name}",
7892
error_msg={
@@ -83,21 +97,23 @@ def create_repository(owner: str, name: str, visibility: str, org: str = None):
8397

8498

8599
def delete_repository(owner: str, name: str, org: str = None):
100+
headers = get_headers()
86101
url = build_url("repos", org, name) if org else build_url("repos", owner, name)
87102

88103
return request_with_handling(
89104
"DELETE",
90105
url,
91-
headers=HEADERS,
106+
headers=headers,
92107
success_msg=f"Repository sucessfully deleted in {owner or org}/{name}",
93108
error_msg={
94109
403: "The authenticated user does not have sufficient permissions to delete this repository.",
95-
404: "The requested repository was not found.",
110+
404: "The requested repository does not exist.",
96111
},
97112
)
98113

99114

100115
def list_repositories(page: int, property: str, role: str):
116+
headers = get_headers()
101117
url = build_url("user", "repos")
102118

103119
params = {"per_page": page, "sort": property, "type": role}
@@ -106,7 +122,7 @@ def list_repositories(page: int, property: str, role: str):
106122
"GET",
107123
url,
108124
params=params,
109-
headers=HEADERS,
125+
headers=headers,
110126
error_msg={401: "Unauthorized access. Please check your token or credentials."},
111127
)
112128

@@ -121,6 +137,7 @@ def list_repositories(page: int, property: str, role: str):
121137
def dependabot_security(owner: str, name: str, enabled: bool, org: str = None):
122138
is_enabled = bool(enabled)
123139

140+
headers = get_headers()
124141
url = build_url("repos", org, name) if org else build_url("repos", owner, name)
125142
security_urls = ["vulnerability-alerts", "automated-security-fixes"]
126143

@@ -130,7 +147,7 @@ def dependabot_security(owner: str, name: str, enabled: bool, org: str = None):
130147
request_with_handling(
131148
"PUT",
132149
url=full_url,
133-
headers=HEADERS,
150+
headers=headers,
134151
success_msg=f"Enabled {endpoint}",
135152
error_msg={
136153
401: "Unauthorized. Please check your credentials.",
@@ -141,13 +158,14 @@ def dependabot_security(owner: str, name: str, enabled: bool, org: str = None):
141158
request_with_handling(
142159
"DELETE",
143160
url=full_url,
144-
headers=HEADERS,
161+
headers=headers,
145162
success_msg=f"Dependabot has been disabled on repository {owner or org}/{name}.",
146163
error_msg={401: "Unauthorized. Please check your credentials."},
147164
)
148165

149166

150167
def deployment_environment(owner: str, name: str, env: str, org: str = None):
168+
headers = get_headers()
151169
url = (
152170
build_url("repos", org, name, "environments", env)
153171
if org
@@ -157,7 +175,7 @@ def deployment_environment(owner: str, name: str, env: str, org: str = None):
157175
return request_with_handling(
158176
"PUT",
159177
url,
160-
headers=HEADERS,
178+
headers=headers,
161179
success_msg=f"Environment {env} has been created successfully in {owner or org}/{name}.",
162180
error_msg={
163181
422: f"Failed to create repository enviroment {owner or org}/{name}"

src/github_rest_cli/config.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
from dynaconf import Dynaconf, Validator
22

33
settings = Dynaconf(
4-
# Default environment variables prefix.
54
envvar_prefix="GITHUB",
6-
# Source configuration files.
75
settings_files=["../../settings.toml", "../../.secrets.toml"],
86
environments=["development", "testing", "production"],
97
env_switcher="SET_ENV",
10-
# The script will not work if the variables
11-
# defined in the Validator class are not defined.
12-
validators=[
13-
Validator(
14-
"AUTH_TOKEN",
15-
must_exist=True,
16-
messages={
17-
"must_exist_true": "Environment variable GITHUB_AUTH_TOKEN is not set. Please set it and try again."
18-
},
19-
),
20-
],
8+
)
9+
10+
# The CLI will not work if the variable
11+
# defined in the Validator class are not defined.
12+
AUTH_TOKEN_VALIDATOR = Validator(
13+
"AUTH_TOKEN",
14+
must_exist=True,
15+
messages={
16+
"must_exist_true": "Environment variable GITHUB_AUTH_TOKEN is not set. Please set it and try again."
17+
},
2118
)
2219

2320
# `envvar_prefix` = export envvars with `export DYNACONF_FOO=bar`.

src/github_rest_cli/globals.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1-
from github_rest_cli.config import settings
1+
from github_rest_cli.config import settings, AUTH_TOKEN_VALIDATOR
2+
from dynaconf.base import ValidationError
3+
import logging
4+
25

36
GITHUB_URL = "https://api.github.com"
4-
GITHUB_TOKEN = f"{settings.AUTH_TOKEN}"
7+
logger = logging.getLogger(__name__)
8+
9+
10+
def get_headers():
11+
try:
12+
AUTH_TOKEN_VALIDATOR.validate(settings)
13+
except ValidationError as e:
14+
logger.error(str(e))
15+
raise SystemExit(1)
516

6-
HEADERS = {
7-
"Accept": "application/vnd.github+json",
8-
"X-GitHub-Api-Version": "2022-11-28",
9-
"Authorization": f"token {GITHUB_TOKEN}",
10-
}
17+
return {
18+
"Accept": "application/vnd.github+json",
19+
"X-GitHub-Api-Version": "2022-11-28",
20+
"Authorization": f"token {settings.AUTH_TOKEN}",
21+
}

0 commit comments

Comments
 (0)