|
12 | 12 | GITHUB_TOKEN = None
|
13 | 13 |
|
14 | 14 | # 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 |
16 | 22 |
|
17 | 23 | def get_repo_name():
|
18 | 24 | """
|
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. |
20 | 27 | """
|
21 | 28 | try:
|
22 | 29 | # Run 'git remote get-url origin' to get the remote URL
|
23 | 30 | remote_url = subprocess.check_output(
|
24 | 31 | ["git", "remote", "get-url", "origin"], text=True
|
25 | 32 | ).strip()
|
26 | 33 |
|
27 |
| - # Extract the repo name from the URL |
| 34 | + # Extract the repo name |
28 | 35 | if remote_url.startswith("https://") or remote_url.startswith("http://"):
|
29 | 36 | repo_name = remote_url.split("/")[-2] + "/" + remote_url.split("/")[-1].replace(".git", "")
|
30 | 37 | elif remote_url.startswith("git@"):
|
31 | 38 | repo_name = remote_url.split(":")[-1].replace(".git", "")
|
32 | 39 | else:
|
33 | 40 | 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 |
39 | 41 | 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 |
45 | 43 |
|
| 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 |
46 | 50 |
|
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() |
49 | 53 |
|
50 |
| -# Path to the config file |
51 |
| -CONFIG_FILE = "config.json" |
| 54 | + print(f"Final repository name: {repo_name}") |
| 55 | + return repo_name |
52 | 56 |
|
53 | 57 | def setup():
|
54 | 58 | """
|
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. |
56 | 62 | """
|
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 = {} |
60 | 65 |
|
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 |
69 | 98 |
|
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.") |
71 | 102 |
|
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) |
75 | 111 |
|
76 |
| - # Save the token to config.json |
77 |
| - config_data = {"github_token": pat} |
| 112 | + config.update(data) # Add or update the provided data |
78 | 113 | 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.") |
81 | 116 |
|
82 | 117 | def validate_token(pat):
|
83 | 118 | """
|
@@ -110,7 +145,6 @@ def load_config():
|
110 | 145 | with open(CONFIG_FILE) as config_file:
|
111 | 146 | return json.load(config_file)
|
112 | 147 |
|
113 |
| - |
114 | 148 | def generate_exception_hash(traceback_details):
|
115 | 149 | """
|
116 | 150 | Generates a unique hash for an exception based on its traceback details.
|
|
0 commit comments