This repository has been archived by the owner on Aug 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: voltron4lyfe <[email protected]> Co-authored-by: l3uddz <[email protected]>
- Loading branch information
1 parent
63edb8e
commit ff17077
Showing
9 changed files
with
313 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,14 +6,17 @@ on: | |
- '*' | ||
tags: | ||
- 'v*' | ||
pull_request: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
# dependencies | ||
- name: goreleaser | ||
uses: goreleaser/goreleaser-action@v2 | ||
uses: goreleaser/goreleaser-action@v3 | ||
with: | ||
install-only: true | ||
version: 1.7.0 | ||
|
@@ -28,20 +31,22 @@ jobs: | |
run: task --version | ||
|
||
- name: qemu | ||
uses: docker/setup-qemu-action@v1 | ||
if: github.event.pull_request.head.repo.fork == false | ||
uses: docker/setup-qemu-action@v2 | ||
|
||
- name: buildx | ||
uses: docker/setup-buildx-action@v1 | ||
if: github.event.pull_request.head.repo.fork == false | ||
uses: docker/setup-buildx-action@v2 | ||
|
||
# checkout | ||
- name: checkout | ||
uses: actions/checkout@v2 | ||
uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 0 | ||
|
||
# setup go | ||
- name: go | ||
uses: actions/setup-go@v1 | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: 1.19 | ||
|
||
|
@@ -51,26 +56,18 @@ jobs: | |
go env | ||
# cache | ||
- name: cache-paths | ||
id: go-cache-paths | ||
run: | | ||
echo "::set-output name=go-build::$(go env GOCACHE)" | ||
echo "::set-output name=go-mod::$(go env GOMODCACHE)" | ||
- name: cache-build | ||
uses: actions/cache@v2 | ||
with: | ||
path: ${{ steps.go-cache-paths.outputs.go-build }} | ||
key: ${{ runner.os }}-go-build-${{ hashFiles('**/go.sum') }} | ||
|
||
- name: cache-mod | ||
uses: actions/cache@v2 | ||
- name: cache-go | ||
uses: actions/cache@v3 | ||
with: | ||
path: ${{ steps.go-cache-paths.outputs.go-mod }} | ||
key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }} | ||
path: | | ||
~/.cache/go-build | ||
~/go/pkg/mod | ||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} | ||
restore-keys: | | ||
${{ runner.os }}-go-cache-mod | ||
- name: cache-task | ||
uses: actions/cache@v2 | ||
uses: actions/cache@v3 | ||
with: | ||
path: .task/**/* | ||
key: ${{ runner.os }}-go-task | ||
|
@@ -106,19 +103,20 @@ jobs: | |
# artifacts | ||
- name: artifact_linux | ||
uses: actions/upload-artifact@v2-preview | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: build_linux | ||
path: dist/*linux* | ||
|
||
- name: artifact_darwin | ||
uses: actions/upload-artifact@v2-preview | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: build_darwin | ||
path: dist/*darwin* | ||
|
||
# docker login | ||
- name: docker login | ||
if: github.event.pull_request.head.repo.fork == false | ||
env: | ||
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} | ||
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} | ||
|
@@ -127,15 +125,15 @@ jobs: | |
# docker build (latest & tag) | ||
- name: release tag | ||
if: startsWith(github.ref, 'refs/tags/') == true | ||
if: startsWith(github.ref, 'refs/tags/') == true && github.event.pull_request.head.repo.fork == false | ||
uses: little-core-labs/[email protected] | ||
id: releasetag | ||
with: | ||
tagRegex: "v?(.+)" | ||
|
||
- name: docker - build release | ||
if: startsWith(github.ref, 'refs/tags/') == true | ||
uses: docker/build-push-action@v2 | ||
if: startsWith(github.ref, 'refs/tags/') == true && github.event.pull_request.head.repo.fork == false | ||
uses: docker/build-push-action@v3 | ||
with: | ||
context: . | ||
file: ./docker/Dockerfile | ||
|
@@ -150,13 +148,13 @@ jobs: | |
# docker build (branch) | ||
- name: branch name | ||
if: startsWith(github.ref, 'refs/tags/') == false | ||
if: startsWith(github.ref, 'refs/tags/') == false && github.event.pull_request.head.repo.fork == false | ||
id: branch-name | ||
uses: tj-actions/branch-names@v2.2 | ||
uses: tj-actions/branch-names@v6.2 | ||
|
||
- name: docker tag | ||
if: startsWith(github.ref, 'refs/tags/') == false | ||
uses: frabert/replace-string-action@master | ||
if: startsWith(github.ref, 'refs/tags/') == false && github.event.pull_request.head.repo.fork == false | ||
uses: frabert/replace-string-action@v2.3 | ||
id: dockertag | ||
with: | ||
pattern: '[:\.\/]+' | ||
|
@@ -165,8 +163,8 @@ jobs: | |
flags: 'g' | ||
|
||
- name: docker - build branch | ||
if: startsWith(github.ref, 'refs/tags/') == false | ||
uses: docker/build-push-action@v2 | ||
if: startsWith(github.ref, 'refs/tags/') == false && github.event.pull_request.head.repo.fork == false | ||
uses: docker/build-push-action@v3 | ||
with: | ||
context: . | ||
file: ./docker/Dockerfile | ||
|
@@ -180,5 +178,6 @@ jobs: | |
# cleanup | ||
- name: cleanup | ||
if: github.event.pull_request.head.repo.fork == false | ||
run: | | ||
rm -f ${HOME}/.docker/config.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package readarr | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"path" | ||
"strings" | ||
"time" | ||
|
||
"github.com/rs/zerolog/hlog" | ||
|
||
"github.com/cloudbox/autoscan" | ||
) | ||
|
||
type Config struct { | ||
Name string `yaml:"name"` | ||
Priority int `yaml:"priority"` | ||
Rewrite []autoscan.Rewrite `yaml:"rewrite"` | ||
Verbosity string `yaml:"verbosity"` | ||
} | ||
|
||
// New creates an autoscan-compatible HTTP Trigger for Readarr webhooks. | ||
func New(c Config) (autoscan.HTTPTrigger, error) { | ||
rewriter, err := autoscan.NewRewriter(c.Rewrite) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
trigger := func(callback autoscan.ProcessorFunc) http.Handler { | ||
return handler{ | ||
callback: callback, | ||
priority: c.Priority, | ||
rewrite: rewriter, | ||
} | ||
} | ||
|
||
return trigger, nil | ||
} | ||
|
||
type handler struct { | ||
priority int | ||
rewrite autoscan.Rewriter | ||
callback autoscan.ProcessorFunc | ||
} | ||
|
||
type readarrEvent struct { | ||
Type string `json:"eventType"` | ||
Upgrade bool `json:"isUpgrade"` | ||
|
||
Files []struct { | ||
Path string | ||
} `json:"bookFiles"` | ||
} | ||
|
||
func (h handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { | ||
var err error | ||
l := hlog.FromRequest(r) | ||
|
||
event := new(readarrEvent) | ||
err = json.NewDecoder(r.Body).Decode(event) | ||
if err != nil { | ||
l.Error().Err(err).Msg("Failed decoding request") | ||
rw.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
l.Trace().Interface("event", event).Msg("Received JSON body") | ||
|
||
if strings.EqualFold(event.Type, "Test") { | ||
l.Info().Msg("Received test event") | ||
rw.WriteHeader(http.StatusOK) | ||
return | ||
} | ||
|
||
//Only handle test and download. Everything else is ignored. | ||
if !strings.EqualFold(event.Type, "Download") || len(event.Files) == 0 { | ||
l.Error().Msg("Required fields are missing") | ||
rw.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
unique := make(map[string]bool) | ||
scans := make([]autoscan.Scan, 0) | ||
|
||
for _, f := range event.Files { | ||
folderPath := path.Dir(h.rewrite(f.Path)) | ||
if _, ok := unique[folderPath]; ok { | ||
continue | ||
} | ||
|
||
// add scan | ||
unique[folderPath] = true | ||
scans = append(scans, autoscan.Scan{ | ||
Folder: folderPath, | ||
Priority: h.priority, | ||
Time: now(), | ||
}) | ||
} | ||
|
||
err = h.callback(scans...) | ||
if err != nil { | ||
l.Error().Err(err).Msg("Processor could not process scans") | ||
rw.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
rw.WriteHeader(http.StatusOK) | ||
l.Info(). | ||
Str("path", scans[0].Folder). | ||
Str("event", event.Type). | ||
Msg("Scan moved to processor") | ||
} | ||
|
||
var now = time.Now |
Oops, something went wrong.