Skip to content

Commit 8ad0547

Browse files
authored
Merge pull request #2954 from actiontech/fix_test_git_connection
fix: move testGitConnectionV1 to configuration.go
2 parents f594360 + 1e1075a commit 8ad0547

File tree

2 files changed

+77
-78
lines changed

2 files changed

+77
-78
lines changed

sqle/api/controller/v1/configuration.go

+77
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package v1
22

33
import (
4+
"github.com/actiontech/sqle/sqle/utils"
5+
"github.com/go-git/go-git/v5/plumbing"
6+
"github.com/go-git/go-git/v5/plumbing/storer"
47
"net/http"
58
"strconv"
69

@@ -430,6 +433,80 @@ func TestGitConnectionV1(c echo.Context) error {
430433
return testGitConnectionV1(c)
431434
}
432435

436+
func testGitConnectionV1(c echo.Context) error {
437+
request := new(TestGitConnectionReqV1)
438+
if err := controller.BindAndValidateReq(c, request); err != nil {
439+
return controller.JSONBaseErrorReq(c, err)
440+
}
441+
repository, _, cleanup, err := utils.CloneGitRepository(c.Request().Context(), request.GitHttpUrl, request.GitUserName, request.GitUserPassword)
442+
if err != nil {
443+
return c.JSON(http.StatusOK, &TestGitConnectionResV1{
444+
BaseRes: controller.NewBaseReq(nil),
445+
Data: TestGitConnectionResDataV1{
446+
IsConnectedSuccess: false,
447+
ErrorMessage: err.Error(),
448+
},
449+
})
450+
}
451+
defer func() {
452+
cleanupError := cleanup()
453+
if cleanupError != nil {
454+
c.Logger().Errorf("cleanup git repository failed, err: %v", cleanupError)
455+
}
456+
}()
457+
references, err := repository.References()
458+
if err != nil {
459+
return c.JSON(http.StatusOK, &TestGitConnectionResV1{
460+
BaseRes: controller.NewBaseReq(nil),
461+
Data: TestGitConnectionResDataV1{
462+
IsConnectedSuccess: false,
463+
ErrorMessage: err.Error(),
464+
},
465+
})
466+
}
467+
branches, err := getBranches(references)
468+
return c.JSON(http.StatusOK, &TestGitConnectionResV1{
469+
BaseRes: controller.NewBaseReq(nil),
470+
Data: TestGitConnectionResDataV1{
471+
IsConnectedSuccess: true,
472+
Branches: branches,
473+
},
474+
})
475+
}
476+
477+
func getBranches(references storer.ReferenceIter) ([]string, error) {
478+
branches := make([]string, 0)
479+
err := references.ForEach(func(ref *plumbing.Reference) error {
480+
if ref.Type() == plumbing.HashReference {
481+
branches = append(branches, ref.Name().Short())
482+
}
483+
return nil
484+
})
485+
if err != nil {
486+
return branches, err
487+
}
488+
if len(branches) < 2 {
489+
return branches, nil
490+
}
491+
// 第一个元素确认了默认分支名,需要把可以checkout的默认分支提到第一个元素
492+
defaultBranch := "origin/" + branches[0]
493+
defaultBranchIndex := -1
494+
for i, branch := range branches {
495+
if branch == defaultBranch {
496+
defaultBranchIndex = i
497+
break
498+
}
499+
}
500+
if defaultBranchIndex == -1 {
501+
return branches, nil
502+
}
503+
//1. 根据第一个元素,找到其余元素中的默认分支
504+
//2. 如果找到,将找到的默认分支名移到第一个元素,并且删除原来的第一个元素。
505+
branches[0], branches[defaultBranchIndex] = branches[defaultBranchIndex], branches[0]
506+
branches = append(branches[:defaultBranchIndex], branches[defaultBranchIndex+1:]...)
507+
return branches, nil
508+
}
509+
433510
type TestGitConnectionReqV1 struct {
434511
GitHttpUrl string `json:"git_http_url" form:"git_http_url" valid:"required"`
435512
GitUserName string `json:"git_user_name" form:"git_user_name" valid:"required"`

sqle/api/controller/v1/configuration_ce.go

-78
Original file line numberDiff line numberDiff line change
@@ -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

1713
var (
@@ -72,77 +68,3 @@ func testCodingAuditConfigV1(c echo.Context) error {
7268
func 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

Comments
 (0)