@@ -31,7 +31,7 @@ func init() {
31
31
// ActionArtifact is a file that is stored in the artifact storage.
32
32
type ActionArtifact struct {
33
33
ID int64 `xorm:"pk autoincr"`
34
- RunID int64 `xorm:"index UNIQUE(runid_name )"` // The run id of the artifact
34
+ RunID int64 `xorm:"index unique(runid_name_path )"` // The run id of the artifact
35
35
RunnerID int64
36
36
RepoID int64 `xorm:"index"`
37
37
OwnerID int64
@@ -40,27 +40,28 @@ type ActionArtifact struct {
40
40
FileSize int64 // The size of the artifact in bytes
41
41
FileCompressedSize int64 // The size of the artifact in bytes after gzip compression
42
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
43
+ ArtifactPath string `xorm:"index unique(runid_name_path)"` // The path to the artifact when runner uploads it
44
+ ArtifactName string `xorm:"index unique(runid_name_path )"` // 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
46
CreatedUnix timeutil.TimeStamp `xorm:"created"`
47
47
UpdatedUnix timeutil.TimeStamp `xorm:"updated index"`
48
48
}
49
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 ) {
50
+ func CreateArtifact (ctx context.Context , t * ActionTask , artifactName , artifactPath string ) (* ActionArtifact , error ) {
52
51
if err := t .LoadJob (ctx ); err != nil {
53
52
return nil , err
54
53
}
55
- artifact , err := getArtifactByArtifactName (ctx , t .Job .RunID , artifactName )
54
+ artifact , err := getArtifactByNameAndPath (ctx , t .Job .RunID , artifactName , artifactPath )
56
55
if errors .Is (err , util .ErrNotExist ) {
57
56
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 ,
57
+ ArtifactName : artifactName ,
58
+ ArtifactPath : artifactPath ,
59
+ RunID : t .Job .RunID ,
60
+ RunnerID : t .RunnerID ,
61
+ RepoID : t .RepoID ,
62
+ OwnerID : t .OwnerID ,
63
+ CommitSHA : t .CommitSHA ,
64
+ Status : ArtifactStatusUploadPending ,
64
65
}
65
66
if _ , err := db .GetEngine (ctx ).Insert (artifact ); err != nil {
66
67
return nil , err
@@ -72,9 +73,9 @@ func CreateArtifact(ctx context.Context, t *ActionTask, artifactName string) (*A
72
73
return artifact , nil
73
74
}
74
75
75
- func getArtifactByArtifactName (ctx context.Context , runID int64 , name string ) (* ActionArtifact , error ) {
76
+ func getArtifactByNameAndPath (ctx context.Context , runID int64 , name , fpath string ) (* ActionArtifact , error ) {
76
77
var art ActionArtifact
77
- has , err := db .GetEngine (ctx ).Where ("run_id = ? AND artifact_name = ?" , runID , name ).Get (& art )
78
+ has , err := db .GetEngine (ctx ).Where ("run_id = ? AND artifact_name = ? AND artifact_path = ? " , runID , name , fpath ).Get (& art )
78
79
if err != nil {
79
80
return nil , err
80
81
} else if ! has {
@@ -109,14 +110,42 @@ func ListArtifactsByRunID(ctx context.Context, runID int64) ([]*ActionArtifact,
109
110
return arts , db .GetEngine (ctx ).Where ("run_id=?" , runID ).Find (& arts )
110
111
}
111
112
113
+ // ListArtifactsByRunIDAndArtifactName returns an artifacts of a run by artifact name
114
+ func ListArtifactsByRunIDAndArtifactName (ctx context.Context , runID int64 , artifactName string ) ([]* ActionArtifact , error ) {
115
+ arts := make ([]* ActionArtifact , 0 , 10 )
116
+ return arts , db .GetEngine (ctx ).Where ("run_id=? AND artifact_name=?" , runID , artifactName ).Find (& arts )
117
+ }
118
+
112
119
// ListUploadedArtifactsByRunID returns all uploaded artifacts of a run
113
120
func ListUploadedArtifactsByRunID (ctx context.Context , runID int64 ) ([]* ActionArtifact , error ) {
114
121
arts := make ([]* ActionArtifact , 0 , 10 )
115
122
return arts , db .GetEngine (ctx ).Where ("run_id=? AND status=?" , runID , ArtifactStatusUploadConfirmed ).Find (& arts )
116
123
}
117
124
125
+ // ActionArtifactMeta is the meta data of an artifact
126
+ type ActionArtifactMeta struct {
127
+ ArtifactName string
128
+ FileSize int64
129
+ }
130
+
131
+ // ListUploadedArtifactsMeta returns all uploaded artifacts meta of a run
132
+ func ListUploadedArtifactsMeta (ctx context.Context , runID int64 ) ([]* ActionArtifactMeta , error ) {
133
+ arts := make ([]* ActionArtifactMeta , 0 , 10 )
134
+ return arts , db .GetEngine (ctx ).Table ("action_artifact" ).
135
+ Where ("run_id=? AND status=?" , runID , ArtifactStatusUploadConfirmed ).
136
+ GroupBy ("artifact_name" ).
137
+ Select ("artifact_name, sum(file_size) as file_size" ).
138
+ Find (& arts )
139
+ }
140
+
118
141
// ListArtifactsByRepoID returns all artifacts of a repo
119
142
func ListArtifactsByRepoID (ctx context.Context , repoID int64 ) ([]* ActionArtifact , error ) {
120
143
arts := make ([]* ActionArtifact , 0 , 10 )
121
144
return arts , db .GetEngine (ctx ).Where ("repo_id=?" , repoID ).Find (& arts )
122
145
}
146
+
147
+ // ListArtifactsByRunIDAndName returns artifacts by name of a run
148
+ func ListArtifactsByRunIDAndName (ctx context.Context , runID int64 , name string ) ([]* ActionArtifact , error ) {
149
+ arts := make ([]* ActionArtifact , 0 , 10 )
150
+ return arts , db .GetEngine (ctx ).Where ("run_id=? AND artifact_name=?" , runID , name ).Find (& arts )
151
+ }
0 commit comments