From e8475d9fb8a3cd2193bbbecce3706a547dcdbd51 Mon Sep 17 00:00:00 2001 From: Michael Hoffman Date: Sat, 11 Jan 2025 20:00:44 -0500 Subject: [PATCH] Initial, working implementation --- pkg/cmd/repo/autolink/autolink.go | 2 + pkg/cmd/repo/autolink/list/list.go | 9 +- pkg/cmd/repo/autolink/shared/autolink.go | 7 ++ pkg/cmd/repo/autolink/view/http.go | 47 ++++++++++ pkg/cmd/repo/autolink/view/view.go | 107 +++++++++++++++++++++++ 5 files changed, 164 insertions(+), 8 deletions(-) create mode 100644 pkg/cmd/repo/autolink/view/http.go create mode 100644 pkg/cmd/repo/autolink/view/view.go diff --git a/pkg/cmd/repo/autolink/autolink.go b/pkg/cmd/repo/autolink/autolink.go index 1fd218354..09b766b85 100644 --- a/pkg/cmd/repo/autolink/autolink.go +++ b/pkg/cmd/repo/autolink/autolink.go @@ -4,6 +4,7 @@ import ( "github.com/MakeNowJust/heredoc" cmdCreate "github.com/cli/cli/v2/pkg/cmd/repo/autolink/create" cmdList "github.com/cli/cli/v2/pkg/cmd/repo/autolink/list" + cmdView "github.com/cli/cli/v2/pkg/cmd/repo/autolink/view" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/spf13/cobra" ) @@ -24,6 +25,7 @@ func NewCmdAutolink(f *cmdutil.Factory) *cobra.Command { cmd.AddCommand(cmdList.NewCmdList(f, nil)) cmd.AddCommand(cmdCreate.NewCmdCreate(f, nil)) + cmd.AddCommand(cmdView.NewCmdView(f, nil)) return cmd } diff --git a/pkg/cmd/repo/autolink/list/list.go b/pkg/cmd/repo/autolink/list/list.go index a68c03f3c..74267e7a4 100644 --- a/pkg/cmd/repo/autolink/list/list.go +++ b/pkg/cmd/repo/autolink/list/list.go @@ -15,13 +15,6 @@ import ( "github.com/spf13/cobra" ) -var autolinkFields = []string{ - "id", - "isAlphanumeric", - "keyPrefix", - "urlTemplate", -} - type listOptions struct { BaseRepo func() (ghrepo.Interface, error) Browser browser.Browser @@ -70,7 +63,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*listOptions) error) *cobra.Comman } cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "List autolink references in the web browser") - cmdutil.AddJSONFlags(cmd, &opts.Exporter, autolinkFields) + cmdutil.AddJSONFlags(cmd, &opts.Exporter, domain.AutolinkFields) return cmd } diff --git a/pkg/cmd/repo/autolink/shared/autolink.go b/pkg/cmd/repo/autolink/shared/autolink.go index d79537c31..28e975e7c 100644 --- a/pkg/cmd/repo/autolink/shared/autolink.go +++ b/pkg/cmd/repo/autolink/shared/autolink.go @@ -9,6 +9,13 @@ type Autolink struct { URLTemplate string `json:"url_template"` } +var AutolinkFields = []string{ + "id", + "isAlphanumeric", + "keyPrefix", + "urlTemplate", +} + func (a *Autolink) ExportData(fields []string) map[string]interface{} { return cmdutil.StructExportData(a, fields) } diff --git a/pkg/cmd/repo/autolink/view/http.go b/pkg/cmd/repo/autolink/view/http.go new file mode 100644 index 000000000..d3fdfc86a --- /dev/null +++ b/pkg/cmd/repo/autolink/view/http.go @@ -0,0 +1,47 @@ +package view + +import ( + "encoding/json" + "fmt" + "net/http" + + "github.com/cli/cli/v2/api" + "github.com/cli/cli/v2/internal/ghinstance" + "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/pkg/cmd/repo/autolink/domain" +) + +type AutolinkViewer struct { + HTTPClient *http.Client +} + +func (a *AutolinkViewer) View(repo ghrepo.Interface, id string) (*domain.Autolink, error) { + path := fmt.Sprintf("repos/%s/%s/autolinks/%s", repo.RepoOwner(), repo.RepoName(), id) + url := ghinstance.RESTPrefix(repo.RepoHost()) + path + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return nil, err + } + + resp, err := a.HTTPClient.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + if resp.StatusCode == http.StatusNotFound { + return nil, fmt.Errorf("HTTP 404: Either no autolink with this ID exists for this repository or you are missing admin rights to the repository. (https://api.github.com/%s)", path) + } else if resp.StatusCode > 299 { + return nil, api.HandleHTTPError(resp) + } + + var autolink domain.Autolink + err = json.NewDecoder(resp.Body).Decode(&autolink) + + if err != nil { + fmt.Println(err.Error()) + return nil, err + } + + return &autolink, nil +} diff --git a/pkg/cmd/repo/autolink/view/view.go b/pkg/cmd/repo/autolink/view/view.go new file mode 100644 index 000000000..2d0cfd6c6 --- /dev/null +++ b/pkg/cmd/repo/autolink/view/view.go @@ -0,0 +1,107 @@ +package view + +import ( + "fmt" + + "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/internal/browser" + "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/pkg/cmd/repo/autolink/domain" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/spf13/cobra" +) + +type viewOptions struct { + BaseRepo func() (ghrepo.Interface, error) + Browser browser.Browser + AutolinkClient AutolinkViewClient + IO *iostreams.IOStreams + Exporter cmdutil.Exporter + + ID string +} + +type AutolinkViewClient interface { + View(repo ghrepo.Interface, id string) (*domain.Autolink, error) +} + +func NewCmdView(f *cmdutil.Factory, runF func(*viewOptions) error) *cobra.Command { + opts := &viewOptions{ + Browser: f.Browser, + IO: f.IOStreams, + } + + cmd := &cobra.Command{ + Use: "view ", + Short: "View an autolink reference", + Long: heredoc.Docf(` + View an autolink reference for a repository. + + Information about autolinks is only available to repository administrators. + `), + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + opts.BaseRepo = f.BaseRepo + httpClient, err := f.HttpClient() + + if err != nil { + return err + } + + opts.ID = args[0] + + if err != nil { + return err + } + + opts.AutolinkClient = &AutolinkViewer{HTTPClient: httpClient} + + if runF != nil { + return runF(opts) + } + + return viewRun(opts) + }, + } + + cmdutil.AddJSONFlags(cmd, &opts.Exporter, domain.AutolinkFields) + + return cmd +} + +func viewRun(opts *viewOptions) error { + repo, err := opts.BaseRepo() + if err != nil { + return err + } + cs := opts.IO.ColorScheme() + + autolink, err := opts.AutolinkClient.View(repo, opts.ID) + + if err != nil { + return fmt.Errorf("%s %w", cs.Red("error viewing autolink:"), err) + } + + if opts.Exporter != nil { + return opts.Exporter.Write(opts.IO, autolink) + } + + msg := heredoc.Docf(` + Autolink in %s + + ID: %d + Key Prefix: %s + URL Template: %s + Alphanumeric: %t + `, + ghrepo.FullName(repo), + autolink.ID, + autolink.KeyPrefix, + autolink.URLTemplate, + autolink.IsAlphanumeric, + ) + fmt.Fprint(opts.IO.Out, msg) + + return nil +}