Skip to content

Commit cf3bb43

Browse files
committed
refactor: move shared helper func to utils package
1 parent abf849e commit cf3bb43

File tree

3 files changed

+63
-50
lines changed

3 files changed

+63
-50
lines changed

routers/api/v1/repo/git_ref.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"net/http"
99

1010
"code.gitea.io/gitea/modules/context"
11-
"code.gitea.io/gitea/modules/git"
1211
api "code.gitea.io/gitea/modules/structs"
12+
"code.gitea.io/gitea/routers/api/v1/utils"
1313
)
1414

1515
// GetGitAllRefs get ref or an list all the refs of a repository
@@ -73,22 +73,8 @@ func GetGitRefs(ctx *context.APIContext) {
7373
getGitRefsInternal(ctx, ctx.Params("*"))
7474
}
7575

76-
func getGitRefs(ctx *context.APIContext, filter string) ([]*git.Reference, string, error) {
77-
gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
78-
if err != nil {
79-
return nil, "OpenRepository", err
80-
}
81-
defer gitRepo.Close()
82-
83-
if len(filter) > 0 {
84-
filter = "refs/" + filter
85-
}
86-
refs, err := gitRepo.GetRefsFiltered(filter)
87-
return refs, "GetRefsFiltered", err
88-
}
89-
9076
func getGitRefsInternal(ctx *context.APIContext, filter string) {
91-
refs, lastMethodName, err := getGitRefs(ctx, filter)
77+
refs, lastMethodName, err := utils.GetGitRefs(ctx, filter)
9278
if err != nil {
9379
ctx.Error(http.StatusInternalServerError, lastMethodName, err)
9480
return

routers/api/v1/repo/status.go

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -171,46 +171,14 @@ func GetCommitStatusesByRef(ctx *context.APIContext) {
171171
// "400":
172172
// "$ref": "#/responses/error"
173173

174-
filter := ResolveRefOrSha(ctx, ctx.Params("ref"))
174+
filter := utils.ResolveRefOrSha(ctx, ctx.Params("ref"))
175175
if ctx.Written() {
176176
return
177177
}
178178

179179
getCommitStatuses(ctx, filter) //By default filter is maybe the raw SHA
180180
}
181181

182-
// ResolveRefOrSha resolve ref to sha if exist
183-
func ResolveRefOrSha(ctx *context.APIContext, ref string) string {
184-
if len(ref) == 0 {
185-
ctx.Error(http.StatusBadRequest, "ref not given", nil)
186-
return ""
187-
}
188-
189-
// Search branches and tags
190-
for _, refType := range []string{"heads", "tags"} {
191-
refSHA, lastMethodName, err := searchRefCommitByType(ctx, refType, ref)
192-
if err != nil {
193-
ctx.Error(http.StatusInternalServerError, lastMethodName, err)
194-
return ""
195-
}
196-
if refSHA != "" {
197-
return refSHA
198-
}
199-
}
200-
return ref
201-
}
202-
203-
func searchRefCommitByType(ctx *context.APIContext, refType, filter string) (string, string, error) {
204-
refs, lastMethodName, err := getGitRefs(ctx, refType+"/"+filter) //Search by type
205-
if err != nil {
206-
return "", lastMethodName, err
207-
}
208-
if len(refs) > 0 {
209-
return refs[0].Object.String(), "", nil //Return found SHA
210-
}
211-
return "", "", nil
212-
}
213-
214182
func getCommitStatuses(ctx *context.APIContext, sha string) {
215183
if len(sha) == 0 {
216184
ctx.Error(http.StatusBadRequest, "ref/sha not given", nil)
@@ -279,7 +247,7 @@ func GetCombinedCommitStatusByRef(ctx *context.APIContext) {
279247
// "400":
280248
// "$ref": "#/responses/error"
281249

282-
sha := ResolveRefOrSha(ctx, ctx.Params("ref"))
250+
sha := utils.ResolveRefOrSha(ctx, ctx.Params("ref"))
283251
if ctx.Written() {
284252
return
285253
}

routers/api/v1/utils/git.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package utils
6+
7+
import (
8+
"net/http"
9+
10+
"code.gitea.io/gitea/modules/context"
11+
"code.gitea.io/gitea/modules/git"
12+
)
13+
14+
// ResolveRefOrSha resolve ref to sha if exist
15+
func ResolveRefOrSha(ctx *context.APIContext, ref string) string {
16+
if len(ref) == 0 {
17+
ctx.Error(http.StatusBadRequest, "ref not given", nil)
18+
return ""
19+
}
20+
21+
// Search branches and tags
22+
for _, refType := range []string{"heads", "tags"} {
23+
refSHA, lastMethodName, err := searchRefCommitByType(ctx, refType, ref)
24+
if err != nil {
25+
ctx.Error(http.StatusInternalServerError, lastMethodName, err)
26+
return ""
27+
}
28+
if refSHA != "" {
29+
return refSHA
30+
}
31+
}
32+
return ref
33+
}
34+
35+
// GetGitRefs return git references based on filter
36+
func GetGitRefs(ctx *context.APIContext, filter string) ([]*git.Reference, string, error) {
37+
gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
38+
if err != nil {
39+
return nil, "OpenRepository", err
40+
}
41+
defer gitRepo.Close()
42+
43+
if len(filter) > 0 {
44+
filter = "refs/" + filter
45+
}
46+
refs, err := gitRepo.GetRefsFiltered(filter)
47+
return refs, "GetRefsFiltered", err
48+
}
49+
50+
func searchRefCommitByType(ctx *context.APIContext, refType, filter string) (string, string, error) {
51+
refs, lastMethodName, err := GetGitRefs(ctx, refType+"/"+filter) //Search by type
52+
if err != nil {
53+
return "", lastMethodName, err
54+
}
55+
if len(refs) > 0 {
56+
return refs[0].Object.String(), "", nil //Return found SHA
57+
}
58+
return "", "", nil
59+
}

0 commit comments

Comments
 (0)