[v13.0/forgejo] fix: package cleaned rule fails if the keep count is too high (#9471)
**Backport:** https://codeberg.org/forgejo/forgejo/pulls/9468 If the keep count of a cleanup rule is greater than the number of available packages, it fails with: ``` panic(boundsError{x: int64(x), signed: true, y: y, code: boundsSliceB}) .../packages/packages.go:175 .../routers/web/org/setting_packages.go:108 ``` Regression of https://codeberg.org/forgejo/forgejo/pulls/9219/files Refs https://codeberg.org/forgejo/forgejo/issues/9461 <!--start release-notes-assistant--> ## Release notes <!--URL:https://codeberg.org/forgejo/forgejo--> - Bug fixes - [PR](https://codeberg.org/forgejo/forgejo/pulls/9468): <!--number 9468 --><!--line 0 --><!--description cGFja2FnZSBjbGVhbmVkIHJ1bGUgZmFpbHMgaWYgdGhlIGtlZXAgY291bnQgaXMgdG9vIGhpZ2g=-->package cleaned rule fails if the keep count is too high<!--description--> <!--end release-notes-assistant--> Co-authored-by: Earl Warren <contact@earl-warren.org> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9471 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org> Co-committed-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
This commit is contained in:
		
					parent
					
						
							
								391f9a2f2c
							
						
					
				
			
			
				commit
				
					
						010366d641
					
				
			
		
					 3 changed files with 17 additions and 2 deletions
				
			
		|  | @ -172,7 +172,8 @@ func SetRulePreviewContext(ctx *context.Context, owner *user_model.User) { | ||||||
| 			ctx.ServerError("SearchVersions", err) | 			ctx.ServerError("SearchVersions", err) | ||||||
| 			return | 			return | ||||||
| 		} | 		} | ||||||
| 		for _, pv := range pvs[pcr.KeepCount:] { | 		keep := min(len(pvs), pcr.KeepCount) | ||||||
|  | 		for _, pv := range pvs[keep:] { | ||||||
| 			if skip, err := container_service.ShouldBeSkipped(ctx, pcr, p, pv); err != nil { | 			if skip, err := container_service.ShouldBeSkipped(ctx, pcr, p, pv); err != nil { | ||||||
| 				ctx.ServerError("ShouldBeSkipped", err) | 				ctx.ServerError("ShouldBeSkipped", err) | ||||||
| 				return | 				return | ||||||
|  |  | ||||||
|  | @ -69,7 +69,8 @@ func ExecuteCleanupRules(outerCtx context.Context) error { | ||||||
| 				return fmt.Errorf("CleanupRule [%d]: SearchVersions failed: %w", pcr.ID, err) | 				return fmt.Errorf("CleanupRule [%d]: SearchVersions failed: %w", pcr.ID, err) | ||||||
| 			} | 			} | ||||||
| 			versionDeleted := false | 			versionDeleted := false | ||||||
| 			for _, pv := range pvs[pcr.KeepCount:] { | 			keep := min(len(pvs), pcr.KeepCount) | ||||||
|  | 			for _, pv := range pvs[keep:] { | ||||||
| 				if pcr.Type == packages_model.TypeContainer { | 				if pcr.Type == packages_model.TypeContainer { | ||||||
| 					if skip, err := container_service.ShouldBeSkipped(ctx, pcr, p, pv); err != nil { | 					if skip, err := container_service.ShouldBeSkipped(ctx, pcr, p, pv); err != nil { | ||||||
| 						return fmt.Errorf("CleanupRule [%d]: container.ShouldBeSkipped failed: %w", pcr.ID, err) | 						return fmt.Errorf("CleanupRule [%d]: container.ShouldBeSkipped failed: %w", pcr.ID, err) | ||||||
|  |  | ||||||
|  | @ -573,6 +573,19 @@ func TestPackageCleanup(t *testing.T) { | ||||||
| 					KeepCount: 2, | 					KeepCount: 2, | ||||||
| 				}, | 				}, | ||||||
| 			}, | 			}, | ||||||
|  | 			{ | ||||||
|  | 				Name: "KeepCountGreaterThanTotal", | ||||||
|  | 				Versions: []version{ | ||||||
|  | 					{Version: "keep", ShouldExist: true}, | ||||||
|  | 					{Version: "v1.0", ShouldExist: true}, | ||||||
|  | 					{Version: "test-3", ShouldExist: true}, | ||||||
|  | 					{Version: "test-4", ShouldExist: true}, | ||||||
|  | 				}, | ||||||
|  | 				Rule: &packages_model.PackageCleanupRule{ | ||||||
|  | 					Enabled:   true, | ||||||
|  | 					KeepCount: 2000, | ||||||
|  | 				}, | ||||||
|  | 			}, | ||||||
| 			{ | 			{ | ||||||
| 				Name: "KeepPattern", | 				Name: "KeepPattern", | ||||||
| 				Versions: []version{ | 				Versions: []version{ | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 forgejo-backport-action
				forgejo-backport-action