fix: allow repository deletion when referenced by a repo-specific access token (#11927)

Fixes #11919.

## Checklist

The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. All work and communication must conform to Forgejo's [AI Agreement](https://codeberg.org/forgejo/governance/src/branch/main/AIAgreement.md). There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org).

### Tests for Go changes

- I added test coverage for Go changes...
  - [x] in their respective `*_test.go` for unit tests.
  - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I ran...
  - [ ] `make pr-go` before pushing

### Documentation

- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.

### Release notes

- [ ] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change.
- [x] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change.
    - Will be a fix before the feature is released, therefore not "visible to users".

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11927
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Reviewed-by: Antonin Delpeuch <wetneb@noreply.codeberg.org>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
This commit is contained in:
Mathieu Fenniak 2026-04-01 16:05:20 +02:00 committed by Mathieu Fenniak
parent d23d895220
commit 8f5dd81537
2 changed files with 19 additions and 0 deletions

View file

@ -13,6 +13,7 @@ import (
activities_model "forgejo.org/models/activities"
admin_model "forgejo.org/models/admin"
asymkey_model "forgejo.org/models/asymkey"
auth_model "forgejo.org/models/auth"
"forgejo.org/models/db"
git_model "forgejo.org/models/git"
issues_model "forgejo.org/models/issues"
@ -189,6 +190,7 @@ func DeleteRepositoryDirectly(ctx context.Context, doer *user_model.User, repoID
&actions_model.ActionUser{RepoID: repoID},
&repo_model.RepoArchiveDownloadCount{RepoID: repoID},
&actions_model.ActionRunnerToken{RepoID: optional.Some(repoID)},
&auth_model.AccessTokenResourceRepo{RepoID: repoID},
); err != nil {
return fmt.Errorf("deleteBeans: %w", err)
}

View file

@ -6,6 +6,7 @@ package repository
import (
"testing"
auth_model "forgejo.org/models/auth"
"forgejo.org/models/db"
repo_model "forgejo.org/models/repo"
"forgejo.org/models/unit"
@ -62,3 +63,19 @@ func TestDeleteRepository(t *testing.T) {
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
require.NoError(t, DeleteRepository(t.Context(), doer, repo, false))
}
func TestDeleteRepositoryWithReferences(t *testing.T) {
require.NoError(t, unittest.PrepareTestDatabase())
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
token1 := unittest.AssertExistsAndLoadBean(t, &auth_model.AccessToken{ID: 1})
err := db.Insert(t.Context(), &auth_model.AccessTokenResourceRepo{
TokenID: token1.ID,
RepoID: repo.ID,
})
require.NoError(t, err)
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
require.NoError(t, DeleteRepository(t.Context(), doer, repo, false))
}