Files
mattermost/server
Felipe Martin 694e6f40dc fix: bot import panic when user exists without bot record (#36072)
* Fix bot import panic when user exists without bot record

The original importBot error handler re-declared `var appErr *model.AppError`
inside the CreateBot failure block, shadowing the outer appErr with a typed nil
pointer. When errors.As received this (*model.AppError)(nil), it was a non-nil
interface — so instead of returning false, it called AppError.Unwrap() on the
nil receiver, causing a panic.

This commit:
- Fixes the variable shadowing that caused the panic
- Adds recovery logic: when CreateBot fails because the username is taken,
  look up the existing user and create/update just the bot record
- Fixes pre-existing silent error swallowing in Bot().GetByUsername — now
  distinguishes store.ErrNotFound from real database errors
- Fixes pre-existing bug where DisplayName changes on re-import were lost
  because DisplayName is stored in Users.FirstName, not the Bots table
- Adds logging at every step of the recovery path (Info for normal flow,
  Warn for fallback/error paths)
- Uses distinct variable names (saveErr/updateErr) in the Save→Update
  fallback to avoid the same class of variable-reuse hazard
- Adds comprehensive test suite (11 subtests) covering dry-run, apply,
  re-import, recovery, regression/panic guard, idempotency, DisplayName
  update, and plugin-owner edge cases

* Address review findings: tighten assertions, fix error ID, add coverage

- Use require.ErrorAs for store.ErrNotFound instead of generic require.Error
  in the dry-run test, so it catches only not-found rather than any store error
- Add time.Sleep before idempotent re-import to ensure any real write would
  produce a different UpdateAt timestamp at millisecond resolution
- Fix misleading error ID "app.bot.createbot.internal_error" to
  "app.bot.update.internal_error" in the Update fallback path
- Add test for non-username CreateBot failure (email conflict) to cover
  the error passthrough at line 907-908

* Add missing i18n translations for bot import error strings

Add translation entries for app.bot.update.internal_error,
app.import.import_bot.lookup_error, and
app.import.import_bot.user_not_found.error to fix enterprise CI
i18n check failure.
2026-04-22 16:19:03 +02:00
..
2023-03-22 17:22:27 -04:00
2023-03-22 17:22:27 -04:00
2023-03-22 17:22:27 -04:00
2026-04-21 02:33:20 -07:00
2023-03-22 17:22:27 -04:00
2024-05-15 12:05:13 -03:00