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
release/v1.15
6543 2021-07-13 09:14:14 +02:00 committed by GitHub
parent 4ce32c9e93
commit b81106be3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 92 additions and 60 deletions

View File

@ -73,26 +73,36 @@ func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) {
//By SHA //By SHA
req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/"+path.Base(commitURL)+"/statuses") req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/"+path.Base(commitURL)+"/statuses")
testRepoCommitsWithStatus(t, session.MakeRequest(t, req, http.StatusOK), state) reqOne := NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/"+path.Base(commitURL)+"/status")
testRepoCommitsWithStatus(t, session.MakeRequest(t, req, http.StatusOK), session.MakeRequest(t, reqOne, http.StatusOK), state)
//By Ref //By Ref
req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/master/statuses") req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/master/statuses")
testRepoCommitsWithStatus(t, session.MakeRequest(t, req, http.StatusOK), state) reqOne = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/master/status")
testRepoCommitsWithStatus(t, session.MakeRequest(t, req, http.StatusOK), session.MakeRequest(t, reqOne, http.StatusOK), state)
req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/v1.1/statuses") req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/v1.1/statuses")
testRepoCommitsWithStatus(t, session.MakeRequest(t, req, http.StatusOK), state) reqOne = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/v1.1/status")
testRepoCommitsWithStatus(t, session.MakeRequest(t, req, http.StatusOK), session.MakeRequest(t, reqOne, http.StatusOK), state)
} }
func testRepoCommitsWithStatus(t *testing.T, resp *httptest.ResponseRecorder, state string) { func testRepoCommitsWithStatus(t *testing.T, resp, respOne *httptest.ResponseRecorder, state string) {
json := jsoniter.ConfigCompatibleWithStandardLibrary json := jsoniter.ConfigCompatibleWithStandardLibrary
decoder := json.NewDecoder(resp.Body) var statuses []*api.CommitStatus
statuses := []*api.CommitStatus{} assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), &statuses))
assert.NoError(t, decoder.Decode(&statuses)) var status api.CombinedStatus
assert.Len(t, statuses, 1) assert.NoError(t, json.Unmarshal(respOne.Body.Bytes(), &status))
for _, s := range statuses { assert.NotNil(t, status)
assert.Equal(t, api.CommitStatusState(state), s.State)
assert.Equal(t, setting.AppURL+"api/v1/repos/user2/repo1/statuses/65f1bf27bc3bf70f64657658635e66094edbcb4d", s.URL) if assert.Len(t, statuses, 1) {
assert.Equal(t, "http://test.ci/", s.TargetURL) assert.Equal(t, api.CommitStatusState(state), statuses[0].State)
assert.Equal(t, "", s.Description) assert.Equal(t, setting.AppURL+"api/v1/repos/user2/repo1/statuses/65f1bf27bc3bf70f64657658635e66094edbcb4d", statuses[0].URL)
assert.Equal(t, "testci", s.Context) assert.Equal(t, "http://test.ci/", statuses[0].TargetURL)
assert.Equal(t, "", statuses[0].Description)
assert.Equal(t, "testci", statuses[0].Context)
assert.Len(t, status.Statuses, 1)
assert.Equal(t, statuses[0], status.Statuses[0])
assert.Equal(t, "65f1bf27bc3bf70f64657658635e66094edbcb4d", status.SHA)
} }
} }

View File

@ -8,8 +8,8 @@ import (
"net/http" "net/http"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/git"
api "code.gitea.io/gitea/modules/structs" 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 // 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("*")) 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) { func getGitRefsInternal(ctx *context.APIContext, filter string) {
refs, lastMethodName, err := getGitRefs(ctx, filter) refs, lastMethodName, err := utils.GetGitRefs(ctx, filter)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, lastMethodName, err) ctx.Error(http.StatusInternalServerError, lastMethodName, err)
return return

View File

@ -171,39 +171,14 @@ func GetCommitStatusesByRef(ctx *context.APIContext) {
// "400": // "400":
// "$ref": "#/responses/error" // "$ref": "#/responses/error"
filter := ctx.Params("ref") filter := utils.ResolveRefOrSha(ctx, ctx.Params("ref"))
if len(filter) == 0 { if ctx.Written() {
ctx.Error(http.StatusBadRequest, "ref not given", nil)
return 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 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) { func getCommitStatuses(ctx *context.APIContext, sha string) {
if len(sha) == 0 { if len(sha) == 0 {
ctx.Error(http.StatusBadRequest, "ref/sha not given", nil) ctx.Error(http.StatusBadRequest, "ref/sha not given", nil)
@ -272,11 +247,11 @@ func GetCombinedCommitStatusByRef(ctx *context.APIContext) {
// "400": // "400":
// "$ref": "#/responses/error" // "$ref": "#/responses/error"
sha := ctx.Params("ref") sha := utils.ResolveRefOrSha(ctx, ctx.Params("ref"))
if len(sha) == 0 { if ctx.Written() {
ctx.Error(http.StatusBadRequest, "ref/sha not given", nil)
return return
} }
repo := ctx.Repo.Repository repo := ctx.Repo.Repository
statuses, err := models.GetLatestCommitStatus(repo.ID, sha, utils.GetListOptions(ctx)) statuses, err := models.GetLatestCommitStatus(repo.ID, sha, utils.GetListOptions(ctx))

View File

@ -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
}