Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Wire meta.DB through codebase #94

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 42 additions & 22 deletions cmd/av/branchmeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,19 @@ var branchMetaDeleteCmd = &cobra.Command{
if err != nil {
return err
}
db, err := getDB(repo)
if err != nil {
return err
}
tx := db.WriteTx()
defer tx.Abort()
for _, branch := range args {
if err := meta.DeleteBranch(repo, branch); err != nil {
return err
}
tx.DeleteBranch(branch)
}
if branchMetaFlags.rebuildChildren {
if err := meta.RebuildChildren(repo); err != nil {
return err
}
meta.RebuildChildren(tx)
}
return nil
return tx.Commit()
},
}

Expand All @@ -68,10 +70,12 @@ var branchMetaListCmd = &cobra.Command{
if err != nil {
return err
}
branches, err := meta.ReadAllBranches(repo)
db, err := getDB(repo)
if err != nil {
return err
}
tx := db.ReadTx()
branches := tx.AllBranches()
bs, err := json.MarshalIndent(branches, "", " ")
if err != nil {
return err
Expand All @@ -89,15 +93,21 @@ var branchMetaRebuildChildrenCmd = &cobra.Command{
if err != nil {
return err
}
if err := meta.RebuildChildren(repo); err != nil {
db, err := getDB(repo)
if err != nil {
return err
}
tx := db.WriteTx()
meta.RebuildChildren(tx)
if err := tx.Commit(); err != nil {
return err
}
return nil
},
}

var branchMetaSetCmd = &cobra.Command{
Use: "set",
Use: "set branch-name",
Short: "modify the branch metadata",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
Expand All @@ -108,26 +118,36 @@ var branchMetaSetCmd = &cobra.Command{
if err != nil {
return err
}
db, err := getDB(repo)
if err != nil {
return err
}
if _, err := repo.RevParse(&git.RevParse{Rev: args[0]}); err != nil {
return errors.WrapIf(err, "cannot check if a branch exists")
}
br, _ := meta.ReadBranch(repo, args[0])
tx := db.WriteTx()
defer tx.Abort()
br, _ := tx.Branch(args[0])
if branchMetaFlags.parent != "" {
br.Parent, err = meta.ReadBranchState(repo, branchMetaFlags.parent, branchMetaFlags.trunk)
if err != nil {
return err
var parentHead string
if branchMetaFlags.trunk {
var err error
parentHead, err = repo.RevParse(&git.RevParse{Rev: branchMetaFlags.parent})
if err != nil {
return err
}
}
br.Parent = meta.BranchState{
Name: branchMetaFlags.parent,
Trunk: branchMetaFlags.trunk,
Head: parentHead,
}
}
if err := meta.WriteBranch(repo, br); err != nil {
return errors.WrapIff(err, "failed to write av internal metadata for branch %q", branchMetaFlags.parent)
}

tx.SetBranch(br)
if branchMetaFlags.rebuildChildren {
if err := meta.RebuildChildren(repo); err != nil {
return err
}
meta.RebuildChildren(tx)
}
return nil
return tx.Commit()
},
}

Expand Down
32 changes: 25 additions & 7 deletions cmd/av/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"context"
"fmt"
"github.com/aviator-co/av/internal/actions"
"github.com/aviator-co/av/internal/utils/cleanup"
"os"

"emperror.dev/errors"
Expand All @@ -19,15 +21,29 @@ import (
var fetchCmd = &cobra.Command{
Use: "fetch",
Short: "fetch latest state from GitHub",
RunE: func(cmd *cobra.Command, args []string) error {
repo, info, err := getRepoInfo()
RunE: func(cmd *cobra.Command, args []string) (reterr error) {
repo, err := getRepo()
if err != nil {
return err
}
branches, err := meta.ReadAllBranches(repo)
db, err := getDB(repo)
if err != nil {
return errors.Wrap(err, "failed to read av branch metadata")
return err
}

tx := db.WriteTx()
var cu cleanup.Cleanup
defer cu.Cleanup()
cu.Add(func() {
logrus.WithError(reterr).Debug("aborting db transaction")
tx.Abort()
})

info, ok := tx.Repository()
if !ok {
return actions.ErrRepoNotInitialized
}
branches := tx.AllBranches()

client, err := getClient(config.Av.GitHub.Token)
if err != nil {
Expand Down Expand Up @@ -95,9 +111,7 @@ var fetchCmd = &cobra.Command{
Number: pr.Number,
Permalink: pr.Permalink,
}
if err := meta.WriteBranch(repo, branchMeta); err != nil {
return errors.Wrap(err, "failed to write branch metadata")
}
tx.SetBranch(branchMeta)
}

if prsPage.HasNextPage {
Expand All @@ -107,6 +121,10 @@ var fetchCmd = &cobra.Command{
}
}

cu.Cancel()
if err := tx.Commit(); err != nil {
return err
}
_, _ = fmt.Fprint(
os.Stderr,
"Updated ", color.GreenString("%d", updatedCount), " pull requests",
Expand Down
38 changes: 29 additions & 9 deletions cmd/av/helpers.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
package main

import (
"emperror.dev/errors"
"github.com/aviator-co/av/internal/git"
"github.com/aviator-co/av/internal/meta"
"github.com/aviator-co/av/internal/meta/jsonfiledb"
"github.com/sirupsen/logrus"
"os/exec"
"strings"
)

func getRepoInfo() (*git.Repo, meta.Repository, error) {
repo, err := getRepo()
if err != nil {
return nil, meta.Repository{}, err
var cachedRepo *git.Repo

func getRepo() (*git.Repo, error) {
if cachedRepo == nil {
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
if rootFlags.Directory != "" {
cmd.Dir = rootFlags.Directory
}
toplevel, err := cmd.Output()
if err != nil {
return nil, errors.Wrap(err, "failed to determine repo toplevel (are you running inside a Git repo?)")
}
cachedRepo, err = git.OpenRepo(strings.TrimSpace(string(toplevel)))
if err != nil {
return nil, errors.Wrap(err, "failed to open git repo")
}
}
return cachedRepo, nil
}

repoMeta, err := meta.ReadRepository(repo)
func getDB(repo *git.Repo) (meta.DB, error) {
db, err := jsonfiledb.OpenRepo(repo)
if err != nil {
return nil, meta.Repository{}, err
return nil, err
}

logrus.Debugf("loaded repository metadata: %+v", repoMeta)
return repo, repoMeta, nil
if len(db.ReadTx().AllBranches()) == 0 {
logrus.Error("TODO: need to import existing ref metadata into database")
}
return db, nil
}
35 changes: 18 additions & 17 deletions cmd/av/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,33 @@ package main
import (
"context"
"fmt"
"github.com/aviator-co/av/internal/utils/cleanup"
"github.com/sirupsen/logrus"

"emperror.dev/errors"
"github.com/aviator-co/av/internal/config"
"github.com/aviator-co/av/internal/meta"
"github.com/spf13/cobra"
)

var initFlags struct {
Force bool
}
var initCmd = &cobra.Command{
Use: "init",
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) (reterr error) {
repo, err := getRepo()
if err != nil {
return err
}

if !initFlags.Force {
_, err := meta.ReadRepository(repo)
if err == nil {
return errors.New("repository is already initialized for use with av")
}
db, err := getDB(repo)
if err != nil {
return err
}
tx := db.WriteTx()
cu := cleanup.New(func() {
logrus.WithError(reterr).Debug("aborting db transaction")
tx.Abort()
})
defer cu.Cleanup()

if config.Av.GitHub.Token == "" {
return errors.New("github token must be set")
Expand All @@ -46,19 +49,17 @@ var initCmd = &cobra.Command{
return err
}

if err := meta.WriteRepository(repo, meta.Repository{
tx.SetRepository(meta.Repository{
ID: ghRepo.ID,
Owner: ghRepo.Owner.Login,
Name: ghRepo.Name,
}); err != nil {
return errors.WrapIff(err, "failed to write repository metadata")
}
})

cu.Cancel()
if err := tx.Commit(); err != nil {
return err
}
_, _ = fmt.Println("Successfully initialized repository for use with av!")
return nil
},
}

func init() {
initCmd.Flags().BoolVar(&initFlags.Force, "force", false, "force initialization even if metadata already exists")
}
23 changes: 0 additions & 23 deletions cmd/av/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ package main
import (
"fmt"
"os"
"os/exec"
"strings"
"sync"
"time"

"emperror.dev/errors"
"github.com/aviator-co/av/internal/config"
"github.com/aviator-co/av/internal/gh"
"github.com/aviator-co/av/internal/git"
"github.com/fatih/color"
"github.com/kr/text"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -160,26 +157,6 @@ func checkCliVersion() {
}
}

var cachedRepo *git.Repo

func getRepo() (*git.Repo, error) {
if cachedRepo == nil {
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
if rootFlags.Directory != "" {
cmd.Dir = rootFlags.Directory
}
toplevel, err := cmd.Output()
if err != nil {
return nil, errors.Wrap(err, "failed to determine repo toplevel (are you running inside a Git repo?)")
}
cachedRepo, err = git.OpenRepo(strings.TrimSpace(string(toplevel)))
if err != nil {
return nil, errors.Wrap(err, "failed to open git repo")
}
}
return cachedRepo, nil
}

var once sync.Once
var lazyGithubClient *gh.Client

Expand Down
14 changes: 12 additions & 2 deletions cmd/av/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Examples:
> Can you please review it?
> EOF
`),
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) (reterr error) {
repo, err := getRepo()
if err != nil {
return err
Expand All @@ -55,6 +55,13 @@ Examples:
return err
}

db, err := getDB(repo)
if err != nil {
return err
}
tx := db.WriteTx()
defer tx.Abort()

body := prCreateFlags.Body
// Special case: ready body from stdin
if prCreateFlags.Body == "-" {
Expand All @@ -66,7 +73,7 @@ Examples:
}

if _, err := actions.CreatePullRequest(
context.Background(), repo, client,
context.Background(), repo, client, tx,
actions.CreatePullRequestOpts{
BranchName: branchName,
Title: prCreateFlags.Title,
Expand All @@ -83,6 +90,9 @@ Examples:
); err != nil {
return err
}
if err := tx.Commit(); err != nil {
return err
}
return nil
},
}
Expand Down
Loading