[MM-54730] Don't use old hardcoded rule for validating imported posts (#25823)

* don't use old hardcoded rule for validating imported posts

* fix http verb in doc

* Use client config

* Handle local mode

* E2E tests

* Enforce default if unable to use real limit

* Unit tests

* Fix tests

* Use model.PostMessageMaxRunesV2 as lower default

* Update direct post message length validation

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Co-authored-by: streamer45 <cstcld91@gmail.com>
Этот коммит содержится в:
Julien Tant
2024-04-19 10:45:32 -07:00
коммит произвёл GitHub
родитель 389990ebe5
Коммит ffc08858cf
9 изменённых файлов: 404 добавлений и 16 удалений

Просмотреть файл

@@ -95,7 +95,9 @@ var ImportValidateCmd = &cobra.Command{
Example: " import validate import_file.zip --team myteam --team myotherteam",
Short: "Validate an import file",
Args: cobra.ExactArgs(1),
RunE: importValidateCmdF,
RunE: func(command *cobra.Command, args []string) error {
return importValidateCmdF(nil, command, args)
},
}
func init() {
@@ -382,7 +384,14 @@ type Statistics struct {
Attachments uint64 `json:"attachments"`
}
func importValidateCmdF(command *cobra.Command, args []string) error {
type ImportValidationResult struct {
FileName string `json:"file_name"`
TotalLines uint64 `json:"total_lines"`
Elapsed time.Duration `json:"elapsed_time_ns"`
Errors []*importer.ImportValidationError `json:"errors"`
}
func importValidateCmdF(c client.Client, command *cobra.Command, args []string) error {
configurePrinter()
defer printer.Print("Validation complete\n")
@@ -391,9 +400,10 @@ func importValidateCmdF(command *cobra.Command, args []string) error {
serverChannels map[importer.ChannelTeam]*model.Channel
serverUsers map[string]*model.User
serverEmails map[string]*model.User
maxPostSize int
)
err := withClient(func(c client.Client, cmd *cobra.Command, args []string) error {
preRunWithClient := func(c client.Client, cmd *cobra.Command, args []string) error {
users, err := getPages(func(page, numPerPage int, etag string) ([]*model.User, *model.Response, error) {
return c.GetUsers(context.TODO(), page, numPerPage, etag)
}, DefaultPageSize)
@@ -401,6 +411,16 @@ func importValidateCmdF(command *cobra.Command, args []string) error {
return err
}
config, _, err := c.GetOldClientConfig(context.TODO(), "")
if err != nil {
return err
}
maxPostSize, err = strconv.Atoi(config["MaxPostSize"])
if err != nil {
return fmt.Errorf("failed to parse MaxPostSize: %w", err)
}
serverUsers = make(map[string]*model.User)
serverEmails = make(map[string]*model.User)
for _, user := range users {
@@ -442,9 +462,16 @@ func importValidateCmdF(command *cobra.Command, args []string) error {
}
return nil
})(command, args)
}
var err error
if c != nil {
err = preRunWithClient(c, command, args)
} else {
err = withClient(preRunWithClient)(command, args)
}
if err != nil {
printer.Print("could not initialize client, skipping online checks\n")
printer.Print(fmt.Sprintf("could not initialize client (%s), skipping online checks\n", err.Error()))
}
injectedTeams, err := command.Flags().GetStringArray("team")
@@ -476,6 +503,10 @@ func importValidateCmdF(command *cobra.Command, args []string) error {
return err
}
if maxPostSize == 0 {
maxPostSize = model.PostMessageMaxRunesV2
}
createMissingTeams := !checkMissingTeams && len(injectedTeams) == 0
validator := importer.NewValidator(
args[0], // input file
@@ -486,11 +517,14 @@ func importValidateCmdF(command *cobra.Command, args []string) error {
serverChannels, // map of existing channels
serverUsers, // map of users by name
serverEmails, // map of users by email
maxPostSize,
)
var errors []*importer.ImportValidationError
templateError := template.Must(template.New("").Parse("{{ .Error }}\n"))
validator.OnError(func(ive *importer.ImportValidationError) error {
printer.PrintPreparedT(templateError, ive)
errors = append(errors, ive)
return nil
})
@@ -529,11 +563,7 @@ func importValidateCmdF(command *cobra.Command, args []string) error {
}{unusedAttachments})
}
printer.PrintT("It took {{ .Elapsed }} to validate {{ .TotalLines }} lines in {{ .FileName }}\n", struct {
FileName string `json:"file_name"`
TotalLines uint64 `json:"total_lines"`
Elapsed time.Duration `json:"elapsed_time_ns"`
}{args[0], validator.Lines(), validator.Duration()})
printer.PrintT("It took {{ .Elapsed }} to validate {{ .TotalLines }} lines in {{ .FileName }}\n", ImportValidationResult{args[0], validator.Lines(), validator.Duration(), errors})
return nil
}