From 1689ac5227a8082385da16fa20aa971c63e33bb1 Mon Sep 17 00:00:00 2001 From: Gusted Date: Mon, 6 Jun 2022 20:30:52 +0200 Subject: [PATCH 1/4] Add breaking change check in doctor - This patch introduces a new kind of doctor type, breaking. This file is made to register checks that helps with detecting when a breaking change might impact a Gitea instance. - For now the only check here(and the reason of creating this) is to check if all users in the database has a valid email address, which might not be the case after https://github.com/go-gitea/gitea/pull/17688. This _simply_ uses the validation function to detect and report these cases. - Helps admins with detecting #19897. - I have no clue which priority should be and IsDefault is true, because when breaking change happen and we have a doctor check for it, we can say "run `gitea doctor` to help you with this and maybe you find other errors :wink:". --- modules/doctor/breaking.go | 64 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 modules/doctor/breaking.go diff --git a/modules/doctor/breaking.go b/modules/doctor/breaking.go new file mode 100644 index 0000000000000..4d8889ee18cdb --- /dev/null +++ b/modules/doctor/breaking.go @@ -0,0 +1,64 @@ +package doctor + +import ( + "context" + "fmt" + + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/log" + "xorm.io/builder" +) + +func iterateUserAccounts(ctx context.Context, each func(*user.User) error) error { + err := db.Iterate( + ctx, + new(user.User), + builder.Gt{"id": 0}, + func(idx int, bean interface{}) error { + return each(bean.(*user.User)) + }, + ) + return err +} + +// Since 1.16.4 new restrictions has been set on email addresses. However users with invalid email +// addresses would be currently facing a error due to their invalid email address. +// Ref: https://github.com/go-gitea/gitea/pull/19085 & https://github.com/go-gitea/gitea/pull/17688 +func checkUserEmail(ctx context.Context, logger log.Logger, _ bool) error { + // We could use quirky SQL to get all users that start without a [a-zA-Z0-9], but that would mean + // DB provider-specific SQL and only works _now_. So instead we iterate trough all user accounts and + // use the user.ValidateEmail function to be future-proof. + var invalidUserCount int64 + if err := iterateUserAccounts(ctx, func(u *user.User) error { + // Only check for users, skip + if u.Type != user.UserTypeIndividual { + return nil + } + + if err := user.ValidateEmail(u.Email); err != nil { + invalidUserCount++ + logger.Warn("User[id=%d name=%q] have not a valid e-mail: %v", u.ID, u.Name, err) + } + return nil + }); err != nil { + return fmt.Errorf("iterateUserAccounts: %v", err) + } + + if invalidUserCount == 0 { + logger.Info("All users have a valid e-mail.") + } else { + logger.Warn("%d user(s) have a non-valid e-mail.", invalidUserCount) + } + return nil +} + +func init() { + Register(&Check{ + Title: "Check if users has an valid email address", + Name: "check-user-email", + IsDefault: true, + Run: checkUserEmail, + Priority: 9, + }) +} From d910bc5172d5027931fa0b54d9c1643b6cce8cfe Mon Sep 17 00:00:00 2001 From: Gusted Date: Mon, 6 Jun 2022 20:41:44 +0200 Subject: [PATCH 2/4] Makes no sense tbh --- modules/doctor/breaking.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/doctor/breaking.go b/modules/doctor/breaking.go index 4d8889ee18cdb..80b0058191239 100644 --- a/modules/doctor/breaking.go +++ b/modules/doctor/breaking.go @@ -57,7 +57,7 @@ func init() { Register(&Check{ Title: "Check if users has an valid email address", Name: "check-user-email", - IsDefault: true, + IsDefault: false, Run: checkUserEmail, Priority: 9, }) From cd96ac7e996dcb5a8e4446dcd5ec007d4b5d4e95 Mon Sep 17 00:00:00 2001 From: Gusted Date: Mon, 6 Jun 2022 20:55:44 +0200 Subject: [PATCH 3/4] Fix copyright --- modules/doctor/breaking.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/doctor/breaking.go b/modules/doctor/breaking.go index 80b0058191239..1f96421dfb36d 100644 --- a/modules/doctor/breaking.go +++ b/modules/doctor/breaking.go @@ -1,3 +1,7 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + package doctor import ( From ce78cd7b53570906dfd79c97b500c73314d64a66 Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Tue, 7 Jun 2022 13:40:52 -0400 Subject: [PATCH 4/4] Update modules/doctor/breaking.go Co-authored-by: Lunny Xiao --- modules/doctor/breaking.go | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/doctor/breaking.go b/modules/doctor/breaking.go index 1f96421dfb36d..c4b58d20fb26d 100644 --- a/modules/doctor/breaking.go +++ b/modules/doctor/breaking.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/log" + "xorm.io/builder" )