2022-09-12 22:12:02 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-09-18 16:49:34 +02:00
|
|
|
"strings"
|
2022-09-12 22:12:02 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-09-18 17:02:47 +02:00
|
|
|
"git.front.kjuulh.io/kjuulh/octopush/internal/actions"
|
|
|
|
"git.front.kjuulh.io/kjuulh/octopush/internal/gitproviders"
|
|
|
|
"git.front.kjuulh.io/kjuulh/octopush/internal/schema"
|
|
|
|
"git.front.kjuulh.io/kjuulh/octopush/internal/services/providers"
|
|
|
|
"git.front.kjuulh.io/kjuulh/octopush/internal/services/storage"
|
2022-09-18 16:49:34 +02:00
|
|
|
giturls "github.com/whilp/git-urls"
|
2022-09-12 22:12:02 +02:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
ProcessRepos struct {
|
2022-09-18 16:49:34 +02:00
|
|
|
logger *zap.Logger
|
|
|
|
storage *storage.Service
|
2022-09-18 17:02:47 +02:00
|
|
|
git *providers.GoGit
|
2022-09-18 16:49:34 +02:00
|
|
|
actionCreator *actions.ActionCreator
|
|
|
|
gitea *gitproviders.Gitea
|
2022-09-12 22:12:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ProcessReposDeps interface {
|
|
|
|
GetStorageService() *storage.Service
|
2022-09-18 17:02:47 +02:00
|
|
|
GetGitProvider() *providers.GoGit
|
2022-09-18 16:49:34 +02:00
|
|
|
GetActionCreator() *actions.ActionCreator
|
|
|
|
GetGitea() *gitproviders.Gitea
|
2022-09-12 22:12:02 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewProcessRepos(logger *zap.Logger, deps ProcessReposDeps) *ProcessRepos {
|
|
|
|
return &ProcessRepos{
|
2022-09-18 16:49:34 +02:00
|
|
|
logger: logger,
|
|
|
|
storage: deps.GetStorageService(),
|
|
|
|
git: deps.GetGitProvider(),
|
|
|
|
actionCreator: deps.GetActionCreator(),
|
|
|
|
gitea: deps.GetGitea(),
|
2022-09-12 22:12:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-18 16:49:34 +02:00
|
|
|
func (pr *ProcessRepos) Process(ctx context.Context, repository string, branch string, actionPath string) error {
|
|
|
|
action, err := pr.actionCreator.Prepare(ctx, &actions.ActionCreatorOps{
|
|
|
|
RepositoryUrl: repository,
|
|
|
|
Branch: branch,
|
|
|
|
Path: actionPath,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
repositoryUrls, err := pr.getRepoUrls(ctx, action.Schema)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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-18 16:49:34 +02:00
|
|
|
err := pr.processRepo(ctx, repoUrl, action)
|
2022-09-12 22:12:02 +02:00
|
|
|
if err != nil {
|
2022-09-18 16:49:34 +02:00
|
|
|
pr.logger.Error("could not process repo", zap.Error(err))
|
2022-09-12 22:12:02 +02:00
|
|
|
}
|
2022-09-18 16:49:34 +02:00
|
|
|
}(ctx, repoUrl)
|
|
|
|
}
|
2022-09-12 22:12:02 +02:00
|
|
|
|
2022-09-18 16:49:34 +02:00
|
|
|
wg.Wait()
|
|
|
|
pr.logger.Debug("finished processing all repos", zap.Strings("repos", repositoryUrls))
|
2022-09-12 22:12:02 +02:00
|
|
|
|
2022-09-18 16:49:34 +02:00
|
|
|
return nil
|
|
|
|
}
|
2022-09-12 22:12:02 +02:00
|
|
|
|
2022-09-18 17:02:47 +02:00
|
|
|
func (pr *ProcessRepos) getRepoUrls(ctx context.Context, schema *schema.OctopushSchema) ([]string, error) {
|
2022-09-18 16:49:34 +02:00
|
|
|
repoUrls := make([]string, 0)
|
2022-09-12 22:12:02 +02:00
|
|
|
|
2022-09-18 16:49:34 +02:00
|
|
|
repoUrls = append(repoUrls, schema.Select.Repositories...)
|
2022-09-12 22:12:02 +02:00
|
|
|
|
2022-09-18 16:49:34 +02:00
|
|
|
for _, provider := range schema.Select.Providers {
|
|
|
|
repos, err := pr.gitea.ListRepositoriesForOrganization(ctx, provider.Gitea, provider.Organisation)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-12 22:12:02 +02:00
|
|
|
|
2022-09-18 16:49:34 +02:00
|
|
|
repoUrls = append(repoUrls, repos...)
|
|
|
|
}
|
2022-09-12 22:12:02 +02:00
|
|
|
|
2022-09-18 16:49:34 +02:00
|
|
|
return repoUrls, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *ProcessRepos) processRepo(ctx context.Context, repoUrl string, action *actions.Action) error {
|
|
|
|
cleanup, area, err := pr.prepareAction(ctx)
|
|
|
|
defer func() {
|
|
|
|
if cleanup != nil {
|
|
|
|
cleanup(ctx)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-09-12 22:12:02 +02:00
|
|
|
}
|
|
|
|
|
2022-09-18 16:49:34 +02:00
|
|
|
repo, err := pr.clone(ctx, area, repoUrl)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(action.Schema.Queries) > 0 {
|
|
|
|
result, found, err := action.Query(ctx, area)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-12 22:12:02 +02:00
|
|
|
|
2022-09-18 16:49:34 +02:00
|
|
|
if found {
|
|
|
|
pr.logger.Info("Query result", zap.Strings("result", result))
|
|
|
|
// TODO: Append to real result, and return together
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(action.Schema.Actions) > 0 {
|
|
|
|
err = action.Execute(ctx, area)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pr.commit(ctx, area, repo, repoUrl)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pr.logger.Debug("processing done", zap.String("path", area.Path), zap.String("repoUrl", repoUrl))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *ProcessRepos) prepareAction(
|
|
|
|
ctx context.Context,
|
|
|
|
) (func(ctx context.Context), *storage.Area, error) {
|
|
|
|
pr.logger.Debug("Creating area")
|
|
|
|
area, err := pr.storage.CreateArea(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanupfunc := func(ctx context.Context) {
|
|
|
|
pr.logger.Debug("Removing area", zap.String("path", area.Path))
|
|
|
|
err = pr.storage.RemoveArea(ctx, area)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cleanupfunc, area, nil
|
|
|
|
}
|
|
|
|
|
2022-09-18 17:02:47 +02:00
|
|
|
func (pr *ProcessRepos) clone(ctx context.Context, area *storage.Area, repoUrl string) (*providers.GoGitRepo, error) {
|
2022-09-18 16:49:34 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return repo, nil
|
|
|
|
}
|
|
|
|
|
2022-09-18 17:02:47 +02:00
|
|
|
func (pr *ProcessRepos) commit(ctx context.Context, area *storage.Area, repo *providers.GoGitRepo, repoUrl string) error {
|
2022-09-18 16:49:34 +02:00
|
|
|
wt, err := pr.git.Add(ctx, area, repo)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not add file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
status, err := wt.Status()
|
|
|
|
if err != nil {
|
2022-09-12 22:12:02 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-18 16:49:34 +02:00
|
|
|
if status.IsClean() {
|
2022-09-18 17:02:47 +02:00
|
|
|
// TODO: check for pr
|
2022-09-18 16:49:34 +02:00
|
|
|
pr.logger.Info("Returning early, as no modifications are detected")
|
2022-09-18 17:02:47 +02:00
|
|
|
//return nil
|
2022-09-18 16:49:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = pr.git.Commit(ctx, repo)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get diff: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dryrun := false
|
|
|
|
if !dryrun {
|
|
|
|
|
|
|
|
err = pr.git.Push(ctx, repo)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not push to repo: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
url, err := giturls.Parse(repoUrl)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
head, err := repo.GetHEAD()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
path := strings.Split(url.Path, "/")
|
|
|
|
pr.logger.Debug("path string", zap.Strings("paths", path), zap.String("HEAD", head))
|
|
|
|
|
|
|
|
org := path[0]
|
|
|
|
repoName := path[1]
|
|
|
|
semanticName, _, ok := strings.Cut(repoName, ".")
|
|
|
|
if !ok {
|
|
|
|
semanticName = repoName
|
|
|
|
}
|
|
|
|
|
|
|
|
originHead, err := pr.git.GetOriginHEADForRepo(ctx, repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-18 17:02:47 +02:00
|
|
|
err = pr.gitea.CreatePr(ctx, fmt.Sprintf("%s://%s", "https", url.Host), org, semanticName, head, originHead, "octopush-apply")
|
2022-09-18 16:49:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-09-12 22:12:02 +02:00
|
|
|
return nil
|
|
|
|
}
|