-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcheckout.go
147 lines (117 loc) · 3.14 KB
/
checkout.go
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package cmd
import (
"context"
"errors"
"fmt"
"log"
"github.com/google/go-github/v67/github"
"github.com/ldez/prm/v3/choose"
"github.com/ldez/prm/v3/config"
"github.com/ldez/prm/v3/local"
"github.com/ldez/prm/v3/types"
)
// InteractiveCheckout checkout a PR.
func InteractiveCheckout(conf *config.Configuration) error {
baseRepository, err := types.GetRepository(conf.BaseRemote)
if err != nil {
return err
}
// Display PRs from GitHub
number, err := getPRNumberFromGitHub(baseRepository)
if err != nil {
return err
}
if number == choose.ExitValue {
return nil
}
checkoutOptions := &types.CheckoutOptions{Number: number}
return Checkout(checkoutOptions)
}
func getPRNumberFromGitHub(baseRepository *types.Repository) (int, error) {
ctx := context.Background()
client := newGitHubClient(ctx)
opt := &github.PullRequestListOptions{
State: "open",
ListOptions: github.ListOptions{PerPage: 50},
}
prs, _, err := client.PullRequests.List(ctx, baseRepository.Owner, baseRepository.Name, opt)
if err != nil {
return 0, fmt.Errorf("fail to retrieve pull request from GitHub: %w", err)
}
return choose.RemotePulRequest(prs)
}
// Checkout checkout a PR.
func Checkout(options *types.CheckoutOptions) error {
// get configuration
confs, err := config.ReadFile()
if err != nil {
return err
}
repoDir, err := local.GetGitRepoRoot()
if err != nil {
return err
}
conf, err := config.Find(confs, repoDir)
if err != nil {
return err
}
// check if already exists in config
pr, err := conf.FindPullRequests(options.Number)
if err == nil {
log.Println("PR already exists.")
// simple checkout
return pr.Checkout(false)
}
log.Println("New Pull Request.")
baseRepository, err := types.GetRepository(conf.BaseRemote)
if err != nil {
return err
}
pr, err = getPullRequest(baseRepository, options.Number)
if err != nil {
return err
}
err = pr.Checkout(true)
if err != nil {
// Remove remote if needed
errRemote := removeRemote(conf, pr)
if errRemote != nil {
log.Println(errRemote)
}
return err
}
// add PR to config
if conf.PullRequests == nil {
conf.PullRequests = make(map[string][]types.PullRequest)
}
conf.PullRequests[pr.Owner] = append(conf.PullRequests[pr.Owner], *pr)
return config.Save(confs)
}
// removeRemote if needed.
func removeRemote(conf *config.Configuration, pr *types.PullRequest) error {
if len(conf.PullRequests[pr.Owner]) == 0 {
errRemote := pr.RemoveRemote()
if errRemote != nil {
return errRemote
}
}
return nil
}
func getPullRequest(baseRepository *types.Repository, number int) (*types.PullRequest, error) {
ctx := context.Background()
client := newGitHubClient(ctx)
pr, _, err := client.PullRequests.Get(ctx, baseRepository.Owner, baseRepository.Name, number)
if err != nil {
return nil, err
}
if pr.Head == nil || pr.Head.Repo == nil || pr.Head.Repo.Owner == nil {
return nil, errors.New("the repository of the pull request has been deleted")
}
return &types.PullRequest{
Project: baseRepository.Name,
Owner: pr.Head.Repo.Owner.GetLogin(),
BranchName: pr.Head.GetRef(),
Number: number,
CloneURL: pr.Head.Repo.GetSSHURL(),
}, nil
}