From 28b91346079fdceb6316f9ced9ace1e7ef708964 Mon Sep 17 00:00:00 2001 From: amarkotasky Date: Tue, 28 May 2024 10:00:57 +0200 Subject: [PATCH] OPS-19814 pr agent code review changes --- pr_agent/servers/launch_github_app.py | 52 +++------------------------ 1 file changed, 5 insertions(+), 47 deletions(-) diff --git a/pr_agent/servers/launch_github_app.py b/pr_agent/servers/launch_github_app.py index a19abe229..4a6f0a68f 100644 --- a/pr_agent/servers/launch_github_app.py +++ b/pr_agent/servers/launch_github_app.py @@ -25,57 +25,15 @@ def get_aws_secrets(secret_name, region_name): secret = get_secret_value_response['SecretString'] return secret -def modify_toml_key(file_path, key_path, new_value): - """ - Modify a key in a TOML file and save the changes. - - :param file_path: Path to the TOML file - :param key_path: The key to modify, specified as a list of nested keys - :param new_value: The new value to set for the key - """ - # Read the existing data from the TOML file - try: - with open(file_path, 'r') as file: - toml_data = toml.load(file) - except FileNotFoundError: - print(f"Error: The file {file_path} does not exist.") - return - except toml.TomlDecodeError as e: - print(f"Error parsing TOML file: {e}") - return - - # Navigate to the key to modify - d = toml_data - if key_path[1] != "private_key": - for key in key_path[:-1]: - d = d.get(key, {}) - print(d) - d[key_path[-1]] = new_value - - # Write the modified data back to the TOML file +def create_toml_file(file_path, secret_content): try: with open("pr_agent/settings/.secrets.toml", 'w') as file: - toml.dump(toml_data, file) - print(f"Successfully updated {key_path} in {file_path}.") + file.write(secret_content) + print(f"Successfully updated in {file_path}.") except Exception as e: print(f"Error writing to TOML file: {e}") - - if key_path[1] == "private_key": - with open("pr_agent/settings/.secrets.toml", 'r') as file: - file_contents = file.read() - - modified_contents = re.sub("", new_value, file_contents) - modified_contents=modified_contents.replace('private_key = "-----BEGIN RSA PRIVATE KEY-----\\n', '''private_key = """\\\n-----BEGIN RSA PRIVATE KEY-----''') - modified_contents=modified_contents.replace('\\n-----END RSA PRIVATE KEY-----\\n', '''\n-----END RSA PRIVATE KEY-----\n""''') - - with open("pr_agent/settings/.secrets.toml", 'w') as file: - file.write(modified_contents) - secret_dict = json.loads(get_aws_secrets(secret_name,region_name)) -modify_toml_key("pr_agent/settings/.secrets_template.toml", ['openai', 'key'], secret_dict.get('open_ai_gpt4_token')) -modify_toml_key("pr_agent/settings/.secrets.toml", ['github', 'app_id'], int(secret_dict.get('app_id'))) -modify_toml_key("pr_agent/settings/.secrets.toml", ['github', 'webhook_secret'], secret_dict.get('webhook_secret')) -modify_toml_key("pr_agent/settings/.secrets.toml", ['github', 'private_key'], secret_dict.get('private_key').replace(" ","\n")) - +secret_toml=secret_dict.get('secret_file') +create_toml_file("pr_agent/settings/.secrets.toml",secret_toml) subprocess.run(['python', 'pr_agent/servers/github_app.py'])