cli/pkg/cmd/project/close/close.go
Ariel Deitcher ad4a489f8d
Introduce gh project commands (#7375)
Co-authored-by: Mislav Marohnić <mislav@github.com>
2023-06-09 16:57:01 +00:00

148 lines
3.6 KiB
Go

package close
import (
"fmt"
"strconv"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/pkg/cmd/project/shared/client"
"github.com/cli/cli/v2/pkg/cmd/project/shared/format"
"github.com/cli/cli/v2/pkg/cmd/project/shared/queries"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/shurcooL/githubv4"
"github.com/spf13/cobra"
)
type closeOpts struct {
number int32
owner string
reopen bool
projectID string
format string
}
type closeConfig struct {
io *iostreams.IOStreams
client *queries.Client
opts closeOpts
}
// the close command relies on the updateProjectV2 mutation
type updateProjectMutation struct {
UpdateProjectV2 struct {
ProjectV2 queries.Project `graphql:"projectV2"`
} `graphql:"updateProjectV2(input:$input)"`
}
func NewCmdClose(f *cmdutil.Factory, runF func(config closeConfig) error) *cobra.Command {
opts := closeOpts{}
closeCmd := &cobra.Command{
Short: "Close a project",
Use: "close [<number>]",
Example: heredoc.Doc(`
# close project "1" owned by monalisa
gh project close 1 --owner monalisa
# reopen closed project "1" owned by github
gh project close 1 --owner github --undo
`),
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := client.New(f)
if err != nil {
return err
}
if len(args) == 1 {
num, err := strconv.ParseInt(args[0], 10, 32)
if err != nil {
return cmdutil.FlagErrorf("invalid number: %v", args[0])
}
opts.number = int32(num)
}
config := closeConfig{
io: f.IOStreams,
client: client,
opts: opts,
}
// allow testing of the command without actually running it
if runF != nil {
return runF(config)
}
return runClose(config)
},
}
closeCmd.Flags().StringVar(&opts.owner, "owner", "", "Login of the owner. Use \"@me\" for the current user.")
closeCmd.Flags().BoolVar(&opts.reopen, "undo", false, "Reopen a closed project")
closeCmd.Flags().StringVar(&opts.format, "format", "", "Output format, must be 'json'")
return closeCmd
}
func runClose(config closeConfig) error {
canPrompt := config.io.CanPrompt()
owner, err := config.client.NewOwner(canPrompt, config.opts.owner)
if err != nil {
return err
}
project, err := config.client.NewProject(canPrompt, owner, config.opts.number, false)
if err != nil {
return err
}
config.opts.projectID = project.ID
query, variables := closeArgs(config)
err = config.client.Mutate("CloseProjectV2", query, variables)
if err != nil {
return err
}
if config.opts.format == "json" {
return printJSON(config, *project)
}
return printResults(config, query.UpdateProjectV2.ProjectV2)
}
func closeArgs(config closeConfig) (*updateProjectMutation, map[string]interface{}) {
closed := !config.opts.reopen
return &updateProjectMutation{}, map[string]interface{}{
"input": githubv4.UpdateProjectV2Input{
ProjectID: githubv4.ID(config.opts.projectID),
Closed: githubv4.NewBoolean(githubv4.Boolean(closed)),
},
"firstItems": githubv4.Int(0),
"afterItems": (*githubv4.String)(nil),
"firstFields": githubv4.Int(0),
"afterFields": (*githubv4.String)(nil),
}
}
func printResults(config closeConfig, project queries.Project) error {
if !config.io.IsStdoutTTY() {
return nil
}
var action string
if config.opts.reopen {
action = "Reopened"
} else {
action = "Closed"
}
_, err := fmt.Fprintf(config.io.Out, "%s project %s\n", action, project.URL)
return err
}
func printJSON(config closeConfig, project queries.Project) error {
b, err := format.JSONProject(project)
if err != nil {
return err
}
_, err = config.io.Out.Write(b)
return err
}