From abfac05b667eb1b220c6a2f730e86fa45e9b343a Mon Sep 17 00:00:00 2001 From: ze0s <43699394+zze0s@users.noreply.github.com> Date: Sun, 11 Aug 2024 20:39:11 +0200 Subject: [PATCH] fix(releases): retry action missing external indexer identifier (#1612) * fix(releases): retry action missing external identifier * fix(actions): arrs set fallback indexer identifier --- internal/action/lidarr.go | 6 ++--- internal/action/radarr.go | 6 ++--- internal/action/readarr.go | 6 ++--- internal/action/sonarr.go | 6 ++--- internal/action/whisparr.go | 6 ++--- internal/database/indexer.go | 45 ++++++++++++++++++++++++++++++++++++ internal/domain/indexer.go | 15 ++++++++++++ internal/indexer/service.go | 15 ++++++++++++ internal/release/service.go | 18 ++++++++++++--- 9 files changed, 105 insertions(+), 18 deletions(-) diff --git a/internal/action/lidarr.go b/internal/action/lidarr.go index cb2455d..c796fa0 100644 --- a/internal/action/lidarr.go +++ b/internal/action/lidarr.go @@ -59,11 +59,11 @@ func (s *service) lidarr(ctx context.Context, action *domain.Action, release dom DownloadUrl: release.DownloadURL, MagnetUrl: release.MagnetURI, Size: int64(release.Size), - Indexer: release.Indexer.IdentifierExternal, + Indexer: release.Indexer.GetExternalIdentifier(), DownloadClientId: externalClientId, DownloadClient: externalClient, - DownloadProtocol: string(release.Protocol), - Protocol: string(release.Protocol), + DownloadProtocol: release.Protocol.String(), + Protocol: release.Protocol.String(), PublishDate: time.Now().Format(time.RFC3339), } diff --git a/internal/action/radarr.go b/internal/action/radarr.go index 9220fd2..3461761 100644 --- a/internal/action/radarr.go +++ b/internal/action/radarr.go @@ -58,11 +58,11 @@ func (s *service) radarr(ctx context.Context, action *domain.Action, release dom DownloadUrl: release.DownloadURL, MagnetUrl: release.MagnetURI, Size: int64(release.Size), - Indexer: release.Indexer.IdentifierExternal, + Indexer: release.Indexer.GetExternalIdentifier(), DownloadClientId: externalClientId, DownloadClient: externalClient, - DownloadProtocol: string(release.Protocol), - Protocol: string(release.Protocol), + DownloadProtocol: release.Protocol.String(), + Protocol: release.Protocol.String(), PublishDate: time.Now().Format(time.RFC3339), } diff --git a/internal/action/readarr.go b/internal/action/readarr.go index f0d1c4e..0ff6f0f 100644 --- a/internal/action/readarr.go +++ b/internal/action/readarr.go @@ -58,11 +58,11 @@ func (s *service) readarr(ctx context.Context, action *domain.Action, release do DownloadUrl: release.DownloadURL, MagnetUrl: release.MagnetURI, Size: int64(release.Size), - Indexer: release.Indexer.IdentifierExternal, + Indexer: release.Indexer.GetExternalIdentifier(), DownloadClientId: externalClientId, DownloadClient: externalClient, - DownloadProtocol: string(release.Protocol), - Protocol: string(release.Protocol), + DownloadProtocol: release.Protocol.String(), + Protocol: release.Protocol.String(), PublishDate: time.Now().Format(time.RFC3339), } diff --git a/internal/action/sonarr.go b/internal/action/sonarr.go index e6b7f1c..6012034 100644 --- a/internal/action/sonarr.go +++ b/internal/action/sonarr.go @@ -58,11 +58,11 @@ func (s *service) sonarr(ctx context.Context, action *domain.Action, release dom DownloadUrl: release.DownloadURL, MagnetUrl: release.MagnetURI, Size: int64(release.Size), - Indexer: release.Indexer.IdentifierExternal, + Indexer: release.Indexer.GetExternalIdentifier(), DownloadClientId: externalClientId, DownloadClient: externalClient, - DownloadProtocol: string(release.Protocol), - Protocol: string(release.Protocol), + DownloadProtocol: release.Protocol.String(), + Protocol: release.Protocol.String(), PublishDate: time.Now().Format(time.RFC3339), } diff --git a/internal/action/whisparr.go b/internal/action/whisparr.go index b98c392..a131ac5 100644 --- a/internal/action/whisparr.go +++ b/internal/action/whisparr.go @@ -58,11 +58,11 @@ func (s *service) whisparr(ctx context.Context, action *domain.Action, release d DownloadUrl: release.DownloadURL, MagnetUrl: release.MagnetURI, Size: int64(release.Size), - Indexer: release.Indexer.IdentifierExternal, + Indexer: release.Indexer.GetExternalIdentifier(), DownloadClientId: externalClientId, DownloadClient: externalClient, - DownloadProtocol: string(release.Protocol), - Protocol: string(release.Protocol), + DownloadProtocol: release.Protocol.String(), + Protocol: release.Protocol.String(), PublishDate: time.Now().Format(time.RFC3339), } diff --git a/internal/database/indexer.go b/internal/database/indexer.go index 6cc34ed..78f09e2 100644 --- a/internal/database/indexer.go +++ b/internal/database/indexer.go @@ -165,6 +165,51 @@ func (r *IndexerRepo) FindByID(ctx context.Context, id int) (*domain.Indexer, er return &i, nil } +func (r *IndexerRepo) GetBy(ctx context.Context, req domain.GetIndexerRequest) (*domain.Indexer, error) { + queryBuilder := r.db.squirrel. + Select("id", "enabled", "name", "identifier", "identifier_external", "implementation", "base_url", "settings"). + From("indexer") + + if req.ID > 0 { + queryBuilder = queryBuilder.Where(sq.Eq{"id": req.ID}) + } else if req.Name != "" { + queryBuilder = queryBuilder.Where(sq.Eq{"name": req.Name}) + } else if req.Identifier != "" { + queryBuilder = queryBuilder.Where(sq.Eq{"identifier": req.Identifier}) + } + + query, args, err := queryBuilder.ToSql() + if err != nil { + return nil, errors.Wrap(err, "error building query") + } + + row := r.db.handler.QueryRowContext(ctx, query, args...) + if err := row.Err(); err != nil { + return nil, errors.Wrap(err, "error executing query") + } + + var i domain.Indexer + + var identifierExternal, implementation, baseURL, settings sql.Null[string] + + if err := row.Scan(&i.ID, &i.Enabled, &i.Name, &i.Identifier, &identifierExternal, &implementation, &baseURL, &settings); err != nil { + return nil, errors.Wrap(err, "error scanning row") + } + + i.IdentifierExternal = identifierExternal.V + i.Implementation = implementation.V + i.BaseURL = baseURL.V + + var settingsMap map[string]string + if err = json.Unmarshal([]byte(settings.V), &settingsMap); err != nil { + return nil, errors.Wrap(err, "error unmarshal settings") + } + + i.Settings = settingsMap + + return &i, nil +} + func (r *IndexerRepo) FindByFilterID(ctx context.Context, id int) ([]domain.Indexer, error) { queryBuilder := r.db.squirrel. Select("id", "enabled", "name", "identifier", "identifier_external", "base_url", "settings"). diff --git a/internal/domain/indexer.go b/internal/domain/indexer.go index 483e16b..7a516c8 100644 --- a/internal/domain/indexer.go +++ b/internal/domain/indexer.go @@ -22,6 +22,7 @@ type IndexerRepo interface { Delete(ctx context.Context, id int) error FindByFilterID(ctx context.Context, id int) ([]Indexer, error) FindByID(ctx context.Context, id int) (*Indexer, error) + GetBy(ctx context.Context, req GetIndexerRequest) (*Indexer, error) ToggleEnabled(ctx context.Context, indexerID int, enabled bool) error } @@ -43,6 +44,14 @@ type IndexerMinimal struct { IdentifierExternal string `json:"identifier_external"` } +func (m IndexerMinimal) GetExternalIdentifier() string { + if m.IdentifierExternal != "" { + return m.IdentifierExternal + } + + return m.Identifier +} + type IndexerDefinition struct { ID int `json:"id,omitempty"` Name string `json:"name"` @@ -412,3 +421,9 @@ type IndexerTestApiRequest struct { ApiUser string `json:"api_user,omitempty"` ApiKey string `json:"api_key"` } + +type GetIndexerRequest struct { + ID int + Identifier string + Name string +} diff --git a/internal/indexer/service.go b/internal/indexer/service.go index fab6389..47c9852 100644 --- a/internal/indexer/service.go +++ b/internal/indexer/service.go @@ -31,6 +31,7 @@ type Service interface { FindByFilterID(ctx context.Context, id int) ([]domain.Indexer, error) FindByID(ctx context.Context, id int) (*domain.Indexer, error) List(ctx context.Context) ([]domain.Indexer, error) + GetBy(ctx context.Context, req domain.GetIndexerRequest) (*domain.Indexer, error) GetAll() ([]*domain.IndexerDefinition, error) GetTemplates() ([]domain.IndexerDefinition, error) LoadIndexerDefinitions() error @@ -98,6 +99,10 @@ func (s *service) Store(ctx context.Context, indexer domain.Indexer) (*domain.In indexer.Identifier = slug.Make(fmt.Sprintf("%s-%s", indexer.Implementation, cleanName)) } + if indexer.IdentifierExternal == "" { + indexer.IdentifierExternal = indexer.Name + } + i, err := s.repo.Store(ctx, indexer) if err != nil { s.log.Error().Err(err).Msgf("failed to store indexer: %s", indexer.Name) @@ -217,6 +222,16 @@ func (s *service) List(ctx context.Context) ([]domain.Indexer, error) { return indexers, err } +func (s *service) GetBy(ctx context.Context, req domain.GetIndexerRequest) (*domain.Indexer, error) { + indexer, err := s.repo.GetBy(ctx, req) + if err != nil { + s.log.Error().Err(err).Msgf("could not get indexer by: %v", req) + return nil, err + } + + return indexer, err +} + func (s *service) GetAll() ([]*domain.IndexerDefinition, error) { var res = make([]*domain.IndexerDefinition, 0) diff --git a/internal/release/service.go b/internal/release/service.go index ceddbcc..7054264 100644 --- a/internal/release/service.go +++ b/internal/release/service.go @@ -375,19 +375,31 @@ func (s *service) Retry(ctx context.Context, req *domain.ReleaseActionRetryReq) // get release release, err := s.Get(ctx, &domain.GetReleaseRequest{Id: req.ReleaseId}) if err != nil { - return err + return errors.Wrap(err, "retry error: could not find release by id: %d", req.ReleaseId) + } + + indexerInfo, err := s.indexerSvc.GetBy(ctx, domain.GetIndexerRequest{Identifier: release.Indexer.Identifier}) + if err != nil { + return errors.Wrap(err, "retry error: could not get indexer by identifier: %s", release.Indexer.Identifier) + } + + release.Indexer = domain.IndexerMinimal{ + ID: int(indexerInfo.ID), + Name: indexerInfo.Name, + Identifier: indexerInfo.Identifier, + IdentifierExternal: indexerInfo.IdentifierExternal, } // get release filter action status status, err := s.GetActionStatus(ctx, &domain.GetReleaseActionStatusRequest{Id: req.ActionStatusId}) if err != nil { - return err + return errors.Wrap(err, "retry error: could not get release action") } // get filter action with action id from status filterAction, err := s.actionSvc.Get(ctx, &domain.GetActionRequest{Id: int(status.ActionID)}) if err != nil { - return err + return errors.Wrap(err, "retry error: could not get filter action for release") } // run filterAction