Co-authored-by: kjuulh <contact@kjuulh.io>
Reviewed-on: kjuulh/kraken#8
This commit is contained in:
2022-09-18 16:49:34 +02:00
parent 15b627a717
commit 1f46f6ac8d
31 changed files with 1111 additions and 129 deletions

View File

@@ -0,0 +1,77 @@
package actions
import (
"context"
"errors"
"git.front.kjuulh.io/kjuulh/kraken/internal/actions/builders"
"git.front.kjuulh.io/kjuulh/kraken/internal/actions/querier"
"git.front.kjuulh.io/kjuulh/kraken/internal/schema"
"git.front.kjuulh.io/kjuulh/kraken/internal/services/storage"
"go.uber.org/zap"
)
type Action struct {
Schema *schema.KrakenSchema
SchemaPath string
}
func (a *Action) Execute(ctx context.Context, area *storage.Area) error {
for _, action := range a.Schema.Actions {
switch action.Type {
case "go":
exe, err := builders.NewGo(zap.L()).Build(ctx, a.SchemaPath, action.Entry)
if err != nil {
return err
}
err = exe(ctx, area.Path)
if err != nil {
return err
}
zap.L().Debug("Execution done")
case "docker-build":
zap.L().Debug("Building docker-build")
runCmd, err := builders.NewDockerBuild(zap.L()).Build(ctx, a.SchemaPath, action.Entry)
if err != nil {
return err
}
err = runCmd(ctx, area.Path)
if err != nil {
return err
}
return nil
default:
return errors.New("could not determine action type")
}
}
return nil
}
func (a *Action) Query(ctx context.Context, area *storage.Area) ([]string, bool, error) {
for _, query := range a.Schema.Queries {
switch query.Type {
case "grep":
exe, err := querier.NewRipGrep(zap.L()).Build(ctx, a.SchemaPath, query.Query)
if err != nil {
return nil, false, err
}
output, found, err := exe(ctx, area.Path)
if err != nil {
return nil, false, err
}
zap.L().Debug("Execution done")
return output, found, nil
default:
return nil, false, errors.New("could not determine query type")
}
}
return nil, false, nil
}

View File

@@ -0,0 +1,85 @@
package actions
import (
"context"
"fmt"
"os"
"path"
"time"
"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"
"go.uber.org/zap"
)
type (
ActionCreatorOps struct {
RepositoryUrl string
Branch string
Path string
}
ActionCreator struct {
logger *zap.Logger
storage *storage.Service
git *providers.Git
}
ActionCreatorDeps interface {
GetStorageService() *storage.Service
GetGitProvider() *providers.Git
}
)
func NewActionCreator(logger *zap.Logger, deps ActionCreatorDeps) *ActionCreator {
return &ActionCreator{
logger: logger,
storage: deps.GetStorageService(),
git: deps.GetGitProvider(),
}
}
func (ac *ActionCreator) Prepare(ctx context.Context, ops *ActionCreatorOps) (*Action, error) {
area, err := ac.storage.CreateArea(ctx)
if err != nil {
ac.logger.Error("failed to allocate area", zap.Error(err))
return nil, err
}
cloneCtx, _ := context.WithTimeout(ctx, time.Second*10)
_, err = ac.git.CloneBranch(cloneCtx, area, ops.RepositoryUrl, ops.Branch)
if err != nil {
ac.logger.Error("could not clone repo", zap.Error(err))
return nil, err
}
executorUrl := path.Join(area.Path, ops.Path)
if _, err = os.Stat(executorUrl); os.IsNotExist(err) {
return nil, fmt.Errorf("path is invalid: %s", ops.Path)
}
contents, err := os.ReadFile(path.Join(executorUrl, "kraken.yml"))
if err != nil {
return nil, err
}
krakenSchema, err := schema.Unmarshal(string(contents))
if err != nil {
return nil, err
}
ac.logger.Debug("Action creator done")
return &Action{
Schema: krakenSchema,
SchemaPath: executorUrl,
}, nil
}
func (ac *ActionCreator) Cleanup(ctx context.Context, area *storage.Area) {
ac.logger.Debug("Removing area", zap.String("path", area.Path))
err := ac.storage.RemoveArea(ctx, area)
if err != nil {
panic(err)
}
}

