Skip to content

Commit d50358f

Browse files
committed
move old and new logic to modules
1 parent d3dbfcb commit d50358f

File tree

4 files changed

+70
-56
lines changed

4 files changed

+70
-56
lines changed

models/actions/artifact.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ type FindArtifactsOptions struct {
112112
RunID int64
113113
ArtifactName string
114114
Status int
115-
FinalizedArtifactsV2 bool
115+
FinalizedArtifactsV4 bool
116116
}
117117

118118
func (opts FindArtifactsOptions) ToConds() builder.Cond {
@@ -129,7 +129,7 @@ func (opts FindArtifactsOptions) ToConds() builder.Cond {
129129
if opts.Status > 0 {
130130
cond = cond.And(builder.Eq{"status": opts.Status})
131131
}
132-
if opts.FinalizedArtifactsV2 {
132+
if opts.FinalizedArtifactsV4 {
133133
cond = cond.And(builder.Eq{"status": ArtifactStatusUploadConfirmed}.Or(builder.Eq{"status": ArtifactStatusExpired}))
134134
cond = cond.And(builder.Eq{"content_encoding": "application/zip"})
135135
}

modules/actions/artifacts.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package actions
2+
3+
import (
4+
"io"
5+
"net/http"
6+
7+
actions_model "code.gitea.io/gitea/models/actions"
8+
"code.gitea.io/gitea/modules/setting"
9+
"code.gitea.io/gitea/modules/storage"
10+
"code.gitea.io/gitea/services/context"
11+
)
12+
13+
// Artifacts using the v4 backend are stored as a single combined zip file per artifact on the backend
14+
// The v4 backend ensures ContentEncoding is set to "application/zip", which is not the case for the old backend
15+
func IsArtifactV4(art *actions_model.ActionArtifact) bool {
16+
return art.ArtifactName+".zip" == art.ArtifactPath && art.ContentEncoding == "application/zip"
17+
}
18+
19+
func DownloadArtifactV4ServeDirectOnly(ctx *context.Base, art *actions_model.ActionArtifact) (bool, error) {
20+
if setting.Actions.ArtifactStorage.ServeDirect() {
21+
u, err := storage.ActionsArtifacts.URL(art.StoragePath, art.ArtifactPath, nil)
22+
if u != nil && err == nil {
23+
ctx.Redirect(u.String(), http.StatusFound)
24+
return true, nil
25+
}
26+
}
27+
return false, nil
28+
}
29+
30+
func DownloadArtifactV4Fallback(ctx *context.Base, art *actions_model.ActionArtifact) error {
31+
f, err := storage.ActionsArtifacts.Open(art.StoragePath)
32+
if err != nil {
33+
return err
34+
}
35+
defer f.Close()
36+
_, _ = io.Copy(ctx.Resp, f)
37+
return nil
38+
}
39+
40+
func DownloadArtifactV4(ctx *context.Base, art *actions_model.ActionArtifact) error {
41+
ok, err := DownloadArtifactV4ServeDirectOnly(ctx, art)
42+
if ok || err != nil {
43+
return err
44+
}
45+
return DownloadArtifactV4Fallback(ctx, art)
46+
}

routers/api/v1/repo/action.go

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ package repo
66
import (
77
"errors"
88
"fmt"
9-
"io"
109
"net/http"
1110
"net/url"
1211
"strings"
1312

1413
actions_model "code.gitea.io/gitea/models/actions"
1514
"code.gitea.io/gitea/models/db"
1615
secret_model "code.gitea.io/gitea/models/secret"
16+
"code.gitea.io/gitea/modules/actions"
1717
"code.gitea.io/gitea/modules/setting"
18-
"code.gitea.io/gitea/modules/storage"
1918
api "code.gitea.io/gitea/modules/structs"
2019
"code.gitea.io/gitea/modules/util"
2120
"code.gitea.io/gitea/modules/web"
@@ -620,11 +619,10 @@ func GetArtifactsOfRun(ctx *context.APIContext) {
620619
runID := ctx.PathParamInt64("run")
621620

622621
artifacts, total, err := db.FindAndCount[actions_model.ActionArtifact](ctx, actions_model.FindArtifactsOptions{
623-
RepoID: repoID,
624-
RunID: runID,
625-
ArtifactName: artifactName,
626-
// Status: int(actions_model.ArtifactStatusUploadConfirmed),
627-
FinalizedArtifactsV2: true,
622+
RepoID: repoID,
623+
RunID: runID,
624+
ArtifactName: artifactName,
625+
FinalizedArtifactsV4: true,
628626
ListOptions: utils.GetListOptions(ctx),
629627
})
630628
if err != nil {
@@ -680,10 +678,9 @@ func GetArtifacts(ctx *context.APIContext) {
680678
artifactName := ctx.PathParam("artifact_name")
681679

682680
artifacts, total, err := db.FindAndCount[actions_model.ActionArtifact](ctx, actions_model.FindArtifactsOptions{
683-
RepoID: repoID,
684-
ArtifactName: artifactName,
685-
// Status: int(actions_model.ArtifactStatusUploadConfirmed),
686-
FinalizedArtifactsV2: true,
681+
RepoID: repoID,
682+
ArtifactName: artifactName,
683+
FinalizedArtifactsV4: true,
687684
ListOptions: utils.GetListOptions(ctx),
688685
})
689686
if err != nil {
@@ -745,9 +742,7 @@ func GetArtifact(ctx *context.APIContext) {
745742
return
746743
}
747744

748-
// Artifacts using the v4 backend are stored as a single combined zip file per artifact on the backend
749-
// The v4 backend enshures ContentEncoding is set to "application/zip", which is not the case for the old backend
750-
if art.ArtifactName+".zip" == art.ArtifactPath && art.ContentEncoding == "application/zip" {
745+
if actions.IsArtifactV4(art) {
751746
repoName := ctx.Repo.Repository.FullName()
752747
convertedArtifact, err := convert.ToActionArtifact(ctx, repoName, art)
753748
if err != nil {
@@ -804,18 +799,15 @@ func DownloadArtifact(ctx *context.APIContext) {
804799
}
805800
ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s.zip; filename*=UTF-8''%s.zip", url.PathEscape(art.ArtifactName), art.ArtifactName))
806801

807-
// Artifacts using the v4 backend are stored as a single combined zip file per artifact on the backend
808-
// The v4 backend enshures ContentEncoding is set to "application/zip", which is not the case for the old backend
809-
if art.ArtifactName+".zip" == art.ArtifactPath && art.ContentEncoding == "application/zip" {
810-
art := art
811-
if setting.Actions.ArtifactStorage.ServeDirect() {
812-
u, err := storage.ActionsArtifacts.URL(art.StoragePath, art.ArtifactPath, nil)
813-
if u != nil && err == nil {
814-
ctx.Redirect(u.String(), http.StatusFound)
815-
return
816-
}
802+
if actions.IsArtifactV4(art) {
803+
ok, err := actions.DownloadArtifactV4ServeDirectOnly(ctx.Base, art)
804+
if ok {
805+
return
806+
}
807+
if err != nil {
808+
ctx.Error(http.StatusInternalServerError, err.Error(), err)
809+
return
817810
}
818-
// ##[error]Unable to download artifact(s): Unable to download artifact. Unexpected status: 200
819811
repoName := ctx.Repo.Repository.FullName()
820812
url := strings.TrimSuffix(setting.AppURL, "/") + "/api/v1/repos/" + repoName + "/actions/artifacts/" + fmt.Sprintf("%d", art.ID) + "/zip/raw"
821813
ctx.Redirect(url, http.StatusFound)
@@ -868,24 +860,12 @@ func DownloadArtifactRaw(ctx *context.APIContext) {
868860
}
869861
ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s.zip; filename*=UTF-8''%s.zip", url.PathEscape(art.ArtifactName), art.ArtifactName))
870862

871-
// Artifacts using the v4 backend are stored as a single combined zip file per artifact on the backend
872-
// The v4 backend enshures ContentEncoding is set to "application/zip", which is not the case for the old backend
873-
if art.ArtifactName+".zip" == art.ArtifactPath && art.ContentEncoding == "application/zip" {
874-
art := art
875-
if setting.Actions.ArtifactStorage.ServeDirect() {
876-
u, err := storage.ActionsArtifacts.URL(art.StoragePath, art.ArtifactPath, nil)
877-
if u != nil && err == nil {
878-
ctx.Redirect(u.String())
879-
return
880-
}
881-
}
882-
f, err := storage.ActionsArtifacts.Open(art.StoragePath)
863+
if actions.IsArtifactV4(art) {
864+
err := actions.DownloadArtifactV4(ctx.Base, art)
883865
if err != nil {
884-
ctx.Error(http.StatusInternalServerError, err.Error(), err)
866+
ctx.Error(http.StatusInternalServerError, "artifact has expired", fmt.Errorf("artifact has expired"))
885867
return
886868
}
887-
_, _ = io.Copy(ctx.Resp, f)
888-
return
889869
}
890870
// v3 not supported due to not having one unique id
891871
ctx.Error(http.StatusNotFound, "artifact not found", fmt.Errorf("artifact not found"))

routers/web/repo/actions/view.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"code.gitea.io/gitea/modules/base"
2929
"code.gitea.io/gitea/modules/git"
3030
"code.gitea.io/gitea/modules/log"
31-
"code.gitea.io/gitea/modules/setting"
3231
"code.gitea.io/gitea/modules/storage"
3332
api "code.gitea.io/gitea/modules/structs"
3433
"code.gitea.io/gitea/modules/templates"
@@ -682,23 +681,12 @@ func ArtifactsDownloadView(ctx *context_module.Context) {
682681

683682
ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s.zip; filename*=UTF-8''%s.zip", url.PathEscape(artifactName), artifactName))
684683

685-
// Artifacts using the v4 backend are stored as a single combined zip file per artifact on the backend
686-
// The v4 backend enshures ContentEncoding is set to "application/zip", which is not the case for the old backend
687-
if len(artifacts) == 1 && artifacts[0].ArtifactName+".zip" == artifacts[0].ArtifactPath && artifacts[0].ContentEncoding == "application/zip" {
688-
art := artifacts[0]
689-
if setting.Actions.ArtifactStorage.ServeDirect() {
690-
u, err := storage.ActionsArtifacts.URL(art.StoragePath, art.ArtifactPath, nil)
691-
if u != nil && err == nil {
692-
ctx.Redirect(u.String())
693-
return
694-
}
695-
}
696-
f, err := storage.ActionsArtifacts.Open(art.StoragePath)
684+
if len(artifacts) == 1 && actions.IsArtifactV4(artifacts[0]) {
685+
err := actions.DownloadArtifactV4(ctx.Base, artifacts[0])
697686
if err != nil {
698687
ctx.Error(http.StatusInternalServerError, err.Error())
699688
return
700689
}
701-
_, _ = io.Copy(ctx.Resp, f)
702690
return
703691
}
704692

0 commit comments

Comments
 (0)