@@ -7,11 +7,7 @@ import (
77 e "errors"
88 "github.com/actiontech/sqle/sqle/api/controller"
99 "github.com/actiontech/sqle/sqle/errors"
10- "github.com/actiontech/sqle/sqle/utils"
11- "github.com/go-git/go-git/v5/plumbing"
12- "github.com/go-git/go-git/v5/plumbing/storer"
1310 "github.com/labstack/echo/v4"
14- "net/http"
1511)
1612
1713var (
@@ -72,77 +68,3 @@ func testCodingAuditConfigV1(c echo.Context) error {
7268func getScheduledTaskDefaultOptionV1 (c echo.Context ) error {
7369 return controller .JSONBaseErrorReq (c , errCommunityEditionDoesNotSupportScheduledNotify )
7470}
75-
76- func testGitConnectionV1 (c echo.Context ) error {
77- request := new (TestGitConnectionReqV1 )
78- if err := controller .BindAndValidateReq (c , request ); err != nil {
79- return controller .JSONBaseErrorReq (c , err )
80- }
81- repository , _ , cleanup , err := utils .CloneGitRepository (c .Request ().Context (), request .GitHttpUrl , request .GitUserName , request .GitUserPassword )
82- if err != nil {
83- return c .JSON (http .StatusOK , & TestGitConnectionResV1 {
84- BaseRes : controller .NewBaseReq (nil ),
85- Data : TestGitConnectionResDataV1 {
86- IsConnectedSuccess : false ,
87- ErrorMessage : err .Error (),
88- },
89- })
90- }
91- defer func () {
92- cleanupError := cleanup ()
93- if cleanupError != nil {
94- c .Logger ().Errorf ("cleanup git repository failed, err: %v" , cleanupError )
95- }
96- }()
97- references , err := repository .References ()
98- if err != nil {
99- return c .JSON (http .StatusOK , & TestGitConnectionResV1 {
100- BaseRes : controller .NewBaseReq (nil ),
101- Data : TestGitConnectionResDataV1 {
102- IsConnectedSuccess : false ,
103- ErrorMessage : err .Error (),
104- },
105- })
106- }
107- branches , err := getBranches (references )
108- return c .JSON (http .StatusOK , & TestGitConnectionResV1 {
109- BaseRes : controller .NewBaseReq (nil ),
110- Data : TestGitConnectionResDataV1 {
111- IsConnectedSuccess : true ,
112- Branches : branches ,
113- },
114- })
115- }
116-
117- func getBranches (references storer.ReferenceIter ) ([]string , error ) {
118- branches := make ([]string , 0 )
119- err := references .ForEach (func (ref * plumbing.Reference ) error {
120- if ref .Type () == plumbing .HashReference {
121- branches = append (branches , ref .Name ().Short ())
122- }
123- return nil
124- })
125- if err != nil {
126- return branches , err
127- }
128- if len (branches ) < 2 {
129- return branches , nil
130- }
131- // 第一个元素确认了默认分支名,需要把可以checkout的默认分支提到第一个元素
132- defaultBranch := "origin/" + branches [0 ]
133- defaultBranchIndex := - 1
134- for i , branch := range branches {
135- if branch == defaultBranch {
136- defaultBranchIndex = i
137- break
138- }
139- }
140- if defaultBranchIndex == - 1 {
141- return branches , nil
142- }
143- //1. 根据第一个元素,找到其余元素中的默认分支
144- //2. 如果找到,将找到的默认分支名移到第一个元素,并且删除原来的第一个元素。
145- branches [0 ], branches [defaultBranchIndex ] = branches [defaultBranchIndex ], branches [0 ]
146- branches = append (branches [:defaultBranchIndex ], branches [defaultBranchIndex + 1 :]... )
147- return branches , nil
148- }
0 commit comments