Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@hoffm
Copy link
Contributor

@hoffm hoffm commented Feb 3, 2025

Fixes #10120

Introduces the gh repo autolink delete <id> subcommand.

Deviation from Spec

The linked issue specifies deletion by key prefix, not ID, which is what I've implemented here.

Because the REST API does not support accessing autolinks by key prefix, we decided to build the CLI wrapper for viewing autolinks with ID, not key prefix, as the argument (see the bottom of this comment). For consistency's sake, I've extended that decision to the deletion case, as I proposed here.

We require a confirmation step for deletion, and our confirmation flow presents the user with a string to type in to confirm deletion. The key prefix of the autolink to be deleted is the natural fit for this string, and this is what the spec requires. To achieve this behavior, we fetch the autolink by ID to learn its key prefix, then use that value to prompt the user for confirmation.

We canβ€”and I believe we shouldβ€”follow up with an enhancement to allow viewing and deletion by either ID or key prefix.

Examples

Succesful Delete

Confirmation Step

$ ./bin/gh repo autolink delete 7196774 --repo nytimes/games-dsr
Autolink 7196774 has key prefix TEST-.
? Type TEST- to confirm deletion: 

Confirmation Failure

$ ./bin/gh repo autolink delete 7196774 --repo nytimes/games-dsr
Autolink 7196774 has key prefix TEST-.
X Sorry, your reply was invalid: You entered foo
? Type TEST- to confirm deletion: 

Confirmation Success

$ ./bin/gh repo autolink delete 7196774 --repo nytimes/games-dsr
Autolink 7196774 has key prefix TEST-.
X Sorry, your reply was invalid: You entered foo
βœ“ Autolink 7196774 deleted from nytimes/games-dsr

Delete, Skip Confirmation

$ ./bin/gh repo autolink delete 7204166 --yes  --repo nytimes/games-dsr
βœ“ Autolink 7204166 deleted from nytimes/games-dsr

404 (repo or autolink not found or no admin access)

