KEMBAR78
feat: Add support for viewing autolink references by hoffm · Pull Request #10324 · cli/cli · GitHub
Skip to content

Conversation

@hoffm
Copy link
Contributor

@hoffm hoffm commented Jan 28, 2025

Fixes #10200

Overview

Introduce the gh repo autolink view <id> subcommand as described in the linked issue.

Example Output

Help

$ bin/gh repo autolink view --help
View an autolink reference for a repository.

For more information about output formatting flags, see `gh help formatting`.

USAGE
  gh repo autolink view <id> [flags]

FLAGS
  -q, --jq expression     Filter JSON output using a jq expression
      --json fields       Output JSON with the specified fields
  -t, --template string   Format JSON output using a Go template; see "gh help formatting"

INHERITED FLAGS
      --help                     Show help for command
  -R, --repo [HOST/]OWNER/REPO   Select another repository using the [HOST/]OWNER/REPO format

JSON FIELDS
  id, isAlphanumeric, keyPrefix, urlTemplate

LEARN MORE
  Use `gh <command> <subcommand> --help` for more information about a command.
  Read the manual at https://cli.github.com/manual
  Learn about exit codes using `gh help exit-codes`

Successful view

$ ./bin/gh repo autolink view 7166925 --repo nytimes/games-dsr
Autolink in nytimes/games-dsr

ID: 7166925
Key Prefix: TEST-
URL Template: https://www.example.com/<num>
Alphanumeric: true

Successful view, JSON

$  ./bin/gh repo autolink view 7168016 --repo nytimes/games-dsr --json id,urlTemplate
{
  "id": 7168016,
  "urlTemplate": "https://www.example.com/<num>"
}

Successful view, JSON w/ jq

$ ./bin/gh repo autolink view 7168016 --repo nytimes/games-dsr --json keyPrefix --jq '.keyPrefix'
TEST-

Successful view, JSON w/ Go template

$ ./bin/gh repo autolink view 7168016 --repo nytimes/games-dsr --json keyPrefix --template 'The key prefix is {{.keyPrefix}}'
The key prefix is TEST-

JSON flag with no args

$   ./bin/gh repo autolink view 7168016 --repo nytimes/games-dsr --json
Specify one or more comma-separated fields for `--json`:
  id
  isAlphanumeric
  keyPrefix
  urlTemplate

404 (No admin access OR autolink doesn't exist OR repo doesn't exist)

$  ./bin/gh repo autolink view 7166925 --repo cli/cli
error viewing autolink: 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/repos/cli/cli/autolinks/7168016)

No Args

$  ./bin/gh repo autolink view
accepts 1 arg(s), received 0

@hoffm hoffm requested a review from a team as a code owner January 28, 2025 18:55
@hoffm hoffm requested a review from andyfeller January 28, 2025 18:55
@cliAutomation cliAutomation added the external pull request originating outside of the CLI core team label Jan 28, 2025
@hoffm hoffm changed the title View autolinks feat: Add support for viewing autolink references Jan 28, 2025
@hoffm hoffm marked this pull request as draft January 28, 2025 19:03
@hoffm hoffm marked this pull request as ready for review January 28, 2025 19:10
@hoffm
Copy link
Contributor Author

hoffm commented Jan 28, 2025

@andyfeller This PR is failing the lint PR step of the pr-auto check with the message

https://github.com/cli/cli/pull/10324
gh: Project already has the associated issue

I'm not sure how to resolve.

@andyfeller
Copy link
Member

andyfeller commented Jan 29, 2025

@andyfeller This PR is failing the lint PR step of the pr-auto check with the message

https://github.com/cli/cli/pull/10324
gh: Project already has the associated issue

I'm not sure how to resolve.

I think we need to modify this workflow as we don't use this v1 project anymore. Additionally, GitHub has deprecated v1 projects, so this will go away. Let me open a PR but rest assured it isn't a required check so we'll be okay 👍

UPDATE: created #10330 to follow up on this

Copy link
Member

@andyfeller andyfeller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solid work and thank you yet again for contributing to the GitHub CLI, @hoffm! ❤️

Aside from a minor edit, there are some questions around displaying the autolink that I didn't consider originally I'd like to soundboard to ensure we're keeping this closer to other experiences.

