Skip to content

Commit

Permalink
pr create: Get effective reviewers, and always suggest all names in n…
Browse files Browse the repository at this point in the history
…ame cache as reviewers
  • Loading branch information
craftamap committed Oct 13, 2023
1 parent 9693539 commit e0d0a67
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
15 changes: 10 additions & 5 deletions client/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ type Repository struct {
// UpdatedOn time.Time `mapstructure:"UpdatedOn"`
}

type DefaultReviewers struct {
Values []*Account `json:"values"`
type EffectiveDefaultReviewer struct {
Type string
ReviewerType string
User *Account
}
type EffectiveDefaultReviewers struct {
Values []*EffectiveDefaultReviewer `mapstructure:"EffectiveDefaultReviewers"`
}

func (c Client) RepositoriesForWorkspace(repoOrga string) ([]Repository, error) {
Expand Down Expand Up @@ -76,19 +81,19 @@ func (c Client) RepositoryGet(repoOrga string, repoSlug string) (*Repository, er
return &repo, nil
}

func (c Client) GetDefaultReviewers(repoOrga string, repoSlug string) (*DefaultReviewers, error) {
func (c Client) GetEffectiveDefaultReviewers(repoOrga string, repoSlug string) (*EffectiveDefaultReviewers, error) {
client := bitbucket.NewBasicAuth(c.Username, c.Password)
opt := &bitbucket.RepositoryOptions{
Owner: repoOrga,
RepoSlug: repoSlug,
}

response, err := client.Repositories.Repository.ListDefaultReviewers(opt)
response, err := client.Repositories.Repository.ListEffectiveDefaultReviewers(opt)
if err != nil {
return nil, err
}

defaultReviewers := DefaultReviewers{}
defaultReviewers := EffectiveDefaultReviewers{}
err = mapstructure.Decode(response, &defaultReviewers)
if err != nil {
return nil, err
Expand Down
19 changes: 13 additions & 6 deletions cmd/commands/pr/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func Add(prCmd *cobra.Command, globalOpts *options.GlobalOptions) {
return
}

defaultReviewers, err := c.GetDefaultReviewers(bbrepo.RepoOrga, bbrepo.RepoSlug)
defaultReviewers, err := c.GetEffectiveDefaultReviewers(bbrepo.RepoOrga, bbrepo.RepoSlug)
if err != nil {
logging.Error(err)
return
Expand All @@ -110,11 +110,11 @@ func Add(prCmd *cobra.Command, globalOpts *options.GlobalOptions) {
logging.Warning("Can't get the current user - this means that the default reviewers won't be added to this pull request. Make sure to grant the account-scope for your access token")
} else {
for _, rev := range defaultReviewers.Values {
if currentUser.Uuid != rev.UUID {
reviewers = append(reviewers, rev.UUID)
if currentUser.Uuid != rev.User.UUID {
reviewers = append(reviewers, rev.User.UUID)
}
// Add the user to the cache in any case
ReviewersNameCache[rev.UUID] = rev.DisplayName
ReviewersNameCache[rev.User.UUID] = rev.User.DisplayName
}
}

Expand Down Expand Up @@ -394,7 +394,7 @@ func manageReviewers(bbrepo *bbgit.BitbucketRepo, c *client.Client, currentUser
nameToUUID[name] = rev
}
if len(listOfNames) == 0 {
logging.Warning("No reviwers to remove available")
logging.Warning("No reviewers to remove available")
continue
}
var removedReviewers []string
Expand All @@ -421,8 +421,15 @@ func manageReviewers(bbrepo *bbgit.BitbucketRepo, c *client.Client, currentUser
logging.Warning(fmt.Sprint("Could not get workspace members - create the pr without reviewers and add them manually using the browser", err))
continue
}
logging.Debugf("members: %+v", members)

var nonReviewersMembers []string
// populate with the name cache first
for uuid := range ReviewersNameCache {
if !stringInSlice(uuid, reviewers) && uuid != currentUser.Uuid {
nonReviewersMembers = append(nonReviewersMembers, uuid)
}
}

Check failure on line 431 in cmd/commands/pr/create/create.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)

Check failure on line 431 in cmd/commands/pr/create/create.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
// then use workspace members if available
for _, member := range members.Values {
ReviewersNameCache[member.User.UUID] = member.User.DisplayName
if !stringInSlice(member.User.UUID, reviewers) && member.User.UUID != currentUser.Uuid {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ require (
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/ktrysmt/go-bitbucket => github.com/craftamap/go-bitbucket v0.6.5-0.20230711114627-bee07ecbb5b5
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,8 @@ github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/craftamap/go-bitbucket v0.6.5-0.20230711114627-bee07ecbb5b5 h1:SkKQhjbqAiWg+NReTFlyCi04kZ7jgBhTB/IxjxDjThM=
github.com/craftamap/go-bitbucket v0.6.5-0.20230711114627-bee07ecbb5b5/go.mod h1:QvxNfWkVjw8mPuvfGOgWHuv51P5yZKFqXdPh0JeG8B8=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creack/pty v1.1.13/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI=
Expand Down Expand Up @@ -911,8 +913,6 @@ github.com/kr/pty v1.1.4/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/ktrysmt/go-bitbucket v0.9.61 h1:D91llgR+g+lPBafyb1bKZpeoqSiXE4UcObACwVXMA44=
github.com/ktrysmt/go-bitbucket v0.9.61/go.mod h1:QvxNfWkVjw8mPuvfGOgWHuv51P5yZKFqXdPh0JeG8B8=
github.com/kyokomi/emoji v2.2.4+incompatible h1:np0woGKwx9LiHAQmwZx79Oc0rHpNw3o+3evou4BEPv4=
github.com/kyokomi/emoji v2.2.4+incompatible/go.mod h1:mZ6aGCD7yk8j6QY6KICwnZ2pxoszVseX1DNoGtU2tBA=
github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
Expand Down

0 comments on commit e0d0a67

Please sign in to comment.