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 hide-prev-plan-comments Feature for BitBucket Cloud #4495

Merged
merged 17 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
77 changes: 76 additions & 1 deletion server/events/vcs/bitbucketcloud/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"fmt"
"io"
"net/http"
"strings"
"unicode/utf8"

validator "github.com/go-playground/validator/v10"
Expand Down Expand Up @@ -107,10 +108,84 @@
return nil
}

func (b *Client) HidePrevCommandComments(_ logging.SimpleLogging, _ models.Repo, _ int, _ string, _ string) error {
func (b *Client) HidePrevCommandComments(logger logging.SimpleLogging, repo models.Repo, pullNum int, command string, _ string) error {
// there is no way to hide comment, so delete them instead
me, err := b.GetMyUUID()
if err != nil {
return errors.Wrapf(err, "Cannot get my uuid! Please check required scope of the auth token!")
}
logger.Debug("My bitbucket user UUID is: %s", me)

comments, err := b.GetPullRequestComments(repo, pullNum)
if err != nil {
return err
}

for _, c := range comments {
logger.Debug("Comment is %v", c.Content.Raw)
if strings.EqualFold(*c.User.UUID, me) {
// do the same crude filtering as github client does
body := strings.Split(c.Content.Raw, "\n")
logger.Debug("Body is %s", body)
if len(body) == 0 {
continue
}
firstLine := strings.ToLower(body[0])
if strings.Contains(firstLine, strings.ToLower(command)) {
// we found our old comment that references that command
logger.Debug("Deleting comment with id %s", *c.ID)
b.DeletePullRequestComment(repo, pullNum, *c.ID)

Check failure on line 137 in server/events/vcs/bitbucketcloud/client.go

View workflow job for this annotation

GitHub Actions / Linting

Error return value of `b.DeletePullRequestComment` is not checked (errcheck)

Check failure on line 137 in server/events/vcs/bitbucketcloud/client.go

View workflow job for this annotation

GitHub Actions / Linting

[golangci-lint] reported by reviewdog 🐶 Error return value of `b.DeletePullRequestComment` is not checked (errcheck) Raw Output: server/events/vcs/bitbucketcloud/client.go:137:31: Error return value of `b.DeletePullRequestComment` is not checked (errcheck) b.DeletePullRequestComment(repo, pullNum, *c.ID) ^
}
}
}
return nil
}

func (b *Client) DeletePullRequestComment(repo models.Repo, pullNum int, commentId int) error {
path := fmt.Sprintf("%s/2.0/repositories/%s/pullrequests/%d/comments/%d", b.BaseURL, repo.FullName, pullNum, commentId)
_, err := b.makeRequest("DELETE", path, nil)
if err != nil {
return err
}
return nil
}

func (b *Client) GetPullRequestComments(repo models.Repo, pullNum int) (comments []PullRequestComment, err error) {
path := fmt.Sprintf("%s/2.0/repositories/%s/pullrequests/%d/comments", b.BaseURL, repo.FullName, pullNum)
res, err := b.makeRequest("GET", path, nil)
if err != nil {
return comments, err
}

var pulls PullRequestComments
if err := json.Unmarshal(res, &pulls); err != nil {
return comments, errors.Wrapf(err, "Could not parse response %q", string(res))
}
return pulls.Values, nil
}

func (b *Client) GetMyUUID() (uuid string, err error) {
path := fmt.Sprintf("%s/2.0/user", b.BaseURL)
resp, err := b.makeRequest("GET", path, nil)

if err != nil {
return uuid, err
}

var user User
if err := json.Unmarshal(resp, &user); err != nil {
return uuid, errors.Wrapf(err, "Could not parse response %q", string(resp))
}

if err := validator.New().Struct(user); err != nil {
return uuid, errors.Wrapf(err, "API response %q was missing a field", string(resp))
}

uuid = *user.UUID
return uuid, nil

}

// PullIsApproved returns true if the merge request was approved.
func (b *Client) PullIsApproved(logger logging.SimpleLogging, repo models.Repo, pull models.PullRequest) (approvalStatus models.ApprovalStatus, err error) {
path := fmt.Sprintf("%s/2.0/repositories/%s/pullrequests/%d", b.BaseURL, repo.FullName, pull.Num)
Expand Down
28 changes: 28 additions & 0 deletions server/events/vcs/bitbucketcloud/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,34 @@ type Repository struct {
FullName *string `json:"full_name,omitempty" validate:"required"`
Links Links `json:"links,omitempty" validate:"required"`
}

type User struct {
Type *string `json:"type,omitempty" validate:"required"`
CreateOn *string `json:"created_on" validate:"required"`
DisplayName *string `json:"display_name" validate:"required"`
Username *string `json:"username" validate:"required"`
UUID *string `json:"uuid" validate:"required"`
}

type UserInComment struct {
Type *string `json:"type,omitempty" validate:"required"`
Nickname *string `json:"nickname" validate:"required"`
DisplayName *string `json:"display_name" validate:"required"`
UUID *string `json:"uuid" validate:"required"`
}

type PullRequestComment struct {
ID *int `json:"id,omitempty" validate:"required"`
User *UserInComment `json:"user" validate:"required"`
Content *struct {
Raw string `json:"raw"`
} `json:"content" validate:"required"`
}

type PullRequestComments struct {
Values []PullRequestComment `json:"values,omitempty"`
}

type PullRequest struct {
ID *int `json:"id,omitempty" validate:"required"`
Source *BranchMeta `json:"source,omitempty" validate:"required"`
Expand Down
Loading