mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 06:41:49 +08:00

* Add repository resources interface for export worker * Add mocks for repository resources * Add unit tests for ExportWorker's IsSupported method * Add unit tests for ExportWorker's Process method, covering scenarios for missing export settings, write permissions, branch restrictions, and client creation failures. * Fix unit tests * Single function * Add more unit tests * Add test for failed folder * Fail export folder errors * Add another test * Positive folder export * Too many folder export errors * Too many errors on folder export * Partial folder errors * Add test for nested folder * Add test dashboard export * More cases * Ignore existing dashboards * Fix folder tests * Fix clonable test * Add clone failure test * Add test clean up without push * Working tests * Use mock clonable * Add unit tests for IsWriteAllowed * Add behaviour to cover ref equal to configured branch * Fix worker test * Fix linting * Split clone and push * Wrapper for clone and push
45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package repository
|
|
|
|
import (
|
|
context "context"
|
|
"fmt"
|
|
|
|
"github.com/grafana/grafana-app-sdk/logging"
|
|
)
|
|
|
|
// WrapWithCloneAndPushIfPossible clones a repository if possible, executes operations on the clone,
|
|
// and automatically pushes changes when the function completes. For repositories that support cloning,
|
|
// all operations are transparently executed on the clone, and the clone is automatically cleaned up
|
|
// afterward. If cloning is not supported, the original repository instance is used directly.
|
|
func WrapWithCloneAndPushIfPossible(
|
|
ctx context.Context,
|
|
repo Repository,
|
|
cloneOptions CloneOptions,
|
|
pushOptions PushOptions,
|
|
fn func(repo Repository, cloned bool) error,
|
|
) error {
|
|
clonable, ok := repo.(ClonableRepository)
|
|
if !ok {
|
|
return fn(repo, false)
|
|
}
|
|
|
|
clone, err := clonable.Clone(ctx, cloneOptions)
|
|
if err != nil {
|
|
return fmt.Errorf("clone repository: %w", err)
|
|
}
|
|
|
|
// We don't, we simply log it
|
|
// FIXME: should we handle this differently?
|
|
defer func() {
|
|
if err := clone.Remove(ctx); err != nil {
|
|
logging.FromContext(ctx).Error("failed to remove cloned repository after export", "err", err)
|
|
}
|
|
}()
|
|
|
|
if err := fn(clone, true); err != nil {
|
|
return err
|
|
}
|
|
|
|
return clone.Push(ctx, pushOptions)
|
|
}
|