./bin/gh repo autolink delete 123
error deleting autolink: HTTP 404: Perhaps you are missing admin rights to the repository? (https://api.github.com/repos/cli/cli/autolinks/123)

Missing Argument

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

Additional Changes

In addition to the work described above, I've included some small changes for the sake of consistency within the gh repo autolink subcommands, including:

  • Bold repo names.
  • Standardize 404 message to: "Perhaps you are missing admin rights to the repository?"
  • Use http constants for status codes in tests.
  • Fix var names where "autolink" is treated as two words, e.g. AutoLink -> "Auolink".

@hoffm hoffm requested a review from a team as a code owner February 3, 2025 02:30
@hoffm hoffm requested a review from williammartin February 3, 2025 02:30
@cliAutomation cliAutomation added the external pull request originating outside of the CLI core team label Feb 3, 2025
} else {
require.NoError(t, err)
assert.Equal(t, tt.wantExporter, gotOpts.Exporter != nil)
assert.Equal(t, tt.output.ID, gotOpts.ID)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test should always have been checking that the opts including the correct ID.

Comment on lines -120 to -122
func listHeader(repoName string, count int) string {
return fmt.Sprintf("Showing %s in %s", text.Pluralize(count, "autolink reference"), repoName)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I inlined this helper. It only had one caller and it's simple enough.

Copy link
Contributor

@jtmcg jtmcg left a comment

Choose a reason for hiding this comment

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

Man, is it refreshing to NOT see http stubs in the command set tests and only in the http tests πŸ₯³ Nice work, there.

I think we're missing some non-TTY use-cases in this and have commented throughout the code where we may add its support. I could have missed some spots, though.

Also, I acknowledge that you deviated from the spec because the API uses the ID instead of the key prefix. It seems fine to break this up, as you suggested:

We canβ€”and I believe we shouldβ€”follow up with an enhancement to allow viewing and deletion by either ID or key prefix

I'm curious to hear about your thoughts on an approach to add key-prefix deletion.

var errTestAutolinkClientView = errors.New("autolink client view error")
var errTestAutolinkClientDelete = errors.New("autolink client delete error")

func TestDeleteRun(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

These tests look solid 🫢 I read them before anything else, and not only do they suggest the separation between your logic layer and api layer is πŸ‘¨β€πŸ³ πŸ’‹, but they also do a great job documenting the logic demonstrating that you've implemented the AC πŸŽ‰

That said, I think we're missing one test for this AC:

Given I have the admin role on the repository
And Given I have a local repository cloned from GitHub
When I run gh repo autolink delete in non-interactive mode
Then I see an informational message stating --yes flag is required to delete autolink in non-interactive mode followed by the usage statement

We'll probably have to force the terminal to be non-tty for this use case. There's an example of how we usually do this in the alias/delete tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanksβ€”I'll definitely add the non-TTY behavior. Appreciate your catching that!

Comment on lines 85 to 93
if !opts.Confirmed {
fmt.Fprintf(out, "Autolink %s has key prefix %s.\n", cs.Cyan(opts.ID), autolink.KeyPrefix)

err := opts.Prompter.ConfirmDeletion(autolink.KeyPrefix)

if err != nil {
return err
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need to handle the non-tty use-case, here. We should be exiting and not prompting for non-interactive terminals. I mentioned this as your missing test case in delete_test.go below as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated the behavior as you suggested, though I followed an extant pattern of doing this in the command definition.

if !opts.IO.CanPrompt() && !opts.Confirmed {
    return cmdutil.FlagErrorf("--yes required when not running interactively")
}

Also, fwiw, I noticed that the app is inconsistent about how it handles deletion for non-TTY use-cases. Some resources (e.g. issues) seem to just delete without confirming in non-interactive mode.

Copy link
Contributor

Choose a reason for hiding this comment

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

πŸ˜… That is concerning! We want to avoid those kind of foot-guns...

Can you open an issue with the inconsistencies you've found? Feel free to tag me on it

@hoffm hoffm requested a review from jtmcg February 7, 2025 21:57
Copy link
Contributor

@jtmcg jtmcg left a comment

Choose a reason for hiding this comment

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

Looks good to me! Thanks again!

@jtmcg jtmcg merged commit 5557958 into cli:trunk Feb 7, 2025
9 checks passed
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Feb 13, 2025
This MR contains the following updates:

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

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.67.0`](https://github.com/cli/cli/releases/tag/v2.67.0): GitHub CLI 2.67.0

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

#### `gh pr checkout` now supports interactively selecting a pull request

Similar to commands like `gh workflow run` which prompts for a workflow to run, now `gh pr checkout` will prompt for a pull request to checkout. The list is currently limited to the most recent 10 pull requests in the repository.

https://github.com/user-attachments/assets/0b2e3761-7318-4573-8a23-ae6f1a44b018

Big thank you to [@&#8203;nilvng](https://github.com/nilvng) for implementing this πŸ™Œ

#### Contributing guidelines updated

We've updated our [`CONTRIBUTING.md`](https://github.com/cli/cli/blob/trunk/.github/CONTRIBUTING.md) guidelines to give more clarity around old `help wanted` issues.

*TLDR*:

-   Please directly mention `@cli/code-reviewers` when an issue you want to work on does not have clear Acceptance Criteria
-   Please only open pull requests for issues with *both*  the help wanted label and clear Acceptance Criteria
-   Please avoid expanding pull request scope to include changes that are not described in the connected issue's Acceptance Criteria

Note: Acceptance Criteria is posted as an issue comment by a core maintainer.

See cli/cli#10381 and cli/cli#10395 for more information.

❓ Have feedback on anything? We'd love to hear from you in a discussion post ❀️

#### What's Changed

##### ✨ Features

-   feat: let user select pr to checkout by [@&#8203;nilvng](https://github.com/nilvng) in cli/cli#9868
-   feat: Add support for deleting autolink references by [@&#8203;hoffm](https://github.com/hoffm) in cli/cli#10362
-   \[gh extensions install] Improve help text and error message by [@&#8203;iamazeem](https://github.com/iamazeem) in cli/cli#10333
-   Error when `gh repo rename` is used with a new repo name that contains an owner by [@&#8203;timrogers](https://github.com/timrogers) in cli/cli#10364
-   Attestation bundle fetch improvements by [@&#8203;malancas](https://github.com/malancas) in cli/cli#10233
-   \[gh project item-list] Add `iterationId` field in ProjectV2ItemFieldIterationValue by [@&#8203;iamazeem](https://github.com/iamazeem) in cli/cli#10329

##### πŸ› Fixes

-   \[gh api] Fix mutual exclusion messages of `--slurp` flag by [@&#8203;iamazeem](https://github.com/iamazeem) in cli/cli#10332
-   Exit with error if no matching predicate type exists by [@&#8203;kommendorkapten](https://github.com/kommendorkapten) in cli/cli#10421
-   Do not try to parse bodies for HEAD requests by [@&#8203;jsoref](https://github.com/jsoref) in cli/cli#10388
-   \[gh project item-edit] Fix number type by [@&#8203;iamazeem](https://github.com/iamazeem) in cli/cli#10374
-   \[gh workflow run] Improve error handling for `--ref` flag by [@&#8203;iamazeem](https://github.com/iamazeem) in cli/cli#10328
-   \[gh config] Escape pipe symbol in Long desc for website manual by [@&#8203;iamazeem](https://github.com/iamazeem) in cli/cli#10371

##### πŸ“š Docs & Chores

-   Fix logic error in contributing docs by [@&#8203;BagToad](https://github.com/BagToad) in cli/cli#10395
-   Docs: Clarify guidelines for `help wanted` issues and pull requests by [@&#8203;BagToad](https://github.com/BagToad) in cli/cli#10381
-   \[gh pr status] Mention `gh pr checks` in the `Long` section by [@&#8203;iamazeem](https://github.com/iamazeem) in cli/cli#10389
-   \[docs/releasing.md] Add basic info for homebrew update flow by [@&#8203;iamazeem](https://github.com/iamazeem) in cli/cli#10344
-   \[gh issue/pr list] Improve help text by [@&#8203;iamazeem](https://github.com/iamazeem) in cli/cli#10335
-   Remove v1 project 'add to board' automation from prauto workflow by [@&#8203;hoffm](https://github.com/hoffm) in cli/cli#10331
-   Note: the following pair of MRs was reverted and never made into a release
    -   \[gh repo edit] Allow setting commit message defaults by [@&#8203;iamazeem](https://github.com/iamazeem) in cli/cli#10363
    -   Revert "\[gh repo edit] Allow setting commit message defaults" by [@&#8203;BagToad](https://github.com/BagToad) in cli/cli#10372

##### :dependabot: Dependencies

-   Bump google.golang.org/protobuf from 1.36.4 to 1.36.5 by [@&#8203;dependabot](https://github.com/dependabot) in cli/cli#10379

**Full Changelog**: cli/cli@v2.66.1...v2.67.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:eyJjcmVhdGVkSW5WZXIiOiIzOS4xNjUuMSIsInVwZGF0ZWRJblZlciI6IjM5LjE2NS4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJSZW5vdmF0ZSBCb3QiXX0=-->
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 delete

4 participants