with commit

This commit is contained in:
Kasper Juul Hermansen 2022-09-12 12:35:10 +02:00
parent f78d84dc8f
commit 0212ce9df4
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
2 changed files with 87 additions and 4 deletions

View File

@ -2,6 +2,11 @@ package commands
import ( import (
"context" "context"
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
"sync" "sync"
"time" "time"
@ -66,7 +71,7 @@ func (pr *ProcessRepos) Process(ctx context.Context, repositoryUrls []string) er
pr.logger.Debug("Cloning repo", zap.String("path", area.Path), zap.String("repoUrl", repoUrl)) pr.logger.Debug("Cloning repo", zap.String("path", area.Path), zap.String("repoUrl", repoUrl))
cloneCtx, _ := context.WithTimeout(ctx, time.Second*5) cloneCtx, _ := context.WithTimeout(ctx, time.Second*5)
_, err = pr.git.Clone(cloneCtx, area, repoUrl) repo, err := pr.git.Clone(cloneCtx, area, repoUrl)
if err != nil { if err != nil {
pr.logger.Error("could not clone repo", zap.Error(err)) pr.logger.Error("could not clone repo", zap.Error(err))
errChan <- err errChan <- err
@ -76,12 +81,39 @@ func (pr *ProcessRepos) Process(ctx context.Context, repositoryUrls []string) er
err = pr.action.Run( err = pr.action.Run(
ctx, ctx,
area, area,
func(ctx context.Context, area *storage.Area) (bool, error) { func(_ context.Context, area *storage.Area) (bool, error) {
pr.logger.Debug("checking predicate", zap.String("area", area.Path)) pr.logger.Debug("checking predicate", zap.String("area", area.Path))
return true, nil 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(ctx context.Context, area *storage.Area) error { func(_ context.Context, area *storage.Area) error {
pr.logger.Debug("running action", zap.String("area", area.Path)) 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 return nil
}, false) }, false)

View File

@ -2,9 +2,11 @@ package providers
import ( import (
"context" "context"
"time"
"git.front.kjuulh.io/kjuulh/kraken/internal/services/storage" "git.front.kjuulh.io/kjuulh/kraken/internal/services/storage"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport" "github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/http" "github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/plumbing/transport/ssh" "github.com/go-git/go-git/v5/plumbing/transport/ssh"
@ -86,6 +88,55 @@ func (g *Git) Clone(ctx context.Context, storageArea *storage.Area, repoUrl stri
return &GitRepo{repo: repo}, nil return &GitRepo{repo: repo}, nil
} }
func (g *Git) Add(ctx context.Context, storageArea *storage.Area, gitRepo *GitRepo) (*git.Worktree, error) {
worktree, err := gitRepo.repo.Worktree()
if err != nil {
return nil, err
}
err = worktree.AddWithOptions(&git.AddOptions{
All: true,
})
if err != nil {
return nil, err
}
status, err := worktree.Status()
if err != nil {
return nil, err
}
g.logger.Info("git status", zap.String("status", status.String()))
return worktree, nil
}
func (g *Git) Commit(ctx context.Context, gitRepo *GitRepo) error {
worktree, err := gitRepo.repo.Worktree()
if err != nil {
return err
}
_, err = worktree.Commit("some-commit", &git.CommitOptions{
All: true,
Author: &object.Signature{
Name: "kraken",
Email: "kraken@kasperhermansen.com",
When: time.Now(),
},
Committer: &object.Signature{
Name: "kraken",
Email: "kraken@kasperhermansen.com",
When: time.Now(),
},
})
if err != nil {
return err
}
return nil
}
func (g *Git) GetAuth() (transport.AuthMethod, error) { func (g *Git) GetAuth() (transport.AuthMethod, error) {
switch g.gitConfig.AuthOption { switch g.gitConfig.AuthOption {
case GIT_AUTH_SSH: case GIT_AUTH_SSH: