]> source.dussan.org Git - gitea.git/commitdiff
Fix `convert.ToTeams` on empty input (#28426)
authorKN4CK3R <admin@oldschoolhack.me>
Thu, 11 Jan 2024 22:12:08 +0000 (23:12 +0100)
committerGitHub <noreply@github.com>
Thu, 11 Jan 2024 22:12:08 +0000 (22:12 +0000)
Fixes #28420

Don't return `nil` if the input was empty.

services/convert/convert.go
services/convert/pull_review.go

index 366782390a7585f60153e4432e4e376ca398002c..860c3ef03a0faf77392f38b0d21b0237174a9b01 100644 (file)
@@ -308,40 +308,38 @@ 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 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
 }
index 0332606285d183d45093dbab97062a409d775970..aa7ad68a47856d740038cde1d642cfac01a1d429 100644 (file)
@@ -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