diff options
author | 6543 <6543@obermui.de> | 2021-07-13 09:14:14 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-13 08:14:14 +0100 |
commit | b81106be3faadf7821c30b18fb1cec6c27041896 (patch) | |
tree | 4e6198ec7013999bf8c85d79a00c4c14ea55c07a /routers | |
parent | 4ce32c9e93591f2449a388201c323ca193f59c07 (diff) | |
download | gitea-b81106be3faadf7821c30b18fb1cec6c27041896.tar.gz gitea-b81106be3faadf7821c30b18fb1cec6c27041896.zip |
Let branch/tag name be a valid ref to get CI status (#16400)
* fix #16384#
* refactor: move shared helper func to utils package
* extend Tests
* use ctx.Repo.GitRepo if not nil
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/repo/git_ref.go | 18 | ||||
-rw-r--r-- | routers/api/v1/repo/status.go | 35 | ||||
-rw-r--r-- | routers/api/v1/utils/git.go | 61 |
3 files changed, 68 insertions, 46 deletions
diff --git a/routers/api/v1/repo/git_ref.go b/routers/api/v1/repo/git_ref.go index bd43ad4fc8..e304e06740 100644 --- a/routers/api/v1/repo/git_ref.go +++ b/routers/api/v1/repo/git_ref.go @@ -8,8 +8,8 @@ import ( "net/http" "code.gitea.io/gitea/modules/context" - "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/routers/api/v1/utils" ) // GetGitAllRefs get ref or an list all the refs of a repository @@ -73,22 +73,8 @@ func GetGitRefs(ctx *context.APIContext) { getGitRefsInternal(ctx, ctx.Params("*")) } -func getGitRefs(ctx *context.APIContext, filter string) ([]*git.Reference, string, error) { - gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath()) - if err != nil { - return nil, "OpenRepository", err - } - defer gitRepo.Close() - - if len(filter) > 0 { - filter = "refs/" + filter - } - refs, err := gitRepo.GetRefsFiltered(filter) - return refs, "GetRefsFiltered", err -} - func getGitRefsInternal(ctx *context.APIContext, filter string) { - refs, lastMethodName, err := getGitRefs(ctx, filter) + refs, lastMethodName, err := utils.GetGitRefs(ctx, filter) if err != nil { ctx.Error(http.StatusInternalServerError, lastMethodName, err) return diff --git a/routers/api/v1/repo/status.go b/routers/api/v1/repo/status.go index 7ab399b572..95c3f00a72 100644 --- a/routers/api/v1/repo/status.go +++ b/routers/api/v1/repo/status.go @@ -171,39 +171,14 @@ func GetCommitStatusesByRef(ctx *context.APIContext) { // "400": // "$ref": "#/responses/error" - filter := ctx.Params("ref") - if len(filter) == 0 { - ctx.Error(http.StatusBadRequest, "ref not given", nil) + filter := utils.ResolveRefOrSha(ctx, ctx.Params("ref")) + if ctx.Written() { return } - for _, reftype := range []string{"heads", "tags"} { //Search branches and tags - refSHA, lastMethodName, err := searchRefCommitByType(ctx, reftype, filter) - if err != nil { - ctx.Error(http.StatusInternalServerError, lastMethodName, err) - return - } - if refSHA != "" { - filter = refSHA - break - } - - } - getCommitStatuses(ctx, filter) //By default filter is maybe the raw SHA } -func searchRefCommitByType(ctx *context.APIContext, refType, filter string) (string, string, error) { - refs, lastMethodName, err := getGitRefs(ctx, refType+"/"+filter) //Search by type - if err != nil { - return "", lastMethodName, err - } - if len(refs) > 0 { - return refs[0].Object.String(), "", nil //Return found SHA - } - return "", "", nil -} - func getCommitStatuses(ctx *context.APIContext, sha string) { if len(sha) == 0 { ctx.Error(http.StatusBadRequest, "ref/sha not given", nil) @@ -272,11 +247,11 @@ func GetCombinedCommitStatusByRef(ctx *context.APIContext) { // "400": // "$ref": "#/responses/error" - sha := ctx.Params("ref") - if len(sha) == 0 { - ctx.Error(http.StatusBadRequest, "ref/sha not given", nil) + sha := utils.ResolveRefOrSha(ctx, ctx.Params("ref")) + if ctx.Written() { return } + repo := ctx.Repo.Repository statuses, err := models.GetLatestCommitStatus(repo.ID, sha, utils.GetListOptions(ctx)) diff --git a/routers/api/v1/utils/git.go b/routers/api/v1/utils/git.go new file mode 100644 index 0000000000..ac68c3625d --- /dev/null +++ b/routers/api/v1/utils/git.go @@ -0,0 +1,61 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package utils + +import ( + "net/http" + + "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/git" +) + +// ResolveRefOrSha resolve ref to sha if exist +func ResolveRefOrSha(ctx *context.APIContext, ref string) string { + if len(ref) == 0 { + ctx.Error(http.StatusBadRequest, "ref not given", nil) + return "" + } + + // Search branches and tags + for _, refType := range []string{"heads", "tags"} { + refSHA, lastMethodName, err := searchRefCommitByType(ctx, refType, ref) + if err != nil { + ctx.Error(http.StatusInternalServerError, lastMethodName, err) + return "" + } + if refSHA != "" { + return refSHA + } + } + return ref +} + +// GetGitRefs return git references based on filter +func GetGitRefs(ctx *context.APIContext, filter string) ([]*git.Reference, string, error) { + if ctx.Repo.GitRepo == nil { + var err error + ctx.Repo.GitRepo, err = git.OpenRepository(ctx.Repo.Repository.RepoPath()) + if err != nil { + return nil, "OpenRepository", err + } + defer ctx.Repo.GitRepo.Close() + } + if len(filter) > 0 { + filter = "refs/" + filter + } + refs, err := ctx.Repo.GitRepo.GetRefsFiltered(filter) + return refs, "GetRefsFiltered", err +} + +func searchRefCommitByType(ctx *context.APIContext, refType, filter string) (string, string, error) { + refs, lastMethodName, err := GetGitRefs(ctx, refType+"/"+filter) //Search by type + if err != nil { + return "", lastMethodName, err + } + if len(refs) > 0 { + return refs[0].Object.String(), "", nil //Return found SHA + } + return "", "", nil +} |