From 80f130184ce0f87de4ad6eac59f6f7db07100980 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 7 Jun 2022 17:54:52 +0200 Subject: [PATCH] repo edit: fix interactive mode in GHES < 3.3 --- .../featuredetection/feature_detection.go | 5 +++++ .../feature_detection_test.go | 18 +++++++++++++++++ pkg/cmd/repo/edit/edit.go | 20 +++++++++++++++++-- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/internal/featuredetection/feature_detection.go b/internal/featuredetection/feature_detection.go index 15eec5852..17254bb4f 100644 --- a/internal/featuredetection/feature_detection.go +++ b/internal/featuredetection/feature_detection.go @@ -37,6 +37,7 @@ type RepositoryFeatures struct { IssueTemplateQuery bool PullRequestTemplateQuery bool VisibilityField bool + AutoMerge bool } var allRepositoryFeatures = RepositoryFeatures{ @@ -44,6 +45,7 @@ var allRepositoryFeatures = RepositoryFeatures{ IssueTemplateQuery: true, PullRequestTemplateQuery: true, VisibilityField: true, + AutoMerge: true, } type detector struct { @@ -107,6 +109,9 @@ func (d *detector) RepositoryFeatures() (RepositoryFeatures, error) { if field.Name == "visibility" { features.VisibilityField = true } + if field.Name == "autoMergeAllowed" { + features.AutoMerge = true + } } return features, nil diff --git a/internal/featuredetection/feature_detection_test.go b/internal/featuredetection/feature_detection_test.go index 97ad0c0b8..b7aa2673f 100644 --- a/internal/featuredetection/feature_detection_test.go +++ b/internal/featuredetection/feature_detection_test.go @@ -73,6 +73,7 @@ func TestRepositoryFeatures(t *testing.T) { IssueTemplateQuery: true, PullRequestTemplateQuery: true, VisibilityField: true, + AutoMerge: true, }, wantErr: false, }, @@ -123,6 +124,23 @@ func TestRepositoryFeatures(t *testing.T) { }, wantErr: false, }, + { + name: "GHE has automerge field", + hostname: "git.my.org", + queryResponse: map[string]string{ + `query Repository_fields\b`: heredoc.Doc(` + { "data": { "Repository": { "fields": [ + {"name": "autoMergeAllowed"} + ] } } } + `), + }, + wantFeatures: RepositoryFeatures{ + IssueTemplateMutation: true, + IssueTemplateQuery: true, + AutoMerge: true, + }, + wantErr: false, + }, } for _, tt := range tests { diff --git a/pkg/cmd/repo/edit/edit.go b/pkg/cmd/repo/edit/edit.go index 953b69dbb..176de1b39 100644 --- a/pkg/cmd/repo/edit/edit.go +++ b/pkg/cmd/repo/edit/edit.go @@ -12,6 +12,7 @@ import ( "github.com/AlecAivazis/survey/v2" "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/api" + fd "github.com/cli/cli/v2/internal/featuredetection" "github.com/cli/cli/v2/internal/ghinstance" "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/pkg/cmdutil" @@ -48,6 +49,7 @@ type EditOptions struct { AddTopics []string RemoveTopics []string InteractiveMode bool + Detector fd.Detector // Cache of current repo topics to avoid retrieving them // in multiple flows. topicsCache []string @@ -158,9 +160,17 @@ func editRun(ctx context.Context, opts *EditOptions) error { repo := opts.Repository if opts.InteractiveMode { + detector := opts.Detector + if detector == nil { + detector = fd.NewDetector(opts.HTTPClient, repo.RepoHost()) + } + repoFeatures, err := detector.RepositoryFeatures() + if err != nil { + return err + } + apiClient := api.NewClientFromHTTP(opts.HTTPClient) fieldsToRetrieve := []string{ - "autoMergeAllowed", "defaultBranchRef", "deleteBranchOnMerge", "description", @@ -174,8 +184,14 @@ func editRun(ctx context.Context, opts *EditOptions) error { "rebaseMergeAllowed", "repositoryTopics", "squashMergeAllowed", - "visibility", } + if repoFeatures.VisibilityField { + fieldsToRetrieve = append(fieldsToRetrieve, "visibility") + } + if repoFeatures.AutoMerge { + fieldsToRetrieve = append(fieldsToRetrieve, "autoMergeAllowed") + } + opts.IO.StartProgressIndicator() fetchedRepo, err := api.FetchRepository(apiClient, opts.Repository, fieldsToRetrieve) opts.IO.StopProgressIndicator()