Skip to content

Commit

Permalink
mr approve/unapprove: Provide better error messages
Browse files Browse the repository at this point in the history
When I attempt to approve or unapprove on a Project that I don't have approve permissions for, I get the following error:

2021/02/10 10:58:17 mr_approve.go:56: POST https://gitlab.com/api/v4/projects/XXXX/merge_requests/208/approve: 403 {message: 403 Forbidden}

Provide a better error message in this case.

Signed-off-by: Prarit Bhargava <[email protected]>
  • Loading branch information
prarit committed Feb 10, 2021
1 parent 635ea0e commit 50a41b1
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ var (
ErrProjectNotFound = errors.New("gitlab project not found, verify you have access to the requested resource")
// ErrGroupNotFound is returned when a GitLab group cannot be found.
ErrGroupNotFound = errors.New("gitlab group not found")
// ErrStatusForbidden is returned when attempting to access a GitLab project with insufficient permissions
ErrStatusForbidden = errors.New("Insufficient permissions for gitlab project")
)

var (
Expand Down Expand Up @@ -499,7 +501,10 @@ func MRMerge(pid interface{}, id int) error {

// MRApprove approves an mr on a GitLab project
func MRApprove(pid interface{}, id int) error {
_, _, err := lab.MergeRequestApprovals.ApproveMergeRequest(pid, id, &gitlab.ApproveMergeRequestOptions{})
_, resp, err := lab.MergeRequestApprovals.ApproveMergeRequest(pid, id, &gitlab.ApproveMergeRequestOptions{})
if resp != nil && resp.StatusCode == http.StatusForbidden {
return ErrStatusForbidden
}
if err != nil {
return err
}
Expand All @@ -508,7 +513,10 @@ func MRApprove(pid interface{}, id int) error {

// MRUnapprove Unapproves a previously approved mr on a GitLab project
func MRUnapprove(pid interface{}, id int) error {
_, err := lab.MergeRequestApprovals.UnapproveMergeRequest(pid, id, nil)
resp, err := lab.MergeRequestApprovals.UnapproveMergeRequest(pid, id, nil)
if resp != nil && resp.StatusCode == http.StatusForbidden {
return ErrStatusForbidden
}
if err != nil {
return err
}
Expand Down

0 comments on commit 50a41b1

Please sign in to comment.