Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enable discard-approval-on-plan for gitlab #5388

Merged
merged 9 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions server/events/vcs/gitlab_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,17 @@ func (g *GitlabClient) MarkdownPullLink(pull models.PullRequest) (string, error)
return fmt.Sprintf("!%d", pull.Num), nil
}

func (g *GitlabClient) DiscardReviews(_ models.Repo, _ models.PullRequest) error {
// TODO implement
// DiscardReviews discards all reviews on a pull request
// This is only available with a bot token and otherwise will return 401 unauthorized
func (g *GitlabClient) DiscardReviews(repo models.Repo, pull models.PullRequest) error {
response, err := g.Client.MergeRequestApprovals.ResetApprovalsOfMergeRequest(repo.FullName, pull.Num)
if err != nil {
return errors.Wrap(err, "unable to discard reviews")
}
if response.StatusCode != http.StatusOK {
return errors.Errorf("discarding reviews failed with status code %d", response.StatusCode)
}

return nil
}

Expand Down
57 changes: 57 additions & 0 deletions server/events/vcs/gitlab_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1200,3 +1200,60 @@ func TestGitlabClient_GetTeamNamesForUser(t *testing.T) {
})
}
}

func TestGithubClient_DiscardReviews(t *testing.T) {

cases := []struct {
description string
repoFullName string
pullReqeustId int
wantErr bool
}{
{
"success",
"runatlantis/atlantis",
42,
false,
},
{
"error",
"runatlantis/atlantis",
32,
true,
},
}
for _, c := range cases {
t.Run(c.description, func(t *testing.T) {
testServer := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.RequestURI {
case "/api/v4/projects/runatlantis%2Fatlantis/merge_requests/42/reset_approvals":
w.WriteHeader(http.StatusOK)
case "/api/v4/projects/runatlantis%2Fatlantis/merge_requests/32/reset_approvals":
http.Error(w, "No bot token", http.StatusUnauthorized)
default:
t.Errorf("got unexpected request at %q", r.RequestURI)
http.Error(w, "not found", http.StatusNotFound)
}
}))
internalClient, err := gitlab.NewClient("token", gitlab.WithBaseURL(testServer.URL))
Ok(t, err)
client := &GitlabClient{
Client: internalClient,
Version: nil,
}

repo := models.Repo{
FullName: c.repoFullName,
}

pr := models.PullRequest{
Num: c.pullReqeustId,
}

if err := client.DiscardReviews(repo, pr); (err != nil) != c.wantErr {
t.Errorf("DiscardReviews() error = %v", err)
}
})
}
}
Loading