Skip to content

Commit abf849e

Browse files
committed
1 parent 2f725cb commit abf849e

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

routers/api/v1/repo/status.go

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

174-
filter := ctx.Params("ref")
175-
if len(filter) == 0 {
176-
ctx.Error(http.StatusBadRequest, "ref not given", nil)
174+
filter := ResolveRefOrSha(ctx, ctx.Params("ref"))
175+
if ctx.Written() {
177176
return
178177
}
179178

180-
for _, reftype := range []string{"heads", "tags"} { //Search branches and tags
181-
refSHA, lastMethodName, err := searchRefCommitByType(ctx, reftype, filter)
179+
getCommitStatuses(ctx, filter) //By default filter is maybe the raw SHA
180+
}
181+
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)
182192
if err != nil {
183193
ctx.Error(http.StatusInternalServerError, lastMethodName, err)
184-
return
194+
return ""
185195
}
186196
if refSHA != "" {
187-
filter = refSHA
188-
break
197+
return refSHA
189198
}
190-
191199
}
192-
193-
getCommitStatuses(ctx, filter) //By default filter is maybe the raw SHA
200+
return ref
194201
}
195202

196203
func searchRefCommitByType(ctx *context.APIContext, refType, filter string) (string, string, error) {
@@ -272,11 +279,11 @@ func GetCombinedCommitStatusByRef(ctx *context.APIContext) {
272279
// "400":
273280
// "$ref": "#/responses/error"
274281

275-
sha := ctx.Params("ref")
276-
if len(sha) == 0 {
277-
ctx.Error(http.StatusBadRequest, "ref/sha not given", nil)
282+
sha := ResolveRefOrSha(ctx, ctx.Params("ref"))
283+
if ctx.Written() {
278284
return
279285
}
286+
280287
repo := ctx.Repo.Repository
281288

282289
statuses, err := models.GetLatestCommitStatus(repo.ID, sha, utils.GetListOptions(ctx))

0 commit comments

Comments
 (0)