Skip to content

Commit

Permalink
[MER-2419] feat: av pr queue support (#188)
Browse files Browse the repository at this point in the history
* [MER-2419] feat: `av pr queue` support

* Fixes

---------

Co-authored-by: Travis DePrato <[email protected]>
  • Loading branch information
ErichKramer and twavv authored Jul 14, 2023
1 parent 1fc67c5 commit b3ef73d
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/av/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var prCmd = &cobra.Command{
func init() {
prCmd.AddCommand(
prCreateCmd,
prQueueCmd,
prStatusCmd,
)
}
103 changes: 103 additions & 0 deletions cmd/av/pr_queue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package main

import (
"context"
"errors"
"fmt"
"os"

"github.com/aviator-co/av/internal/utils/colors"
"github.com/sirupsen/logrus"

"github.com/aviator-co/av/internal/actions"
"github.com/aviator-co/av/internal/avgql"
"github.com/shurcooL/graphql"
"github.com/spf13/cobra"
)

var prQueueFlags struct {
SkipLine bool
Targets string
}

var prQueueCmd = &cobra.Command{
Use: "queue",
Short: "queue a pull request for the current branch",
SilenceUsage: true,
Args: cobra.NoArgs,
// error or reterr from emperror.dev/errors here?
RunE: func(cmd *cobra.Command, args []string) error {
repo, err := getRepo()
if err != nil {
return err
}

db, err := getDB(repo)
if err != nil {
return err
}

tx := db.ReadTx()
currentBranchName, err := repo.CurrentBranchName()
if err != nil {
return err
}

branch, _ := tx.Branch(currentBranchName)
if branch.PullRequest == nil {
return errors.New(
"this branch has no associated pull request (run `av pr create` to create one)",
)
}

prNumber := branch.PullRequest.Number
repository, exists := tx.Repository()
if !exists {
return actions.ErrRepoNotInitialized
}

var variables = map[string]interface{}{
"repoOwner": graphql.String(repository.Owner),
"repoName": graphql.String(repository.Name),
"prNumber": graphql.Int(prNumber),
}

// I have a feeling this would be better written inside of av/internals
client := avgql.NewClient()

var mutation struct {
QueuePullRequest struct {
QueuePullRequestPayload struct {
PullRequest struct {
// We don't currently use anything here, but we need to select
// at least one field to make the GraphQL query valid.
Status graphql.String
}
} `graphql:"... on QueuePullRequestPayload"`
} `graphql:"queuePullRequest(input: {repoOwner: $repoOwner, repoName:$repoName, number:$prNumber})"`
}

err = client.Mutate(context.Background(), &mutation, variables)
if err != nil {
logrus.WithError(err).Debug("failed to queue pull request")
return fmt.Errorf("failed to queue pull request: %s", err)
}
_, _ = fmt.Fprint(
os.Stderr,
"Queued pull request ", colors.UserInput(branch.PullRequest.Permalink), ".\n",
)

return nil
},
}

func init() {
prQueueCmd.Flags().BoolVar(
&prQueueFlags.SkipLine, "skip-line", false,
"skip in front of the existing pull requests, merge this pull request right now",
)
prQueueCmd.Flags().StringVarP(
&prQueueFlags.Targets, "targets", "t", "",
"additional targets affected by this pull request",
)
}

0 comments on commit b3ef73d

Please sign in to comment.