forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer.go
More file actions
114 lines (96 loc) · 2.7 KB
/
transfer.go
File metadata and controls
114 lines (96 loc) · 2.7 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
package transfer
import (
"context"
"fmt"
"net/http"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/internal/ghinstance"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/cmd/issue/shared"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/shurcooL/githubv4"
"github.com/shurcooL/graphql"
"github.com/spf13/cobra"
)
type TransferOptions struct {
HttpClient func() (*http.Client, error)
Config func() (config.Config, error)
IO *iostreams.IOStreams
BaseRepo func() (ghrepo.Interface, error)
IssueSelector string
DestRepoSelector string
}
func NewCmdTransfer(f *cmdutil.Factory, runF func(*TransferOptions) error) *cobra.Command {
opts := TransferOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
Config: f.Config,
}
cmd := &cobra.Command{
Use: "transfer {<number> | <url>} <destination-repo>",
Short: "Transfer issue to another repository",
Args: cmdutil.ExactArgs(2, "issue and destination repository are required"),
RunE: func(cmd *cobra.Command, args []string) error {
opts.BaseRepo = f.BaseRepo
opts.IssueSelector = args[0]
opts.DestRepoSelector = args[1]
if runF != nil {
return runF(&opts)
}
return transferRun(&opts)
},
}
return cmd
}
func transferRun(opts *TransferOptions) error {
httpClient, err := opts.HttpClient()
if err != nil {
return err
}
apiClient := api.NewClientFromHTTP(httpClient)
issue, _, err := shared.IssueFromArg(apiClient, opts.BaseRepo, opts.IssueSelector)
if err != nil {
return err
}
destRepo, err := ghrepo.FromFullName(opts.DestRepoSelector)
if err != nil {
return err
}
url, err := issueTransfer(httpClient, issue.ID, destRepo)
if err != nil {
return err
}
_, err = fmt.Fprintln(opts.IO.Out, url)
return err
}
func issueTransfer(httpClient *http.Client, issueID string, destRepo ghrepo.Interface) (string, error) {
var destinationRepoID string
if r, ok := destRepo.(*api.Repository); ok {
destinationRepoID = r.ID
} else {
apiClient := api.NewClientFromHTTP(httpClient)
r, err := api.GitHubRepo(apiClient, destRepo)
if err != nil {
return "", err
}
destinationRepoID = r.ID
}
var mutation struct {
TransferIssue struct {
Issue struct {
URL string
}
} `graphql:"transferIssue(input: $input)"`
}
variables := map[string]interface{}{
"input": githubv4.TransferIssueInput{
IssueID: issueID,
RepositoryID: destinationRepoID,
},
}
gql := graphql.NewClient(ghinstance.GraphQLEndpoint(destRepo.RepoHost()), httpClient)
err := gql.MutateNamed(context.Background(), "IssueTransfer", &mutation, variables)
return mutation.TransferIssue.Issue.URL, err
}