Comment on lines +19 to +20
path := fmt.Sprintf("repos/%s/%s/autolinks/%s", repo.RepoOwner(), repo.RepoName(), id)
url := ghinstance.RESTPrefix(repo.RepoHost()) + path
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting it took me a hot minute to see path being reused later for error messaging if 404 arises.

Comment on lines 86 to 99
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,
)
Copy link
Member

@andyfeller andyfeller Jan 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm second guessing my original AC effort as the design doesn't really fit with other gh <primitive> view commands.

Like gh pr view 10324 --repo cli/cli, should the fields be bolded?

Screenshot 2025-01-29 at 10 29 22 AM

func renderReleaseTTY(io *iostreams.IOStreams, release *shared.Release) error {
iofmt := io.ColorScheme()
w := io.Out
fmt.Fprintf(w, "%s\n", iofmt.Bold(release.TagName))
if release.IsDraft {
fmt.Fprintf(w, "%s • ", iofmt.Red("Draft"))
} else if release.IsPrerelease {
fmt.Fprintf(w, "%s • ", iofmt.Yellow("Pre-release"))
}
if release.IsDraft {
fmt.Fprintf(w, "%s\n", iofmt.Gray(fmt.Sprintf("%s created this %s", release.Author.Login, text.FuzzyAgo(time.Now(), release.CreatedAt))))
} else {
fmt.Fprintf(w, "%s\n", iofmt.Gray(fmt.Sprintf("%s released this %s", release.Author.Login, text.FuzzyAgo(time.Now(), *release.PublishedAt))))
}
renderedDescription, err := markdown.Render(release.Body,
markdown.WithTheme(io.TerminalTheme()),
markdown.WithWrap(io.TerminalWidth()))
if err != nil {
return err
}
fmt.Fprintln(w, renderedDescription)
if len(release.Assets) > 0 {
fmt.Fprintf(w, "%s\n", iofmt.Bold("Assets"))
//nolint:staticcheck // SA1019: Showing NAME|SIZE headers adds nothing to table.
table := tableprinter.New(io, tableprinter.NoHeader)
for _, a := range release.Assets {
table.AddField(a.Name)
table.AddField(humanFileSize(a.Size))
table.EndRow()
}
err := table.Render()
if err != nil {
return err
}
fmt.Fprint(w, "\n")
}
fmt.Fprintf(w, "%s\n", iofmt.Gray(fmt.Sprintf("View on GitHub: %s", release.URL)))
return nil
}
func renderReleasePlain(w io.Writer, release *shared.Release) error {
fmt.Fprintf(w, "title:\t%s\n", release.Name)
fmt.Fprintf(w, "tag:\t%s\n", release.TagName)
fmt.Fprintf(w, "draft:\t%v\n", release.IsDraft)
fmt.Fprintf(w, "prerelease:\t%v\n", release.IsPrerelease)
fmt.Fprintf(w, "author:\t%s\n", release.Author.Login)
fmt.Fprintf(w, "created:\t%s\n", release.CreatedAt.Format(time.RFC3339))
if !release.IsDraft {
fmt.Fprintf(w, "published:\t%s\n", release.PublishedAt.Format(time.RFC3339))
}
fmt.Fprintf(w, "url:\t%s\n", release.URL)
for _, a := range release.Assets {
fmt.Fprintf(w, "asset:\t%s\n", a.Name)
}
fmt.Fprint(w, "--\n")
fmt.Fprint(w, release.Body)
if !strings.HasSuffix(release.Body, "\n") {
fmt.Fprintf(w, "\n")
}
return nil
}

Like gh workflow view 25016 --repo cli/cli, should IDs be cyan?

Screenshot 2025-01-29 at 10 34 53 AM

