@@ -3,7 +3,6 @@ package maven
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "strconv"
7
6
"strings"
8
7
9
8
"code.gitea.io/gitea/models/packages"
@@ -13,105 +12,122 @@ import (
13
12
packages_service "code.gitea.io/gitea/services/packages"
14
13
)
15
14
16
- // CleanupSnapshotVersion removes outdated files for SNAPHOT versions for all Maven packages.
15
+ // CleanupSnapshotVersions removes outdated files for SNAPHOT versions for all Maven packages.
17
16
func CleanupSnapshotVersions (ctx context.Context ) error {
18
17
retainBuilds := setting .Packages .RetainMavenSnapshotBuilds
19
- log .Info ("Starting CleanupSnapshotVersion with retainBuilds: %d" , retainBuilds )
18
+ debugSession := setting .Packages .DebugMavenCleanup
19
+ log .Debug ("Starting Maven CleanupSnapshotVersions with retainBuilds: %d, debugSession: %t" , retainBuilds , debugSession )
20
20
21
21
if retainBuilds == - 1 {
22
- log .Info ("CleanupSnapshotVersion skipped because retainBuilds is set to -1" )
22
+ log .Info ("Maven CleanupSnapshotVersions skipped because retainBuilds is set to -1" )
23
23
return nil
24
24
}
25
25
26
26
if retainBuilds < 1 {
27
- return fmt .Errorf ("forbidden value for retainBuilds: %d. Minimum 1 build should be retained" , retainBuilds )
27
+ return fmt .Errorf ("Maven CleanupSnapshotVersions: forbidden value for retainBuilds: %d. Minimum 1 build should be retained" , retainBuilds )
28
28
}
29
29
30
30
versions , err := packages .GetVersionsByPackageType (ctx , 0 , packages .TypeMaven )
31
31
if err != nil {
32
- return fmt .Errorf ("failed to retrieve Maven package versions: %w" , err )
32
+ return fmt .Errorf ("Maven CleanupSnapshotVersions: failed to retrieve Maven package versions: %w" , err )
33
33
}
34
34
35
- for _ , version := range versions {
36
- log .Info ("Processing version: %s (ID: %d)" , version .Version , version .ID )
35
+ var errors []error
37
36
37
+ for _ , version := range versions {
38
38
if ! isSnapshotVersion (version .Version ) {
39
- log .Info ("Skipping non-SNAPSHOT version: %s (ID: %d)" , version .Version , version .ID )
40
39
continue
41
40
}
42
41
43
- if err := cleanSnapshotFiles (ctx , version .ID , retainBuilds ); err != nil {
44
- log .Error ("Failed to clean up snapshot files for version '%s' (ID: %d): %v" , version .Version , version .ID , err )
45
- return err
42
+ if err := cleanSnapshotFiles (ctx , version .ID , retainBuilds , debugSession ); err != nil {
43
+ errors = append (errors , fmt .Errorf ("Maven CleanupSnapshotVersions: version '%s' (ID: %d): %w" , version .Version , version .ID , err ))
44
+ }
45
+ }
46
+
47
+ if len (errors ) > 0 {
48
+ for _ , err := range errors {
49
+ log .Warn ("Maven CleanupSnapshotVersions: Error during cleanup: %v" , err )
46
50
}
51
+ return fmt .Errorf ("Maven CleanupSnapshotVersions: cleanup completed with errors: %v" , errors )
47
52
}
48
53
49
- log .Info ("Completed CleanupSnapshotVersion " )
54
+ log .Debug ("Completed Maven CleanupSnapshotVersions " )
50
55
return nil
51
56
}
52
57
53
58
func isSnapshotVersion (version string ) bool {
54
59
return strings .HasSuffix (version , "-SNAPSHOT" )
55
60
}
56
61
57
- func cleanSnapshotFiles (ctx context.Context , versionID int64 , retainBuilds int ) error {
58
- log .Info ("Starting cleanSnapshotFiles for versionID: %d with retainBuilds: %d" , versionID , retainBuilds )
62
+ func cleanSnapshotFiles (ctx context.Context , versionID int64 , retainBuilds int , debugSession bool ) error {
63
+ log .Debug ("Starting Maven cleanSnapshotFiles for versionID: %d with retainBuilds: %d, debugSession: %t " , versionID , retainBuilds , debugSession )
59
64
60
65
metadataFile , err := packages .GetFileForVersionByName (ctx , versionID , "maven-metadata.xml" , packages .EmptyFileKey )
61
66
if err != nil {
62
- return fmt .Errorf ("failed to retrieve Maven metadata file for version ID %d: %w" , versionID , err )
67
+ return fmt .Errorf ("cleanSnapshotFiles: failed to retrieve Maven metadata file for version ID %d: %w" , versionID , err )
63
68
}
64
69
65
- maxBuildNumber , err := extractMaxBuildNumberFromMetadata (ctx , metadataFile )
70
+ maxBuildNumber , classifiers , err := extractMaxBuildNumber (ctx , metadataFile )
66
71
if err != nil {
67
- return fmt .Errorf ("failed to extract max build number from maven-metadata.xml for version ID %d: %w" , versionID , err )
72
+ return fmt .Errorf ("cleanSnapshotFiles: failed to extract max build number from maven-metadata.xml for version ID %d: %w" , versionID , err )
68
73
}
69
74
70
- log .Info ("Max build number for versionID %d: %d" , versionID , maxBuildNumber )
71
-
72
75
thresholdBuildNumber := maxBuildNumber - retainBuilds
73
76
if thresholdBuildNumber <= 0 {
74
- log .Info ( " No files to clean up, as the threshold build number is less than or equal to zero for versionID %d" , versionID )
77
+ log .Debug ( "cleanSnapshotFiles: No files to clean up, as the threshold build number is less than or equal to zero for versionID %d" , versionID )
75
78
return nil
76
79
}
77
80
78
- filesToRemove , err := packages .GetFilesByBuildNumber (ctx , versionID , thresholdBuildNumber )
81
+ filesToRemove , skippedFiles , err := packages .GetFilesBelowBuildNumber (ctx , versionID , thresholdBuildNumber , classifiers ... )
79
82
if err != nil {
80
- return fmt .Errorf ("failed to retrieve files for version ID %d: %w" , versionID , err )
83
+ return fmt .Errorf ("cleanSnapshotFiles: failed to retrieve files for version ID %d: %w" , versionID , err )
84
+ }
85
+
86
+ if debugSession {
87
+ var fileNamesToRemove , skippedFileNames []string
88
+
89
+ for _ , file := range filesToRemove {
90
+ fileNamesToRemove = append (fileNamesToRemove , file .Name )
91
+ }
92
+
93
+ for _ , file := range skippedFiles {
94
+ skippedFileNames = append (skippedFileNames , file .Name )
95
+ }
96
+
97
+ log .Info ("cleanSnapshotFiles: Debug session active. Files to remove: %v, Skipped files: %v" , fileNamesToRemove , skippedFileNames )
98
+ return nil
81
99
}
82
100
83
101
for _ , file := range filesToRemove {
84
102
log .Debug ("Removing file '%s' below threshold %d" , file .Name , thresholdBuildNumber )
85
103
if err := packages_service .DeletePackageFile (ctx , file ); err != nil {
86
- return fmt .Errorf ("failed to delete file '%s': %w" , file .Name , err )
104
+ return fmt .Errorf ("Maven cleanSnapshotFiles: failed to delete file '%s': %w" , file .Name , err )
87
105
}
88
106
}
89
107
90
- log .Info ("Completed cleanSnapshotFiles for versionID: %d" , versionID )
108
+ log .Debug ("Completed Maven cleanSnapshotFiles for versionID: %d" , versionID )
91
109
return nil
92
110
}
93
111
94
- func extractMaxBuildNumberFromMetadata (ctx context.Context , metadataFile * packages.PackageFile ) (int , error ) {
112
+ func extractMaxBuildNumber (ctx context.Context , metadataFile * packages.PackageFile ) (int , [] string , error ) {
95
113
pb , err := packages .GetBlobByID (ctx , metadataFile .BlobID )
96
114
if err != nil {
97
- return 0 , fmt .Errorf ("failed to get package blob: %w" , err )
115
+ return 0 , nil , fmt .Errorf ("extractMaxBuildNumber: failed to get package blob: %w" , err )
98
116
}
99
117
100
118
content , _ , _ , err := packages_service .GetPackageBlobStream (ctx , metadataFile , pb , nil , true )
101
119
if err != nil {
102
- return 0 , fmt .Errorf ("failed to get package file stream: %w" , err )
120
+ return 0 , nil , fmt .Errorf ("extractMaxBuildNumber: failed to get package file stream: %w" , err )
103
121
}
104
122
defer content .Close ()
105
123
106
- buildNumberStr , err := maven .ParseMavenMetaData (content )
124
+ snapshotMetadata , err := maven .ParseSnapshotVersionMetaData (content )
107
125
if err != nil {
108
- return 0 , fmt .Errorf ("failed to parse maven-metadata.xml: %w" , err )
126
+ return 0 , nil , fmt .Errorf ("extractMaxBuildNumber: failed to parse maven-metadata.xml: %w" , err )
109
127
}
110
128
111
- buildNumber , err := strconv .Atoi (buildNumberStr )
112
- if err != nil {
113
- return 0 , fmt .Errorf ("invalid build number format: %w" , err )
114
- }
129
+ buildNumber := snapshotMetadata .BuildNumber
130
+ classifiers := snapshotMetadata .Classifiers
115
131
116
- return buildNumber , nil
132
+ return buildNumber , classifiers , nil
117
133
}
0 commit comments