Repository navigation
Preserve/restore pr and issue input - #2408
Conversation
2deca4e to
656d228
Compare
mislav
left a comment
There was a problem hiding this comment.
Thanks for taking this on! 🌟
| ) | ||
|
|
||
| func dumpPath(random int64) string { | ||
| r := fmt.Sprintf("%x", random) |
There was a problem hiding this comment.
Would ioutil.TempFile("", "gh*.json") be suitable here to generate a random filename?
There was a problem hiding this comment.
yes, good point; I didn't realize tempfile took a directory argument for some reason 🤦♀️
There was a problem hiding this comment.
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)() |
There was a problem hiding this comment.
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()
}
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
mislav
left a comment
There was a problem hiding this comment.
The updates look great. Thank you!
| 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") |
There was a problem hiding this comment.
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.
229682a to
4b3ae61
Compare
4b3ae61 to
cf37ce7
Compare
Fixes #906
This PR:
of failure
--recover <filename>toissue/pr createthat restores the lost data and allows user to edit before submittingWhen recovering, the title and body from the dump are used as default values and any metadata is preset.