forgejo/services/packages/debian/notifier.go
Mathieu Fenniak dcd431b0d4 fix: ensure deleted Debian package does not remain referenced in the apt repository files (#9386)
Introduces a new `Notifier` which listens for `PackageDelete` events and triggers a rebuild of the `Packages` & `Release` files which are stored in the debian package repository.

Fixes #9369.

## Checklist

The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. 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

- I added test coverage for Go changes...
  - [ ] in their respective `*_test.go` for unit tests.
  - [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I added test coverage for JavaScript changes...
  - [ ] in `web_src/js/*.test.js` if it can be unit tested.
  - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)).

### 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

- [ ] I do not want this change to show in the release notes.
- [x] I want the title to show in the release notes with a link to this pull request.
- [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9386
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
2025-10-01 03:11:03 +02:00

55 lines
1.8 KiB
Go

// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package debian
import (
"context"
packages_model "forgejo.org/models/packages"
user_model "forgejo.org/models/user"
"forgejo.org/modules/log"
debian_module "forgejo.org/modules/packages/debian"
"forgejo.org/services/notify"
)
func init() {
notify.RegisterNotifier(&debianPackageNotifier{})
}
type debianPackageNotifier struct {
notify.NullNotifier
}
func (m *debianPackageNotifier) PackageDelete(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) {
rebuildFromPackageEvent(ctx, pd)
}
func rebuildFromPackageEvent(ctx context.Context, pd *packages_model.PackageDescriptor) {
if pd.Package == nil || pd.Package.Type != packages_model.TypeDebian {
return
}
pv, err := GetOrCreateRepositoryVersion(ctx, pd.Owner.ID)
if err != nil {
log.Error("GetOrCreateRepositoryVersion failed with error: %v", err)
return
}
for _, file := range pd.Files {
distribution := file.Properties.GetByName(debian_module.PropertyDistribution)
component := file.Properties.GetByName(debian_module.PropertyComponent)
architecture := file.Properties.GetByName(debian_module.PropertyArchitecture)
if distribution == "" || component == "" || architecture == "" {
log.Warn("Debian package had file missing all expected properties; distribution = %q, component = %q, architecture = %q", distribution, component, architecture)
return
}
log.Trace("Debian package change triggered rebuild of repository index for distribution = %q, component = %q, architecture = %q", distribution, component, architecture)
err = buildRepositoryFiles(ctx, pd.Owner.ID, pv, distribution, component, architecture)
if err != nil {
log.Error("buildRepositoryFiles failed with error: %v", err)
}
}
}