github.com/gfleury/gobbs@v0.0.0-20200831213239-44ca2b94c1a1/pullrequests/delete.go (about) 1 package pullrequests 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "net" 8 "net/http" 9 "strconv" 10 11 "github.com/gfleury/gobbs/common" 12 "github.com/gfleury/gobbs/common/log" 13 14 "github.com/spf13/cobra" 15 ) 16 17 // Delete is the cmd implementation for Deleting Pull Requests 18 var Delete = &cobra.Command{ 19 Use: "delete pullRequestID", 20 Aliases: []string{"del"}, 21 Short: "Delete pull requests for repository", 22 Args: cobra.MinimumNArgs(1), 23 RunE: func(cmd *cobra.Command, args []string) error { 24 prID, err := strconv.ParseInt(args[0], 10, 64) 25 if err != nil { 26 log.Critical("Argument must be a pull request ID. Err: %s", err.Error()) 27 return err 28 } 29 30 apiClient, cancel, err := common.APIClient(cmd) 31 defer cancel() 32 33 if err != nil { 34 log.Critical("Argument must be a pull request ID. Err: %s", err.Error()) 35 return err 36 } 37 38 stashInfo := cmd.Context().Value(common.StashInfoKey).(*common.StashInfo) 39 err = mustHaveProjectRepo(stashInfo) 40 if err != nil { 41 return err 42 } 43 44 response, err := apiClient.DefaultApi.DeleteWithVersion(*stashInfo.Project(), *stashInfo.Repo(), prID, *prVersion) 45 46 if netError, ok := err.(net.Error); (!ok || (ok && !netError.Timeout())) && 47 !errors.Is(err, context.Canceled) && 48 !errors.Is(err, context.DeadlineExceeded) && 49 response != nil && response.Response != nil && 50 response.Response.StatusCode >= http.StatusMultipleChoices { 51 common.PrintApiError(response.Values) 52 cmd.SilenceUsage = true 53 log.Debugf(err.Error()) 54 return fmt.Errorf("Unable to process request, API Error") 55 } else if err != nil { 56 cmd.SilenceUsage = true 57 return err 58 } 59 60 if response.StatusCode == http.StatusNoContent { 61 log.Infof("Pull request ID: %v sucessfully deleted.", prID) 62 } 63 return nil 64 }, 65 }