View File

@@ -0,0 +1,95 @@
package builders
import (
"context"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"os"
"os/exec"
"go.uber.org/zap"
"go.uber.org/zap/zapio"
)
type DockerBuild struct {
logger *zap.Logger
}
func NewDockerBuild(logger *zap.Logger) *DockerBuild {
return &DockerBuild{logger: logger}
}
type DockerRunCommand func(ctx context.Context, victimPath string) error
func (g *DockerBuild) Build(ctx context.Context, modulePath, entryPath string) (DockerRunCommand, error) {
g.logger.Debug("Building docker image", zap.String("actiondir", modulePath), zap.String("entry", entryPath))
if _, err := os.Stat(fmt.Sprintf("%s/%s", modulePath, entryPath)); os.IsNotExist(err) {
return nil, errors.New("could not find entry")
}
b := make([]byte, 20)
_, err := rand.Reader.Read(b)
if err != nil {
return nil, err
}
tag := hex.EncodeToString(b)
buildDockerCmd := fmt.Sprintf("(cd %s; docker build -f %s --tag kraken/%s .)", modulePath, entryPath, tag)
g.logger.Debug("Running command", zap.String("command", buildDockerCmd))
cmd := exec.CommandContext(
ctx,
"/bin/bash",
"-c",
buildDockerCmd,
)
debugwriter := &zapio.Writer{
Log: g.logger,
Level: zap.DebugLevel,
}
defer debugwriter.Close()
cmd.Stdout = debugwriter
cmd.Stderr = debugwriter
err = cmd.Start()
if err != nil {
return nil, err
}
err = cmd.Wait()
if err != nil {
return nil, err
}
g.logger.Debug("Docker image built!")
return func(ctx context.Context, victimPath string) error {
g.logger.Debug("Executing script", zap.String("victim", victimPath))
cmd := exec.CommandContext(
ctx,
"/bin/bash",
"-c",
fmt.Sprintf("docker run --rm -v %s/:/src/work/ kraken/%s", victimPath, tag),
)
runDockerWriter := &zapio.Writer{
Log: g.logger,
Level: zap.DebugLevel,
}
defer runDockerWriter.Close()
cmd.Stdout = runDockerWriter
cmd.Stderr = runDockerWriter
err = cmd.Start()
if err != nil {
return err
}
return cmd.Wait()
}, nil
}

View File

@@ -0,0 +1,46 @@
package builders
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"go.uber.org/zap"
)
type Go struct {
logger *zap.Logger
}
func NewGo(logger *zap.Logger) *Go {
return &Go{logger: logger}
}
type GoExecutable func(ctx context.Context, victimPath string) error
func (g *Go) Build(ctx context.Context, modulePath, entryPath string) (GoExecutable, error) {
g.logger.Debug("Building go binary", zap.String("actiondir", modulePath), zap.String("entry", entryPath))
if _, err := os.Stat(fmt.Sprintf("%s/%s", modulePath, entryPath)); os.IsNotExist(err) {
return nil, errors.New("could not find entry")
}
err := exec.CommandContext(
ctx,
"/bin/bash",
"-c",
fmt.Sprintf("(cd %s; go build -o main %s)", modulePath, entryPath),
).Run()
if err != nil {
return nil, err
}
g.logger.Debug("Go binary built!")
return func(ctx context.Context, victimPath string) error {
g.logger.Debug("Executing script", zap.String("victim", victimPath))
return exec.CommandContext(ctx, "/bin/bash", "-c", fmt.Sprintf("(cd %s; %s/main)", victimPath, modulePath)).Run()
}, nil
}

View File

