Without cibus

This commit is contained in:
Kasper Juul Hermansen 2022-09-17 18:28:38 +02:00
parent e3a672f9f7
commit eef25c4c7a
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
6 changed files with 72 additions and 14 deletions

View File

@ -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"

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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
}

View File

@ -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()

View File

@ -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 (