From be40a497caa34f705eccf1662d26f218e1607e92 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Wed, 21 Sep 2022 22:46:55 +0200 Subject: [PATCH] fixed creating prs and returning early --- cmd/octopush/commands/process.go | 12 +++++++++++- go.mod | 2 +- go.sum | 4 ++-- internal/cli/cli.go | 6 +++--- internal/commands/process_repos.go | 3 ++- internal/services/providers/gogit.go | 18 +++++++++--------- 6 files changed, 28 insertions(+), 17 deletions(-) diff --git a/cmd/octopush/commands/process.go b/cmd/octopush/commands/process.go index 35ad362..dcfa8ba 100644 --- a/cmd/octopush/commands/process.go +++ b/cmd/octopush/commands/process.go @@ -1,6 +1,9 @@ package commands import ( + "context" + "time" + "git.front.kjuulh.io/kjuulh/octopush/internal/cli" "git.front.kjuulh.io/kjuulh/octopush/internal/commands" "github.com/spf13/cobra" @@ -23,11 +26,18 @@ func CreateOctopushProcessCmd(logger *zap.Logger) *cobra.Command { ctx := cmd.Context() - deps, err := cli.Start(ctx, logger) + deps, cleanupFunc, err := cli.Start(ctx, logger) if err != nil { return err } + defer func() { + ctx, _ = context.WithTimeout(ctx, time.Second*5) + if err := cleanupFunc(ctx); err != nil { + panic(err) + } + }() + err = commands. NewProcessRepos(logger, deps). Process(ctx, actionsRepo, branch, path) diff --git a/go.mod b/go.mod index ca837eb..42aca25 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.19 require ( code.gitea.io/sdk/gitea v0.15.1 - git.front.kjuulh.io/kjuulh/curre v1.3.1 + git.front.kjuulh.io/kjuulh/curre v1.3.5 github.com/ProtonMail/go-crypto v0.0.0-20220822140716-1678d6eb0cbe github.com/gin-contrib/zap v0.0.2 github.com/gin-gonic/gin v1.8.1 diff --git a/go.sum b/go.sum index 5ad6a5d..d326efc 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,8 @@ code.gitea.io/gitea-vet v0.2.1/go.mod h1:zcNbT/aJEmivCAhfmkHOlT645KNOf9W2KnkLgFjGGfE= code.gitea.io/sdk/gitea v0.15.1 h1:WJreC7YYuxbn0UDaPuWIe/mtiNKTvLN8MLkaw71yx/M= code.gitea.io/sdk/gitea v0.15.1/go.mod h1:klY2LVI3s3NChzIk/MzMn7G1FHrfU7qd63iSMVoHRBA= -git.front.kjuulh.io/kjuulh/curre v1.3.1 h1:fPVEAZgf6qST51IEOrrEiKNASvU0aaf8kKS/zqQ3z18= -git.front.kjuulh.io/kjuulh/curre v1.3.1/go.mod h1:m7WpSehONLqPh/XF3F0BI0UOpLOfGuDmDEFI1XsM6fE= +git.front.kjuulh.io/kjuulh/curre v1.3.5 h1:oKYh5Z0vInjViLnS4ppzK0G2Mnj7vXq8mA5i/rsWId4= +git.front.kjuulh.io/kjuulh/curre v1.3.5/go.mod h1:m7WpSehONLqPh/XF3F0BI0UOpLOfGuDmDEFI1XsM6fE= github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA= github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0= github.com/Microsoft/go-winio v0.5.2 h1:a9IhgEQBCUEk6QCdml9CiJGhAws+YwffDHEMp1VMrpA= diff --git a/internal/cli/cli.go b/internal/cli/cli.go index a2182fc..d128ad9 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -10,12 +10,12 @@ import ( "go.uber.org/zap" ) -func Start(ctx context.Context, logger *zap.Logger) (*serverdeps.ServerDeps, error) { +func Start(ctx context.Context, logger *zap.Logger) (*serverdeps.ServerDeps, curre.CleanupFunc, error) { deps := serverdeps.NewServerDeps(logger) readyChan := make(chan curre.ComponentsAreReady, 1) - err := curre.NewManager(). + cleanupFunc, err := curre.NewManager(). Register( server.NewStorageServer(logger.With(zap.Namespace("storage")), deps), ). @@ -26,5 +26,5 @@ func Start(ctx context.Context, logger *zap.Logger) (*serverdeps.ServerDeps, err <-readyChan - return deps, err + return deps, cleanupFunc, err } diff --git a/internal/commands/process_repos.go b/internal/commands/process_repos.go index fdf949b..316298e 100644 --- a/internal/commands/process_repos.go +++ b/internal/commands/process_repos.go @@ -189,8 +189,9 @@ func (pr *ProcessRepos) commit(ctx context.Context, area *storage.Area, repo *pr } if status.IsClean() { + // TODO: check for pr pr.logger.Info("Returning early, as no modifications are detected") - return nil + //return nil } err = pr.git.Commit(ctx, repo) diff --git a/internal/services/providers/gogit.go b/internal/services/providers/gogit.go index edff30a..3e1eacd 100644 --- a/internal/services/providers/gogit.go +++ b/internal/services/providers/gogit.go @@ -84,7 +84,7 @@ func (g *GoGit) GetOriginHEADForRepo(ctx context.Context, gitRepo *GoGitRepo) (s headRef := "" for _, ref := range refs { //g.logger.Debug(ref.String()) - if !ref.Name().IsBranch() { + if ref.Target().IsBranch() { headRef = ref.Target().Short() } } @@ -149,7 +149,7 @@ func (g *GoGit) Clone(ctx context.Context, storageArea *storage.Area, repoUrl st URL: repoUrl, Auth: auth, RemoteName: "origin", - ReferenceName: "refs/heads/main", + ReferenceName: "", SingleBranch: false, NoCheckout: false, Depth: 1, @@ -227,12 +227,12 @@ func (g *GoGit) CreateBranch(ctx context.Context, gitRepo *GoGitRepo) error { return fmt.Errorf("could not checkout branch: %w", err) } - remoteRef := plumbing.NewRemoteReferenceName("origin", "octopush-apply") - ref := plumbing.NewSymbolicReference(refSpec, remoteRef) - err = gitRepo.repo.Storer.SetReference(ref) - if err != nil { - return fmt.Errorf("could not set reference: %w", err) - } + //remoteRef := plumbing.NewRemoteReferenceName("origin", "octopush-apply") + //ref := plumbing.NewSymbolicReference(refSpec, remoteRef) + //err = gitRepo.repo.Storer.SetReference(ref) + //if err != nil { + // return fmt.Errorf("could not set reference: %w", err) + //} auth, err := g.GetAuth() if err != nil { @@ -241,7 +241,7 @@ func (g *GoGit) CreateBranch(ctx context.Context, gitRepo *GoGitRepo) error { err = worktree.PullContext(ctx, &git.PullOptions{ RemoteName: "origin", - ReferenceName: "refs/heads/main", + ReferenceName: "", SingleBranch: false, Depth: 1, Auth: auth,