|
1 | 1 | import io
|
2 | 2 | import json
|
| 3 | +import os |
| 4 | +import random |
| 5 | +import string |
3 | 6 | import time
|
4 | 7 |
|
5 | 8 | from ruamel.yaml.comments import CommentedSeq
|
@@ -30,7 +33,10 @@ async def generate_honeybee_request_from_change_model_array(
|
30 | 33 | repositories_for_request = {}
|
31 | 34 | primary_principal = None
|
32 | 35 | t = int(time.time())
|
33 |
| - generated_branch_name = f"{user}-{t}" |
| 36 | + suffix = "".join( |
| 37 | + random.choices(string.ascii_lowercase + string.digits, k=10) # nosec |
| 38 | + ) |
| 39 | + generated_branch_name = f"{user}-{t}-{suffix}" |
34 | 40 | policy_name = config.get(
|
35 | 41 | "generate_honeybee_request_from_change_model_array.policy_name",
|
36 | 42 | "self_service_generated",
|
@@ -78,10 +84,26 @@ async def generate_honeybee_request_from_change_model_array(
|
78 | 84 | main_branch_name = repositories_for_request[change.principal.repository_name][
|
79 | 85 | "main_branch_name"
|
80 | 86 | ]
|
81 |
| - git_client.checkout( |
82 |
| - f"origin/{main_branch_name}", change.principal.resource_identifier |
| 87 | + |
| 88 | + change_file_path = os.path.abspath( |
| 89 | + f"{repo.working_dir}/{change.principal.resource_identifier}" |
83 | 90 | )
|
84 |
| - change_file_path = f"{repo.working_dir}/{change.principal.resource_identifier}" |
| 91 | + clone_wd_path = os.path.abspath(repo.working_dir) |
| 92 | + if os.path.commonprefix((clone_wd_path, change_file_path)) != clone_wd_path: |
| 93 | + log.exception( |
| 94 | + f"User attempted to reference a file outside of the repository: {change_file_path} is not within {clone_wd_path}" |
| 95 | + ) |
| 96 | + raise ValueError("Unable to raise change request for this resource") |
| 97 | + |
| 98 | + try: |
| 99 | + git_client.checkout( |
| 100 | + f"origin/{main_branch_name}", "--", change.principal.resource_identifier |
| 101 | + ) |
| 102 | + except Exception: |
| 103 | + log.exception( |
| 104 | + f"Unable to checkout {main_branch_name} for {change.principal.resource_identifier}" |
| 105 | + ) |
| 106 | + raise ValueError("Unable to raise change request for this resource") |
85 | 107 | with open(change_file_path, "r") as f:
|
86 | 108 | yaml_content = yaml.load(f)
|
87 | 109 |
|
|
0 commit comments