feature/gitea-integration #10

Merged
kjuulh merged 10 commits from feature/gitea-integration into v0.1 2022-09-18 00:10:44 +02:00
6 changed files with 72 additions and 14 deletions
Showing only changes of commit eef25c4c7a - Show all commits

View File

@ -3,9 +3,9 @@ name: write-a-readme
select: select:
repositories: repositories:
- git@git.front.kjuulh.io:kjuulh/kraken-test.git - git@git.front.kjuulh.io:kjuulh/kraken-test.git
providers: # providers:
- gitea: https://git.front.kjuulh.io # - gitea: https://git.front.kjuulh.io
organisation: "cibus" # organisation: "cibus"
actions: actions:
- type: go - type: go
entry: "main.go" entry: "main.go"

1
go.mod
View File

@ -49,6 +49,7 @@ require (
github.com/sirupsen/logrus v1.7.0 // indirect github.com/sirupsen/logrus v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/pflag v1.0.5 // indirect
github.com/ugorji/go/codec v1.2.7 // 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 github.com/xanzy/ssh-agent v0.3.2 // indirect
go.uber.org/atomic v1.10.0 // indirect go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.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.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 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY= 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.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0=
github.com/xanzy/ssh-agent v0.3.2 h1:eKj4SX2Fe7mui28ZgnFW5fmTz1EIr7ugo5s6wDxdHBM= github.com/xanzy/ssh-agent v0.3.2 h1:eKj4SX2Fe7mui28ZgnFW5fmTz1EIr7ugo5s6wDxdHBM=
github.com/xanzy/ssh-agent v0.3.2/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw= github.com/xanzy/ssh-agent v0.3.2/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=

View File

@ -3,6 +3,7 @@ package commands
import ( import (
"context" "context"
"fmt" "fmt"
"strings"
"sync" "sync"
"time" "time"
@ -11,6 +12,7 @@ import (
"git.front.kjuulh.io/kjuulh/kraken/internal/schema" "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/providers"
"git.front.kjuulh.io/kjuulh/kraken/internal/services/storage" "git.front.kjuulh.io/kjuulh/kraken/internal/services/storage"
giturls "github.com/whilp/git-urls"
"go.uber.org/zap" "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 { 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{ action, err := pr.actionCreator.Prepare(ctx, &actions.ActionCreatorOps{
RepositoryUrl: repository, RepositoryUrl: repository,
Branch: branch, Branch: branch,
@ -68,19 +68,14 @@ func (pr *ProcessRepos) Process(ctx context.Context, repository string, branch s
}() }()
err := pr.processRepo(ctx, repoUrl, action) err := pr.processRepo(ctx, repoUrl, action)
if err != nil { if err != nil {
errChan <- err pr.logger.Error("could not process repo", zap.Error(err))
} }
}(ctx, repoUrl) }(ctx, repoUrl)
} }
wg.Wait() wg.Wait()
close(errChan)
pr.logger.Debug("finished processing all repos", zap.Strings("repos", repositoryUrls)) pr.logger.Debug("finished processing all repos", zap.Strings("repos", repositoryUrls))
for err := range errChan {
return err
}
return nil return nil
} }
@ -122,11 +117,13 @@ func (pr *ProcessRepos) processRepo(ctx context.Context, repoUrl string, action
return err return err
} }
err = pr.commit(ctx, area, repo) err = pr.commit(ctx, area, repo, repoUrl)
if err != nil { if err != nil {
return err return err
} }
//pr.gitea.CreatePr(ctx, )
pr.logger.Debug("processing done", zap.String("path", area.Path), zap.String("repoUrl", repoUrl)) pr.logger.Debug("processing done", zap.String("path", area.Path), zap.String("repoUrl", repoUrl))
return nil return nil
} }
@ -167,7 +164,7 @@ func (pr *ProcessRepos) clone(ctx context.Context, area *storage.Area, repoUrl s
return repo, nil 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) _, err := pr.git.Add(ctx, area, repo)
if err != nil { if err != nil {
return fmt.Errorf("could not add file: %w", err) 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 { if err != nil {
return fmt.Errorf("could not push to repo: %w", err) 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 return nil
} }

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"sync" "sync"
"time"
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
"go.uber.org/zap" "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) client, err := g.getOrCreateClient(ctx, server)
if err != nil { if err != nil {
return nil, err return nil, err
@ -52,6 +57,35 @@ func (g *Gitea) ListRepositoriesForOrganization(ctx context.Context, server stri
return repoUrls, err 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) { func (g *Gitea) getOrCreateClient(ctx context.Context, server string) (*gitea.Client, error) {
g.giteamu.Lock() g.giteamu.Lock()
defer g.giteamu.Unlock() defer g.giteamu.Unlock()

View File

@ -30,6 +30,15 @@ type GitRepo struct {
repo *git.Repository 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 type GitAuth string
const ( const (