@@ -0,0 +1,106 @@
package querier
import (
"context"
"fmt"
"io"
"os/exec"
"strings"
"go.uber.org/zap"
"go.uber.org/zap/zapio"
)
type RipGrep struct {
logger *zap.Logger
}
func NewRipGrep(logger *zap.Logger) *RipGrep {
return &RipGrep{logger: logger}
}
type RipGrepCommand func(ctx context.Context, victimPath string) ([]string, bool, error)
func (g *RipGrep) Build(ctx context.Context, modulePath, query string) (RipGrepCommand, error) {
g.logger.Debug("Pulling docker image", zap.String("actiondir", modulePath), zap.String("query", query))
pullDockerImage := "docker pull mbologna/docker-ripgrep"
g.logger.Debug("Running command", zap.String("command", pullDockerImage))
cmd := exec.CommandContext(
ctx,
"/bin/bash",
"-c",
pullDockerImage,
)
debugwriter := &zapio.Writer{
Log: g.logger,
Level: zap.DebugLevel,
}
defer debugwriter.Close()
cmd.Stdout = debugwriter
cmd.Stderr = debugwriter
err := cmd.Start()
if err != nil {
return nil, err
}
err = cmd.Wait()
if err != nil {
return nil, err
}
g.logger.Debug("Docker image pulled")
return func(ctx context.Context, victimPath string) ([]string, bool, error) {
g.logger.Debug("Executing script", zap.String("victim", victimPath))
runRipGrepCmd := fmt.Sprintf("docker run --rm -v %s/:/data:ro mbologna/docker-ripgrep rg -i '%s' || true", victimPath, query)
g.logger.Debug("Execute ripgrep query", zap.String("command", runRipGrepCmd))
cmd := exec.CommandContext(
ctx,
"/bin/bash",
"-c",
runRipGrepCmd,
)
runDockerWriter := &zapio.Writer{
Log: g.logger,
Level: zap.DebugLevel,
}
defer runDockerWriter.Close()
builder := &strings.Builder{}
combinedWriter := io.MultiWriter(runDockerWriter, builder)
cmd.Stdout = combinedWriter
cmd.Stderr = combinedWriter
err = cmd.Start()
if err != nil {
return nil, false, err
}
err = cmd.Wait()
if err != nil {
return nil, false, err
}
contents := strings.Split(builder.String(), "\n")
validatedOutput := make([]string, 0)
for _, c := range contents {
if !strings.Contains(c, "WARNING:") {
validatedOutput = append(validatedOutput, c)
}
}
found := len(validatedOutput) > 0
return validatedOutput, found, nil
}, nil
}

View File

