with actions repo

This commit is contained in:
Kasper Juul Hermansen 2022-09-14 21:57:14 +02:00
parent ce55f6523c
commit e40e7cc88b
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
6 changed files with 55 additions and 34 deletions

View File

@ -23,7 +23,7 @@ func CreateKrakenProcessCmd() *cobra.Command {
}{
Repository: "git@git.front.kjuulh.io:kjuulh/kraken.git",
Branch: "v0.1",
Path: "_examples/actions/write_a_readme/kraken.yml",
Path: "_examples/actions/write_a_readme/",
})
if err != nil {
panic(err)

View File

@ -47,18 +47,13 @@ func (ac *ActionCreator) Prepare(ctx context.Context, ops *ActionCreatorOps) (*A
return nil, err
}
cloneCtx, _ := context.WithTimeout(ctx, time.Second*5)
repo, err := ac.git.Clone(cloneCtx, area, ops.RepositoryUrl)
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
}
err = ac.git.Checkout(ctx, repo, ops.Branch)
if err != nil {
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)
@ -74,6 +69,8 @@ func (ac *ActionCreator) Prepare(ctx context.Context, ops *ActionCreatorOps) (*A
return nil, err
}
ac.logger.Debug("Action creator done")
return &Action{
Schema: krakenSchema,
}, nil

View File

@ -34,6 +34,9 @@ func CommandRoute(logger *zap.Logger, app *gin.Engine, deps *serverdeps.ServerDe
ctx := context.WithValue(context.Background(), jobs.JobId{}, jobId)
processRepos := commands.NewProcessRepos(logger, deps)
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

@ -48,7 +48,7 @@ func (pr *ProcessRepos) Process(ctx context.Context, repository string, branch s
return err
}
repositoryUrls := make([]string, 0)
repositoryUrls := action.Schema.Select.Repositories
wg := sync.WaitGroup{}
wg.Add(len(repositoryUrls))
@ -60,7 +60,6 @@ func (pr *ProcessRepos) Process(ctx context.Context, repository string, branch s
}()
err := pr.processRepo(ctx, repoUrl, action)
if err != nil {
pr.logger.Error("could not process repo", zap.Error(err))
errChan <- err
}
}(ctx, repoUrl)
@ -113,7 +112,6 @@ func (pr *ProcessRepos) prepareAction(
pr.logger.Debug("Creating area")
area, err := pr.storage.CreateArea(ctx)
if err != nil {
pr.logger.Error("failed to allocate area", zap.Error(err))
return nil, nil, err
}
@ -133,13 +131,11 @@ func (pr *ProcessRepos) clone(ctx context.Context, area *storage.Area, repoUrl s
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))
return nil, err
}
err = pr.git.CreateBranch(ctx, repo)
if err != nil {
pr.logger.Error("could not create branch", zap.Error(err))
return nil, err
}
@ -157,9 +153,12 @@ func (pr *ProcessRepos) commit(ctx context.Context, area *storage.Area, repo *pr
return fmt.Errorf("could not get diff: %w", err)
}
dryrun := true
if !dryrun {
err = pr.git.Push(ctx, repo)
if err != nil {
return fmt.Errorf("could not push to repo: %w", err)
}
}
return nil
}

View File

@ -53,7 +53,7 @@ func NewGit(logger *zap.Logger, gitConfig *GitConfig, openPGP *signer.OpenPGP) *
return &Git{logger: logger, gitConfig: gitConfig, openPGP: openPGP}
}
func (g *Git) Clone(ctx context.Context, storageArea *storage.Area, repoUrl string) (*GitRepo, error) {
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),
@ -69,8 +69,8 @@ func (g *Git) Clone(ctx context.Context, storageArea *storage.Area, repoUrl stri
URL: repoUrl,
Auth: auth,
RemoteName: "origin",
ReferenceName: "refs/heads/main",
SingleBranch: false,
ReferenceName: plumbing.NewBranchReferenceName(branch),
SingleBranch: true,
NoCheckout: false,
Depth: 1,
RecurseSubmodules: 1,
@ -90,19 +90,41 @@ func (g *Git) Clone(ctx context.Context, storageArea *storage.Area, repoUrl stri
return &GitRepo{repo: repo}, nil
}
func (g *Git) Checkout(ctx context.Context, gitRepo *GitRepo, branch string) error {
wt, err := gitRepo.repo.Worktree()
func (g *Git) Clone(ctx context.Context, storageArea *storage.Area, repoUrl 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 err
return nil, err
}
return wt.Checkout(&git.CheckoutOptions{
Hash: [20]byte{},
Branch: plumbing.NewBranchReferenceName(branch),
Create: false,
Force: false,
Keep: false,
})
cloneOptions := git.CloneOptions{
URL: repoUrl,
Auth: auth,
RemoteName: "origin",
ReferenceName: "refs/heads/main",
SingleBranch: true,
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 {
return nil, err
}
g.logger.Debug("done cloning repo")
return &GitRepo{repo: repo}, nil
}
func (g *Git) getProgressWriter() *zapio.Writer {

View File

@ -11,14 +11,14 @@
### Not in scope
- [ ] Pooled runners
- [ ] CLI with options
- [ ] Server app
- [ ] Git hosting providers
- Pooled runners
- CLI with options
- Server app
- Git hosting providers
## Version 0.1
- [ ] Setup a way to choose actions and predicates
- [x] Setup a way to choose actions and predicates
- [ ] Allow instantiation of actions, kraken template repo etc.
- [ ] Create pr for gitea provider
- [ ] Think about some sort of isolation