X Tutup
Skip to content

Commit aad291a

Browse files
committed
Merge branch 'tmp' into pin-ext
2 parents 95fa7f9 + 619774d commit aad291a

File tree

8 files changed

+188
-28
lines changed

8 files changed

+188
-28
lines changed

pkg/cmd/extension/command.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,20 @@ func NewCmdExtension(f *cmdutil.Factory) *cobra.Command {
6161

6262
t.AddField(fmt.Sprintf("gh %s", c.Name()), nil, nil)
6363
t.AddField(repo, nil, nil)
64+
version := c.CurrentVersion()
65+
if !c.IsBinary() && len(version) > 8 {
66+
version = version[:8]
67+
}
6468

65-
if c.Pin() != "" {
66-
t.AddField(c.Pin(), nil, cs.Cyan)
67-
} else if c.UpdateAvailable() {
68-
t.AddField("Upgrade available", nil, cs.Green)
69+
if c.IsPinned() {
70+
t.AddField(version, nil, cs.Cyan)
6971
} else {
70-
t.AddField("", nil, nil)
72+
t.AddField(version, nil, nil)
73+
}
74+
75+
var updateAvailable string
76+
if c.UpdateAvailable() {
77+
updateAvailable = "Upgrade available"
7178
}
7279
t.EndRow()
7380
}

pkg/cmd/extension/command_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ func TestNewCmdExtension(t *testing.T) {
299299
assert.Equal(t, 1, len(em.ListCalls()))
300300
}
301301
},
302-
wantStdout: "gh test\tcli/gh-test\t\ngh test2\tcli/gh-test2\tUpgrade available\n",
302+
wantStdout: "gh test\tcli/gh-test\t1\t\ngh test2\tcli/gh-test2\t1\tUpgrade available\n",
303303
},
304304
{
305305
name: "create extension interactive",

pkg/cmd/extension/extension.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type Extension struct {
1818
path string
1919
url string
2020
isLocal bool
21-
pin string
21+
isPinned bool
2222
currentVersion string
2323
latestVersion string
2424
kind ExtensionKind
@@ -40,12 +40,16 @@ func (e *Extension) IsLocal() bool {
4040
return e.isLocal
4141
}
4242

43-
func (e *Extension) Pin() string {
44-
return e.pin
43+
func (e *Extension) CurrentVersion() string {
44+
return e.currentVersion
45+
}
46+
47+
func (e *Extension) IsPinned() bool {
48+
return e.isPinned
4549
}
4650

4751
func (e *Extension) UpdateAvailable() bool {
48-
if e.pin != "" ||
52+
if e.isPinned ||
4953
e.isLocal ||
5054
e.currentVersion == "" ||
5155
e.latestVersion == "" ||

pkg/cmd/extension/manager.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,13 @@ func (m *Manager) parseBinaryExtensionDir(fi fs.FileInfo) (Extension, error) {
198198
remoteURL := ghrepo.GenerateRepoURL(repo, "")
199199
ext.url = remoteURL
200200
ext.currentVersion = bm.Tag
201+
<<<<<<< HEAD
201202
if bm.Pinned {
202203
ext.pin = bm.Tag
203204
}
205+
=======
206+
ext.isPinned = bm.IsPinned
207+
>>>>>>> tmp
204208
return ext, nil
205209
}
206210

@@ -210,10 +214,17 @@ func (m *Manager) parseGitExtensionDir(fi fs.FileInfo) (Extension, error) {
210214
remoteUrl := m.getRemoteUrl(fi.Name())
211215
currentVersion := m.getCurrentVersion(fi.Name())
212216

217+
<<<<<<< HEAD
213218
var pinnedVersion string
214219
pinPath := filepath.Join(id, fi.Name(), fmt.Sprintf(".pin-%s", currentVersion))
215220
if _, err := os.Stat(pinPath); err == nil && len(currentVersion) > 6 {
216221
pinnedVersion = currentVersion[:7]
222+
=======
223+
var isPinned bool
224+
pinPath := filepath.Join(id, fi.Name(), fmt.Sprintf(".pin-%s", currentVersion))
225+
if _, err := os.Stat(pinPath); err == nil {
226+
isPinned = true
227+
>>>>>>> tmp
217228
}
218229

219230
return Extension{
@@ -222,7 +233,11 @@ func (m *Manager) parseGitExtensionDir(fi fs.FileInfo) (Extension, error) {
222233
isLocal: false,
223234
currentVersion: currentVersion,
224235
kind: GitKind,
236+
<<<<<<< HEAD
225237
pin: pinnedVersion,
238+
=======
239+
isPinned: isPinned,
240+
>>>>>>> tmp
226241
}, nil
227242
}
228243

@@ -324,11 +339,19 @@ func (m *Manager) InstallLocal(dir string) error {
324339
}
325340

326341
type binManifest struct {
342+
<<<<<<< HEAD
327343
Owner string
328344
Name string
329345
Host string
330346
Tag string
331347
Pinned bool
348+
=======
349+
Owner string
350+
Name string
351+
Host string
352+
Tag string
353+
IsPinned bool
354+
>>>>>>> tmp
332355
// TODO I may end up not using this; just thinking ahead to local installs
333356
Path string
334357
}
@@ -400,12 +423,21 @@ func (m *Manager) installBin(repo ghrepo.Interface, target string) error {
400423
}
401424

402425
manifest := binManifest{
426+
<<<<<<< HEAD
403427
Name: name,
404428
Owner: repo.RepoOwner(),
405429
Host: repo.RepoHost(),
406430
Path: binPath,
407431
Tag: r.Tag,
408432
Pinned: isPinned,
433+
=======
434+
Name: name,
435+
Owner: repo.RepoOwner(),
436+
Host: repo.RepoHost(),
437+
Path: binPath,
438+
Tag: r.Tag,
439+
IsPinned: isPinned,
440+
>>>>>>> tmp
409441
}
410442

411443
bs, err := yaml.Marshal(manifest)
@@ -469,7 +501,11 @@ func (m *Manager) installGit(repo ghrepo.Interface, target string, stdout, stder
469501
pinPath := filepath.Join(targetDir, fmt.Sprintf(".pin-%s", commitSHA))
470502
f, err := os.OpenFile(pinPath, os.O_WRONLY|os.O_CREATE, 0600)
471503
if err != nil {
504+
<<<<<<< HEAD
472505
return fmt.Errorf("failed to create pin in directory: %w", err)
506+
=======
507+
return fmt.Errorf("failed to create pin file in directory: %w", err)
508+
>>>>>>> tmp
473509
}
474510
return f.Close()
475511
}
@@ -533,7 +569,11 @@ func (m *Manager) upgradeExtension(ext Extension, force bool) error {
533569
if ext.isLocal {
534570
return localExtensionUpgradeError
535571
}
572+
<<<<<<< HEAD
536573
if ext.pin != "" {
574+
=======
575+
if ext.IsPinned() {
576+
>>>>>>> tmp
537577
return pinnedExtensionUpgradeError
538578
}
539579
if !ext.UpdateAvailable() {

pkg/cmd/pr/close/close.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,34 +90,36 @@ func closeRun(opts *CloseOptions) error {
9090
if opts.DeleteBranch {
9191
branchSwitchString := ""
9292
apiClient := api.NewClientFromHTTP(httpClient)
93+
localBranchExists := git.HasLocalBranch(pr.HeadRefName)
9394

9495
if opts.DeleteLocalBranch {
95-
currentBranch, err := opts.Branch()
96-
if err != nil {
97-
return err
98-
}
99-
100-
var branchToSwitchTo string
101-
if currentBranch == pr.HeadRefName {
102-
branchToSwitchTo, err = api.RepoDefaultBranch(apiClient, baseRepo)
96+
if localBranchExists {
97+
currentBranch, err := opts.Branch()
10398
if err != nil {
10499
return err
105100
}
106-
err = git.CheckoutBranch(branchToSwitchTo)
107-
if err != nil {
108-
return err
101+
102+
var branchToSwitchTo string
103+
if currentBranch == pr.HeadRefName {
104+
branchToSwitchTo, err = api.RepoDefaultBranch(apiClient, baseRepo)
105+
if err != nil {
106+
return err
107+
}
108+
err = git.CheckoutBranch(branchToSwitchTo)
109+
if err != nil {
110+
return err
111+
}
109112
}
110-
}
111113

112-
localBranchExists := git.HasLocalBranch(pr.HeadRefName)
113-
if localBranchExists {
114114
if err := git.DeleteLocalBranch(pr.HeadRefName); err != nil {
115115
return fmt.Errorf("failed to delete local branch %s: %w", cs.Cyan(pr.HeadRefName), err)
116116
}
117-
}
118117

119-
if branchToSwitchTo != "" {
120-
branchSwitchString = fmt.Sprintf(" and switched to branch %s", cs.Cyan(branchToSwitchTo))
118+
if branchToSwitchTo != "" {
119+
branchSwitchString = fmt.Sprintf(" and switched to branch %s", cs.Cyan(branchToSwitchTo))
120+
}
121+
} else {
122+
fmt.Fprintf(opts.IO.ErrOut, "%s Skipped deleting the local branch since current directory is not a git repository \n", cs.WarningIcon())
121123
}
122124
}
123125

pkg/cmd/pr/close/close_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,37 @@ func TestPrClose_deleteBranch_sameBranch(t *testing.T) {
230230
✓ Deleted branch trunk and switched to branch main
231231
`), output.Stderr())
232232
}
233+
234+
func TestPrClose_deleteBranch_notInGitRepo(t *testing.T) {
235+
http := &httpmock.Registry{}
236+
defer http.Verify(t)
237+
238+
baseRepo, pr := stubPR("OWNER/REPO:main", "OWNER/REPO:trunk")
239+
pr.Title = "The title of the PR"
240+
shared.RunCommandFinder("96", pr, baseRepo)
241+
242+
http.Register(
243+
httpmock.GraphQL(`mutation PullRequestClose\b`),
244+
httpmock.GraphQLMutation(`{"id": "THE-ID"}`,
245+
func(inputs map[string]interface{}) {
246+
assert.Equal(t, inputs["pullRequestId"], "THE-ID")
247+
}),
248+
)
249+
http.Register(
250+
httpmock.REST("DELETE", "repos/OWNER/REPO/git/refs/heads/trunk"),
251+
httpmock.StringResponse(`{}`))
252+
253+
cs, cmdTeardown := run.Stub()
254+
defer cmdTeardown(t)
255+
256+
cs.Register(`git rev-parse --verify refs/heads/trunk`, 128, "could not determine current branch: fatal: not a git repository (or any of the parent directories): .git")
257+
258+
output, err := runCommand(http, true, `96 --delete-branch`)
259+
assert.NoError(t, err)
260+
assert.Equal(t, "", output.String())
261+
assert.Equal(t, heredoc.Doc(`
262+
✓ Closed pull request #96 (The title of the PR)
263+
! Skipped deleting the local branch since current directory is not a git repository
264+
✓ Deleted branch trunk
265+
`), output.Stderr())
266+
}

pkg/extensions/extension.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ type Extension interface {
1919
Name() string // Extension Name without gh-
2020
Path() string // Path to executable
2121
URL() string
22-
Pin() string // Pinned version
22+
CurrentVersion() string
23+
IsPinned() bool
2324
UpdateAvailable() bool
2425
IsBinary() bool
2526
IsLocal() bool

0 commit comments

Comments
 (0)
X Tutup