@@ -3,7 +3,6 @@ package maven
33import (
44 "context"
55 "fmt"
6- "strconv"
76 "strings"
87
98 "code.gitea.io/gitea/models/packages"
@@ -13,105 +12,122 @@ import (
1312 packages_service "code.gitea.io/gitea/services/packages"
1413)
1514
16- // CleanupSnapshotVersion removes outdated files for SNAPHOT versions for all Maven packages.
15+ // CleanupSnapshotVersions removes outdated files for SNAPHOT versions for all Maven packages.
1716func CleanupSnapshotVersions (ctx context.Context ) error {
1817 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 )
2020
2121 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" )
2323 return nil
2424 }
2525
2626 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 )
2828 }
2929
3030 versions , err := packages .GetVersionsByPackageType (ctx , 0 , packages .TypeMaven )
3131 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 )
3333 }
3434
35- for _ , version := range versions {
36- log .Info ("Processing version: %s (ID: %d)" , version .Version , version .ID )
35+ var errors []error
3736
37+ for _ , version := range versions {
3838 if ! isSnapshotVersion (version .Version ) {
39- log .Info ("Skipping non-SNAPSHOT version: %s (ID: %d)" , version .Version , version .ID )
4039 continue
4140 }
4241
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 )
4650 }
51+ return fmt .Errorf ("Maven CleanupSnapshotVersions: cleanup completed with errors: %v" , errors )
4752 }
4853
49- log .Info ("Completed CleanupSnapshotVersion " )
54+ log .Debug ("Completed Maven CleanupSnapshotVersions " )
5055 return nil
5156}
5257
5358func isSnapshotVersion (version string ) bool {
5459 return strings .HasSuffix (version , "-SNAPSHOT" )
5560}
5661
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 )
5964
6065 metadataFile , err := packages .GetFileForVersionByName (ctx , versionID , "maven-metadata.xml" , packages .EmptyFileKey )
6166 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 )
6368 }
6469
65- maxBuildNumber , err := extractMaxBuildNumberFromMetadata (ctx , metadataFile )
70+ maxBuildNumber , classifiers , err := extractMaxBuildNumber (ctx , metadataFile )
6671 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 )
6873 }
6974
70- log .Info ("Max build number for versionID %d: %d" , versionID , maxBuildNumber )
71-
7275 thresholdBuildNumber := maxBuildNumber - retainBuilds
7376 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 )
7578 return nil
7679 }
7780
78- filesToRemove , err := packages .GetFilesByBuildNumber (ctx , versionID , thresholdBuildNumber )
81+ filesToRemove , skippedFiles , err := packages .GetFilesBelowBuildNumber (ctx , versionID , thresholdBuildNumber , classifiers ... )
7982 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
8199 }
82100
83101 for _ , file := range filesToRemove {
84102 log .Debug ("Removing file '%s' below threshold %d" , file .Name , thresholdBuildNumber )
85103 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 )
87105 }
88106 }
89107
90- log .Info ("Completed cleanSnapshotFiles for versionID: %d" , versionID )
108+ log .Debug ("Completed Maven cleanSnapshotFiles for versionID: %d" , versionID )
91109 return nil
92110}
93111
94- func extractMaxBuildNumberFromMetadata (ctx context.Context , metadataFile * packages.PackageFile ) (int , error ) {
112+ func extractMaxBuildNumber (ctx context.Context , metadataFile * packages.PackageFile ) (int , [] string , error ) {
95113 pb , err := packages .GetBlobByID (ctx , metadataFile .BlobID )
96114 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 )
98116 }
99117
100118 content , _ , _ , err := packages_service .GetPackageBlobStream (ctx , metadataFile , pb , nil , true )
101119 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 )
103121 }
104122 defer content .Close ()
105123
106- buildNumberStr , err := maven .ParseMavenMetaData (content )
124+ snapshotMetadata , err := maven .ParseSnapshotVersionMetaData (content )
107125 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 )
109127 }
110128
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
115131
116- return buildNumber , nil
132+ return buildNumber , classifiers , nil
117133}
0 commit comments