Skip to content

Preserve/restore pr and issue input - #2408

Merged
vilmibm merged 7 commits into
trunkfrom
preserve-input
Nov 23, 2020
Merged

vilmibm merged 7 commits into
trunkfrom
preserve-input

Conversation

@vilmibm

@vilmibm vilmibm commented Nov 13, 2020 •

Copy link
Copy Markdown
Contributor

Fixes #906

This PR:

  • detects when a user has provided input during pr/issue creation, saving it to a JSON file in case
    of failure
  • adds --recover <filename> to issue/pr create that restores the lost data and allows user to edit before submitting

image

When recovering, the title and body from the dump are used as default values and any metadata is preset.

@vilmibm vilmibm changed the title Preserve/restore pr and issue input [WIP] Preserve/restore pr and issue input Nov 13, 2020
@vilmibm vilmibm changed the title [WIP] Preserve/restore pr and issue input Preserve/restore pr and issue input Nov 13, 2020
@vilmibm
vilmibm requested a review from a team November 13, 2020 23:50
@vilmibm
vilmibm marked this pull request as ready for review November 13, 2020 23:50

@mislav mislav left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for taking this on! 🌟

Comment thread pkg/cmd/issue/create/create.go Resolved
Comment thread pkg/cmd/pr/shared/preserve.go Outdated
)

func dumpPath(random int64) string {
r := fmt.Sprintf("%x", random)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would ioutil.TempFile("", "gh*.json") be suitable here to generate a random filename?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes, good point; I didn't realize tempfile took a directory argument for some reason 🤦‍♀️

@vilmibm vilmibm Nov 20, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated to use TempFile which is certainly better; I'm not happy with how I ended up testing it though

return
}

defer prShared.PreserveInput(opts.IO, &tb, &err)()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wondered why you named the (err error) return value in the parent function, and now I understand. Clever solution for capturing any error from this point onward!

Could preserving input on any error be over-zealous, however? I can imagine some instances where an error being returned from createRun() doesn't necessarily have to result in restore instructions being printed to stderr. For instance, the user might have explicitly aborted editing in their text editor (e.g. :cq in vim), or pressed Ctrl-C to abort the interactive flow. Since the user chose to bail out from the command, I don't think we need to preserve their input there.

Additionally, the defer approach will fail to preserve input if the program crashes while making the request or processing the HTTP response. (We don't know how often this happens, but it's worth keeping in mind.)

Would it be enough to simply preserve input just before dispatching the API command, and only handle the error that is the result of it? Sort of like (pseudocode):

if wasInteractive {
  PreserveInput(tb)
}
pr, err := createPR(data)
if err != nil {
  ShowRestoreInstructions()
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

For instance, the user might have explicitly aborted editing in their text editor (e.g. :cq in vim), or pressed Ctrl-C

I pretty strongly think that manually aborting is an appropriate time to save input. I've personally realized that something is wrong with my branch or create pr invocation after drafting a body and am forced to either re-type the body or submit something I'm not ready to submit then clean it up.

Additionally, the defer approach will fail to preserve input if the program crashes while making the request or processing the HTTP response. (We don't know how often this happens, but it's worth keeping in mind.)

While I am open to moving away from defer, just to be clear, this would be an appropriate usecase for recover, yes?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If your intended goal is to capture any error after this point, not just the API error from the createPullRequest mutation, then I do agree that the defer approach is worth sticking with. 👍

I would not advise using recover for most things other than crash/error reporting.

@vilmibm vilmibm Nov 23, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

My thinking with recover would be that we could just dump any user input to STDERR in case of a panic but I think we can worry about that later if it actually bites users.

Comment thread pkg/cmd/pr/create/create.go Resolved
@vilmibm
vilmibm requested a review from mislav November 20, 2020 21:22

@mislav mislav left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The updates look great. Thank you!

Comment thread pkg/cmd/pr/create/create.go Outdated
fl.StringSliceVarP(&opts.Projects, "project", "p", nil, "Add the pull request to projects by `name`")
fl.StringVarP(&opts.Milestone, "milestone", "m", "", "Add the pull request to a milestone by `name`")
fl.StringVarP(&opts.JSONInput, "json", "j", "", "Use JSON to populate and submit PR")
fl.StringVarP(&opts.RecoverFile, "recover", "e", "", "Recover input from a failed run of create")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In order to free up -e for some future flag, do you think it would be okay to expose the flag only as --recover without any shorthand? The way I see it, we already print copy-pasteable instructions in the error message, so it shouldn't matter that the flag should be typed in long-form.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Friendlier recovery when issue/pr create fails

4 participants