X Tutup
Skip to content

Commit 759e8a9

Browse files
committed
resolve conflicts with trunk
2 parents 3fb4c48 + 4bbbf46 commit 759e8a9

File tree

20 files changed

+1159
-94
lines changed

20 files changed

+1159
-94
lines changed

api/queries_repo.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,26 @@ func RenameRepo(client *Client, repo ghrepo.Interface, newRepoName string) (*Rep
556556
}, nil
557557
}
558558

559+
func LastCommit(client *Client, repo ghrepo.Interface) (*Commit, error) {
560+
var responseData struct {
561+
Repository struct {
562+
DefaultBranchRef struct {
563+
Target struct {
564+
Commit `graphql:"... on Commit"`
565+
}
566+
}
567+
} `graphql:"repository(owner: $owner, name: $repo)"`
568+
}
569+
variables := map[string]interface{}{
570+
"owner": githubv4.String(repo.RepoOwner()), "repo": githubv4.String(repo.RepoName()),
571+
}
572+
gql := graphQLClient(client.http, repo.RepoHost())
573+
if err := gql.QueryNamed(context.Background(), "LastCommit", &responseData, variables); err != nil {
574+
return nil, err
575+
}
576+
return &responseData.Repository.DefaultBranchRef.Target.Commit, nil
577+
}
578+
559579
// RepoFindForks finds forks of the repo that are affiliated with the viewer
560580
func RepoFindForks(client *Client, repo ghrepo.Interface, limit int) ([]*Repository, error) {
561581
result := struct {

pkg/cmd/browse/browse.go

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type BrowseOptions struct {
2929
HttpClient func() (*http.Client, error)
3030
IO *iostreams.IOStreams
3131
PathFromRepoRoot func() string
32+
GitClient gitClient
3233

3334
SelectorArg string
3435

@@ -46,6 +47,7 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
4647
HttpClient: f.HttpClient,
4748
IO: f.IOStreams,
4849
PathFromRepoRoot: git.PathFromRepoRoot,
50+
GitClient: &localGitClient{},
4951
}
5052

5153
cmd := &cobra.Command{
@@ -97,6 +99,9 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
9799
); err != nil {
98100
return err
99101
}
102+
if cmd.Flags().Changed("repo") {
103+
opts.GitClient = &remoteGitClient{opts.BaseRepo, opts.HttpClient}
104+
}
100105

101106
if runF != nil {
102107
return runF(opts)
@@ -123,10 +128,11 @@ func runBrowse(opts *BrowseOptions) error {
123128
}
124129

125130
if opts.CommitFlag {
126-
commit, err := git.LastCommit()
127-
if err == nil {
128-
opts.Branch = commit.Sha
131+
commit, err := opts.GitClient.LastCommit()
132+
if err != nil {
133+
return err
129134
}
135+
opts.Branch = commit.Sha
130136
}
131137

132138
section, err := parseSection(baseRepo, opts)
@@ -245,3 +251,33 @@ func isNumber(arg string) bool {
245251
_, err := strconv.Atoi(arg)
246252
return err == nil
247253
}
254+
255+
// gitClient is used to implement functions that can be performed on both local and remote git repositories
256+
type gitClient interface {
257+
LastCommit() (*git.Commit, error)
258+
}
259+
260+
type localGitClient struct{}
261+
262+
type remoteGitClient struct {
263+
repo func() (ghrepo.Interface, error)
264+
httpClient func() (*http.Client, error)
265+
}
266+
267+
func (gc *localGitClient) LastCommit() (*git.Commit, error) { return git.LastCommit() }
268+
269+
func (gc *remoteGitClient) LastCommit() (*git.Commit, error) {
270+
httpClient, err := gc.httpClient()
271+
if err != nil {
272+
return nil, err
273+
}
274+
repo, err := gc.repo()
275+
if err != nil {
276+
return nil, err
277+
}
278+
commit, err := api.LastCommit(api.NewClientFromHTTP(httpClient), repo)
279+
if err != nil {
280+
return nil, err
281+
}
282+
return &git.Commit{Sha: commit.OID}, nil
283+
}

pkg/cmd/browse/browse_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ func TestNewCmdBrowse(t *testing.T) {
143143
assert.Equal(t, tt.wants.WikiFlag, opts.WikiFlag)
144144
assert.Equal(t, tt.wants.NoBrowserFlag, opts.NoBrowserFlag)
145145
assert.Equal(t, tt.wants.SettingsFlag, opts.SettingsFlag)
146+
assert.Equal(t, tt.wants.CommitFlag, opts.CommitFlag)
146147
})
147148
}
148149
}
@@ -156,6 +157,12 @@ func setGitDir(t *testing.T, dir string) {
156157
})
157158
}
158159

