diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index b6279d092e..3b3c3e9836 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -3568,6 +3568,7 @@ owner.settings.cargo.rebuild = Rebuild index owner.settings.cargo.rebuild.description = Rebuilding can be useful if the index is not synchronized with the stored Cargo packages. owner.settings.cargo.rebuild.error = Failed to rebuild Cargo index: %v owner.settings.cargo.rebuild.success = The Cargo index was successfully rebuild. +owner.settings.cargo.rebuild.no_index = Cannot rebuild, no index is initialized. owner.settings.cleanuprules.title = Manage cleanup rules owner.settings.cleanuprules.add = Add cleanup rule owner.settings.cleanuprules.edit = Edit cleanup rule diff --git a/routers/web/shared/packages/packages.go b/routers/web/shared/packages/packages.go index 57671ad8f1..af960f1c0c 100644 --- a/routers/web/shared/packages/packages.go +++ b/routers/web/shared/packages/packages.go @@ -4,16 +4,19 @@ package packages import ( + "errors" "fmt" "net/http" "time" "code.gitea.io/gitea/models/db" packages_model "code.gitea.io/gitea/models/packages" + repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/optional" + "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/services/context" "code.gitea.io/gitea/services/forms" @@ -29,6 +32,12 @@ func SetPackagesContext(ctx *context.Context, owner *user_model.User) { } ctx.Data["CleanupRules"] = pcrs + + ctx.Data["CargoIndexExists"], err = repo_model.IsRepositoryModelExist(ctx, owner, cargo_service.IndexRepositoryName) + if err != nil { + ctx.ServerError("IsRepositoryModelExist", err) + return + } } func SetRuleAddContext(ctx *context.Context) { @@ -240,7 +249,11 @@ func RebuildCargoIndex(ctx *context.Context, owner *user_model.User) { err := cargo_service.RebuildIndex(ctx, owner, owner) if err != nil { log.Error("RebuildIndex failed: %v", err) - ctx.Flash.Error(ctx.Tr("packages.owner.settings.cargo.rebuild.error", err)) + if errors.Is(err, util.ErrNotExist) { + ctx.Flash.Error(ctx.Tr("packages.owner.settings.cargo.rebuild.no_index")) + } else { + ctx.Flash.Error(ctx.Tr("packages.owner.settings.cargo.rebuild.error", err)) + } } else { ctx.Flash.Success(ctx.Tr("packages.owner.settings.cargo.rebuild.success")) } diff --git a/services/packages/cargo/index.go b/services/packages/cargo/index.go index e8a8313625..59823cd3de 100644 --- a/services/packages/cargo/index.go +++ b/services/packages/cargo/index.go @@ -62,9 +62,9 @@ func InitializeIndexRepository(ctx context.Context, doer, owner *user_model.User } func RebuildIndex(ctx context.Context, doer, owner *user_model.User) error { - repo, err := getOrCreateIndexRepository(ctx, doer, owner) + repo, err := repo_model.GetRepositoryByOwnerAndName(ctx, owner.Name, IndexRepositoryName) if err != nil { - return err + return fmt.Errorf("GetRepositoryByOwnerAndName: %w", err) } ps, err := packages_model.GetPackagesByType(ctx, owner.ID, packages_model.TypeCargo) diff --git a/templates/package/shared/cargo.tmpl b/templates/package/shared/cargo.tmpl index 401d909002..5b0f63965d 100644 --- a/templates/package/shared/cargo.tmpl +++ b/templates/package/shared/cargo.tmpl @@ -3,13 +3,7 @@