-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Expand file tree
/
Copy pathclose.go
More file actions
133 lines (112 loc) · 3.32 KB
/
Copy pathclose.go
File metadata and controls
133 lines (112 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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/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
exporter cmdutil.Exporter
}
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.ProjectMutationQuery `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")
cmdutil.AddFormatFlags(closeCmd, &opts.exporter)
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.exporter != nil {
return config.opts.exporter.Write(config.io, query.UpdateProjectV2.ProjectV2)
}
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.ProjectMutationQuery) error {
if !config.io.IsStdoutTTY() {
return nil
}
_, err := fmt.Fprintf(config.io.Out, "%s\n", project.URL)
return err
}