forked from forgejo/forgejo
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
This commit is contained in:
parent
4ce32c9e93
commit
b81106be3f
4 changed files with 92 additions and 60 deletions
|
@ -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))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue