88from rest_framework .response import Response
99
1010from backend .core .services import git_repo_config_service
11+ from backend .core .services .git_service import get_git_service
1112from backend .core .utils import handle_http_request
1213from backend .utils .constants import HTTPMethods
14+ from rbac .factory import handle_permission
1315
1416logger = logging .getLogger (__name__ )
1517
@@ -26,6 +28,7 @@ def get_git_config(request: Request, project_id: str) -> Response:
2628
2729@api_view ([HTTPMethods .POST ])
2830@handle_http_request
31+ @handle_permission
2932def save_git_config (request : Request , project_id : str ) -> Response :
3033 """Create or update git repository configuration for a project."""
3134 config = git_repo_config_service .save_config (project_id = project_id , config_data = request .data )
@@ -34,6 +37,7 @@ def save_git_config(request: Request, project_id: str) -> Response:
3437
3538@api_view ([HTTPMethods .DELETE ])
3639@handle_http_request
40+ @handle_permission
3741def delete_git_config (request : Request , project_id : str ) -> Response :
3842 """Remove git repository configuration (disable versioning)."""
3943 git_repo_config_service .delete_config (project_id = project_id )
@@ -45,6 +49,7 @@ def delete_git_config(request: Request, project_id: str) -> Response:
4549
4650@api_view ([HTTPMethods .POST ])
4751@handle_http_request
52+ @handle_permission
4853def test_git_connection (request : Request , project_id : str ) -> Response :
4954 """Test connection to a git repository."""
5055 result = git_repo_config_service .test_connection (project_id = project_id , config_data = request .data )
@@ -57,3 +62,63 @@ def get_available_repos(request: Request, project_id: str) -> Response:
5762 """List repos already configured in the organization."""
5863 repos = git_repo_config_service .get_available_repos ()
5964 return Response (data = {"status" : "success" , "data" : repos }, status = status .HTTP_200_OK )
65+
66+
67+ @api_view ([HTTPMethods .GET ])
68+ @handle_http_request
69+ def list_branches (request : Request , project_id : str ) -> Response :
70+ """List branches in the project's configured git repository."""
71+ from backend .core .models .git_repo_config import GitRepoConfig
72+
73+ config = GitRepoConfig .objects .filter (
74+ project_id = project_id , is_deleted = False , is_active = True ,
75+ ).first ()
76+ if not config :
77+ return Response (
78+ data = {"status" : "failed" , "error_message" : "No git configuration found for this project." },
79+ status = status .HTTP_404_NOT_FOUND ,
80+ )
81+ service = get_git_service (config )
82+ branches = service .list_branches ()
83+ return Response (data = {"status" : "success" , "data" : {"branches" : branches }}, status = status .HTTP_200_OK )
84+
85+
86+ @api_view ([HTTPMethods .POST ])
87+ @handle_http_request
88+ @handle_permission
89+ def enable_pr_workflow (request : Request , project_id : str ) -> Response :
90+ """Enable PR workflow for a project's git configuration."""
91+ payload = request .data
92+ pr_mode = payload .get ("pr_mode" , "auto" )
93+ pr_base_branch = payload .get ("pr_base_branch" , "main" )
94+ pr_branch_prefix = payload .get ("pr_branch_prefix" , "visitran/" )
95+
96+ from backend .core .models .git_repo_config import GitRepoConfig
97+
98+ config = GitRepoConfig .objects .filter (
99+ project_id = project_id , is_deleted = False , is_active = True ,
100+ ).first ()
101+ if not config :
102+ return Response (
103+ data = {"status" : "failed" , "error_message" : "No git configuration found for this project." },
104+ status = status .HTTP_404_NOT_FOUND ,
105+ )
106+
107+ # Validate that the base branch exists
108+ service = get_git_service (config )
109+ branch_info = service .get_branch (pr_base_branch )
110+ if not branch_info :
111+ return Response (
112+ data = {"status" : "failed" , "error_message" : f"Branch '{ pr_base_branch } ' not found in repository." },
113+ status = status .HTTP_400_BAD_REQUEST ,
114+ )
115+
116+ updated = git_repo_config_service .save_config (
117+ project_id = project_id ,
118+ config_data = {
119+ "pr_mode" : pr_mode ,
120+ "pr_base_branch" : pr_base_branch ,
121+ "pr_branch_prefix" : pr_branch_prefix ,
122+ },
123+ )
124+ return Response (data = {"status" : "success" , "data" : updated }, status = status .HTTP_200_OK )
0 commit comments