2022-09-12 22:12:02 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-09-13 22:54:49 +02:00
|
|
|
"git.front.kjuulh.io/kjuulh/kraken/internal/actions"
|
2022-09-12 22:12:02 +02:00
|
|
|
"git.front.kjuulh.io/kjuulh/kraken/internal/services/providers"
|
|
|
|
"git.front.kjuulh.io/kjuulh/kraken/internal/services/storage"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
ProcessRepos struct {
|
2022-09-13 22:54:49 +02:00
|
|
|
logger *zap.Logger
|
|
|
|
storage *storage.Service
|
|
|
|
git *providers.Git
|
|
|
|
actionCreator *actions.ActionCreator
|
2022-09-12 22:12:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ProcessReposDeps interface {
|
|
|
|
GetStorageService() *storage.Service
|
|
|
|
GetGitProvider() *providers.Git
|
2022-09-13 22:54:49 +02:00
|
|
|
GetActionCreator() *actions.ActionCreator
|
2022-09-12 22:12:02 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewProcessRepos(logger *zap.Logger, deps ProcessReposDeps) *ProcessRepos {
|
|
|
|
return &ProcessRepos{
|
2022-09-13 22:54:49 +02:00
|
|
|
logger: logger,
|
|
|
|
storage: deps.GetStorageService(),
|
|
|
|
git: deps.GetGitProvider(),
|
|
|
|
actionCreator: deps.GetActionCreator(),
|
2022-09-12 22:12:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 22:54:49 +02:00
|
|
|
func (pr *ProcessRepos) Process(ctx context.Context, repository string, branch string, actionPath string) error {
|
2022-09-13 20:55:41 +02:00
|
|
|
errChan := make(chan error, 1)
|
|
|
|
|
2022-09-13 22:54:49 +02:00
|
|
|
action, err := pr.actionCreator.Prepare(ctx, &actions.ActionCreatorOps{
|
|
|
|
RepositoryUrl: repository,
|
|
|
|
Branch: branch,
|
|
|
|
Path: actionPath,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-14 21:57:14 +02:00
|
|
|
repositoryUrls := action.Schema.Select.Repositories
|
2022-09-13 22:54:49 +02:00
|
|
|
|
2022-09-12 22:12:02 +02:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(len(repositoryUrls))
|
|
|
|
|
|
|
|
for _, repoUrl := range repositoryUrls {
|
|
|
|
go func(ctx context.Context, repoUrl string) {
|
|
|
|
defer func() {
|
|
|
|
wg.Done()
|
|
|
|
}()
|
2022-09-13 22:54:49 +02:00
|
|
|
err := pr.processRepo(ctx, repoUrl, action)
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
}
|
2022-09-13 20:55:41 +02:00
|
|
|
}(ctx, repoUrl)
|
|
|
|
}
|
2022-09-12 22:12:02 +02:00
|
|
|
|
2022-09-13 20:55:41 +02:00
|
|
|
wg.Wait()
|
|
|
|
close(errChan)
|
|
|
|
pr.logger.Debug("finished processing all repos")
|
2022-09-12 22:12:02 +02:00
|
|
|
|
2022-09-13 20:55:41 +02:00
|
|
|
for err := range errChan {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-12 22:12:02 +02:00
|
|
|
|
2022-09-13 20:55:41 +02:00
|
|
|
return nil
|
|
|
|
}
|
2022-09-12 22:12:02 +02:00
|
|
|
|
2022-09-13 22:54:49 +02:00
|
|
|
func (pr *ProcessRepos) processRepo(ctx context.Context, repoUrl string, action *actions.Action) error {
|
2022-09-13 20:55:41 +02:00
|
|
|
cleanup, area, err := pr.prepareAction(ctx)
|
|
|
|
defer func() {
|
|
|
|
if cleanup != nil {
|
2022-09-13 22:54:49 +02:00
|
|
|
cleanup(ctx)
|
2022-09-13 20:55:41 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err != nil {
|
2022-09-13 22:54:49 +02:00
|
|
|
return err
|
2022-09-13 20:55:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
repo, err := pr.clone(ctx, area, repoUrl)
|
|
|
|
if err != nil {
|
2022-09-13 22:54:49 +02:00
|
|
|
return err
|
2022-09-13 20:55:41 +02:00
|
|
|
}
|
|
|
|
|
2022-09-13 22:54:49 +02:00
|
|
|
err = action.Execute(ctx, area)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-12 22:12:02 +02:00
|
|
|
|
2022-09-13 22:54:49 +02:00
|
|
|
err = pr.commit(ctx, area, repo)
|
2022-09-13 20:55:41 +02:00
|
|
|
if err != nil {
|
2022-09-13 22:54:49 +02:00
|
|
|
return err
|
2022-09-12 22:12:02 +02:00
|
|
|
}
|
|
|
|
|
2022-09-13 20:55:41 +02:00
|
|
|
pr.logger.Debug("processing done", zap.String("path", area.Path), zap.String("repoUrl", repoUrl))
|
2022-09-13 22:54:49 +02:00
|
|
|
return nil
|
2022-09-13 20:55:41 +02:00
|
|
|
}
|
2022-09-12 22:12:02 +02:00
|
|
|
|
2022-09-13 20:55:41 +02:00
|
|
|
func (pr *ProcessRepos) prepareAction(
|
|
|
|
ctx context.Context,
|
2022-09-13 22:54:49 +02:00
|
|
|
) (func(ctx context.Context), *storage.Area, error) {
|
2022-09-13 20:55:41 +02:00
|
|
|
pr.logger.Debug("Creating area")
|
|
|
|
area, err := pr.storage.CreateArea(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2022-09-13 22:54:49 +02:00
|
|
|
cleanupfunc := func(ctx context.Context) {
|
2022-09-13 20:55:41 +02:00
|
|
|
pr.logger.Debug("Removing area", zap.String("path", area.Path))
|
|
|
|
err = pr.storage.RemoveArea(ctx, area)
|
|
|
|
if err != nil {
|
2022-09-13 22:54:49 +02:00
|
|
|
panic(err)
|
2022-09-13 20:55:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cleanupfunc, area, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *ProcessRepos) clone(ctx context.Context, area *storage.Area, repoUrl string) (*providers.GitRepo, error) {
|
|
|
|
pr.logger.Debug("Cloning repo", zap.String("path", area.Path), zap.String("repoUrl", repoUrl))
|
|
|
|
cloneCtx, _ := context.WithTimeout(ctx, time.Second*5)
|
|
|
|
repo, err := pr.git.Clone(cloneCtx, area, repoUrl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pr.git.CreateBranch(ctx, repo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-09-12 22:12:02 +02:00
|
|
|
}
|
|
|
|
|
2022-09-13 20:55:41 +02:00
|
|
|
return repo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *ProcessRepos) commit(ctx context.Context, area *storage.Area, repo *providers.GitRepo) error {
|
|
|
|
_, err := pr.git.Add(ctx, area, repo)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not add file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pr.git.Commit(ctx, repo)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get diff: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-09-14 21:57:14 +02:00
|
|
|
dryrun := true
|
|
|
|
if !dryrun {
|
|
|
|
err = pr.git.Push(ctx, repo)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not push to repo: %w", err)
|
|
|
|
}
|
2022-09-13 20:55:41 +02:00
|
|
|
}
|
2022-09-12 22:12:02 +02:00
|
|
|
return nil
|
|
|
|
}
|