Skip to content

Commit d3dbfcb

Browse files
committed
extract getArtifactByID into a helper function
1 parent d5520bd commit d3dbfcb

File tree

1 file changed

+24
-34
lines changed

1 file changed

+24
-34
lines changed

routers/api/v1/repo/action.go

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -740,21 +740,8 @@ func GetArtifact(ctx *context.APIContext) {
740740
// "404":
741741
// "$ref": "#/responses/notFound"
742742

743-
artifactID := ctx.PathParamInt64("artifact_id")
744-
745-
art, ok, err := db.GetByID[actions_model.ActionArtifact](ctx, artifactID)
746-
if err != nil {
747-
ctx.Error(http.StatusInternalServerError, err.Error(), err)
748-
return
749-
}
743+
art, ok := getArtifactByID(ctx)
750744
if !ok {
751-
ctx.Error(http.StatusNotFound, "artifact not found", fmt.Errorf("artifact not found"))
752-
return
753-
}
754-
755-
// if artifacts status is not uploaded-confirmed or expired, treat it as not found
756-
if art.Status != int64(actions_model.ArtifactStatusUploadConfirmed) && art.Status != int64(actions_model.ArtifactStatusExpired) {
757-
ctx.Error(http.StatusNotFound, "artifact not found", fmt.Errorf("artifact not found"))
758745
return
759746
}
760747

@@ -805,21 +792,14 @@ func DownloadArtifact(ctx *context.APIContext) {
805792
// "404":
806793
// "$ref": "#/responses/notFound"
807794

808-
artifactID := ctx.PathParamInt64("artifact_id")
809-
810-
art, ok, err := db.GetByID[actions_model.ActionArtifact](ctx, artifactID)
811-
if err != nil {
812-
ctx.Error(http.StatusInternalServerError, err.Error(), err)
813-
return
814-
}
795+
art, ok := getArtifactByID(ctx)
815796
if !ok {
816-
ctx.Error(http.StatusNotFound, "artifact not found", fmt.Errorf("artifact not found"))
817797
return
818798
}
819799

820800
// if artifacts status is not uploaded-confirmed, treat it as not found
821-
if art.Status != int64(actions_model.ArtifactStatusUploadConfirmed) {
822-
ctx.Error(http.StatusNotFound, "artifact not found", fmt.Errorf("artifact not found"))
801+
if art.Status == int64(actions_model.ArtifactStatusExpired) {
802+
ctx.Error(http.StatusNotFound, "artifact has expired", fmt.Errorf("artifact has expired"))
823803
return
824804
}
825805
ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s.zip; filename*=UTF-8''%s.zip", url.PathEscape(art.ArtifactName), art.ArtifactName))
@@ -876,21 +856,14 @@ func DownloadArtifactRaw(ctx *context.APIContext) {
876856
// "404":
877857
// "$ref": "#/responses/notFound"
878858

879-
artifactID := ctx.PathParamInt64("artifact_id")
880-
881-
art, ok, err := db.GetByID[actions_model.ActionArtifact](ctx, artifactID)
882-
if err != nil {
883-
ctx.Error(http.StatusInternalServerError, err.Error(), err)
884-
return
885-
}
859+
art, ok := getArtifactByID(ctx)
886860
if !ok {
887-
ctx.Error(http.StatusNotFound, "artifact not found", fmt.Errorf("artifact not found"))
888861
return
889862
}
890863

891864
// if artifacts status is not uploaded-confirmed, treat it as not found
892-
if art.Status != int64(actions_model.ArtifactStatusUploadConfirmed) {
893-
ctx.Error(http.StatusNotFound, "artifact not found", fmt.Errorf("artifact not found"))
865+
if art.Status == int64(actions_model.ArtifactStatusExpired) {
866+
ctx.Error(http.StatusNotFound, "artifact has expired", fmt.Errorf("artifact has expired"))
894867
return
895868
}
896869
ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s.zip; filename*=UTF-8''%s.zip", url.PathEscape(art.ArtifactName), art.ArtifactName))
@@ -917,3 +890,20 @@ func DownloadArtifactRaw(ctx *context.APIContext) {
917890
// v3 not supported due to not having one unique id
918891
ctx.Error(http.StatusNotFound, "artifact not found", fmt.Errorf("artifact not found"))
919892
}
893+
894+
// Try to get the artifact by ID and check access
895+
func getArtifactByID(ctx *context.APIContext) (*actions_model.ActionArtifact, bool) {
896+
artifactID := ctx.PathParamInt64("artifact_id")
897+
898+
art, ok, err := db.GetByID[actions_model.ActionArtifact](ctx, artifactID)
899+
if err != nil {
900+
ctx.Error(http.StatusInternalServerError, err.Error(), err)
901+
return nil, false
902+
}
903+
// if artifacts status is not uploaded-confirmed, treat it as not found
904+
if !ok || art.RepoID != ctx.Repo.Repository.ID || art.OwnerID != ctx.Repo.Repository.OwnerID || art.Status != int64(actions_model.ArtifactStatusUploadConfirmed) && art.Status != int64(actions_model.ArtifactStatusExpired) {
905+
ctx.Error(http.StatusNotFound, "artifact not found", fmt.Errorf("artifact not found"))
906+
return nil, false
907+
}
908+
return art, true
909+
}

0 commit comments

Comments
 (0)