@@ -16,7 +16,9 @@ func CommandRoute(logger *zap.Logger, app *gin.Engine, deps *serverdeps.ServerDe
commandRoute := app.Group("commands")
commandRoute.POST("processRepos", func(c *gin.Context) {
type processReposRequest struct {
RepositoryUrls []string `json:"repositoryUrls"`
Repository string `json:"repository"`
Branch string `json:"branch"`
Path string `json:"path"`
}
var request processReposRequest
err := c.BindJSON(&request)
@@ -28,11 +30,14 @@ func CommandRoute(logger *zap.Logger, app *gin.Engine, deps *serverdeps.ServerDe
jobId := uuid.New().String()
go func(repositoryUrls []string, jobId string) {
go func(repository string, branch string, path string, jobId string) {
ctx := context.WithValue(context.Background(), jobs.JobId{}, jobId)
processRepos := commands.NewProcessRepos(logger, deps)
err = processRepos.Process(ctx, repositoryUrls)
}(request.RepositoryUrls, jobId)
err = processRepos.Process(ctx, repository, branch, path)
if err != nil {
logger.Error("could not process repo", zap.Error(err))
}
}(request.Repository, request.Branch, request.Path, jobId)
c.Status(http.StatusAccepted)
})

View File

@@ -3,150 +3,238 @@ package commands
import (
"context"
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
"sync"
"time"
"git.front.kjuulh.io/kjuulh/kraken/internal/services/actions"
"git.front.kjuulh.io/kjuulh/kraken/internal/actions"
"git.front.kjuulh.io/kjuulh/kraken/internal/gitproviders"
"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"
)
type (
ProcessRepos struct {
logger *zap.Logger
storage *storage.Service
git *providers.Git
action *actions.Action
logger *zap.Logger
storage *storage.Service
git *providers.Git
actionCreator *actions.ActionCreator
gitea *gitproviders.Gitea
}
ProcessReposDeps interface {
GetStorageService() *storage.Service
GetGitProvider() *providers.Git
GetAction() *actions.Action
GetActionCreator() *actions.ActionCreator
GetGitea() *gitproviders.Gitea
}
)
func NewProcessRepos(logger *zap.Logger, deps ProcessReposDeps) *ProcessRepos {
return &ProcessRepos{
logger: logger,
storage: deps.GetStorageService(),
git: deps.GetGitProvider(),
action: deps.GetAction(),
logger: logger,
storage: deps.GetStorageService(),
git: deps.GetGitProvider(),
actionCreator: deps.GetActionCreator(),
gitea: deps.GetGitea(),
}
}
func (pr *ProcessRepos) Process(ctx context.Context, repositoryUrls []string) error {
// Clone repos
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
}
wg := sync.WaitGroup{}
wg.Add(len(repositoryUrls))
errChan := make(chan error, 1)
for _, repoUrl := range repositoryUrls {
go func(ctx context.Context, repoUrl string) {
defer func() {
wg.Done()
}()
pr.logger.Debug("Creating area", zap.String("repoUrl", repoUrl))
area, err := pr.storage.CreateArea(ctx)
err := pr.processRepo(ctx, repoUrl, action)
if err != nil {
pr.logger.Error("failed to allocate area", zap.Error(err))
errChan <- err
return
pr.logger.Error("could not process repo", zap.Error(err))
}
defer func(ctx context.Context) {
pr.logger.Debug("Removing area", zap.String("path", area.Path), zap.String("repoUrl", repoUrl))
err = pr.storage.RemoveArea(ctx, area)
if err != nil {
errChan <- err
return
}
}(ctx)
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 {
pr.logger.Error("could not clone repo", zap.Error(err))
errChan <- err
return
}
err = pr.git.CreateBranch(ctx, repo)
if err != nil {
pr.logger.Error("could not create branch", zap.Error(err))
errChan <- err
return
}
err = pr.action.Run(
ctx,
area,
func(_ context.Context, area *storage.Area) (bool, error) {
pr.logger.Debug("checking predicate", zap.String("area", area.Path))
contains := false
filepath.WalkDir(area.Path, func(path string, d fs.DirEntry, err error) error {
if d.Name() == "roadmap.md" {
contains = true
}
return nil
})
return contains, nil
},
func(_ context.Context, area *storage.Area) error {
pr.logger.Debug("running action", zap.String("area", area.Path))
readme := path.Join(area.Path, "README.md")
file, err := os.Create(readme)
if err != nil {
return fmt.Errorf("could not create readme: %w", err)
}
_, err = file.WriteString("# Readme")
if err != nil {
return fmt.Errorf("could not write readme: %w", err)
}
_, 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)
}
return nil
}, false)
if err != nil {
pr.logger.Error("could not run action", zap.Error(err))
errChan <- err
return
}
err = pr.git.Push(ctx, repo)
if err != nil {
pr.logger.Error("could not push to repo", zap.Error(err))
errChan <- err
return
}
pr.logger.Debug("processing done", zap.String("path", area.Path), zap.String("repoUrl", repoUrl))
}(ctx, repoUrl)
}
wg.Wait()
close(errChan)
pr.logger.Debug("finished processing all repos")
for err := range errChan {
return err
}
pr.logger.Debug("finished processing all repos", zap.Strings("repos", repositoryUrls))
return nil
}
func (pr *ProcessRepos) getRepoUrls(ctx context.Context, schema *schema.KrakenSchema) ([]string, error) {
repoUrls := make([]string, 0)
repoUrls = append(repoUrls, schema.Select.Repositories...)
for _, provider := range schema.Select.Providers {
repos, err := pr.gitea.ListRepositoriesForOrganization(ctx, provider.Gitea, provider.Organisation)
if err != nil {
return nil, err
}
repoUrls = append(repoUrls, repos...)
}
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
}
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
}
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
}
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
}
return repo, nil
}
func (pr *ProcessRepos) commit(ctx context.Context, area *storage.Area, repo *providers.GitRepo, repoUrl string) error {
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 {
return err
}
if status.IsClean() {
pr.logger.Info("Returning early, as no modifications are detected")
return nil
}
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
}
err = pr.gitea.CreatePr(ctx, fmt.Sprintf("%s://%s", "https", url.Host), org, semanticName, head, originHead, "kraken-apply")
if err != nil {
return err
}
}
return nil
}

View File

