Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update #9380

Merged
merged 6 commits into from
May 16, 2024
Merged

update #9380

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions consoleme/lib/scm/git/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ async def clone(self, no_checkout=True, depth: Optional[int] = None):
await sync_to_async(git.Git(self.tempdir).clone)(*args, **kwargs)
self.repo = git.Repo(os.path.join(self.tempdir, self.repo_name))
self.repo.config_writer().set_value("user", "name", "ConsoleMe").release()
self.repo.config_writer().set_value("core", "symlinks", "false").release()
if self.git_email:
self.repo.config_writer().set_value(
"user", "email", self.git_email
Expand Down
30 changes: 26 additions & 4 deletions consoleme/lib/templated_resources/requests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import io
import json
import os
import random
import string
import time

from ruamel.yaml.comments import CommentedSeq
Expand Down Expand Up @@ -30,7 +33,10 @@ async def generate_honeybee_request_from_change_model_array(
repositories_for_request = {}
primary_principal = None
t = int(time.time())
generated_branch_name = f"{user}-{t}"
suffix = "".join(
random.choices(string.ascii_lowercase + string.digits, k=10) # nosec
)
generated_branch_name = f"{user}-{t}-{suffix}"
policy_name = config.get(
"generate_honeybee_request_from_change_model_array.policy_name",
"self_service_generated",
Expand Down Expand Up @@ -78,10 +84,26 @@ async def generate_honeybee_request_from_change_model_array(
main_branch_name = repositories_for_request[change.principal.repository_name][
"main_branch_name"
]
git_client.checkout(
f"origin/{main_branch_name}", change.principal.resource_identifier

change_file_path = os.path.abspath(
f"{repo.working_dir}/{change.principal.resource_identifier}"
)
change_file_path = f"{repo.working_dir}/{change.principal.resource_identifier}"
clone_wd_path = os.path.abspath(repo.working_dir)
if os.path.commonprefix((clone_wd_path, change_file_path)) != clone_wd_path:
log.exception(
f"User attempted to reference a file outside of the repository: {change_file_path} is not within {clone_wd_path}"
)
raise ValueError("Unable to raise change request for this resource")

try:
git_client.checkout(
f"origin/{main_branch_name}", "--", change.principal.resource_identifier
)
except Exception:
log.exception(
f"Unable to checkout {main_branch_name} for {change.principal.resource_identifier}"
)
raise ValueError("Unable to raise change request for this resource")
with open(change_file_path, "r") as f:
yaml_content = yaml.load(f)

Expand Down
1 change: 1 addition & 0 deletions tests/handlers/v2/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,7 @@ def test_post_honeybee_request_dry_run(self, mock_git, mock_repo):
- '*'
Sid: admin"""
with patch("builtins.open", mock_open(read_data=template_data)):
mock_repo.return_value.working_dir = "/tmp"
response = self.fetch(
"/api/v2/request",
method="POST",
Expand Down
Loading