Skip to content

Commit 595a929

Browse files
authored
Fix convert.ToTeams on empty input (#28426)
Fixes #28420 Don't return `nil` if the input was empty.
1 parent abfdaef commit 595a929

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

services/convert/convert.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -308,40 +308,38 @@ func ToTeam(ctx context.Context, team *organization.Team, loadOrg ...bool) (*api
308308

309309
// ToTeams convert models.Team list to api.Team list
310310
func ToTeams(ctx context.Context, teams []*organization.Team, loadOrgs bool) ([]*api.Team, error) {
311-
if len(teams) == 0 || teams[0] == nil {
312-
return nil, nil
313-
}
314-
315311
cache := make(map[int64]*api.Organization)
316-
apiTeams := make([]*api.Team, len(teams))
317-
for i := range teams {
318-
if err := teams[i].LoadUnits(ctx); err != nil {
312+
apiTeams := make([]*api.Team, 0, len(teams))
313+
for _, t := range teams {
314+
if err := t.LoadUnits(ctx); err != nil {
319315
return nil, err
320316
}
321317

322-
apiTeams[i] = &api.Team{
323-
ID: teams[i].ID,
324-
Name: teams[i].Name,
325-
Description: teams[i].Description,
326-
IncludesAllRepositories: teams[i].IncludesAllRepositories,
327-
CanCreateOrgRepo: teams[i].CanCreateOrgRepo,
328-
Permission: teams[i].AccessMode.String(),
329-
Units: teams[i].GetUnitNames(),
330-
UnitsMap: teams[i].GetUnitsMap(),
318+
apiTeam := &api.Team{
319+
ID: t.ID,
320+
Name: t.Name,
321+
Description: t.Description,
322+
IncludesAllRepositories: t.IncludesAllRepositories,
323+
CanCreateOrgRepo: t.CanCreateOrgRepo,
324+
Permission: t.AccessMode.String(),
325+
Units: t.GetUnitNames(),
326+
UnitsMap: t.GetUnitsMap(),
331327
}
332328

333329
if loadOrgs {
334-
apiOrg, ok := cache[teams[i].OrgID]
330+
apiOrg, ok := cache[t.OrgID]
335331
if !ok {
336-
org, err := organization.GetOrgByID(ctx, teams[i].OrgID)
332+
org, err := organization.GetOrgByID(ctx, t.OrgID)
337333
if err != nil {
338334
return nil, err
339335
}
340336
apiOrg = ToOrganization(ctx, org)
341-
cache[teams[i].OrgID] = apiOrg
337+
cache[t.OrgID] = apiOrg
342338
}
343-
apiTeams[i].Organization = apiOrg
339+
apiTeam.Organization = apiOrg
344340
}
341+
342+
apiTeams = append(apiTeams, apiTeam)
345343
}
346344
return apiTeams, nil
347345
}

services/convert/pull_review.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,9 @@ func ToPullReview(ctx context.Context, r *issues_model.Review, doer *user_model.
2121
r.Reviewer = user_model.NewGhostUser()
2222
}
2323

24-
apiTeam, err := ToTeam(ctx, r.ReviewerTeam)
25-
if err != nil {
26-
return nil, err
27-
}
28-
2924
result := &api.PullReview{
3025
ID: r.ID,
3126
Reviewer: ToUser(ctx, r.Reviewer, doer),
32-
ReviewerTeam: apiTeam,
3327
State: api.ReviewStateUnknown,
3428
Body: r.Content,
3529
CommitID: r.CommitID,
@@ -43,6 +37,14 @@ func ToPullReview(ctx context.Context, r *issues_model.Review, doer *user_model.
4337
HTMLPullURL: r.Issue.HTMLURL(),
4438
}
4539

40+
if r.ReviewerTeam != nil {
41+
var err error
42+
result.ReviewerTeam, err = ToTeam(ctx, r.ReviewerTeam)
43+
if err != nil {
44+
return nil, err
45+
}
46+
}
47+
4648
switch r.Type {
4749
case issues_model.ReviewTypeApprove:
4850
result.State = api.ReviewStateApproved

0 commit comments

Comments
 (0)