forked from hinterland/hearth
feat: vendor gitea 1.16.2
This commit is contained in:
@@ -102,10 +102,14 @@ func DeleteOrganization(ctx context.Context, org *org_model.Organization, purge
|
||||
return nil
|
||||
}
|
||||
|
||||
func updateOrgRepoForVisibilityChanged(ctx context.Context, repo *repo_model.Repository, makePrivate bool) error {
|
||||
func updateRepoForVisibilityChanged(ctx context.Context, repo *repo_model.Repository, makePrivate bool) error {
|
||||
if err := repo.LoadOwner(ctx); err != nil {
|
||||
return fmt.Errorf("LoadOwner: %w", err)
|
||||
}
|
||||
|
||||
// Organization repository need to recalculate access table when visibility is changed.
|
||||
if err := access_model.RecalculateTeamAccesses(ctx, repo, 0); err != nil {
|
||||
return fmt.Errorf("recalculateTeamAccesses: %w", err)
|
||||
if err := access_model.RecalculateAccesses(ctx, repo); err != nil {
|
||||
return fmt.Errorf("RecalculateAccesses: %w", err)
|
||||
}
|
||||
|
||||
if makePrivate {
|
||||
@@ -135,7 +139,7 @@ func updateOrgRepoForVisibilityChanged(ctx context.Context, repo *repo_model.Rep
|
||||
return fmt.Errorf("getRepositoriesByForkID: %w", err)
|
||||
}
|
||||
for i := range forkRepos {
|
||||
if err := updateOrgRepoForVisibilityChanged(ctx, forkRepos[i], makePrivate); err != nil {
|
||||
if err := updateRepoForVisibilityChanged(ctx, forkRepos[i], makePrivate); err != nil {
|
||||
return fmt.Errorf("updateRepoForVisibilityChanged[%s]: %w", forkRepos[i].FullName(), err)
|
||||
}
|
||||
}
|
||||
@@ -161,10 +165,27 @@ func ChangeOrganizationVisibility(ctx context.Context, org *org_model.Organizati
|
||||
return err
|
||||
}
|
||||
for _, repo := range repos {
|
||||
if err := updateOrgRepoForVisibilityChanged(ctx, repo, visibility == structs.VisibleTypePrivate); err != nil {
|
||||
return fmt.Errorf("updateOrgRepoForVisibilityChanged: %w", err)
|
||||
if err := updateRepoForVisibilityChanged(ctx, repo, visibility == structs.VisibleTypePrivate); err != nil {
|
||||
return fmt.Errorf("updateRepoForVisibilityChanged: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateOrgEmailAddress validates and updates the organization's contact email.
|
||||
// A nil email means no change.
|
||||
func UpdateOrgEmailAddress(ctx context.Context, org *org_model.Organization, email *string) error {
|
||||
if email == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if *email != "" {
|
||||
if err := user_model.ValidateEmail(*email); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
org.Email = *email
|
||||
return user_model.UpdateUserCols(ctx, org.AsUser(), "email")
|
||||
}
|
||||
|
||||
@@ -10,28 +10,62 @@ import (
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
unittest.MainTest(m)
|
||||
}
|
||||
|
||||
func TestDeleteOrganization(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 6})
|
||||
assert.NoError(t, DeleteOrganization(t.Context(), org, false))
|
||||
unittest.AssertNotExistsBean(t, &organization.Organization{ID: 6})
|
||||
unittest.AssertNotExistsBean(t, &organization.OrgUser{OrgID: 6})
|
||||
unittest.AssertNotExistsBean(t, &organization.Team{OrgID: 6})
|
||||
func TestOrg(t *testing.T) {
|
||||
require.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
org = unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
|
||||
err := DeleteOrganization(t.Context(), org, false)
|
||||
assert.Error(t, err)
|
||||
assert.True(t, repo_model.IsErrUserOwnRepos(err))
|
||||
t.Run("UpdateOrgEmailAddress", func(t *testing.T) {
|
||||
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
|
||||
originalEmail := org.Email
|
||||
|
||||
user := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 5})
|
||||
assert.Error(t, DeleteOrganization(t.Context(), user, false))
|
||||
unittest.CheckConsistencyFor(t, &user_model.User{}, &organization.Team{})
|
||||
require.NoError(t, UpdateOrgEmailAddress(t.Context(), org, nil))
|
||||
unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3, Email: originalEmail})
|
||||
|
||||
newEmail := "contact@org3.example.com"
|
||||
require.NoError(t, UpdateOrgEmailAddress(t.Context(), org, &newEmail))
|
||||
unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3, Email: newEmail})
|
||||
|
||||
invalidEmail := "invalid email"
|
||||
err := UpdateOrgEmailAddress(t.Context(), org, &invalidEmail)
|
||||
require.ErrorIs(t, err, util.ErrInvalidArgument)
|
||||
unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3, Email: newEmail})
|
||||
|
||||
require.NoError(t, UpdateOrgEmailAddress(t.Context(), org, new("")))
|
||||
org = unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3, Email: ""})
|
||||
assert.Empty(t, org.Email)
|
||||
})
|
||||
|
||||
t.Run("DeleteOrganization", func(t *testing.T) {
|
||||
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 6})
|
||||
assert.NoError(t, DeleteOrganization(t.Context(), org, false))
|
||||
unittest.AssertNotExistsBean(t, &organization.Organization{ID: 6})
|
||||
unittest.AssertNotExistsBean(t, &organization.OrgUser{OrgID: 6})
|
||||
unittest.AssertNotExistsBean(t, &organization.Team{OrgID: 6})
|
||||
|
||||
org = unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
|
||||
err := DeleteOrganization(t.Context(), org, false)
|
||||
assert.Error(t, err)
|
||||
assert.True(t, repo_model.IsErrUserOwnRepos(err))
|
||||
|
||||
user := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 5})
|
||||
assert.Error(t, DeleteOrganization(t.Context(), user, false))
|
||||
unittest.CheckConsistencyFor(t, &user_model.User{}, &organization.Team{})
|
||||
})
|
||||
|
||||
t.Run("ChangeVisibilityWithUserFork", func(t *testing.T) {
|
||||
// org 19 has a repository 27 which has a forked repository 29 by user 20
|
||||
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 19})
|
||||
require.NoError(t, ChangeOrganizationVisibility(t.Context(), org, structs.VisibleTypePrivate))
|
||||
unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: org.ID, Visibility: structs.VisibleTypePrivate})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ package org
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
@@ -306,19 +307,19 @@ func removeTeamMember(ctx context.Context, team *organization.Team, user *user_m
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete access to team repositories.
|
||||
// Delete access to team repositories. If any user or repo is missing, we can continue.
|
||||
for _, repo := range repos {
|
||||
if err := access_model.RecalculateUserAccess(ctx, repo, user.ID); err != nil {
|
||||
if err := access_model.RecalculateUserAccess(ctx, repo, user.ID); err != nil && !errors.Is(err, util.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
|
||||
// Remove watches from now unaccessible
|
||||
if err := repo_service.ReconsiderWatches(ctx, repo, user); err != nil {
|
||||
// Remove watches from now inaccessible
|
||||
if err := repo_service.ReconsiderWatches(ctx, repo, user); err != nil && !errors.Is(err, util.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
|
||||
// Remove issue assignments from now unaccessible
|
||||
if err := repo_service.ReconsiderRepoIssuesAssignee(ctx, repo, user); err != nil {
|
||||
// Remove issue assignments from now inaccessible
|
||||
if err := repo_service.ReconsiderRepoIssuesAssignee(ctx, repo, user); err != nil && !errors.Is(err, util.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user