func viewWorkflowInfo(opts *ViewOptions, client *api.Client, repo ghrepo.Interface, workflow *shared.Workflow) error {
wr, err := runShared.GetRuns(client, repo, &runShared.FilterOptions{
WorkflowID: workflow.ID,
WorkflowName: workflow.Name,
}, 5)
if err != nil {
return fmt.Errorf("failed to get runs: %w", err)
}
out := opts.IO.Out
cs := opts.IO.ColorScheme()
// Header
filename := workflow.Base()
fmt.Fprintf(out, "%s - %s\n", cs.Bold(workflow.Name), cs.Cyan(filename))
fmt.Fprintf(out, "ID: %s\n\n", cs.Cyanf("%d", workflow.ID))
// Runs
fmt.Fprintf(out, "Total runs %d\n", wr.TotalCount)
if wr.TotalCount != 0 {
fmt.Fprintln(out, "Recent runs")
}

🤔 thoughts?

Copy link
Contributor Author

@hoffm hoffm Jan 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like both of these suggestions. I wanted to add some styling to this text for readability, but I hesitated because I didn't really know what styling conventions to follow. I think these high-visibility precedents point the way!


Tangent: As some point it might be nice to write up a lightweight style guide that answers questions such as:

  • When do we use colored text and what are the conventions around use of each color?
  • When do we use other formatting features (bold, caps, etc)?
  • How do we decide whether data should be displayed as a table versus not?
  • What are the formatting conventions for non-tabular data?

Copy link
Contributor Author

@hoffm hoffm Jan 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I've done the following:

  • Bolded field names in view output.
  • Used Cyan for ID in view output.
  • Updated create and list output to use Cyan for IDs as well.

Copy link
Member

@andyfeller andyfeller Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like both of these suggestions. I wanted to add some styling to this text for readability, but I hesitated because I didn't really know what styling conventions to follow. I think these high-visibility precedents point the way!

Tangent: As some point it might be nice to write up a lightweight style guide that answers questions such as:

  • When do we use colored text and what are the conventions around use of each color?
  • When do we use other formatting features (bold, caps, etc)?
  • How do we decide whether data should be displayed as a table versus not?
  • What are the formatting conventions for non-tabular data?

That is fair and valid feedback, which I too have brought up and interested in.

👉 https://primer.style/native/cli/components

There is some uncertainty if Primer site should be the home of this or not. Definitely need to build upon it and review the experiences we do have, bringing them more into alignment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I didn't know about Primer. Very helpful.

@hoffm hoffm requested a review from andyfeller January 29, 2025 21:27
Copy link
Member

@andyfeller andyfeller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, @hoffm! 😎

shipit-squirrel-fireworks

@andyfeller
Copy link
Member

Just demonstrating that the stylistic changes react appropriately to NO_COLOR env var

Screenshot demonstrating gh repo autolink commands react to NO_COLOR env var correctly

@andyfeller andyfeller merged commit 068f92b into cli:trunk Jan 30, 2025
9 checks passed
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Feb 4, 2025
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [cli/cli](https://github.com/cli/cli) | minor | `v2.65.0` -> `v2.66.1` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>cli/cli (cli/cli)</summary>

### [`v2.66.1`](https://github.com/cli/cli/releases/tag/v2.66.1): GitHub CLI 2.66.1

[Compare Source](cli/cli@v2.66.0...v2.66.1)

#### Hotfix: `gh pr view` fails with provided URL

This addresses a regression in `gh pr view` was reported in [#&#8203;10352](cli/cli#10352). This regression was due to a change in `v2.66.0` that no longer allowed `gh pr` subcommands to execute properly outside of a git repo.

#### What's Changed

-   Hotfix: `gh pr view` fails with provided URL by [@&#8203;jtmcg](https://github.com/jtmcg) in cli/cli#10354

**Full Changelog**: cli/cli@v2.66.0...v2.66.1

### [`v2.66.0`](https://github.com/cli/cli/releases/tag/v2.66.0): GitHub CLI 2.66.0

[Compare Source](cli/cli@v2.65.0...v2.66.0)

#### `gh pr view` and `gh pr status` now respect common triangular workflow configurations

Previously, `gh pr view` and `gh pr status` would fail for pull request's (MR) open in triangular workflows. This was due to `gh` being unable to identify the MR's corresponding remote and branch refs on GitHub.

Now, `gh pr view` and `gh pr status` should successfully identify the MR's refs when the following common git configurations are used:

-   [`branch.<branchName>.pushremote`](https://git-scm.com/docs/git-config#Documentation/git-config.txt-branchltnamegtpushRemote) is set
-   [`remote.pushDefault`](https://git-scm.com/docs/git-config#Documentation/git-config.txt-remotepushDefault) is set

Branch specific configuration, the former, supersedes repo specific configuration, the latter.

Additionally, if the [`@{push}` revision syntax](https://git-scm.com/docs/gitrevisions#Documentation/gitrevisions.txt-emltbranchnamegtpushemegemmasterpushemempushem) for git resolves for a branch, `gh pr view` and `gh pr status` should work regardless of additional config settings.

For more information, see

-   cli/cli#9363
-   cli/cli#9364
-   cli/cli#9365
-   cli/cli#9374

#### `gh secret list`, `gh secret set`, and `gh secret delete` now require repository selection when multiple `git` remotes are present

Previously, `gh secret list`, `gh secret set`, and `gh secret delete` would determine which remote to target for interacting with GitHub Actions secrets.  Remotes marked as default using `gh repo set-default` or through other `gh` commands had higher priority when figuring out which repository to interact with.  This could have unexpected outcomes when using `gh secret` commands with forked repositories as the upstream repository would generally be selected.

Now, `gh secret` commands require users to disambiguate which repository should be the target if multiple remotes are present and the `-R, --repo` flag is not provided.

For more information, see cli/cli#4688

#### Extension update notices now notify once every 24 hours per extension and can be disabled

Previously, the GitHub CLI would notify users about newer versions every time an extension was executed.  This did not match GitHub CLI notices, which only notified users once every 24 hours and could be disabled through an environment variable.

Now, extension update notices will behave similar to GitHub CLI notices.  To disable extension update notices, set the `GH_NO_EXTENSION_UPDATE_NOTIFIER` environment variable.

For more information, see cli/cli#9925

#### What's Changed

##### ✨ Features

-   Draft for discussing testing around extension update checking behavior by [@&#8203;andyfeller](https://github.com/andyfeller) in cli/cli#9985
-   Make extension update check non-blocking by [@&#8203;andyfeller](https://github.com/andyfeller) in cli/cli#10239
-   Ensure extension update notices only notify once within 24 hours, provide ability to disable all extension update notices by [@&#8203;andyfeller](https://github.com/andyfeller) in cli/cli#9934
-   feat: make the extension upgrade fancier by [@&#8203;nobe4](https://github.com/nobe4) in cli/cli#10194
-   fix: padded display by [@&#8203;nobe4](https://github.com/nobe4) in cli/cli#10216
-   Update `gh attestation` attestation bundle fetching logic by [@&#8203;malancas](https://github.com/malancas) in cli/cli#10185
-   Require repo disambiguation for secret commands by [@&#8203;williammartin](https://github.com/williammartin) in cli/cli#10209
-   show error message for rerun workflow older than a month ago by [@&#8203;iamrajhans](https://github.com/iamrajhans) in cli/cli#10227
-   Update `gh attestation verify` table output by [@&#8203;malancas](https://github.com/malancas) in cli/cli#10104
-   Enable MSI building for Windows arm64 by [@&#8203;dennisameling](https://github.com/dennisameling) in cli/cli#10297
-   feat: Add support for creating autolink references by [@&#8203;hoffm](https://github.com/hoffm) in cli/cli#10180
-   Find MRs using `@{push}` by [@&#8203;Frederick888](https://github.com/Frederick888) in cli/cli#9208
-   feat: Add support for viewing autolink references by [@&#8203;hoffm](https://github.com/hoffm) in cli/cli#10324
-   Update `gh attestation` bundle fetching logic by [@&#8203;malancas](https://github.com/malancas) in cli/cli#10339

##### 🐛 Fixes

-   gh gist delete: prompt for gist id by [@&#8203;danochoa](https://github.com/danochoa) in cli/cli#10154
-   Better handling for waiting for codespaces to become ready by [@&#8203;cmbrose](https://github.com/cmbrose) in cli/cli#10198
-   Fix: `gh gist view` and `gh gist edit` prompts with no TTY by [@&#8203;mateusmarquezini](https://github.com/mateusmarquezini) in cli/cli#10048
-   Remove naked return values from `ReadBranchConfig` and `prSelectorForCurrentBranch` by [@&#8203;jtmcg](https://github.com/jtmcg) in cli/cli#10197
-   Add job to deployment workflow to validate the tag name for a given release by [@&#8203;jtmcg](https://github.com/jtmcg) in cli/cli#10121
-   \[gh run list] Stop progress indicator on failure from `--workflow` flag by [@&#8203;iamazeem](https://github.com/iamazeem) in cli/cli#10323
-   Update deployment.yml by [@&#8203;andyfeller](https://github.com/andyfeller) in cli/cli#10340

##### 📚 Docs & Chores

-   Add affected version heading to bug report issue form by [@&#8203;BagToad](https://github.com/BagToad) in cli/cli#10269
-   chore: fix some comments by [@&#8203;petercover](https://github.com/petercover) in cli/cli#10296
-   Update triage.md to reflect FR experiment outcome by [@&#8203;jtmcg](https://github.com/jtmcg) in cli/cli#10196
-   Clear up --with-token fine grained PAT usage by [@&#8203;williammartin](https://github.com/williammartin) in cli/cli#10186
-   Correct help documentation around template use in `gh issue create` by [@&#8203;andyfeller](https://github.com/andyfeller) in cli/cli#10208
-   chore: fix some function names in comment by [@&#8203;zhuhaicity](https://github.com/zhuhaicity) in cli/cli#10225
-   Tiny typo fix by [@&#8203;robmorgan](https://github.com/robmorgan) in cli/cli#10265
-   add install instructions for Manjaro Linux by [@&#8203;AMS21](https://github.com/AMS21) in cli/cli#10236
-   Update test to be compatible with latest Glamour v0.8.0 by [@&#8203;ottok](https://github.com/ottok) in cli/cli#10151
-   Add more `gh attestation verify` integration tests by [@&#8203;malancas](https://github.com/malancas) in cli/cli#10102

##### :dependabot: Dependencies

-   Bump github.com/mattn/go-colorable from 0.1.13 to 0.1.14 by [@&#8203;dependabot](https://github.com/dependabot) in cli/cli#10215
-   Bump github.com/sigstore/protobuf-specs from 0.3.2 to 0.3.3 by [@&#8203;dependabot](https://github.com/dependabot) in cli/cli#10214
-   Bump github.com/gabriel-vasile/mimetype from 1.4.7 to 1.4.8 by [@&#8203;dependabot](https://github.com/dependabot) in cli/cli#10184
-   Bump google.golang.org/protobuf from 1.36.2 to 1.36.3 by [@&#8203;dependabot](https://github.com/dependabot) in cli/cli#10250
-   Bump golangci-linter and address failures to prepare for Go 1.24 strictness by [@&#8203;mikelolasagasti](https://github.com/mikelolasagasti) in cli/cli#10279
-   Bump github.com/google/go-containerregistry from 0.20.2 to 0.20.3 by [@&#8203;dependabot](https://github.com/dependabot) in cli/cli#10257
-   Bump actions/attest-build-provenance from 2.1.0 to 2.2.0 by [@&#8203;dependabot](https://github.com/dependabot) in cli/cli#10300
-   Bump google.golang.org/protobuf from 1.36.3 to 1.36.4 by [@&#8203;dependabot](https://github.com/dependabot) in cli/cli#10306
-   Upgrade sigstore-go to v0.7.0: fixes [#&#8203;10114](cli/cli#10114) formatting issue by [@&#8203;codysoyland](https://github.com/codysoyland) in cli/cli#10309
-   Bump github.com/in-toto/attestation from 1.1.0 to 1.1.1 by [@&#8203;dependabot](https://github.com/dependabot) in cli/cli#10319

#### New Contributors

Big thank you to our many new *and* longtime contributors making this release happen!! ❤️ ✨

-   [@&#8203;zhuhaicity](https://github.com/zhuhaicity) made their first contribution in cli/cli#10225
-   [@&#8203;danochoa](https://github.com/danochoa) made their first contribution in cli/cli#10154
-   [@&#8203;robmorgan](https://github.com/robmorgan) made their first contribution in cli/cli#10265
-   [@&#8203;iamrajhans](https://github.com/iamrajhans) made their first contribution in cli/cli#10227
-   [@&#8203;AMS21](https://github.com/AMS21) made their first contribution in cli/cli#10236
-   [@&#8203;petercover](https://github.com/petercover) made their first contribution in cli/cli#10296
-   [@&#8203;ottok](https://github.com/ottok) made their first contribution in cli/cli#10151
-   [@&#8203;dennisameling](https://github.com/dennisameling) made their first contribution in cli/cli#10297
-   [@&#8203;iamazeem](https://github.com/iamazeem) made their first contribution in cli/cli#10323
-   [@&#8203;Frederick888](https://github.com/Frederick888) made their first contribution in cli/cli#9208

**Full Changelog**: cli/cli@v2.65.0...v2.66.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOS4xNDMuMCIsInVwZGF0ZWRJblZlciI6IjM5LjE0Ni40IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJSZW5vdmF0ZSBCb3QiXX0=-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external pull request originating outside of the CLI core team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for gh repo autolink view

3 participants