Skip to content

Commit 6e700a5

Browse files
author
Douglas
committed
Updated setup process to include repo name detection and automation.
1 parent 59d34d9 commit 6e700a5

File tree

1 file changed

+72
-38
lines changed

1 file changed

+72
-38
lines changed

main.py

Lines changed: 72 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,72 +12,107 @@
1212
GITHUB_TOKEN = None
1313

1414
# Define the current version of the app
15-
APP_VERSION = "1.0.1a"
15+
APP_VERSION = "1.0.2a"
16+
17+
# Path to the config file
18+
CONFIG_FILE = "config.json"
19+
20+
# Initialize the repository name
21+
REPO_NAME = None
1622

1723
def get_repo_name():
1824
"""
19-
Dynamically retrieves the repository name from the Git remote URL.
25+
Dynamically retrieves the repository name from the Git remote URL
26+
or prompts the user for input if automatic detection fails.
2027
"""
2128
try:
2229
# Run 'git remote get-url origin' to get the remote URL
2330
remote_url = subprocess.check_output(
2431
["git", "remote", "get-url", "origin"], text=True
2532
).strip()
2633

27-
# Extract the repo name from the URL
34+
# Extract the repo name
2835
if remote_url.startswith("https://") or remote_url.startswith("http://"):
2936
repo_name = remote_url.split("/")[-2] + "/" + remote_url.split("/")[-1].replace(".git", "")
3037
elif remote_url.startswith("git@"):
3138
repo_name = remote_url.split(":")[-1].replace(".git", "")
3239
else:
3340
repo_name = None
34-
if repo_name is not None:
35-
print(f"Detected repository: {repo_name}")
36-
else:
37-
print(f"Could not detect repo name.")
38-
return repo_name
3941
except subprocess.CalledProcessError:
40-
print("Error: Unable to detect repository name. Ensure you're in a Git repository.")
41-
return None
42-
43-
# Use the dynamic repo name
44-
REPO_NAME = get_repo_name()
42+
repo_name = None
4543

44+
# Confirm with the user or ask for manual input
45+
if repo_name:
46+
print(f"Detected repository: {repo_name}")
47+
user_input = input("Is this correct? (yes/no): ").strip().lower()
48+
if user_input != "yes":
49+
repo_name = None
4650

47-
# Hardcoded repository name
48-
# REPO_NAME = "RedNeckSnailSpit/IssueAutomationTest"
51+
if not repo_name:
52+
repo_name = input("Please enter the repository name (e.g., owner/repo): ").strip()
4953

50-
# Path to the config file
51-
CONFIG_FILE = "config.json"
54+
print(f"Final repository name: {repo_name}")
55+
return repo_name
5256

5357
def setup():
5458
"""
55-
Sets up the application by checking for or creating a valid config.json file.
59+
Sets up the application by ensuring a valid config.json file exists
60+
and that required fields (GitHub PAT and repository name) are set.
61+
Prompts the user for any missing information.
5662
"""
57-
if os.path.exists(CONFIG_FILE):
58-
print("Config file already exists. Skipping setup.")
59-
return
63+
global REPO_NAME # Declare as global so we can modify it inside the function
64+
config = {}
6065

61-
print("Config file not found. Starting setup process.")
62-
print(
63-
"You need to provide a Personal Access Token (PAT) from GitHub to use this script.\n"
64-
"1. Go to https://github.com/settings/tokens\n"
65-
"2. Generate a new token with the following required access:\n"
66-
" - repo\n"
67-
"3. Copy the token and paste it below."
68-
)
66+
# Load existing config if available
67+
if os.path.exists(CONFIG_FILE):
68+
with open(CONFIG_FILE) as config_file:
69+
config = json.load(config_file)
70+
71+
# Ensure GitHub PAT exists in config
72+
if "github_token" not in config or not config["github_token"].strip():
73+
print("GitHub Personal Access Token (PAT) is not configured.")
74+
print(
75+
"You need to provide a Personal Access Token (PAT) from GitHub to use this script.\n"
76+
"1. Go to https://github.com/settings/tokens\n"
77+
"2. Generate a new token with the following required access:\n"
78+
" - repo\n"
79+
"3. Copy the token and paste it below."
80+
)
81+
pat = input("Enter your GitHub Personal Access Token (PAT): ").strip()
82+
83+
if not validate_token(pat):
84+
print("Exiting setup due to invalid or insecure token.")
85+
sys.exit(1)
86+
87+
config["github_token"] = pat
88+
print("GitHub PAT validated and added to configuration.")
89+
90+
# Ensure repository name exists in config
91+
if "repo_name" not in config or not config["repo_name"].strip():
92+
print("Repository name is not configured.")
93+
repo_name = get_repo_name()
94+
config["repo_name"] = repo_name
95+
REPO_NAME = repo_name # Set the global REPO_NAME here
96+
else:
97+
REPO_NAME = config["repo_name"] # Load directly from config
6998

70-
pat = input("Enter your GitHub Personal Access Token (PAT): ").strip()
99+
# Save updated config to file
100+
save_config(config)
101+
print("Setup complete. Your configuration has been saved to config.json.")
71102

72-
if not validate_token(pat):
73-
print("Exiting setup due to invalid or insecure token.")
74-
sys.exit(1)
103+
def save_config(data):
104+
"""
105+
Saves data to config.json, preserving existing keys if the file already exists.
106+
"""
107+
config = {}
108+
if os.path.exists(CONFIG_FILE):
109+
with open(CONFIG_FILE) as config_file:
110+
config = json.load(config_file)
75111

76-
# Save the token to config.json
77-
config_data = {"github_token": pat}
112+
config.update(data) # Add or update the provided data
78113
with open(CONFIG_FILE, "w", encoding="utf-8") as config_file:
79-
json.dump(config_data, config_file, ensure_ascii=False)
80-
print("Setup complete. Your PAT has been saved securely to config.json.")
114+
json.dump(config, config_file, ensure_ascii=False)
115+
print("Configuration saved to config.json.")
81116

82117
def validate_token(pat):
83118
"""
@@ -110,7 +145,6 @@ def load_config():
110145
with open(CONFIG_FILE) as config_file:
111146
return json.load(config_file)
112147

113-
114148
def generate_exception_hash(traceback_details):
115149
"""
116150
Generates a unique hash for an exception based on its traceback details.

0 commit comments

Comments
 (0)