-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Add time-based cleanup for Maven snapshot versions #33420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dianaStr7
wants to merge
4
commits into
go-gitea:main
Choose a base branch
from
dianaStr7:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package maven | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/models/packages" | ||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/packages/maven" | ||
"code.gitea.io/gitea/modules/setting" | ||
packages_service "code.gitea.io/gitea/services/packages" | ||
) | ||
|
||
// CleanupSnapshotVersions removes outdated files for SNAPHOT versions for all Maven packages. | ||
func CleanupSnapshotVersions(ctx context.Context) error { | ||
retainBuilds := setting.Packages.RetainMavenSnapshotBuilds | ||
debugSession := setting.Packages.DebugMavenCleanup | ||
log.Debug("Starting Maven CleanupSnapshotVersions with retainBuilds: %d, debugSession: %t", retainBuilds, debugSession) | ||
|
||
if retainBuilds == -1 { | ||
log.Info("Maven CleanupSnapshotVersions skipped because retainBuilds is set to -1") | ||
return nil | ||
} | ||
|
||
if retainBuilds < 1 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd merge this condition into the one above: |
||
return fmt.Errorf("Maven CleanupSnapshotVersions: forbidden value for retainBuilds: %d. Minimum 1 build should be retained", retainBuilds) | ||
} | ||
|
||
versions, err := packages.GetVersionsByPackageType(ctx, 0, packages.TypeMaven) | ||
if err != nil { | ||
return fmt.Errorf("Maven CleanupSnapshotVersions: failed to retrieve Maven package versions: %w", err) | ||
} | ||
|
||
var errors []error | ||
|
||
for _, version := range versions { | ||
if !isSnapshotVersion(version.Version) { | ||
continue | ||
} | ||
|
||
if err := cleanSnapshotFiles(ctx, version.ID, retainBuilds, debugSession); err != nil { | ||
errors = append(errors, fmt.Errorf("Maven CleanupSnapshotVersions: version '%s' (ID: %d): %w", version.Version, version.ID, err)) | ||
} | ||
} | ||
|
||
if len(errors) > 0 { | ||
for _, err := range errors { | ||
log.Warn("Maven CleanupSnapshotVersions: Error during cleanup: %v", err) | ||
} | ||
return fmt.Errorf("Maven CleanupSnapshotVersions: cleanup completed with errors: %v", errors) | ||
} | ||
|
||
log.Debug("Completed Maven CleanupSnapshotVersions") | ||
return nil | ||
} | ||
|
||
func isSnapshotVersion(version string) bool { | ||
return strings.HasSuffix(version, "-SNAPSHOT") | ||
} | ||
|
||
func cleanSnapshotFiles(ctx context.Context, versionID int64, retainBuilds int, debugSession bool) error { | ||
log.Debug("Starting Maven cleanSnapshotFiles for versionID: %d with retainBuilds: %d, debugSession: %t", versionID, retainBuilds, debugSession) | ||
|
||
metadataFile, err := packages.GetFileForVersionByName(ctx, versionID, "maven-metadata.xml", packages.EmptyFileKey) | ||
if err != nil { | ||
return fmt.Errorf("cleanSnapshotFiles: failed to retrieve Maven metadata file for version ID %d: %w", versionID, err) | ||
} | ||
|
||
maxBuildNumber, classifiers, err := extractMaxBuildNumber(ctx, metadataFile) | ||
if err != nil { | ||
return fmt.Errorf("cleanSnapshotFiles: failed to extract max build number from maven-metadata.xml for version ID %d: %w", versionID, err) | ||
} | ||
|
||
thresholdBuildNumber := maxBuildNumber - retainBuilds | ||
if thresholdBuildNumber <= 0 { | ||
log.Debug("cleanSnapshotFiles: No files to clean up, as the threshold build number is less than or equal to zero for versionID %d", versionID) | ||
return nil | ||
} | ||
|
||
filesToRemove, skippedFiles, err := packages.GetFilesBelowBuildNumber(ctx, versionID, thresholdBuildNumber, classifiers...) | ||
if err != nil { | ||
return fmt.Errorf("cleanSnapshotFiles: failed to retrieve files for version ID %d: %w", versionID, err) | ||
} | ||
|
||
if debugSession { | ||
var fileNamesToRemove, skippedFileNames []string | ||
|
||
for _, file := range filesToRemove { | ||
fileNamesToRemove = append(fileNamesToRemove, file.Name) | ||
} | ||
|
||
for _, file := range skippedFiles { | ||
skippedFileNames = append(skippedFileNames, file.Name) | ||
} | ||
|
||
log.Info("cleanSnapshotFiles: Debug session active. Files to remove: %v, Skipped files: %v", fileNamesToRemove, skippedFileNames) | ||
return nil | ||
} | ||
|
||
for _, file := range filesToRemove { | ||
log.Debug("Removing file '%s' below threshold %d", file.Name, thresholdBuildNumber) | ||
if err := packages_service.DeletePackageFile(ctx, file); err != nil { | ||
return fmt.Errorf("Maven cleanSnapshotFiles: failed to delete file '%s': %w", file.Name, err) | ||
} | ||
} | ||
|
||
log.Debug("Completed Maven cleanSnapshotFiles for versionID: %d", versionID) | ||
return nil | ||
} | ||
|
||
func extractMaxBuildNumber(ctx context.Context, metadataFile *packages.PackageFile) (int, []string, error) { | ||
pb, err := packages.GetBlobByID(ctx, metadataFile.BlobID) | ||
if err != nil { | ||
return 0, nil, fmt.Errorf("extractMaxBuildNumber: failed to get package blob: %w", err) | ||
} | ||
|
||
content, _, _, err := packages_service.GetPackageBlobStream(ctx, metadataFile, pb, nil, true) | ||
if err != nil { | ||
return 0, nil, fmt.Errorf("extractMaxBuildNumber: failed to get package file stream: %w", err) | ||
} | ||
defer content.Close() | ||
|
||
snapshotMetadata, err := maven.ParseSnapshotVersionMetaData(content) | ||
if err != nil { | ||
return 0, nil, fmt.Errorf("extractMaxBuildNumber: failed to parse maven-metadata.xml: %w", err) | ||
} | ||
|
||
buildNumber := snapshotMetadata.BuildNumber | ||
classifiers := snapshotMetadata.Classifiers | ||
|
||
return buildNumber, classifiers, nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.