diff --git a/cmd/ci_run_test.go b/cmd/ci_run_test.go index 6c3782e3..7d4242d1 100644 --- a/cmd/ci_run_test.go +++ b/cmd/ci_run_test.go @@ -123,7 +123,7 @@ func Test_getCIRunOptions(t *testing.T) { []string{}, nil, // https://gitlab.com/zaquestion/test project ID "", - "gitlab project not found, verify you have access to the requested resource", + "GitLab project not found, verify you have access to the requested resource", }, } diff --git a/cmd/mr_approve.go b/cmd/mr_approve.go index 99d25d3f..ce1492d7 100644 --- a/cmd/mr_approve.go +++ b/cmd/mr_approve.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "log" + "os" "github.com/rsteube/carapace" "github.com/spf13/cobra" @@ -52,7 +53,13 @@ var mrApproveCmd = &cobra.Command{ err = lab.MRApprove(p.ID, int(id)) if err != nil { - log.Fatal(err) + if err == lab.ErrStatusForbidden { + log.Fatal(err) + } + if err == lab.ErrActionRepeated { + fmt.Printf("Merge Request !%d already approved\n", id) + os.Exit(1) + } } fmt.Printf("Merge Request !%d approved\n", id) }, diff --git a/cmd/mr_unapprove.go b/cmd/mr_unapprove.go index 60511b87..279119ac 100644 --- a/cmd/mr_unapprove.go +++ b/cmd/mr_unapprove.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "log" + "os" "github.com/rsteube/carapace" "github.com/spf13/cobra" @@ -52,7 +53,13 @@ var mrUnapproveCmd = &cobra.Command{ err = lab.MRUnapprove(p.ID, int(id)) if err != nil { - log.Fatal(err) + if err == lab.ErrStatusForbidden { + log.Fatal(err) + } + if err == lab.ErrActionRepeated { + fmt.Printf("Merge Request !%d already unapproved\n", id) + os.Exit(1) + } } fmt.Printf("Merge Request !%d unapproved\n", id) }, diff --git a/internal/gitlab/gitlab.go b/internal/gitlab/gitlab.go index b6281992..c90c171b 100644 --- a/internal/gitlab/gitlab.go +++ b/internal/gitlab/gitlab.go @@ -26,6 +26,9 @@ import ( ) var ( + // ErrActionRepeated is returned when a GitLab action is executed again. For example + // this can be returned when an MR is approved twice. + ErrActionRepeated = errors.New("GitLab action repeated") // ErrGroupNotFound is returned when a GitLab group cannot be found. ErrGroupNotFound = errors.New("GitLab group not found") // ErrNotModified is returned when adding an already existing item to a Todo list @@ -526,6 +529,10 @@ func MRApprove(pid interface{}, id int) error { if resp != nil && resp.StatusCode == http.StatusForbidden { return ErrStatusForbidden } + if resp != nil && resp.StatusCode == http.StatusUnauthorized { + // returns 401 if the MR has already been approved + return ErrActionRepeated + } if err != nil { return err } @@ -538,6 +545,10 @@ func MRUnapprove(pid interface{}, id int) error { if resp != nil && resp.StatusCode == http.StatusForbidden { return ErrStatusForbidden } + if resp != nil && resp.StatusCode == http.StatusNotFound { + // returns 404 if the MR has already been unapproved + return ErrActionRepeated + } if err != nil { return err }