@@ -0,0 +1,143 @@
package gitproviders
import (
"context"
"errors"
"fmt"
"os"
"sync"
"code.gitea.io/sdk/gitea"
"go.uber.org/zap"
)
type Gitea struct {
logger *zap.Logger
giteamu sync.Mutex
giteaClients map[string]*gitea.Client
}
func NewGitea(logger *zap.Logger) *Gitea {
return &Gitea{
logger: logger,
giteamu: sync.Mutex{},
giteaClients: make(map[string]*gitea.Client, 0),
}
}
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
}
g.logger.Debug("Listing repos for gitea", zap.String("server", server))
repos, resp, err := client.ListOrgRepos(organization, gitea.ListOrgReposOptions{
ListOptions: gitea.ListOptions{
Page: 0,
PageSize: 20,
},
})
if err != nil {
return nil, fmt.Errorf("could not list repos: %w", err)
}
if resp.StatusCode >= 300 {
return nil, fmt.Errorf("gitea responded with a non 200 status code (gitea response: %s)", resp.Status)
}
repoUrls := make([]string, len(repos))
for i, repo := range repos {
repoUrls[i] = repo.SSHURL
}
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
}
prs, _, err := client.ListRepoPullRequests(organization, repository, gitea.ListPullRequestsOptions{
ListOptions: gitea.ListOptions{
Page: 0,
PageSize: 30,
},
State: gitea.StateOpen,
Sort: "recentupdate",
Milestone: 0,
})
if err != nil {
return fmt.Errorf(
"could not list repos, needed because we need to check for conflicts. Original error: %w",
err,
)
}
for _, pr := range prs {
if pr.Head.Name == head {
g.logger.Info(
"returning early from creating pull-request, as it already exists.",
zap.String("repository", repository),
zap.String("pull-request", pr.URL),
)
return nil
}
}
pr, _, err := client.CreatePullRequest(organization, repository, gitea.CreatePullRequestOption{
Head: head,
Base: base,
Title: actionName,
})
if err != nil {
return err
}
g.logger.Debug(
"Created pr",
zap.String("repository", repository),
zap.String("branch", head),
zap.String("pull-request", pr.URL),
)
return nil
}
func (g *Gitea) getOrCreateClient(ctx context.Context, server string) (*gitea.Client, error) {
g.giteamu.Lock()
defer g.giteamu.Unlock()
client, ok := g.giteaClients[server]
if !ok || client == nil {
c, err := gitea.NewClient(server)
username, ok := os.LookupEnv("GITEA_USERNAME")
if !ok {
return nil, errors.New("missing environment variable GITEA_USERNAME")
}
apitoken, ok := os.LookupEnv("GITEA_API_TOKEN")
if !ok {
return nil, errors.New("missing environment variable GITEA_API_TOKEN")
}
c.SetBasicAuth(username, apitoken)
if err != nil {
return nil, err
}
g.giteaClients[server] = c
return c, nil
}
return client, nil
}

32
internal/schema/kraken.go Normal file
View File

@@ -0,0 +1,32 @@
package schema
import "gopkg.in/yaml.v3"
type KrakenSchema struct {
ApiVersion string `yaml:"apiVersion"`
Name string `yaml:"name"`
Select struct {
Repositories []string `yaml:"repositories"`
Providers []struct {
Gitea string `yaml:"gitea"`
Organisation string `yaml:"organisation"`
} `yaml:"providers"`
} `yaml:"select"`
Actions []struct {
Type string `yaml:"type"`
Entry string `yaml:"entry"`
} `yaml:"actions"`
Queries []struct {
Type string `yaml:"type"`
Query string `yaml:"query"`
} `yaml:"queries"`
}
func Unmarshal(raw string) (*KrakenSchema, error) {
k := &KrakenSchema{}
err := yaml.Unmarshal([]byte(raw), k)
if err != nil {
return nil, err
}
return k, nil
}

View File

