Files
upstream-mirror 240db4e2a7
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 00:29:55 +05:00

184 lines
4.1 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package commands
import (
"archive/zip"
"fmt"
"io"
"os"
"path/filepath"
"github.com/mattermost/mattermost/server/public/model"
"github.com/pkg/errors"
)
const (
DefaultPageSize = 200
MaxPageSize = 200
)
func checkInteractiveTerminal() error {
fileInfo, err := os.Stdout.Stat()
if err != nil {
return err
}
if (fileInfo.Mode() & os.ModeCharDevice) == 0 {
return errors.New("this is not an interactive shell")
}
return nil
}
func zipDir(zipPath, dir string) error {
zipFile, err := os.Create(zipPath)
if err != nil {
return fmt.Errorf("cannot create file %q: %w", zipPath, err)
}
defer zipFile.Close()
zipWriter := zip.NewWriter(zipFile)
defer zipWriter.Close()
if err := addToZip(zipWriter, dir, "."); err != nil {
return fmt.Errorf("could not add %q to zip: %w", dir, err)
}
return nil
}
func addToZip(zipWriter *zip.Writer, basedir, path string) error {
dirPath := filepath.Join(basedir, path)
dirEntries, err := os.ReadDir(dirPath)
if err != nil {
return fmt.Errorf("cannot read directory %q: %w", dirPath, err)
}
for _, dirEntry := range dirEntries {
fileInfo, err := dirEntry.Info()
if err != nil {
return fmt.Errorf("cannot get file info for directory entry %q: %w", dirEntry.Name(), err)
}
filePath := filepath.Join(path, fileInfo.Name())
if fileInfo.IsDir() {
filePath += "/"
}
header, err := zip.FileInfoHeader(fileInfo)
if err != nil {
return fmt.Errorf("cannot create zip file info header for %q path: %w", filePath, err)
}
header.Name = filePath
header.Method = zip.Deflate
w, err := zipWriter.CreateHeader(header)
if err != nil {
return fmt.Errorf("cannot create header for path %q: %w", filePath, err)
}
if fileInfo.IsDir() {
if err = addToZip(zipWriter, basedir, filePath); err != nil {
return err
}
continue
}
file, err := os.Open(filepath.Join(dirPath, fileInfo.Name()))
if err != nil {
return fmt.Errorf("cannot open file %q: %w", filePath, err)
}
_, err = io.Copy(w, file)
file.Close()
if err != nil {
return fmt.Errorf("cannot zip file contents for file %q: %w", filePath, err)
}
}
return nil
}
func getPages[T any](fn func(page, numPerPage int, etag string) ([]T, *model.Response, error), perPage int) ([]T, error) {
var (
results []T
etag string
)
for i := 0; ; i++ {
result, resp, err := fn(i, perPage, etag)
if err != nil {
return results, err
}
if len(result) == 0 {
break
}
results = append(results, result...)
etag = resp.Etag
}
return results, nil
}
func MergePluginConfigs(base, patch map[string]map[string]any) map[string]map[string]any {
mergedConfigs := map[string]map[string]any{}
for k, baseVal := range base {
if patchVal, ok := patch[k]; ok {
// both values are present, so we do a deep merge
mergedConfigs[k] = DeepMergeMaps(baseVal, patchVal)
continue
}
// value was not present in patch, so we use base
mergedConfigs[k] = baseVal
}
for k, patchVal := range patch {
if _, ok := mergedConfigs[k]; ok {
// value has already been merged
continue
}
// value was not present in base, so we use patch
mergedConfigs[k] = patchVal
}
return mergedConfigs
}
func DeepMergeMaps(base, patch map[string]any) map[string]any {
mergedMaps := map[string]any{}
for k, baseVal := range base {
if patchVal, ok := patch[k]; ok {
if baseMap, ok := baseVal.(map[string]any); ok {
if patchMap, ok := patchVal.(map[string]any); ok {
// both values are map, so we merge recursively
mergedMaps[k] = DeepMergeMaps(baseMap, patchMap)
continue
}
}
// either base is a map but patch is not, or base is not a
// map; in any case, patch has precedence
mergedMaps[k] = patchVal
continue
}
// patch doesn't contain the key, so we use base
mergedMaps[k] = baseVal
}
for k, patchVal := range patch {
if _, ok := mergedMaps[k]; ok {
// value has already been calculated
continue
}
// value was not present in base, so we directly use patch
mergedMaps[k] = patchVal
}
return mergedMaps
}