From 1cc22f8d95db6982e782018411138d0568632d19 Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Mon, 11 Dec 2023 19:10:59 +0000 Subject: [PATCH 1/3] Don't return nil on empty team. --- services/convert/convert.go | 43 ++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/services/convert/convert.go b/services/convert/convert.go index 366782390a758..779c9ede6c0f7 100644 --- a/services/convert/convert.go +++ b/services/convert/convert.go @@ -308,40 +308,43 @@ func ToTeam(ctx context.Context, team *organization.Team, loadOrg ...bool) (*api // ToTeams convert models.Team list to api.Team list func ToTeams(ctx context.Context, teams []*organization.Team, loadOrgs bool) ([]*api.Team, error) { - if len(teams) == 0 || teams[0] == nil { - return nil, nil - } - cache := make(map[int64]*api.Organization) - apiTeams := make([]*api.Team, len(teams)) - for i := range teams { - if err := teams[i].LoadUnits(ctx); err != nil { + apiTeams := make([]*api.Team, 0, len(teams)) + for _, t := range teams { + if t == nil { + // Could be nil if teams was created with make([]*Team, x) + continue + } + + if err := t.LoadUnits(ctx); err != nil { return nil, err } - apiTeams[i] = &api.Team{ - ID: teams[i].ID, - Name: teams[i].Name, - Description: teams[i].Description, - IncludesAllRepositories: teams[i].IncludesAllRepositories, - CanCreateOrgRepo: teams[i].CanCreateOrgRepo, - Permission: teams[i].AccessMode.String(), - Units: teams[i].GetUnitNames(), - UnitsMap: teams[i].GetUnitsMap(), + apiTeam := &api.Team{ + ID: t.ID, + Name: t.Name, + Description: t.Description, + IncludesAllRepositories: t.IncludesAllRepositories, + CanCreateOrgRepo: t.CanCreateOrgRepo, + Permission: t.AccessMode.String(), + Units: t.GetUnitNames(), + UnitsMap: t.GetUnitsMap(), } if loadOrgs { - apiOrg, ok := cache[teams[i].OrgID] + apiOrg, ok := cache[t.OrgID] if !ok { - org, err := organization.GetOrgByID(ctx, teams[i].OrgID) + org, err := organization.GetOrgByID(ctx, t.OrgID) if err != nil { return nil, err } apiOrg = ToOrganization(ctx, org) - cache[teams[i].OrgID] = apiOrg + cache[t.OrgID] = apiOrg } - apiTeams[i].Organization = apiOrg + apiTeam.Organization = apiOrg } + + apiTeams = append(apiTeams, apiTeam) } return apiTeams, nil } From cc286f8c726c0edf3e3da5a6f585389ace6647a9 Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Tue, 19 Dec 2023 18:03:07 +0000 Subject: [PATCH 2/3] Remove nil check. --- services/convert/convert.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/services/convert/convert.go b/services/convert/convert.go index 779c9ede6c0f7..860c3ef03a0fa 100644 --- a/services/convert/convert.go +++ b/services/convert/convert.go @@ -311,11 +311,6 @@ func ToTeams(ctx context.Context, teams []*organization.Team, loadOrgs bool) ([] cache := make(map[int64]*api.Organization) apiTeams := make([]*api.Team, 0, len(teams)) for _, t := range teams { - if t == nil { - // Could be nil if teams was created with make([]*Team, x) - continue - } - if err := t.LoadUnits(ctx); err != nil { return nil, err } From e737433640eb38940a90686d19c976ae037f8c91 Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Thu, 11 Jan 2024 21:47:45 +0000 Subject: [PATCH 3/3] Fix `ToPullReview`. --- services/convert/pull_review.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/services/convert/pull_review.go b/services/convert/pull_review.go index 0332606285d18..aa7ad68a47856 100644 --- a/services/convert/pull_review.go +++ b/services/convert/pull_review.go @@ -21,15 +21,9 @@ func ToPullReview(ctx context.Context, r *issues_model.Review, doer *user_model. r.Reviewer = user_model.NewGhostUser() } - apiTeam, err := ToTeam(ctx, r.ReviewerTeam) - if err != nil { - return nil, err - } - result := &api.PullReview{ ID: r.ID, Reviewer: ToUser(ctx, r.Reviewer, doer), - ReviewerTeam: apiTeam, State: api.ReviewStateUnknown, Body: r.Content, CommitID: r.CommitID, @@ -43,6 +37,14 @@ func ToPullReview(ctx context.Context, r *issues_model.Review, doer *user_model. HTMLPullURL: r.Issue.HTMLURL(), } + if r.ReviewerTeam != nil { + var err error + result.ReviewerTeam, err = ToTeam(ctx, r.ReviewerTeam) + if err != nil { + return nil, err + } + } + switch r.Type { case issues_model.ReviewTypeApprove: result.State = api.ReviewStateApproved