diff --git a/_examples/actions/write_a_readme/kraken.yml b/_examples/actions/write_a_readme/kraken.yml index e4065e6..0e5c44e 100644 --- a/_examples/actions/write_a_readme/kraken.yml +++ b/_examples/actions/write_a_readme/kraken.yml @@ -3,9 +3,9 @@ name: write-a-readme select: repositories: - git@git.front.kjuulh.io:kjuulh/kraken-test.git - providers: - - gitea: https://git.front.kjuulh.io - organisation: "cibus" + # providers: + # - gitea: https://git.front.kjuulh.io + # organisation: "cibus" actions: - type: go entry: "main.go" diff --git a/go.mod b/go.mod index 9b543eb..2fab6ad 100644 --- a/go.mod +++ b/go.mod @@ -49,6 +49,7 @@ require ( github.com/sirupsen/logrus v1.7.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/ugorji/go/codec v1.2.7 // indirect + github.com/whilp/git-urls v1.0.0 // indirect github.com/xanzy/ssh-agent v0.3.2 // indirect go.uber.org/atomic v1.10.0 // indirect go.uber.org/multierr v1.8.0 // indirect diff --git a/go.sum b/go.sum index 9f45bfb..5f09503 100644 --- a/go.sum +++ b/go.sum @@ -153,6 +153,8 @@ github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6 github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0= github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY= +github.com/whilp/git-urls v1.0.0 h1:95f6UMWN5FKW71ECsXRUd3FVYiXdrE7aX4NZKcPmIjU= +github.com/whilp/git-urls v1.0.0/go.mod h1:J16SAmobsqc3Qcy98brfl5f5+e0clUvg1krgwk/qCfE= github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0= github.com/xanzy/ssh-agent v0.3.2 h1:eKj4SX2Fe7mui28ZgnFW5fmTz1EIr7ugo5s6wDxdHBM= github.com/xanzy/ssh-agent v0.3.2/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw= diff --git a/internal/commands/process_repos.go b/internal/commands/process_repos.go index becfb03..00634f4 100644 --- a/internal/commands/process_repos.go +++ b/internal/commands/process_repos.go @@ -3,6 +3,7 @@ package commands import ( "context" "fmt" + "strings" "sync" "time" @@ -11,6 +12,7 @@ import ( "git.front.kjuulh.io/kjuulh/kraken/internal/schema" "git.front.kjuulh.io/kjuulh/kraken/internal/services/providers" "git.front.kjuulh.io/kjuulh/kraken/internal/services/storage" + giturls "github.com/whilp/git-urls" "go.uber.org/zap" ) @@ -42,8 +44,6 @@ func NewProcessRepos(logger *zap.Logger, deps ProcessReposDeps) *ProcessRepos { } func (pr *ProcessRepos) Process(ctx context.Context, repository string, branch string, actionPath string) error { - errChan := make(chan error, 1) - action, err := pr.actionCreator.Prepare(ctx, &actions.ActionCreatorOps{ RepositoryUrl: repository, Branch: branch, @@ -68,19 +68,14 @@ func (pr *ProcessRepos) Process(ctx context.Context, repository string, branch s }() err := pr.processRepo(ctx, repoUrl, action) if err != nil { - errChan <- err + pr.logger.Error("could not process repo", zap.Error(err)) } }(ctx, repoUrl) } wg.Wait() - close(errChan) pr.logger.Debug("finished processing all repos", zap.Strings("repos", repositoryUrls)) - for err := range errChan { - return err - } - return nil } @@ -122,11 +117,13 @@ func (pr *ProcessRepos) processRepo(ctx context.Context, repoUrl string, action return err } - err = pr.commit(ctx, area, repo) + err = pr.commit(ctx, area, repo, repoUrl) if err != nil { return err } + //pr.gitea.CreatePr(ctx, ) + pr.logger.Debug("processing done", zap.String("path", area.Path), zap.String("repoUrl", repoUrl)) return nil } @@ -167,7 +164,7 @@ func (pr *ProcessRepos) clone(ctx context.Context, area *storage.Area, repoUrl s return repo, nil } -func (pr *ProcessRepos) commit(ctx context.Context, area *storage.Area, repo *providers.GitRepo) error { +func (pr *ProcessRepos) commit(ctx context.Context, area *storage.Area, repo *providers.GitRepo, repoUrl string) error { _, err := pr.git.Add(ctx, area, repo) if err != nil { return fmt.Errorf("could not add file: %w", err) @@ -184,6 +181,21 @@ func (pr *ProcessRepos) commit(ctx context.Context, area *storage.Area, repo *pr 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)) + + pr.gitea.CreatePr(ctx, url.Host, path[0], repoUrl, head, "", "kraken-apply") } return nil } diff --git a/internal/gitproviders/gitea.go b/internal/gitproviders/gitea.go index ea85d1e..4ba214d 100644 --- a/internal/gitproviders/gitea.go +++ b/internal/gitproviders/gitea.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "sync" + "time" "code.gitea.io/sdk/gitea" "go.uber.org/zap" @@ -23,7 +24,11 @@ func NewGitea(logger *zap.Logger) *Gitea { } } -func (g *Gitea) ListRepositoriesForOrganization(ctx context.Context, server string, organization string) ([]string, error) { +func (g *Gitea) ListRepositoriesForOrganization( + ctx context.Context, + server string, + organization string, +) ([]string, error) { client, err := g.getOrCreateClient(ctx, server) if err != nil { return nil, err @@ -52,6 +57,35 @@ func (g *Gitea) ListRepositoriesForOrganization(ctx context.Context, server stri return repoUrls, err } +func (g *Gitea) CreatePr( + ctx context.Context, + server string, + organization string, + repository string, + head string, + base string, + actionName string, +) error { + client, err := g.getOrCreateClient(ctx, server) + if err != nil { + return err + } + + client.CreatePullRequest(organization, repository, gitea.CreatePullRequestOption{ + Head: head, + Base: base, + Title: actionName, + Body: "", + Assignee: "", + Assignees: []string{}, + Milestone: 0, + Labels: []int64{}, + Deadline: &time.Time{}, + }) + + return nil +} + func (g *Gitea) getOrCreateClient(ctx context.Context, server string) (*gitea.Client, error) { g.giteamu.Lock() defer g.giteamu.Unlock() diff --git a/internal/services/providers/git.go b/internal/services/providers/git.go index 4a91121..df13898 100644 --- a/internal/services/providers/git.go +++ b/internal/services/providers/git.go @@ -30,6 +30,15 @@ type GitRepo struct { repo *git.Repository } +func (gr *GitRepo) GetHEAD() (string, error) { + head, err := gr.repo.Head() + if err != nil { + return "", err + } + + return head.Name().Short(), nil +} + type GitAuth string const (