@@ -1,6 +1,8 @@
package serverdeps
import (
actionc "git.front.kjuulh.io/kjuulh/kraken/internal/actions"
"git.front.kjuulh.io/kjuulh/kraken/internal/gitproviders"
"git.front.kjuulh.io/kjuulh/kraken/internal/services/actions"
"git.front.kjuulh.io/kjuulh/kraken/internal/services/providers"
"git.front.kjuulh.io/kjuulh/kraken/internal/services/signer"
@@ -59,6 +61,14 @@ func (deps *ServerDeps) GetAction() *actions.Action {
return actions.NewAction(deps.logger.With(zap.Namespace("action")))
}
func (deps *ServerDeps) GetActionCreator() *actionc.ActionCreator {
return actionc.NewActionCreator(deps.logger.With(zap.Namespace("action")), deps)
}
func (deps *ServerDeps) GetGitea() *gitproviders.Gitea {
return gitproviders.NewGitea(deps.logger.With(zap.Namespace("gitea")))
}
func (deps *ServerDeps) GetOpenPGP() *signer.OpenPGP {
return deps.openPGP
}

View File

@@ -2,6 +2,7 @@ package providers
import (
"context"
"errors"
"fmt"
"time"
@@ -30,6 +31,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 (
@@ -53,6 +63,76 @@ func NewGit(logger *zap.Logger, gitConfig *GitConfig, openPGP *signer.OpenPGP) *
return &Git{logger: logger, gitConfig: gitConfig, openPGP: openPGP}
}
func (g *Git) GetOriginHEADForRepo(ctx context.Context, gitRepo *GitRepo) (string, error) {
auth, err := g.GetAuth()
if err != nil {
return "", err
}
remote, err := gitRepo.repo.Remote("origin")
if err != nil {
return "", err
}
refs, err := remote.ListContext(ctx, &git.ListOptions{
Auth: auth,
})
if err != nil {
return "", err
}
headRef := ""
for _, ref := range refs {
//g.logger.Debug(ref.String())
if !ref.Name().IsBranch() {
headRef = ref.Target().Short()
}
}
if headRef == "" {
return "", errors.New("no upstream HEAD branch could be found")
}
return headRef, nil
}
func (g *Git) CloneBranch(ctx context.Context, storageArea *storage.Area, repoUrl string, branch string) (*GitRepo, error) {
g.logger.Debug(
"cloning repository",
zap.String("repoUrl", repoUrl),
zap.String("path", storageArea.Path),
)
auth, err := g.GetAuth()
if err != nil {
return nil, err
}
cloneOptions := git.CloneOptions{
URL: repoUrl,
Auth: auth,
RemoteName: "origin",
ReferenceName: plumbing.NewBranchReferenceName(branch),
SingleBranch: false,
NoCheckout: false,
Depth: 1,
RecurseSubmodules: 1,
Progress: g.getProgressWriter(),
Tags: 0,
InsecureSkipTLS: false,
CABundle: []byte{},
}
repo, err := git.PlainCloneContext(ctx, storageArea.Path, false, &cloneOptions)
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
return nil, err
}
g.logger.Debug("done cloning repo")
return &GitRepo{repo: repo}, nil
}
func (g *Git) Clone(ctx context.Context, storageArea *storage.Area, repoUrl string) (*GitRepo, error) {
g.logger.Debug(
"cloning repository",
@@ -70,7 +150,7 @@ func (g *Git) Clone(ctx context.Context, storageArea *storage.Area, repoUrl stri
Auth: auth,
RemoteName: "origin",
ReferenceName: "refs/heads/main",
SingleBranch: true,
SingleBranch: false,
NoCheckout: false,
Depth: 1,
RecurseSubmodules: 1,
@@ -162,7 +242,7 @@ func (g *Git) CreateBranch(ctx context.Context, gitRepo *GitRepo) error {
err = worktree.PullContext(ctx, &git.PullOptions{
RemoteName: "origin",
ReferenceName: "refs/heads/main",
SingleBranch: true,
SingleBranch: false,
Depth: 1,
Auth: auth,
RecurseSubmodules: 1,
@@ -171,7 +251,7 @@ func (g *Git) CreateBranch(ctx context.Context, gitRepo *GitRepo) error {
InsecureSkipTLS: false,
CABundle: []byte{},
})
if err != nil {
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
return fmt.Errorf("could not pull from origin: %w", err)
}
@@ -213,12 +293,11 @@ func (g *Git) Push(ctx context.Context, gitRepo *GitRepo) error {
Auth: auth,
Progress: g.getProgressWriter(),
Prune: false,
Force: false,
Force: true,
InsecureSkipTLS: false,
CABundle: []byte{},
RequireRemoteRefs: []config.RefSpec{},
})
if err != nil {
return err
}