@@ -646,10 +646,22 @@ func (apiServer *HelixAPIServer) browseGitRepositoryTree(w http.ResponseWriter,
646646 }
647647
648648 branch := r .URL .Query ().Get ("branch" )
649+ if branch == "" {
650+ repository , err := apiServer .gitRepositoryService .GetRepository (r .Context (), repoID )
651+ if err != nil {
652+ log .Error ().Err (err ).Str ("repo_id" , repoID ).Msg ("Failed to get repository for default branch" )
653+ http .Error (w , fmt .Sprintf ("Failed to get repository: %s" , err .Error ()), http .StatusInternalServerError )
654+ return
655+ }
656+ branch = repository .DefaultBranch
657+ if branch == "" {
658+ branch = "main"
659+ }
660+ }
649661
650662 entries , err := apiServer .gitRepositoryService .BrowseTree (r .Context (), repoID , path , branch )
651663 if err != nil {
652- log .Error ().Err (err ).Str ("repo_id" , repoID ).Str ("path" , path ).Msg ("Failed to browse repository tree" )
664+ log .Error ().Err (err ).Str ("repo_id" , repoID ).Str ("path" , path ).Str ( "branch" , branch ). Msg ("Failed to browse repository tree" )
653665 http .Error (w , fmt .Sprintf ("Failed to browse repository: %s" , err .Error ()), http .StatusInternalServerError )
654666 return
655667 }
@@ -722,7 +734,19 @@ func (apiServer *HelixAPIServer) getGitRepositoryFileContents(w http.ResponseWri
722734 return
723735 }
724736
725- branch := r .URL .Query ().Get ("branch" ) // Optional branch parameter
737+ branch := r .URL .Query ().Get ("branch" )
738+ if branch == "" {
739+ repository , err := apiServer .gitRepositoryService .GetRepository (r .Context (), repoID )
740+ if err != nil {
741+ log .Error ().Err (err ).Str ("repo_id" , repoID ).Msg ("Failed to get repository for default branch" )
742+ http .Error (w , fmt .Sprintf ("Failed to get repository: %s" , err .Error ()), http .StatusInternalServerError )
743+ return
744+ }
745+ branch = repository .DefaultBranch
746+ if branch == "" {
747+ branch = "main"
748+ }
749+ }
726750
727751 content , err := apiServer .gitRepositoryService .GetFileContents (r .Context (), repoID , path , branch )
728752 if err != nil {
@@ -798,9 +822,11 @@ func (s *HelixAPIServer) createOrUpdateGitRepositoryFileContents(w http.Response
798822
799823 if request .Branch == "" {
800824 request .Branch = existing .DefaultBranch
801- if request .Branch == "" {
802- request .Branch = "main"
803- }
825+ }
826+
827+ if request .Branch == "" {
828+ writeErrResponse (w , system .NewHTTPError400 ("either specify a branch or set the default branch for the repository" ), http .StatusBadRequest )
829+ return
804830 }
805831
806832 if request .Message == "" {
@@ -849,8 +875,7 @@ func (s *HelixAPIServer) createOrUpdateGitRepositoryFileContents(w http.Response
849875 Content : fileContent ,
850876 }
851877
852- w .Header ().Set ("Content-Type" , "application/json" )
853- json .NewEncoder (w ).Encode (response )
878+ writeResponse (w , response , http .StatusOK )
854879}
855880
856881// pushPullGitRepository pulls latest commits and pushes local commits to the remote repository
@@ -905,9 +930,11 @@ func (s *HelixAPIServer) pushPullGitRepository(w http.ResponseWriter, r *http.Re
905930 }
906931 }
907932
908- err = s .gitRepositoryService .PushPullRequest (r .Context (), repoID , branchName )
933+ force := r .URL .Query ().Get ("force" ) == "true"
934+
935+ err = s .gitRepositoryService .PushPullRequest (r .Context (), repoID , branchName , force )
909936 if err != nil {
910- log .Error ().Err (err ).Str ("repo_id" , repoID ).Str ("branch" , branchName ).Msg ("Failed to push/pull repository" )
937+ log .Error ().Err (err ).Str ("repo_id" , repoID ).Str ("branch" , branchName ).Bool ( "force" , force ). Msg ("Failed to push/pull repository" )
911938 http .Error (w , fmt .Sprintf ("Failed to push/pull repository: %s" , err .Error ()), http .StatusInternalServerError )
912939 return
913940 }
@@ -1016,3 +1043,85 @@ func (s *HelixAPIServer) listGitRepositoryCommits(w http.ResponseWriter, r *http
10161043 w .Header ().Set ("Content-Type" , "application/json" )
10171044 json .NewEncoder (w ).Encode (response )
10181045}
1046+
1047+ // createGitRepositoryBranch creates a new branch in the repository
1048+ // @Summary Create branch
1049+ // @Description Create a new branch in a repository
1050+ // @ID createGitRepositoryBranch
1051+ // @Tags git-repositories
1052+ // @Accept json
1053+ // @Produce json
1054+ // @Param id path string true "Repository ID"
1055+ // @Param request body types.CreateBranchRequest true "Create branch request"
1056+ // @Success 200 {object} types.CreateBranchResponse
1057+ // @Failure 400 {object} types.APIError
1058+ // @Failure 404 {object} types.APIError
1059+ // @Failure 500 {object} types.APIError
1060+ // @Router /api/v1/git/repositories/{id}/branches [post]
1061+ // @Security BearerAuth
1062+ func (s * HelixAPIServer ) createGitRepositoryBranch (w http.ResponseWriter , r * http.Request ) {
1063+ vars := mux .Vars (r )
1064+ repoID := vars ["id" ]
1065+ if repoID == "" {
1066+ http .Error (w , "Repository ID is required" , http .StatusBadRequest )
1067+ return
1068+ }
1069+
1070+ user := getRequestUser (r )
1071+
1072+ repository , err := s .gitRepositoryService .GetRepository (r .Context (), repoID )
1073+ if err != nil {
1074+ log .Error ().Err (err ).Str ("repo_id" , repoID ).Msg ("Failed to get git repository" )
1075+ http .Error (w , fmt .Sprintf ("Repository not found: %s" , err .Error ()), http .StatusNotFound )
1076+ return
1077+ }
1078+
1079+ if repository .OrganizationID != "" {
1080+ _ , err := s .authorizeOrgMember (r .Context (), user , repository .OrganizationID )
1081+ if err != nil {
1082+ writeErrResponse (w , err , http .StatusForbidden )
1083+ return
1084+ }
1085+ }
1086+
1087+ if repository .OwnerID != user .ID {
1088+ writeErrResponse (w , system .NewHTTPError403 ("unauthorized" ), http .StatusForbidden )
1089+ return
1090+ }
1091+
1092+ var request types.CreateBranchRequest
1093+ if err := json .NewDecoder (r .Body ).Decode (& request ); err != nil {
1094+ http .Error (w , fmt .Sprintf ("Invalid request format: %s" , err .Error ()), http .StatusBadRequest )
1095+ return
1096+ }
1097+
1098+ if request .BranchName == "" {
1099+ http .Error (w , "Branch name is required" , http .StatusBadRequest )
1100+ return
1101+ }
1102+
1103+ baseBranch := request .BaseBranch
1104+ if baseBranch == "" {
1105+ baseBranch = repository .DefaultBranch
1106+ if baseBranch == "" {
1107+ baseBranch = "main"
1108+ }
1109+ }
1110+
1111+ err = s .gitRepositoryService .CreateBranch (r .Context (), repoID , request .BranchName , baseBranch )
1112+ if err != nil {
1113+ log .Error ().Err (err ).Str ("repo_id" , repoID ).Str ("branch" , request .BranchName ).Msg ("Failed to create branch" )
1114+ http .Error (w , fmt .Sprintf ("Failed to create branch: %s" , err .Error ()), http .StatusInternalServerError )
1115+ return
1116+ }
1117+
1118+ response := & types.CreateBranchResponse {
1119+ RepositoryID : repoID ,
1120+ BranchName : request .BranchName ,
1121+ BaseBranch : baseBranch ,
1122+ Message : "Branch created successfully" ,
1123+ }
1124+
1125+ w .Header ().Set ("Content-Type" , "application/json" )
1126+ json .NewEncoder (w ).Encode (response )
1127+ }
0 commit comments