|
| 1 | +// Copyright 2023 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +// This artifact server is inspired by https://github.com/nektos/act/blob/master/pkg/artifacts/server.go. |
| 5 | +// It updates url setting and uses ObjectStore to handle artifacts persistence. |
| 6 | + |
| 7 | +package actions |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "errors" |
| 12 | + |
| 13 | + "code.gitea.io/gitea/models/db" |
| 14 | + "code.gitea.io/gitea/modules/timeutil" |
| 15 | + "code.gitea.io/gitea/modules/util" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + // ArtifactStatusUploadPending is the status of an artifact upload that is pending |
| 20 | + ArtifactStatusUploadPending = 1 |
| 21 | + // ArtifactStatusUploadConfirmed is the status of an artifact upload that is confirmed |
| 22 | + ArtifactStatusUploadConfirmed = 2 |
| 23 | + // ArtifactStatusUploadError is the status of an artifact upload that is errored |
| 24 | + ArtifactStatusUploadError = 3 |
| 25 | +) |
| 26 | + |
| 27 | +func init() { |
| 28 | + db.RegisterModel(new(ActionArtifact)) |
| 29 | +} |
| 30 | + |
| 31 | +// ActionArtifact is a file that is stored in the artifact storage. |
| 32 | +type ActionArtifact struct { |
| 33 | + ID int64 `xorm:"pk autoincr"` |
| 34 | + RunID int64 `xorm:"index UNIQUE(runid_name)"` // The run id of the artifact |
| 35 | + RunnerID int64 |
| 36 | + RepoID int64 `xorm:"index"` |
| 37 | + OwnerID int64 |
| 38 | + CommitSHA string |
| 39 | + StoragePath string // The path to the artifact in the storage |
| 40 | + FileSize int64 // The size of the artifact in bytes |
| 41 | + FileCompressedSize int64 // The size of the artifact in bytes after gzip compression |
| 42 | + ContentEncoding string // The content encoding of the artifact |
| 43 | + ArtifactPath string // The path to the artifact when runner uploads it |
| 44 | + ArtifactName string `xorm:"UNIQUE(runid_name)"` // The name of the artifact when runner uploads it |
| 45 | + Status int64 `xorm:"index"` // The status of the artifact, uploading, expired or need-delete |
| 46 | + CreatedUnix timeutil.TimeStamp `xorm:"created"` |
| 47 | + UpdatedUnix timeutil.TimeStamp `xorm:"updated index"` |
| 48 | +} |
| 49 | + |
| 50 | +// CreateArtifact create a new artifact with task info or get same named artifact in the same run |
| 51 | +func CreateArtifact(ctx context.Context, t *ActionTask, artifactName string) (*ActionArtifact, error) { |
| 52 | + if err := t.LoadJob(ctx); err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + artifact, err := getArtifactByArtifactName(ctx, t.Job.RunID, artifactName) |
| 56 | + if errors.Is(err, util.ErrNotExist) { |
| 57 | + artifact := &ActionArtifact{ |
| 58 | + RunID: t.Job.RunID, |
| 59 | + RunnerID: t.RunnerID, |
| 60 | + RepoID: t.RepoID, |
| 61 | + OwnerID: t.OwnerID, |
| 62 | + CommitSHA: t.CommitSHA, |
| 63 | + Status: ArtifactStatusUploadPending, |
| 64 | + } |
| 65 | + if _, err := db.GetEngine(ctx).Insert(artifact); err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + return artifact, nil |
| 69 | + } else if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + return artifact, nil |
| 73 | +} |
| 74 | + |
| 75 | +func getArtifactByArtifactName(ctx context.Context, runID int64, name string) (*ActionArtifact, error) { |
| 76 | + var art ActionArtifact |
| 77 | + has, err := db.GetEngine(ctx).Where("run_id = ? AND artifact_name = ?", runID, name).Get(&art) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } else if !has { |
| 81 | + return nil, util.ErrNotExist |
| 82 | + } |
| 83 | + return &art, nil |
| 84 | +} |
| 85 | + |
| 86 | +// GetArtifactByID returns an artifact by id |
| 87 | +func GetArtifactByID(ctx context.Context, id int64) (*ActionArtifact, error) { |
| 88 | + var art ActionArtifact |
| 89 | + has, err := db.GetEngine(ctx).ID(id).Get(&art) |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } else if !has { |
| 93 | + return nil, util.ErrNotExist |
| 94 | + } |
| 95 | + |
| 96 | + return &art, nil |
| 97 | +} |
| 98 | + |
| 99 | +// UpdateArtifactByID updates an artifact by id |
| 100 | +func UpdateArtifactByID(ctx context.Context, id int64, art *ActionArtifact) error { |
| 101 | + art.ID = id |
| 102 | + _, err := db.GetEngine(ctx).ID(id).AllCols().Update(art) |
| 103 | + return err |
| 104 | +} |
| 105 | + |
| 106 | +// ListArtifactsByRunID returns all artifacts of a run |
| 107 | +func ListArtifactsByRunID(ctx context.Context, runID int64) ([]*ActionArtifact, error) { |
| 108 | + arts := make([]*ActionArtifact, 0, 10) |
| 109 | + return arts, db.GetEngine(ctx).Where("run_id=?", runID).Find(&arts) |
| 110 | +} |
| 111 | + |
| 112 | +// ListUploadedArtifactsByRunID returns all uploaded artifacts of a run |
| 113 | +func ListUploadedArtifactsByRunID(ctx context.Context, runID int64) ([]*ActionArtifact, error) { |
| 114 | + arts := make([]*ActionArtifact, 0, 10) |
| 115 | + return arts, db.GetEngine(ctx).Where("run_id=? AND status=?", runID, ArtifactStatusUploadConfirmed).Find(&arts) |
| 116 | +} |
| 117 | + |
| 118 | +// ListArtifactsByRepoID returns all artifacts of a repo |
| 119 | +func ListArtifactsByRepoID(ctx context.Context, repoID int64) ([]*ActionArtifact, error) { |
| 120 | + arts := make([]*ActionArtifact, 0, 10) |
| 121 | + return arts, db.GetEngine(ctx).Where("repo_id=?", repoID).Find(&arts) |
| 122 | +} |
0 commit comments