Add bulk repository deletion for organizations (#36763)

Fixes #36512

This PR adds a new API endpoint to delete all repositories within an
organization in a single operation, improving efficiency for
organization cleanup and management tasks.

---------

Signed-off-by: Karthik Bhandary <34509856+karthikbhandary2@users.noreply.github.com>
Co-authored-by: karthik.bhandary <karthik.bhandary@kfintech.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Karthik Bhandary
2026-04-08 10:02:22 +05:30
committed by GitHub
parent 03205d94da
commit fc178e3203
5 changed files with 158 additions and 6 deletions

View File

@@ -8,10 +8,12 @@ import (
"net/http"
"strings"
"testing"
"time"
auth_model "code.gitea.io/gitea/models/auth"
org_model "code.gitea.io/gitea/models/organization"
"code.gitea.io/gitea/models/perm"
repo_model "code.gitea.io/gitea/models/repo"
unit_model "code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
@@ -24,8 +26,14 @@ import (
"github.com/stretchr/testify/require"
)
func TestAPIOrgCreateRename(t *testing.T) {
func TestAPIOrg(t *testing.T) {
defer tests.PrepareTestEnv(t)()
t.Run("General", testAPIOrgGeneral)
t.Run("CreateAndRename", testAPIOrgCreateRename)
t.Run("DeleteOrgRepos", testAPIDeleteOrgRepos)
}
func testAPIOrgCreateRename(t *testing.T) {
token := getUserToken(t, "user1", auth_model.AccessTokenScopeWriteOrganization)
org := api.CreateOrgOption{
@@ -110,8 +118,7 @@ func TestAPIOrgCreateRename(t *testing.T) {
})
}
func TestAPIOrgGeneral(t *testing.T) {
defer tests.PrepareTestEnv(t)()
func testAPIOrgGeneral(t *testing.T) {
user1Session := loginUser(t, "user1")
user1Token := getTokenForLoggedInUser(t, user1Session, auth_model.AccessTokenScopeWriteOrganization)
@@ -260,3 +267,33 @@ func TestAPIOrgGeneral(t *testing.T) {
MakeRequest(t, req, http.StatusForbidden)
})
}
func testAPIDeleteOrgRepos(t *testing.T) {
org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "org3"})
orgRepos, err := repo_model.GetOrgRepositories(t.Context(), org3.ID)
require.NoError(t, err)
assert.NotEmpty(t, orgRepos) // this org contains repositories, so we can test the deletion of all org repos
t.Run("NoPermission", func(t *testing.T) {
nonOwnerSession := loginUser(t, "user4")
nonOwnerToken := getTokenForLoggedInUser(t, nonOwnerSession, auth_model.AccessTokenScopeWriteOrganization)
req := NewRequest(t, "DELETE", "/api/v1/orgs/org3/repos").AddTokenAuth(nonOwnerToken)
MakeRequest(t, req, http.StatusForbidden)
})
t.Run("DeleteAllOrgRepos", func(t *testing.T) {
session := loginUser(t, "user1")
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteOrganization, auth_model.AccessTokenScopeWriteRepository)
req := NewRequest(t, "DELETE", fmt.Sprintf("/api/v1/orgs/%s/repos", org3.Name)).AddTokenAuth(token)
MakeRequest(t, req, http.StatusAccepted)
assert.Eventually(t, func() bool {
repos, err := repo_model.GetOrgRepositories(t.Context(), org3.ID)
require.NoError(t, err)
return len(repos) == 0
}, 2*time.Second, 50*time.Millisecond)
req = NewRequest(t, "DELETE", fmt.Sprintf("/api/v1/orgs/%s/repos", org3.Name)).AddTokenAuth(token)
MakeRequest(t, req, http.StatusNoContent) // The org contains no repositories, so the API should return StatusNoContent
})
}