160+
type testGitClient struct{}
161+
162+
func (gc *testGitClient) LastCommit() (*git.Commit, error) {
163+
return &git.Commit{Sha: "6f1a2405cace1633d89a79c74c65f22fe78f9659"}, nil
164+
}
165+
159166
func Test_runBrowse(t *testing.T) {
160167
s := string(os.PathSeparator)
161168
setGitDir(t, "../../../git/fixtures/simple.git")
@@ -354,6 +361,7 @@ func Test_runBrowse(t *testing.T) {
354361
name: "open last commit",
355362
opts: BrowseOptions{
356363
CommitFlag: true,
364+
GitClient: &testGitClient{},
357365
},
358366
baseRepo: ghrepo.New("vilmibm", "gh-user-status"),
359367
wantsErr: false,
@@ -364,6 +372,7 @@ func Test_runBrowse(t *testing.T) {
364372
opts: BrowseOptions{
365373
CommitFlag: true,
366374
SelectorArg: "main.go",
375+
GitClient: &testGitClient{},
367376
},
368377
baseRepo: ghrepo.New("vilmibm", "gh-user-status"),
369378
wantsErr: false,

pkg/cmd/gist/edit/edit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman
4444
editorCmd,
4545
"*."+filename,
4646
defaultContent,
47-
io.In, io.Out, io.ErrOut, nil)
47+
io.In, io.Out, io.ErrOut)
4848
},
4949
}
5050

pkg/cmd/issue/edit/edit_test.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99
"testing"
1010

