Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.

Commit

Permalink
refactor: adjustments to bernard and webhooks (#48)
Browse files Browse the repository at this point in the history
* fix(triggers/bernard): add addedfolders to diff folders result

* refactor(triggers/bernard): wrap getFolder error responses for additional context

* refactor(triggers/bernard): add removed folders to diff folder map

* ci: remove artifact cleaner

* refactor(triggers/bernard): show drive name in logs

* refactor(triggers): case insensitive event type
  • Loading branch information
l3uddz authored Oct 10, 2020
1 parent 57455d0 commit 6dd262b
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 29 deletions.
18 changes: 0 additions & 18 deletions .github/workflows/artifact.yml

This file was deleted.

7 changes: 6 additions & 1 deletion triggers/bernard/bernard.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,5 +375,10 @@ func (d daemon) getScanTask(drive *drive, paths *Paths) *scanTask {
}

func (d daemon) withDriveLog(driveID string) zerolog.Logger {
return d.log.With().Str("drive_id", driveID).Logger()
drive, err := d.store.GetDrive(driveID)
if err != nil {
return d.log.With().Str("drive_id", driveID).Logger()
}

return d.log.With().Str("drive_id", driveID).Str("drive_name", drive.Name).Logger()
}
20 changes: 20 additions & 0 deletions triggers/bernard/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,23 @@ func (d *bds) GetFolder(driveID string, folderID string) (*datastore.Folder, err

return f, nil
}

const sqlSelectDrive = `SELECT d.id, f.name, d.pageToken FROM drive d JOIN folder f ON f.id = d.id WHERE d.id = $1 LIMIT 1`

func (d *bds) GetDrive(driveID string) (*datastore.Drive, error) {
drv := new(datastore.Drive)

row := d.DB.QueryRow(sqlSelectDrive, driveID)
err := row.Scan(&drv.ID, &drv.Name, &drv.PageToken)

switch {
case errors.Is(err, sql.ErrNoRows):
return nil, fmt.Errorf("%v: drive not found: %w", driveID, sql.ErrNoRows)
case err != nil:
return nil, err
default:
break
}

return drv, nil
}
12 changes: 8 additions & 4 deletions triggers/bernard/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func getDiffFolderMaps(diff *sqlite.Difference) *diffFolderMaps {
oldFolders[f.Old.ID] = diff.ChangedFolders[i].Old
}

for i, f := range diff.RemovedFolders {
oldFolders[f.ID] = diff.RemovedFolders[i]
}

return &diffFolderMaps{
Current: currentFolders,
Old: oldFolders,
Expand Down Expand Up @@ -105,7 +109,7 @@ func getDiffFolders(store *bds, driveId string, diff *sqlite.Difference) (*Paren
for _, file := range diff.AddedFiles {
folder, err := getFolder(store, driveId, file.Parent, folderMaps.Current)
if err != nil {
return nil, err
return nil, fmt.Errorf("added file: %w", err)
}

newParents[folder.ID] = *folder
Expand All @@ -116,15 +120,15 @@ func getDiffFolders(store *bds, driveId string, diff *sqlite.Difference) (*Paren
// current
currentFolder, err := getFolder(store, driveId, file.New.Parent, folderMaps.Current)
if err != nil {
return nil, err
return nil, fmt.Errorf("changed new file: %w", err)
}

newParents[currentFolder.ID] = *currentFolder

// old
oldFolder, err := getFolder(store, driveId, file.Old.Parent, folderMaps.Old)
if err != nil {
return nil, err
return nil, fmt.Errorf("changed old file: %w", err)
}

oldParents[oldFolder.ID] = *oldFolder
Expand All @@ -134,7 +138,7 @@ func getDiffFolders(store *bds, driveId string, diff *sqlite.Difference) (*Paren
for _, file := range diff.RemovedFiles {
oldFolder, err := getFolder(store, driveId, file.Parent, folderMaps.Old)
if err != nil {
return nil, err
return nil, fmt.Errorf("removed file: %w", err)
}

oldParents[oldFolder.ID] = *oldFolder
Expand Down
5 changes: 3 additions & 2 deletions triggers/lidarr/lidarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"path"
"strings"
"time"

"github.com/cloudbox/autoscan"
Expand Down Expand Up @@ -64,13 +65,13 @@ func (h handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {

l.Trace().Interface("event", event).Msg("Received JSON body")

if event.Type == "Test" {
if strings.EqualFold(event.Type, "Test") {
l.Debug().Msg("Received test event")
rw.WriteHeader(http.StatusOK)
return
}

if event.Type != "Download" || len(event.Files) == 0 {
if !strings.EqualFold(event.Type, "Download") || len(event.Files) == 0 {
l.Error().Msg("Required fields are missing")
rw.WriteHeader(http.StatusBadRequest)
return
Expand Down
5 changes: 3 additions & 2 deletions triggers/radarr/radarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"path"
"strings"
"time"

"github.com/cloudbox/autoscan"
Expand Down Expand Up @@ -68,13 +69,13 @@ func (h handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {

rlog.Trace().Interface("event", event).Msg("Received JSON body")

if event.Type == "Test" {
if strings.EqualFold(event.Type, "Test") {
rlog.Debug().Msg("Received test event")
rw.WriteHeader(http.StatusOK)
return
}

if event.Type != "Download" || event.File.RelativePath == "" || event.Movie.FolderPath == "" {
if !strings.EqualFold(event.Type, "Download") || event.File.RelativePath == "" || event.Movie.FolderPath == "" {
rlog.Error().Msg("Required fields are missing")
rw.WriteHeader(http.StatusBadRequest)
return
Expand Down
5 changes: 3 additions & 2 deletions triggers/sonarr/sonarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"path"
"strings"
"time"

"github.com/cloudbox/autoscan"
Expand Down Expand Up @@ -68,13 +69,13 @@ func (h handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {

rlog.Trace().Interface("event", event).Msg("Received JSON body")

if event.Type == "Test" {
if strings.EqualFold(event.Type, "Test") {
rlog.Debug().Msg("Received test event")
rw.WriteHeader(http.StatusOK)
return
}

if event.Type != "Download" || event.File.RelativePath == "" || event.Series.Path == "" {
if !strings.EqualFold(event.Type, "Download") || event.File.RelativePath == "" || event.Series.Path == "" {
rlog.Error().Msg("Required fields are missing")
rw.WriteHeader(http.StatusBadRequest)
return
Expand Down

0 comments on commit 6dd262b

Please sign in to comment.