11+
"github.com/cli/cli/v2/api"
1112
"github.com/cli/cli/v2/internal/ghrepo"
1213
prShared "github.com/cli/cli/v2/pkg/cmd/pr/shared"
1314
"github.com/cli/cli/v2/pkg/cmdutil"
@@ -286,13 +287,19 @@ func Test_editRun(t *testing.T) {
286287
Value: "GA",
287288
Edited: true,
288289
},
290+
Metadata: api.RepoMetadataResult{
291+
Labels: []api.RepoLabel{
292+
{Name: "docs", ID: "DOCSID"},
293+
},
294+
},
289295
},
290296
FetchOptions: prShared.FetchOptions,
291297
},
292298
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
293299
mockIssueGet(t, reg)
294300
mockRepoMetadata(t, reg)
295301
mockIssueUpdate(t, reg)
302+
mockIssueUpdateLabels(t, reg)
296303
},
297304
stdout: "https://github.com/OWNER/REPO/issue/123\n",
298305
},
@@ -386,7 +393,8 @@ func mockRepoMetadata(_ *testing.T, reg *httpmock.Registry) {
386393
"nodes": [
387394
{ "name": "feature", "id": "FEATUREID" },
388395
{ "name": "TODO", "id": "TODOID" },
389-
{ "name": "bug", "id": "BUGID" }
396+
{ "name": "bug", "id": "BUGID" },
397+
{ "name": "docs", "id": "DOCSID" }
390398
],
391399
"pageInfo": { "hasNextPage": false }
392400
} } } }
@@ -429,9 +437,22 @@ func mockIssueUpdate(t *testing.T, reg *httpmock.Registry) {
429437
reg.Register(
430438
httpmock.GraphQL(`mutation IssueUpdate\b`),
431439
httpmock.GraphQLMutation(`
432-
{ "data": { "updateIssue": { "issue": {
433-
"id": "123"
434-
} } } }`,
440+
{ "data": { "updateIssue": { "__typename": "" } } }`,
441+
func(inputs map[string]interface{}) {}),
442+
)
443+
}
444+
445+
func mockIssueUpdateLabels(t *testing.T, reg *httpmock.Registry) {
446+
reg.Register(
447+
httpmock.GraphQL(`mutation LabelAdd\b`),
448+
httpmock.GraphQLMutation(`
449+
{ "data": { "addLabelsToLabelable": { "__typename": "" } } }`,
450+
func(inputs map[string]interface{}) {}),
451+
)
452+
reg.Register(
453+
httpmock.GraphQL(`mutation LabelRemove\b`),
454+
httpmock.GraphQLMutation(`
455+
{ "data": { "removeLabelsFromLabelable": { "__typename": "" } } }`,
435456
func(inputs map[string]interface{}) {}),
436457
)
437458
}

pkg/cmd/pr/edit/edit.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/cli/cli/v2/pkg/iostreams"
1414
"github.com/shurcooL/githubv4"
1515
"github.com/spf13/cobra"
16+
"golang.org/x/sync/errgroup"
1617
)
1718

1819
type EditOptions struct {
@@ -214,16 +215,19 @@ func editRun(opts *EditOptions) error {
214215
}
215216

216217
func updatePullRequest(httpClient *http.Client, repo ghrepo.Interface, id string, editable shared.Editable) error {
217-
if err := shared.UpdateIssue(httpClient, repo, id, true, editable); err != nil {
218-
return err
218+
var wg errgroup.Group
219+
wg.Go(func() error {
220+
return shared.UpdateIssue(httpClient, repo, id, true, editable)
221+
})
222+
if editable.Reviewers.Edited {
223+
wg.Go(func() error {
224+
return updatePullRequestReviews(httpClient, repo, id, editable)
225+
})
219226
}
220-
return updatePullRequestReviews(httpClient, repo, id, editable)
227+
return wg.Wait()
221228
}
222229

223230
func updatePullRequestReviews(httpClient *http.Client, repo ghrepo.Interface, id string, editable shared.Editable) error {
224-
if !editable.Reviewers.Edited {
225-
return nil
226-
}
227231
userIds, teamIds, err := editable.ReviewerIds()
228232
if err != nil {
229233
return err

pkg/cmd/pr/edit/edit_test.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ func Test_editRun(t *testing.T) {
357357
mockRepoMetadata(t, reg, false)
358358
mockPullRequestUpdate(t, reg)
359359
mockPullRequestReviewersUpdate(t, reg)
360+
mockPullRequestUpdateLabels(t, reg)
360361
},
361362
stdout: "https://github.com/OWNER/REPO/pull/123\n",
362363
},
@@ -387,7 +388,7 @@ func Test_editRun(t *testing.T) {
387388
Edited: true,
388389
},
389390
Labels: shared.EditableSlice{
390-
Value: []string{"feature", "TODO", "bug"},
391+
Add: []string{"feature", "TODO", "bug"},
391392
Remove: []string{"docs"},
392393
Edited: true,
393394
},
@@ -406,6 +407,7 @@ func Test_editRun(t *testing.T) {
406407
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
407408
mockRepoMetadata(t, reg, true)
408409
mockPullRequestUpdate(t, reg)
410+
mockPullRequestUpdateLabels(t, reg)
409411
},
410412
stdout: "https://github.com/OWNER/REPO/pull/123\n",
411413
},
@@ -490,7 +492,8 @@ func mockRepoMetadata(_ *testing.T, reg *httpmock.Registry, skipReviewers bool)
490492
"nodes": [
491493
{ "name": "feature", "id": "FEATUREID" },
492494
{ "name": "TODO", "id": "TODOID" },
493-
{ "name": "bug", "id": "BUGID" }
495+
{ "name": "bug", "id": "BUGID" },
496+
{ "name": "docs", "id": "DOCSID" }
494497
],
495498
"pageInfo": { "hasNextPage": false }
496499
} } } }
@@ -554,6 +557,21 @@ func mockPullRequestReviewersUpdate(t *testing.T, reg *httpmock.Registry) {
554557
httpmock.StringResponse(`{}`))
555558
}
556559

560+
func mockPullRequestUpdateLabels(t *testing.T, reg *httpmock.Registry) {
561+
reg.Register(
562+
httpmock.GraphQL(`mutation LabelAdd\b`),
563+
httpmock.GraphQLMutation(`
564+
{ "data": { "addLabelsToLabelable": { "__typename": "" } } }`,
565+
func(inputs map[string]interface{}) {}),
566+
)
567+
reg.Register(
568+
httpmock.GraphQL(`mutation LabelRemove\b`),
569+
httpmock.GraphQLMutation(`
570+
{ "data": { "removeLabelsFromLabelable": { "__typename": "" } } }`,
571+
func(inputs map[string]interface{}) {}),
572+
)
573+
}
574+
557575
type testFetcher struct{}
558576
type testSurveyor struct {
559577
skipReviewers bool

pkg/cmd/pr/merge/merge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ func (e *userEditor) Edit(filename, startingText string) (string, error) {
494494
return "", err
495495
}
496496

497-
return surveyext.Edit(editorCommand, filename, startingText, e.io.In, e.io.Out, e.io.ErrOut, nil)
497+
return surveyext.Edit(editorCommand, filename, startingText, e.io.In, e.io.Out, e.io.ErrOut)
498498
}
499499

500500
// blockedReason translates various MergeStateStatus GraphQL values into human-readable reason

pkg/cmd/pr/shared/commentable.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func CommentableInteractiveEditSurvey(cf func() (config.Config, error), io *iost
151151
cs := io.ColorScheme()
152152
fmt.Fprintf(io.Out, "- %s to draft your comment in %s... ", cs.Bold("Press Enter"), cs.Bold(editorCommand))
153153
_ = waitForEnter(io.In)
154-
return surveyext.Edit(editorCommand, "*.md", "", io.In, io.Out, io.ErrOut, nil)
154+
return surveyext.Edit(editorCommand, "*.md", "", io.In, io.Out, io.ErrOut)
155155
}
156156
}
157157

@@ -161,7 +161,7 @@ func CommentableEditSurvey(cf func() (config.Config, error), io *iostreams.IOStr
161161
if err != nil {
162162
return "", err
163163
}
164-
return surveyext.Edit(editorCommand, "*.md", "", io.In, io.Out, io.ErrOut, nil)
164+
return surveyext.Edit(editorCommand, "*.md", "", io.In, io.Out, io.ErrOut)
165165
}
166166
}
167167

pkg/cmd/pr/shared/editable.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,6 @@ func (e Editable) AssigneeIds(client *api.Client, repo ghrepo.Interface) (*[]str
120120
return &a, err
121121
}
122122

123-
func (e Editable) LabelIds() (*[]string, error) {
124-
if !e.Labels.Edited {
125-
return nil, nil
126-
}
127-
if len(e.Labels.Add) != 0 || len(e.Labels.Remove) != 0 {
128-
s := set.NewStringSet()
129-
s.AddValues(e.Labels.Default)
130-
s.AddValues(e.Labels.Add)
131-
s.RemoveValues(e.Labels.Remove)
132-
e.Labels.Value = s.ToSlice()
133-
}
134-
l, err := e.Metadata.LabelsToIDs(e.Labels.Value)
135-
return &l, err
136-
}
137-
138123
func (e Editable) ProjectIds() (*[]string, error) {
139124
if !e.Projects.Edited {
140125
return nil, nil
@@ -189,10 +174,22 @@ func EditFieldsSurvey(editable *Editable, editorCommand string) error {
189174
}
190175
}
191176
if editable.Labels.Edited {
192-
editable.Labels.Value, err = multiSelectSurvey("Labels", editable.Labels.Default, editable.Labels.Options)
177+
editable.Labels.Add, err = multiSelectSurvey("Labels", editable.Labels.Default, editable.Labels.Options)
193178
if err != nil {
194179
return err
195180
}
181+
for _, prev := range editable.Labels.Default {
182+
var found bool
183+
for _, selected := range editable.Labels.Add {
184+
if prev == selected {
185+
found = true
186+
break
187+
}
188+
}
189+
if !found {
190+
editable.Labels.Remove = append(editable.Labels.Remove, prev)
191+
}
192+
}
196193
}
197194
if editable.Projects.Edited {
198195
editable.Projects.Value, err = multiSelectSurvey("Projects", editable.Projects.Default, editable.Projects.Options)

0 commit comments

Comments
 (0)
X Tutup