mirror of
https://gitea.com/gitea/act_runner.git
synced 2025-06-13 10:07:14 +02:00
Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
de4160b023 | |||
609c0a0773 | |||
0c029f7e79 | |||
eef3c32eb2 | |||
c40b651873 | |||
b498341857 | |||
0d727eb262 |
@ -58,6 +58,11 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
fetch-depth: 0 # all history for all branches and tags
|
fetch-depth: 0 # all history for all branches and tags
|
||||||
|
|
||||||
|
- name: dockerfile lint check
|
||||||
|
uses: https://github.com/hadolint/hadolint-action@v3.1.0
|
||||||
|
with:
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
|
||||||
- name: Set up QEMU
|
- name: Set up QEMU
|
||||||
uses: docker/setup-qemu-action@v2
|
uses: docker/setup-qemu-action@v2
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
name: release-tag
|
name: release-tag
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
tags:
|
tags:
|
||||||
- '*'
|
- '*'
|
||||||
@ -69,6 +69,11 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
fetch-depth: 0 # all history for all branches and tags
|
fetch-depth: 0 # all history for all branches and tags
|
||||||
|
|
||||||
|
- name: dockerfile lint check
|
||||||
|
uses: https://github.com/hadolint/hadolint-action@v3.1.0
|
||||||
|
with:
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
|
||||||
- name: Set up QEMU
|
- name: Set up QEMU
|
||||||
uses: docker/setup-qemu-action@v2
|
uses: docker/setup-qemu-action@v2
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
name: checks
|
name: checks
|
||||||
on:
|
on:
|
||||||
- push
|
- push
|
||||||
- pull_request
|
- pull_request
|
||||||
|
|
||||||
@ -36,3 +36,7 @@ jobs:
|
|||||||
run: make build
|
run: make build
|
||||||
- name: test
|
- name: test
|
||||||
run: make test
|
run: make test
|
||||||
|
- name: dockerfile lint check
|
||||||
|
uses: https://github.com/hadolint/hadolint-action@v3.1.0
|
||||||
|
with:
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -8,3 +8,5 @@ coverage.txt
|
|||||||
# MS VSCode
|
# MS VSCode
|
||||||
.vscode
|
.vscode
|
||||||
__debug_bin
|
__debug_bin
|
||||||
|
# gorelease binary folder
|
||||||
|
dist
|
||||||
|
@ -71,10 +71,8 @@ builds:
|
|||||||
no_unique_dist_dir: true
|
no_unique_dist_dir: true
|
||||||
hooks:
|
hooks:
|
||||||
post:
|
post:
|
||||||
- cmd: tar -cJf {{ .Path }}.xz {{ .Path }}
|
- cmd: xz -k -9 {{ .Path }}
|
||||||
dir: ./dist/
|
dir: ./dist/
|
||||||
env:
|
|
||||||
- XZ_OPT=-9
|
|
||||||
- cmd: sh .goreleaser.checksum.sh {{ .Path }}
|
- cmd: sh .goreleaser.checksum.sh {{ .Path }}
|
||||||
- cmd: sh .goreleaser.checksum.sh {{ .Path }}.xz
|
- cmd: sh .goreleaser.checksum.sh {{ .Path }}.xz
|
||||||
|
|
||||||
@ -108,5 +106,10 @@ gitea_urls:
|
|||||||
api: https://gitea.com/api/v1
|
api: https://gitea.com/api/v1
|
||||||
download: https://gitea.com
|
download: https://gitea.com
|
||||||
|
|
||||||
|
release:
|
||||||
|
extra_files:
|
||||||
|
- glob: ./**.xz
|
||||||
|
- glob: ./**.xz.sha256
|
||||||
|
|
||||||
# yaml-language-server: $schema=https://goreleaser.com/static/schema-pro.json
|
# yaml-language-server: $schema=https://goreleaser.com/static/schema-pro.json
|
||||||
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
|
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
|
||||||
|
12
Dockerfile
12
Dockerfile
@ -1,15 +1,15 @@
|
|||||||
FROM golang:alpine as builder
|
FROM golang:1.20-alpine3.17 as builder
|
||||||
RUN apk add --update-cache make git
|
RUN apk add --no-cache make=4.3-r1
|
||||||
|
|
||||||
COPY . /opt/src/act_runner
|
COPY . /opt/src/act_runner
|
||||||
WORKDIR /opt/src/act_runner
|
WORKDIR /opt/src/act_runner
|
||||||
|
|
||||||
RUN make clean && make build
|
RUN make clean && make build
|
||||||
|
|
||||||
FROM alpine as runner
|
FROM alpine:3.17
|
||||||
RUN apk add --update-cache \
|
RUN apk add --no-cache \
|
||||||
git bash \
|
git=2.38.5-r0 bash=5.2.15-r0 \
|
||||||
&& rm -rf /var/cache/apk/*
|
&& rm -rf /var/cache/apk/*
|
||||||
|
|
||||||
COPY --from=builder /opt/src/act_runner/act_runner /usr/local/bin/act_runner
|
COPY --from=builder /opt/src/act_runner/act_runner /usr/local/bin/act_runner
|
||||||
COPY run.sh /opt/act/run.sh
|
COPY run.sh /opt/act/run.sh
|
||||||
|
5
Makefile
5
Makefile
@ -65,6 +65,9 @@ else
|
|||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
GO_PACKAGES_TO_VET ?= $(filter-out gitea.com/gitea/act_runner/internal/pkg/client/mocks,$(shell $(GO) list ./...))
|
||||||
|
|
||||||
|
|
||||||
TAGS ?=
|
TAGS ?=
|
||||||
LDFLAGS ?= -X "gitea.com/gitea/act_runner/internal/pkg/ver.version=$(RELASE_VERSION)"
|
LDFLAGS ?= -X "gitea.com/gitea/act_runner/internal/pkg/ver.version=$(RELASE_VERSION)"
|
||||||
|
|
||||||
@ -105,7 +108,7 @@ test: fmt-check
|
|||||||
vet:
|
vet:
|
||||||
@echo "Running go vet..."
|
@echo "Running go vet..."
|
||||||
@$(GO) build code.gitea.io/gitea-vet
|
@$(GO) build code.gitea.io/gitea-vet
|
||||||
@$(GO) vet -vettool=gitea-vet ./...
|
@$(GO) vet -vettool=gitea-vet $(GO_PACKAGES_TO_VET)
|
||||||
|
|
||||||
install: $(GOFILES)
|
install: $(GOFILES)
|
||||||
$(GO) install -v -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)'
|
$(GO) install -v -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)'
|
||||||
|
@ -10,7 +10,7 @@ Docker Engine Community version is required for docker mode. To install Docker C
|
|||||||
|
|
||||||
### Download pre-built binary
|
### Download pre-built binary
|
||||||
|
|
||||||
Visit https://dl.gitea.com/act_runner/ and download the right version for your platform.
|
Visit [here](https://dl.gitea.com/act_runner/) and download the right version for your platform.
|
||||||
|
|
||||||
### Build from source
|
### Build from source
|
||||||
|
|
||||||
|
79
go.mod
79
go.mod
@ -7,107 +7,84 @@ require (
|
|||||||
code.gitea.io/gitea-vet v0.2.3-0.20230113022436-2b1561217fa5
|
code.gitea.io/gitea-vet v0.2.3-0.20230113022436-2b1561217fa5
|
||||||
github.com/avast/retry-go/v4 v4.3.1
|
github.com/avast/retry-go/v4 v4.3.1
|
||||||
github.com/bufbuild/connect-go v1.3.1
|
github.com/bufbuild/connect-go v1.3.1
|
||||||
github.com/docker/docker v23.0.1+incompatible
|
github.com/docker/docker v23.0.4+incompatible
|
||||||
github.com/go-chi/chi/v5 v5.0.8
|
|
||||||
github.com/go-chi/render v1.0.2
|
|
||||||
github.com/joho/godotenv v1.5.1
|
github.com/joho/godotenv v1.5.1
|
||||||
github.com/mattn/go-isatty v0.0.18
|
github.com/mattn/go-isatty v0.0.18
|
||||||
github.com/mattn/go-sqlite3 v1.14.16
|
|
||||||
github.com/nektos/act v0.0.0
|
github.com/nektos/act v0.0.0
|
||||||
github.com/sirupsen/logrus v1.9.0
|
github.com/sirupsen/logrus v1.9.0
|
||||||
github.com/spf13/cobra v1.6.1
|
github.com/spf13/cobra v1.7.0
|
||||||
github.com/stretchr/testify v1.8.1
|
github.com/stretchr/testify v1.8.2
|
||||||
golang.org/x/term v0.7.0
|
golang.org/x/term v0.7.0
|
||||||
golang.org/x/time v0.1.0
|
golang.org/x/time v0.1.0
|
||||||
google.golang.org/protobuf v1.28.1
|
google.golang.org/protobuf v1.28.1
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
gotest.tools/v3 v3.4.0
|
gotest.tools/v3 v3.4.0
|
||||||
modernc.org/sqlite v1.22.0
|
|
||||||
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978
|
|
||||||
xorm.io/xorm v1.3.2
|
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
|
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
|
||||||
github.com/Masterminds/semver v1.5.0 // indirect
|
github.com/Masterminds/semver v1.5.0 // indirect
|
||||||
github.com/Microsoft/go-winio v0.5.2 // indirect
|
github.com/Microsoft/go-winio v0.5.2 // indirect
|
||||||
github.com/ProtonMail/go-crypto v0.0.0-20220404123522-616f957b79ad // indirect
|
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
|
||||||
github.com/acomagu/bufpipe v1.0.3 // indirect
|
github.com/acomagu/bufpipe v1.0.4 // indirect
|
||||||
github.com/ajg/form v1.5.1 // indirect
|
github.com/cloudflare/circl v1.1.0 // indirect
|
||||||
github.com/containerd/containerd v1.6.18 // indirect
|
github.com/containerd/containerd v1.6.20 // indirect
|
||||||
github.com/creack/pty v1.1.18 // indirect
|
github.com/creack/pty v1.1.18 // indirect
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/docker/cli v23.0.1+incompatible // indirect
|
github.com/docker/cli v23.0.4+incompatible // indirect
|
||||||
github.com/docker/distribution v2.8.1+incompatible // indirect
|
github.com/docker/distribution v2.8.1+incompatible // indirect
|
||||||
github.com/docker/docker-credential-helpers v0.7.0 // indirect
|
github.com/docker/docker-credential-helpers v0.7.0 // indirect
|
||||||
github.com/docker/go-connections v0.4.0 // indirect
|
github.com/docker/go-connections v0.4.0 // indirect
|
||||||
github.com/docker/go-units v0.5.0 // indirect
|
github.com/docker/go-units v0.5.0 // indirect
|
||||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
github.com/emirpasic/gods v1.18.1 // indirect
|
||||||
github.com/emirpasic/gods v1.12.0 // indirect
|
github.com/fatih/color v1.15.0 // indirect
|
||||||
github.com/fatih/color v1.13.0 // indirect
|
|
||||||
github.com/go-git/gcfg v1.5.0 // indirect
|
github.com/go-git/gcfg v1.5.0 // indirect
|
||||||
github.com/go-git/go-billy/v5 v5.4.1 // indirect
|
github.com/go-git/go-billy/v5 v5.4.1 // indirect
|
||||||
github.com/go-git/go-git/v5 v5.4.2 // indirect
|
github.com/go-git/go-git/v5 v5.6.2-0.20230411180853-ce62f3e9ff86 // indirect
|
||||||
github.com/goccy/go-json v0.8.1 // indirect
|
|
||||||
github.com/gogo/protobuf v1.3.2 // indirect
|
github.com/gogo/protobuf v1.3.2 // indirect
|
||||||
github.com/golang/snappy v0.0.4 // indirect
|
|
||||||
github.com/google/go-cmp v0.5.9 // indirect
|
github.com/google/go-cmp v0.5.9 // indirect
|
||||||
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
|
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
|
||||||
github.com/google/uuid v1.3.0 // indirect
|
github.com/imdario/mergo v0.3.15 // indirect
|
||||||
github.com/imdario/mergo v0.3.13 // indirect
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
github.com/inconshreveable/mousetrap v1.0.1 // indirect
|
|
||||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
|
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
|
||||||
github.com/json-iterator/go v1.1.12 // indirect
|
|
||||||
github.com/julienschmidt/httprouter v1.3.0 // indirect
|
github.com/julienschmidt/httprouter v1.3.0 // indirect
|
||||||
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
|
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
|
||||||
github.com/kevinburke/ssh_config v1.2.0 // indirect
|
github.com/kevinburke/ssh_config v1.2.0 // indirect
|
||||||
github.com/klauspost/compress v1.15.12 // indirect
|
github.com/klauspost/compress v1.15.12 // indirect
|
||||||
|
github.com/kr/pretty v0.3.0 // indirect
|
||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
github.com/mattn/go-runewidth v0.0.14 // indirect
|
github.com/mattn/go-runewidth v0.0.14 // indirect
|
||||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||||
github.com/mitchellh/mapstructure v1.1.2 // indirect
|
github.com/moby/buildkit v0.11.6 // indirect
|
||||||
github.com/moby/buildkit v0.11.4 // indirect
|
|
||||||
github.com/moby/patternmatcher v0.5.0 // indirect
|
github.com/moby/patternmatcher v0.5.0 // indirect
|
||||||
github.com/moby/sys/sequential v0.5.0 // indirect
|
github.com/moby/sys/sequential v0.5.0 // indirect
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
|
||||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
|
||||||
github.com/onsi/ginkgo v1.12.1 // indirect
|
|
||||||
github.com/onsi/gomega v1.10.3 // indirect
|
|
||||||
github.com/opencontainers/go-digest v1.0.0 // indirect
|
github.com/opencontainers/go-digest v1.0.0 // indirect
|
||||||
github.com/opencontainers/image-spec v1.1.0-rc2 // indirect
|
github.com/opencontainers/image-spec v1.1.0-rc2.0.20221005185240-3a7f492d3f1b // indirect
|
||||||
github.com/opencontainers/runc v1.1.3 // indirect
|
github.com/opencontainers/runc v1.1.5 // indirect
|
||||||
github.com/opencontainers/selinux v1.11.0 // indirect
|
github.com/opencontainers/selinux v1.11.0 // indirect
|
||||||
|
github.com/pjbgf/sha1cd v0.3.0 // indirect
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
github.com/rhysd/actionlint v1.6.24 // indirect
|
||||||
github.com/rhysd/actionlint v1.6.23 // indirect
|
github.com/rivo/uniseg v0.4.4 // indirect
|
||||||
github.com/rivo/uniseg v0.4.3 // indirect
|
|
||||||
github.com/robfig/cron v1.2.0 // indirect
|
github.com/robfig/cron v1.2.0 // indirect
|
||||||
github.com/sergi/go-diff v1.2.0 // indirect
|
github.com/sergi/go-diff v1.2.0 // indirect
|
||||||
|
github.com/skeema/knownhosts v1.1.0 // indirect
|
||||||
github.com/spf13/pflag v1.0.5 // indirect
|
github.com/spf13/pflag v1.0.5 // indirect
|
||||||
github.com/syndtr/goleveldb v1.0.0 // indirect
|
github.com/stretchr/objx v0.5.0 // indirect
|
||||||
github.com/xanzy/ssh-agent v0.3.1 // indirect
|
github.com/timshannon/bolthold v0.0.0-20210913165410-232392fc8a6a // indirect
|
||||||
|
github.com/xanzy/ssh-agent v0.3.3 // indirect
|
||||||
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
|
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
|
||||||
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
|
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
|
||||||
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
|
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
|
||||||
golang.org/x/crypto v0.2.0 // indirect
|
go.etcd.io/bbolt v1.3.7 // indirect
|
||||||
golang.org/x/mod v0.10.0 // indirect
|
golang.org/x/crypto v0.6.0 // indirect
|
||||||
golang.org/x/net v0.9.0 // indirect
|
golang.org/x/net v0.9.0 // indirect
|
||||||
golang.org/x/sync v0.1.0 // indirect
|
golang.org/x/sync v0.1.0 // indirect
|
||||||
golang.org/x/sys v0.7.0 // indirect
|
golang.org/x/sys v0.7.0 // indirect
|
||||||
golang.org/x/tools v0.8.0 // indirect
|
golang.org/x/tools v0.8.0 // indirect
|
||||||
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
|
|
||||||
gopkg.in/warnings.v0 v0.1.2 // indirect
|
gopkg.in/warnings.v0 v0.1.2 // indirect
|
||||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
lukechampine.com/uint128 v1.3.0 // indirect
|
|
||||||
modernc.org/cc/v3 v3.40.0 // indirect
|
|
||||||
modernc.org/ccgo/v3 v3.16.13 // indirect
|
|
||||||
modernc.org/libc v1.22.5 // indirect
|
|
||||||
modernc.org/mathutil v1.5.0 // indirect
|
|
||||||
modernc.org/memory v1.5.0 // indirect
|
|
||||||
modernc.org/opt v0.1.3 // indirect
|
|
||||||
modernc.org/strutil v1.1.3 // indirect
|
|
||||||
modernc.org/token v1.1.0 // indirect
|
|
||||||
)
|
)
|
||||||
|
|
||||||
replace github.com/nektos/act => gitea.com/gitea/act v0.243.3-0.20230420082431-e12252a43a3f
|
replace github.com/nektos/act => gitea.com/gitea/act v0.245.1
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
//go:build cgo
|
|
||||||
// +build cgo
|
|
||||||
|
|
||||||
package artifactcache
|
|
||||||
|
|
||||||
import _ "github.com/mattn/go-sqlite3"
|
|
||||||
|
|
||||||
var sqliteDriverName = "sqlite3"
|
|
@ -1,11 +0,0 @@
|
|||||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
//go:build !cgo
|
|
||||||
// +build !cgo
|
|
||||||
|
|
||||||
package artifactcache
|
|
||||||
|
|
||||||
import _ "modernc.org/sqlite"
|
|
||||||
|
|
||||||
var sqliteDriverName = "sqlite"
|
|
@ -1,12 +0,0 @@
|
|||||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
// Package artifactcache provides a cache handler for the runner.
|
|
||||||
//
|
|
||||||
// Inspired by https://github.com/sp-ricard-valverde/github-act-cache-server
|
|
||||||
//
|
|
||||||
// TODO: Authorization
|
|
||||||
// TODO: Restrictions for accessing a cache, see https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#restrictions-for-accessing-a-cache
|
|
||||||
// TODO: Force deleting cache entries, see https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#force-deleting-cache-entries
|
|
||||||
|
|
||||||
package artifactcache
|
|
@ -1,415 +0,0 @@
|
|||||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
package artifactcache
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
|
||||||
"github.com/go-chi/chi/v5/middleware"
|
|
||||||
"github.com/go-chi/render"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"xorm.io/builder"
|
|
||||||
"xorm.io/xorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
urlBase = "/_apis/artifactcache"
|
|
||||||
)
|
|
||||||
|
|
||||||
var logger = log.StandardLogger().WithField("module", "cache_request")
|
|
||||||
|
|
||||||
type Handler struct {
|
|
||||||
engine engine
|
|
||||||
storage *Storage
|
|
||||||
router *chi.Mux
|
|
||||||
listener net.Listener
|
|
||||||
|
|
||||||
gc atomic.Bool
|
|
||||||
gcAt time.Time
|
|
||||||
|
|
||||||
outboundIP string
|
|
||||||
}
|
|
||||||
|
|
||||||
func StartHandler(dir, outboundIP string, port uint16) (*Handler, error) {
|
|
||||||
h := &Handler{}
|
|
||||||
|
|
||||||
if dir == "" {
|
|
||||||
if home, err := os.UserHomeDir(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
} else {
|
|
||||||
dir = filepath.Join(home, ".cache", "actcache")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
e, err := xorm.NewEngine(sqliteDriverName, filepath.Join(dir, "sqlite.db"))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err := e.Sync(&Cache{}); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
h.engine = engine{e: e}
|
|
||||||
|
|
||||||
storage, err := NewStorage(filepath.Join(dir, "cache"))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
h.storage = storage
|
|
||||||
|
|
||||||
if outboundIP != "" {
|
|
||||||
h.outboundIP = outboundIP
|
|
||||||
} else if ip, err := getOutboundIP(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
} else {
|
|
||||||
h.outboundIP = ip.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
router := chi.NewRouter()
|
|
||||||
router.Use(middleware.RequestLogger(&middleware.DefaultLogFormatter{Logger: logger}))
|
|
||||||
router.Use(func(handler http.Handler) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
handler.ServeHTTP(w, r)
|
|
||||||
go h.gcCache()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
router.Use(middleware.Logger)
|
|
||||||
router.Route(urlBase, func(r chi.Router) {
|
|
||||||
r.Get("/cache", h.find)
|
|
||||||
r.Route("/caches", func(r chi.Router) {
|
|
||||||
r.Post("/", h.reserve)
|
|
||||||
r.Route("/{id}", func(r chi.Router) {
|
|
||||||
r.Patch("/", h.upload)
|
|
||||||
r.Post("/", h.commit)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
r.Get("/artifacts/{id}", h.get)
|
|
||||||
r.Post("/clean", h.clean)
|
|
||||||
})
|
|
||||||
|
|
||||||
h.router = router
|
|
||||||
|
|
||||||
h.gcCache()
|
|
||||||
|
|
||||||
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) // listen on all interfaces
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
go func() {
|
|
||||||
if err := http.Serve(listener, h.router); err != nil {
|
|
||||||
logger.Errorf("http serve: %v", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
h.listener = listener
|
|
||||||
|
|
||||||
return h, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handler) ExternalURL() string {
|
|
||||||
// TODO: make the external url configurable if necessary
|
|
||||||
return fmt.Sprintf("http://%s:%d",
|
|
||||||
h.outboundIP,
|
|
||||||
h.listener.Addr().(*net.TCPAddr).Port)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GET /_apis/artifactcache/cache
|
|
||||||
func (h *Handler) find(w http.ResponseWriter, r *http.Request) {
|
|
||||||
keys := strings.Split(r.URL.Query().Get("keys"), ",")
|
|
||||||
version := r.URL.Query().Get("version")
|
|
||||||
|
|
||||||
cache, err := h.findCache(r.Context(), keys, version)
|
|
||||||
if err != nil {
|
|
||||||
responseJson(w, r, 500, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if cache == nil {
|
|
||||||
responseJson(w, r, 204)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if ok, err := h.storage.Exist(cache.ID); err != nil {
|
|
||||||
responseJson(w, r, 500, err)
|
|
||||||
return
|
|
||||||
} else if !ok {
|
|
||||||
_ = h.engine.Exec(func(sess *xorm.Session) error {
|
|
||||||
_, err := sess.Delete(cache)
|
|
||||||
return err
|
|
||||||
})
|
|
||||||
responseJson(w, r, 204)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
responseJson(w, r, 200, map[string]any{
|
|
||||||
"result": "hit",
|
|
||||||
"archiveLocation": fmt.Sprintf("%s%s/artifacts/%d", h.ExternalURL(), urlBase, cache.ID),
|
|
||||||
"cacheKey": cache.Key,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// POST /_apis/artifactcache/caches
|
|
||||||
func (h *Handler) reserve(w http.ResponseWriter, r *http.Request) {
|
|
||||||
cache := &Cache{}
|
|
||||||
if err := render.Bind(r, cache); err != nil {
|
|
||||||
responseJson(w, r, 400, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if ok, err := h.engine.ExecBool(func(sess *xorm.Session) (bool, error) {
|
|
||||||
return sess.Where(builder.Eq{"key": cache.Key, "version": cache.Version}).Get(&Cache{})
|
|
||||||
}); err != nil {
|
|
||||||
responseJson(w, r, 500, err)
|
|
||||||
return
|
|
||||||
} else if ok {
|
|
||||||
responseJson(w, r, 400, fmt.Errorf("already exist"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := h.engine.Exec(func(sess *xorm.Session) error {
|
|
||||||
_, err := sess.Insert(cache)
|
|
||||||
return err
|
|
||||||
}); err != nil {
|
|
||||||
responseJson(w, r, 500, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
responseJson(w, r, 200, map[string]any{
|
|
||||||
"cacheId": cache.ID,
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// PATCH /_apis/artifactcache/caches/:id
|
|
||||||
func (h *Handler) upload(w http.ResponseWriter, r *http.Request) {
|
|
||||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
responseJson(w, r, 400, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
cache := &Cache{
|
|
||||||
ID: id,
|
|
||||||
}
|
|
||||||
|
|
||||||
if ok, err := h.engine.ExecBool(func(sess *xorm.Session) (bool, error) {
|
|
||||||
return sess.Get(cache)
|
|
||||||
}); err != nil {
|
|
||||||
responseJson(w, r, 500, err)
|
|
||||||
return
|
|
||||||
} else if !ok {
|
|
||||||
responseJson(w, r, 400, fmt.Errorf("cache %d: not reserved", id))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if cache.Complete {
|
|
||||||
responseJson(w, r, 400, fmt.Errorf("cache %v %q: already complete", cache.ID, cache.Key))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
start, _, err := parseContentRange(r.Header.Get("Content-Range"))
|
|
||||||
if err != nil {
|
|
||||||
responseJson(w, r, 400, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err := h.storage.Write(cache.ID, start, r.Body); err != nil {
|
|
||||||
responseJson(w, r, 500, err)
|
|
||||||
}
|
|
||||||
h.useCache(r.Context(), id)
|
|
||||||
responseJson(w, r, 200)
|
|
||||||
}
|
|
||||||
|
|
||||||
// POST /_apis/artifactcache/caches/:id
|
|
||||||
func (h *Handler) commit(w http.ResponseWriter, r *http.Request) {
|
|
||||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
responseJson(w, r, 400, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
cache := &Cache{
|
|
||||||
ID: id,
|
|
||||||
}
|
|
||||||
if ok, err := h.engine.ExecBool(func(sess *xorm.Session) (bool, error) {
|
|
||||||
return sess.Get(cache)
|
|
||||||
}); err != nil {
|
|
||||||
responseJson(w, r, 500, err)
|
|
||||||
return
|
|
||||||
} else if !ok {
|
|
||||||
responseJson(w, r, 400, fmt.Errorf("cache %d: not reserved", id))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if cache.Complete {
|
|
||||||
responseJson(w, r, 400, fmt.Errorf("cache %v %q: already complete", cache.ID, cache.Key))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := h.storage.Commit(cache.ID, cache.Size); err != nil {
|
|
||||||
responseJson(w, r, 500, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
cache.Complete = true
|
|
||||||
if err := h.engine.Exec(func(sess *xorm.Session) error {
|
|
||||||
_, err := sess.ID(cache.ID).Cols("complete").Update(cache)
|
|
||||||
return err
|
|
||||||
}); err != nil {
|
|
||||||
responseJson(w, r, 500, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
responseJson(w, r, 200)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GET /_apis/artifactcache/artifacts/:id
|
|
||||||
func (h *Handler) get(w http.ResponseWriter, r *http.Request) {
|
|
||||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
responseJson(w, r, 400, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
h.useCache(r.Context(), id)
|
|
||||||
h.storage.Serve(w, r, id)
|
|
||||||
}
|
|
||||||
|
|
||||||
// POST /_apis/artifactcache/clean
|
|
||||||
func (h *Handler) clean(w http.ResponseWriter, r *http.Request) {
|
|
||||||
// TODO: don't support force deleting cache entries
|
|
||||||
// see: https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#force-deleting-cache-entries
|
|
||||||
|
|
||||||
responseJson(w, r, 200)
|
|
||||||
}
|
|
||||||
|
|
||||||
// if not found, return (nil, nil) instead of an error.
|
|
||||||
func (h *Handler) findCache(ctx context.Context, keys []string, version string) (*Cache, error) {
|
|
||||||
if len(keys) == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
key := keys[0] // the first key is for exact match.
|
|
||||||
|
|
||||||
cache := &Cache{}
|
|
||||||
if ok, err := h.engine.ExecBool(func(sess *xorm.Session) (bool, error) {
|
|
||||||
return sess.Where(builder.Eq{"key": key, "version": version, "complete": true}).Get(cache)
|
|
||||||
}); err != nil {
|
|
||||||
return nil, err
|
|
||||||
} else if ok {
|
|
||||||
return cache, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, prefix := range keys[1:] {
|
|
||||||
if ok, err := h.engine.ExecBool(func(sess *xorm.Session) (bool, error) {
|
|
||||||
return sess.Where(builder.And(
|
|
||||||
builder.Like{"key", prefix + "%"},
|
|
||||||
builder.Eq{"version": version, "complete": true},
|
|
||||||
)).OrderBy("id DESC").Get(cache)
|
|
||||||
}); err != nil {
|
|
||||||
return nil, err
|
|
||||||
} else if ok {
|
|
||||||
return cache, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handler) useCache(ctx context.Context, id int64) {
|
|
||||||
// keep quiet
|
|
||||||
_ = h.engine.Exec(func(sess *xorm.Session) error {
|
|
||||||
_, err := sess.Context(ctx).Cols("used_at").Update(&Cache{
|
|
||||||
ID: id,
|
|
||||||
UsedAt: time.Now().Unix(),
|
|
||||||
})
|
|
||||||
return err
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handler) gcCache() {
|
|
||||||
if h.gc.Load() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if !h.gc.CompareAndSwap(false, true) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer h.gc.Store(false)
|
|
||||||
|
|
||||||
if time.Since(h.gcAt) < time.Hour {
|
|
||||||
logger.Infof("skip gc: %v", h.gcAt.String())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
h.gcAt = time.Now()
|
|
||||||
logger.Infof("gc: %v", h.gcAt.String())
|
|
||||||
|
|
||||||
const (
|
|
||||||
keepUsed = 30 * 24 * time.Hour
|
|
||||||
keepUnused = 7 * 24 * time.Hour
|
|
||||||
keepTemp = 5 * time.Minute
|
|
||||||
)
|
|
||||||
|
|
||||||
var caches []*Cache
|
|
||||||
if err := h.engine.Exec(func(sess *xorm.Session) error {
|
|
||||||
return sess.Where(builder.And(builder.Lt{"used_at": time.Now().Add(-keepTemp).Unix()}, builder.Eq{"complete": false})).
|
|
||||||
Find(&caches)
|
|
||||||
}); err != nil {
|
|
||||||
logger.Warnf("find caches: %v", err)
|
|
||||||
} else {
|
|
||||||
for _, cache := range caches {
|
|
||||||
h.storage.Remove(cache.ID)
|
|
||||||
if err := h.engine.Exec(func(sess *xorm.Session) error {
|
|
||||||
_, err := sess.Delete(cache)
|
|
||||||
return err
|
|
||||||
}); err != nil {
|
|
||||||
logger.Warnf("delete cache: %v", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
logger.Infof("deleted cache: %+v", cache)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
caches = caches[:0]
|
|
||||||
if err := h.engine.Exec(func(sess *xorm.Session) error {
|
|
||||||
return sess.Where(builder.Lt{"used_at": time.Now().Add(-keepUnused).Unix()}).
|
|
||||||
Find(&caches)
|
|
||||||
}); err != nil {
|
|
||||||
logger.Warnf("find caches: %v", err)
|
|
||||||
} else {
|
|
||||||
for _, cache := range caches {
|
|
||||||
h.storage.Remove(cache.ID)
|
|
||||||
if err := h.engine.Exec(func(sess *xorm.Session) error {
|
|
||||||
_, err := sess.Delete(cache)
|
|
||||||
return err
|
|
||||||
}); err != nil {
|
|
||||||
logger.Warnf("delete cache: %v", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
logger.Infof("deleted cache: %+v", cache)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
caches = caches[:0]
|
|
||||||
if err := h.engine.Exec(func(sess *xorm.Session) error {
|
|
||||||
return sess.Where(builder.Lt{"created_at": time.Now().Add(-keepUsed).Unix()}).
|
|
||||||
Find(&caches)
|
|
||||||
}); err != nil {
|
|
||||||
logger.Warnf("find caches: %v", err)
|
|
||||||
} else {
|
|
||||||
for _, cache := range caches {
|
|
||||||
h.storage.Remove(cache.ID)
|
|
||||||
if err := h.engine.Exec(func(sess *xorm.Session) error {
|
|
||||||
_, err := sess.Delete(cache)
|
|
||||||
return err
|
|
||||||
}); err != nil {
|
|
||||||
logger.Warnf("delete cache: %v", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
logger.Infof("deleted cache: %+v", cache)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,30 +0,0 @@
|
|||||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
package artifactcache
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Cache struct {
|
|
||||||
ID int64 `xorm:"id pk autoincr" json:"-"`
|
|
||||||
Key string `xorm:"TEXT index unique(key_version)" json:"key"`
|
|
||||||
Version string `xorm:"TEXT unique(key_version)" json:"version"`
|
|
||||||
Size int64 `json:"cacheSize"`
|
|
||||||
Complete bool `xorm:"index(complete_used_at)" json:"-"`
|
|
||||||
UsedAt int64 `xorm:"index(complete_used_at) updated" json:"-"`
|
|
||||||
CreatedAt int64 `xorm:"index created" json:"-"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Bind implements render.Binder
|
|
||||||
func (c *Cache) Bind(_ *http.Request) error {
|
|
||||||
if c.Key == "" {
|
|
||||||
return fmt.Errorf("missing key")
|
|
||||||
}
|
|
||||||
if c.Version == "" {
|
|
||||||
return fmt.Errorf("missing version")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -1,129 +0,0 @@
|
|||||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
package artifactcache
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Storage struct {
|
|
||||||
rootDir string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewStorage(rootDir string) (*Storage, error) {
|
|
||||||
if err := os.MkdirAll(rootDir, 0o755); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &Storage{
|
|
||||||
rootDir: rootDir,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Storage) Exist(id int64) (bool, error) {
|
|
||||||
name := s.filename(id)
|
|
||||||
if _, err := os.Stat(name); os.IsNotExist(err) {
|
|
||||||
return false, nil
|
|
||||||
} else if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Storage) Write(id int64, offset int64, reader io.Reader) error {
|
|
||||||
name := s.tempName(id, offset)
|
|
||||||
if err := os.MkdirAll(filepath.Dir(name), 0o755); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
file, err := os.Create(name)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
_, err = io.Copy(file, reader)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Storage) Commit(id int64, size int64) error {
|
|
||||||
defer func() {
|
|
||||||
_ = os.RemoveAll(s.tempDir(id))
|
|
||||||
}()
|
|
||||||
|
|
||||||
name := s.filename(id)
|
|
||||||
tempNames, err := s.tempNames(id)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := os.MkdirAll(filepath.Dir(name), 0o755); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
file, err := os.Create(name)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
var written int64
|
|
||||||
for _, v := range tempNames {
|
|
||||||
f, err := os.Open(v)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
n, err := io.Copy(file, f)
|
|
||||||
_ = f.Close()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
written += n
|
|
||||||
}
|
|
||||||
|
|
||||||
if written != size {
|
|
||||||
_ = file.Close()
|
|
||||||
_ = os.Remove(name)
|
|
||||||
return fmt.Errorf("broken file: %v != %v", written, size)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Storage) Serve(w http.ResponseWriter, r *http.Request, id int64) {
|
|
||||||
name := s.filename(id)
|
|
||||||
http.ServeFile(w, r, name)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Storage) Remove(id int64) {
|
|
||||||
_ = os.Remove(s.filename(id))
|
|
||||||
_ = os.RemoveAll(s.tempDir(id))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Storage) filename(id int64) string {
|
|
||||||
return filepath.Join(s.rootDir, fmt.Sprintf("%02x", id%0xff), fmt.Sprint(id))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Storage) tempDir(id int64) string {
|
|
||||||
return filepath.Join(s.rootDir, "tmp", fmt.Sprint(id))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Storage) tempName(id, offset int64) string {
|
|
||||||
return filepath.Join(s.tempDir(id), fmt.Sprintf("%016x", offset))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Storage) tempNames(id int64) ([]string, error) {
|
|
||||||
dir := s.tempDir(id)
|
|
||||||
files, err := os.ReadDir(dir)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var names []string
|
|
||||||
for _, v := range files {
|
|
||||||
if !v.IsDir() {
|
|
||||||
names = append(names, filepath.Join(dir, v.Name()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return names, nil
|
|
||||||
}
|
|
@ -1,100 +0,0 @@
|
|||||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
package artifactcache
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/go-chi/render"
|
|
||||||
"xorm.io/xorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
func responseJson(w http.ResponseWriter, r *http.Request, code int, v ...any) {
|
|
||||||
render.Status(r, code)
|
|
||||||
if len(v) == 0 || v[0] == nil {
|
|
||||||
render.JSON(w, r, struct{}{})
|
|
||||||
} else if err, ok := v[0].(error); ok {
|
|
||||||
logger.Errorf("%v %v: %v", r.Method, r.RequestURI, err)
|
|
||||||
render.JSON(w, r, map[string]any{
|
|
||||||
"error": err.Error(),
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
render.JSON(w, r, v[0])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseContentRange(s string) (int64, int64, error) {
|
|
||||||
// support the format like "bytes 11-22/*" only
|
|
||||||
s, _, _ = strings.Cut(strings.TrimPrefix(s, "bytes "), "/")
|
|
||||||
s1, s2, _ := strings.Cut(s, "-")
|
|
||||||
|
|
||||||
start, err := strconv.ParseInt(s1, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return 0, 0, fmt.Errorf("parse %q: %w", s, err)
|
|
||||||
}
|
|
||||||
stop, err := strconv.ParseInt(s2, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return 0, 0, fmt.Errorf("parse %q: %w", s, err)
|
|
||||||
}
|
|
||||||
return start, stop, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getOutboundIP() (net.IP, error) {
|
|
||||||
// FIXME: It makes more sense to use the gateway IP address of container network
|
|
||||||
if conn, err := net.Dial("udp", "8.8.8.8:80"); err == nil {
|
|
||||||
defer conn.Close()
|
|
||||||
return conn.LocalAddr().(*net.UDPAddr).IP, nil
|
|
||||||
}
|
|
||||||
if ifaces, err := net.Interfaces(); err == nil {
|
|
||||||
for _, i := range ifaces {
|
|
||||||
if addrs, err := i.Addrs(); err == nil {
|
|
||||||
for _, addr := range addrs {
|
|
||||||
var ip net.IP
|
|
||||||
switch v := addr.(type) {
|
|
||||||
case *net.IPNet:
|
|
||||||
ip = v.IP
|
|
||||||
case *net.IPAddr:
|
|
||||||
ip = v.IP
|
|
||||||
}
|
|
||||||
if ip.IsGlobalUnicast() {
|
|
||||||
return ip, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("no outbound IP address found")
|
|
||||||
}
|
|
||||||
|
|
||||||
// engine is a wrapper of *xorm.Engine, with a lock.
|
|
||||||
// To avoid racing of sqlite, we don't care performance here.
|
|
||||||
type engine struct {
|
|
||||||
e *xorm.Engine
|
|
||||||
m sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *engine) Exec(f func(*xorm.Session) error) error {
|
|
||||||
e.m.Lock()
|
|
||||||
defer e.m.Unlock()
|
|
||||||
|
|
||||||
sess := e.e.NewSession()
|
|
||||||
defer sess.Close()
|
|
||||||
|
|
||||||
return f(sess)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *engine) ExecBool(f func(*xorm.Session) (bool, error)) (bool, error) {
|
|
||||||
e.m.Lock()
|
|
||||||
defer e.m.Unlock()
|
|
||||||
|
|
||||||
sess := e.e.NewSession()
|
|
||||||
defer sess.Close()
|
|
||||||
|
|
||||||
return f(sess)
|
|
||||||
}
|
|
@ -14,6 +14,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
|
"github.com/nektos/act/pkg/artifactcache"
|
||||||
"github.com/nektos/act/pkg/artifacts"
|
"github.com/nektos/act/pkg/artifacts"
|
||||||
"github.com/nektos/act/pkg/common"
|
"github.com/nektos/act/pkg/common"
|
||||||
"github.com/nektos/act/pkg/model"
|
"github.com/nektos/act/pkg/model"
|
||||||
@ -21,8 +22,6 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/internal/app/artifactcache"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type executeArgs struct {
|
type executeArgs struct {
|
||||||
@ -314,7 +313,7 @@ func runExec(ctx context.Context, execArgs *executeArgs) func(cmd *cobra.Command
|
|||||||
|
|
||||||
if len(execArgs.event) > 0 {
|
if len(execArgs.event) > 0 {
|
||||||
log.Infof("Using chosed event for filtering: %s", execArgs.event)
|
log.Infof("Using chosed event for filtering: %s", execArgs.event)
|
||||||
eventName = args[0]
|
eventName = execArgs.event
|
||||||
} else if len(events) == 1 && len(events[0]) > 0 {
|
} else if len(events) == 1 && len(events[0]) > 0 {
|
||||||
log.Infof("Using the only detected workflow event: %s", events[0])
|
log.Infof("Using the only detected workflow event: %s", events[0])
|
||||||
eventName = events[0]
|
eventName = events[0]
|
||||||
@ -349,7 +348,7 @@ func runExec(ctx context.Context, execArgs *executeArgs) func(cmd *cobra.Command
|
|||||||
}
|
}
|
||||||
|
|
||||||
// init a cache server
|
// init a cache server
|
||||||
handler, err := artifactcache.StartHandler("", "", 0)
|
handler, err := artifactcache.StartHandler("", "", 0, log.StandardLogger().WithField("module", "cache_request"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -392,12 +391,10 @@ func runExec(ctx context.Context, execArgs *executeArgs) func(cmd *cobra.Command
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: handle log level config
|
if !execArgs.debug {
|
||||||
// waiting https://gitea.com/gitea/act/pulls/19
|
logLevel := log.Level(log.InfoLevel)
|
||||||
// if !execArgs.debug {
|
config.JobLoggerLevel = &logLevel
|
||||||
// logLevel := log.Level(log.InfoLevel)
|
}
|
||||||
// config.JobLoggerLevel = &logLevel
|
|
||||||
// }
|
|
||||||
|
|
||||||
r, err := runner.New(config)
|
r, err := runner.New(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -13,12 +13,12 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
|
runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
|
||||||
|
"github.com/nektos/act/pkg/artifactcache"
|
||||||
"github.com/nektos/act/pkg/common"
|
"github.com/nektos/act/pkg/common"
|
||||||
"github.com/nektos/act/pkg/model"
|
"github.com/nektos/act/pkg/model"
|
||||||
"github.com/nektos/act/pkg/runner"
|
"github.com/nektos/act/pkg/runner"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"gitea.com/gitea/act_runner/internal/app/artifactcache"
|
|
||||||
"gitea.com/gitea/act_runner/internal/pkg/client"
|
"gitea.com/gitea/act_runner/internal/pkg/client"
|
||||||
"gitea.com/gitea/act_runner/internal/pkg/config"
|
"gitea.com/gitea/act_runner/internal/pkg/config"
|
||||||
"gitea.com/gitea/act_runner/internal/pkg/labels"
|
"gitea.com/gitea/act_runner/internal/pkg/labels"
|
||||||
@ -51,7 +51,12 @@ func NewRunner(cfg *config.Config, reg *config.Registration, cli client.Client)
|
|||||||
envs[k] = v
|
envs[k] = v
|
||||||
}
|
}
|
||||||
if cfg.Cache.Enabled == nil || *cfg.Cache.Enabled {
|
if cfg.Cache.Enabled == nil || *cfg.Cache.Enabled {
|
||||||
cacheHandler, err := artifactcache.StartHandler(cfg.Cache.Dir, cfg.Cache.Host, cfg.Cache.Port)
|
cacheHandler, err := artifactcache.StartHandler(
|
||||||
|
cfg.Cache.Dir,
|
||||||
|
cfg.Cache.Host,
|
||||||
|
cfg.Cache.Port,
|
||||||
|
log.StandardLogger().WithField("module", "cache_request"),
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("cannot init cache server, it will be disabled: %v", err)
|
log.Errorf("cannot init cache server, it will be disabled: %v", err)
|
||||||
// go on
|
// go on
|
||||||
|
@ -9,6 +9,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// A Client manages communication with the runner.
|
// A Client manages communication with the runner.
|
||||||
|
//
|
||||||
|
//go:generate mockery --name Client
|
||||||
type Client interface {
|
type Client interface {
|
||||||
pingv1connect.PingServiceClient
|
pingv1connect.PingServiceClient
|
||||||
runnerv1connect.RunnerServiceClient
|
runnerv1connect.RunnerServiceClient
|
||||||
|
193
internal/pkg/client/mocks/Client.go
Normal file
193
internal/pkg/client/mocks/Client.go
Normal file
@ -0,0 +1,193 @@
|
|||||||
|
// Code generated by mockery v2.26.1. DO NOT EDIT.
|
||||||
|
|
||||||
|
package mocks
|
||||||
|
|
||||||
|
import (
|
||||||
|
context "context"
|
||||||
|
|
||||||
|
connect "github.com/bufbuild/connect-go"
|
||||||
|
|
||||||
|
mock "github.com/stretchr/testify/mock"
|
||||||
|
|
||||||
|
pingv1 "code.gitea.io/actions-proto-go/ping/v1"
|
||||||
|
|
||||||
|
runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Client is an autogenerated mock type for the Client type
|
||||||
|
type Client struct {
|
||||||
|
mock.Mock
|
||||||
|
}
|
||||||
|
|
||||||
|
// Address provides a mock function with given fields:
|
||||||
|
func (_m *Client) Address() string {
|
||||||
|
ret := _m.Called()
|
||||||
|
|
||||||
|
var r0 string
|
||||||
|
if rf, ok := ret.Get(0).(func() string); ok {
|
||||||
|
r0 = rf()
|
||||||
|
} else {
|
||||||
|
r0 = ret.Get(0).(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r0
|
||||||
|
}
|
||||||
|
|
||||||
|
// FetchTask provides a mock function with given fields: _a0, _a1
|
||||||
|
func (_m *Client) FetchTask(_a0 context.Context, _a1 *connect.Request[runnerv1.FetchTaskRequest]) (*connect.Response[runnerv1.FetchTaskResponse], error) {
|
||||||
|
ret := _m.Called(_a0, _a1)
|
||||||
|
|
||||||
|
var r0 *connect.Response[runnerv1.FetchTaskResponse]
|
||||||
|
var r1 error
|
||||||
|
if rf, ok := ret.Get(0).(func(context.Context, *connect.Request[runnerv1.FetchTaskRequest]) (*connect.Response[runnerv1.FetchTaskResponse], error)); ok {
|
||||||
|
return rf(_a0, _a1)
|
||||||
|
}
|
||||||
|
if rf, ok := ret.Get(0).(func(context.Context, *connect.Request[runnerv1.FetchTaskRequest]) *connect.Response[runnerv1.FetchTaskResponse]); ok {
|
||||||
|
r0 = rf(_a0, _a1)
|
||||||
|
} else {
|
||||||
|
if ret.Get(0) != nil {
|
||||||
|
r0 = ret.Get(0).(*connect.Response[runnerv1.FetchTaskResponse])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if rf, ok := ret.Get(1).(func(context.Context, *connect.Request[runnerv1.FetchTaskRequest]) error); ok {
|
||||||
|
r1 = rf(_a0, _a1)
|
||||||
|
} else {
|
||||||
|
r1 = ret.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r0, r1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insecure provides a mock function with given fields:
|
||||||
|
func (_m *Client) Insecure() bool {
|
||||||
|
ret := _m.Called()
|
||||||
|
|
||||||
|
var r0 bool
|
||||||
|
if rf, ok := ret.Get(0).(func() bool); ok {
|
||||||
|
r0 = rf()
|
||||||
|
} else {
|
||||||
|
r0 = ret.Get(0).(bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ping provides a mock function with given fields: _a0, _a1
|
||||||
|
func (_m *Client) Ping(_a0 context.Context, _a1 *connect.Request[pingv1.PingRequest]) (*connect.Response[pingv1.PingResponse], error) {
|
||||||
|
ret := _m.Called(_a0, _a1)
|
||||||
|
|
||||||
|
var r0 *connect.Response[pingv1.PingResponse]
|
||||||
|
var r1 error
|
||||||
|
if rf, ok := ret.Get(0).(func(context.Context, *connect.Request[pingv1.PingRequest]) (*connect.Response[pingv1.PingResponse], error)); ok {
|
||||||
|
return rf(_a0, _a1)
|
||||||
|
}
|
||||||
|
if rf, ok := ret.Get(0).(func(context.Context, *connect.Request[pingv1.PingRequest]) *connect.Response[pingv1.PingResponse]); ok {
|
||||||
|
r0 = rf(_a0, _a1)
|
||||||
|
} else {
|
||||||
|
if ret.Get(0) != nil {
|
||||||
|
r0 = ret.Get(0).(*connect.Response[pingv1.PingResponse])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if rf, ok := ret.Get(1).(func(context.Context, *connect.Request[pingv1.PingRequest]) error); ok {
|
||||||
|
r1 = rf(_a0, _a1)
|
||||||
|
} else {
|
||||||
|
r1 = ret.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r0, r1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register provides a mock function with given fields: _a0, _a1
|
||||||
|
func (_m *Client) Register(_a0 context.Context, _a1 *connect.Request[runnerv1.RegisterRequest]) (*connect.Response[runnerv1.RegisterResponse], error) {
|
||||||
|
ret := _m.Called(_a0, _a1)
|
||||||
|
|
||||||
|
var r0 *connect.Response[runnerv1.RegisterResponse]
|
||||||
|
var r1 error
|
||||||
|
if rf, ok := ret.Get(0).(func(context.Context, *connect.Request[runnerv1.RegisterRequest]) (*connect.Response[runnerv1.RegisterResponse], error)); ok {
|
||||||
|
return rf(_a0, _a1)
|
||||||
|
}
|
||||||
|
if rf, ok := ret.Get(0).(func(context.Context, *connect.Request[runnerv1.RegisterRequest]) *connect.Response[runnerv1.RegisterResponse]); ok {
|
||||||
|
r0 = rf(_a0, _a1)
|
||||||
|
} else {
|
||||||
|
if ret.Get(0) != nil {
|
||||||
|
r0 = ret.Get(0).(*connect.Response[runnerv1.RegisterResponse])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if rf, ok := ret.Get(1).(func(context.Context, *connect.Request[runnerv1.RegisterRequest]) error); ok {
|
||||||
|
r1 = rf(_a0, _a1)
|
||||||
|
} else {
|
||||||
|
r1 = ret.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r0, r1
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateLog provides a mock function with given fields: _a0, _a1
|
||||||
|
func (_m *Client) UpdateLog(_a0 context.Context, _a1 *connect.Request[runnerv1.UpdateLogRequest]) (*connect.Response[runnerv1.UpdateLogResponse], error) {
|
||||||
|
ret := _m.Called(_a0, _a1)
|
||||||
|
|
||||||
|
var r0 *connect.Response[runnerv1.UpdateLogResponse]
|
||||||
|
var r1 error
|
||||||
|
if rf, ok := ret.Get(0).(func(context.Context, *connect.Request[runnerv1.UpdateLogRequest]) (*connect.Response[runnerv1.UpdateLogResponse], error)); ok {
|
||||||
|
return rf(_a0, _a1)
|
||||||
|
}
|
||||||
|
if rf, ok := ret.Get(0).(func(context.Context, *connect.Request[runnerv1.UpdateLogRequest]) *connect.Response[runnerv1.UpdateLogResponse]); ok {
|
||||||
|
r0 = rf(_a0, _a1)
|
||||||
|
} else {
|
||||||
|
if ret.Get(0) != nil {
|
||||||
|
r0 = ret.Get(0).(*connect.Response[runnerv1.UpdateLogResponse])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if rf, ok := ret.Get(1).(func(context.Context, *connect.Request[runnerv1.UpdateLogRequest]) error); ok {
|
||||||
|
r1 = rf(_a0, _a1)
|
||||||
|
} else {
|
||||||
|
r1 = ret.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r0, r1
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateTask provides a mock function with given fields: _a0, _a1
|
||||||
|
func (_m *Client) UpdateTask(_a0 context.Context, _a1 *connect.Request[runnerv1.UpdateTaskRequest]) (*connect.Response[runnerv1.UpdateTaskResponse], error) {
|
||||||
|
ret := _m.Called(_a0, _a1)
|
||||||
|
|
||||||
|
var r0 *connect.Response[runnerv1.UpdateTaskResponse]
|
||||||
|
var r1 error
|
||||||
|
if rf, ok := ret.Get(0).(func(context.Context, *connect.Request[runnerv1.UpdateTaskRequest]) (*connect.Response[runnerv1.UpdateTaskResponse], error)); ok {
|
||||||
|
return rf(_a0, _a1)
|
||||||
|
}
|
||||||
|
if rf, ok := ret.Get(0).(func(context.Context, *connect.Request[runnerv1.UpdateTaskRequest]) *connect.Response[runnerv1.UpdateTaskResponse]); ok {
|
||||||
|
r0 = rf(_a0, _a1)
|
||||||
|
} else {
|
||||||
|
if ret.Get(0) != nil {
|
||||||
|
r0 = ret.Get(0).(*connect.Response[runnerv1.UpdateTaskResponse])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if rf, ok := ret.Get(1).(func(context.Context, *connect.Request[runnerv1.UpdateTaskRequest]) error); ok {
|
||||||
|
r1 = rf(_a0, _a1)
|
||||||
|
} else {
|
||||||
|
r1 = ret.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r0, r1
|
||||||
|
}
|
||||||
|
|
||||||
|
type mockConstructorTestingTNewClient interface {
|
||||||
|
mock.TestingT
|
||||||
|
Cleanup(func())
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewClient creates a new instance of Client. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
|
||||||
|
func NewClient(t mockConstructorTestingTNewClient) *Client {
|
||||||
|
mock := &Client{}
|
||||||
|
mock.Mock.Test(t)
|
||||||
|
|
||||||
|
t.Cleanup(func() { mock.AssertExpectations(t) })
|
||||||
|
|
||||||
|
return mock
|
||||||
|
}
|
@ -139,11 +139,13 @@ func (r *Reporter) Fire(entry *log.Entry) error {
|
|||||||
}
|
}
|
||||||
if v, ok := entry.Data["raw_output"]; ok {
|
if v, ok := entry.Data["raw_output"]; ok {
|
||||||
if rawOutput, ok := v.(bool); ok && rawOutput {
|
if rawOutput, ok := v.(bool); ok && rawOutput {
|
||||||
if step.LogLength == 0 {
|
if row := r.parseLogRow(entry); row != nil {
|
||||||
step.LogIndex = int64(r.logOffset + len(r.logRows))
|
if step.LogLength == 0 {
|
||||||
|
step.LogIndex = int64(r.logOffset + len(r.logRows))
|
||||||
|
}
|
||||||
|
step.LogLength++
|
||||||
|
r.logRows = append(r.logRows, row)
|
||||||
}
|
}
|
||||||
step.LogLength++
|
|
||||||
r.logRows = appendIfNotNil(r.logRows, r.parseLogRow(entry))
|
|
||||||
}
|
}
|
||||||
} else if !r.duringSteps() {
|
} else if !r.duringSteps() {
|
||||||
r.logRows = appendIfNotNil(r.logRows, r.parseLogRow(entry))
|
r.logRows = appendIfNotNil(r.logRows, r.parseLogRow(entry))
|
||||||
|
@ -4,11 +4,19 @@
|
|||||||
package report
|
package report
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
|
||||||
|
connect_go "github.com/bufbuild/connect-go"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"gotest.tools/v3/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"google.golang.org/protobuf/types/known/structpb"
|
||||||
|
|
||||||
|
"gitea.com/gitea/act_runner/internal/pkg/client/mocks"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestReporter_parseLogRow(t *testing.T) {
|
func TestReporter_parseLogRow(t *testing.T) {
|
||||||
@ -146,3 +154,44 @@ func TestReporter_parseLogRow(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReporter_Fire(t *testing.T) {
|
||||||
|
t.Run("ignore command lines", func(t *testing.T) {
|
||||||
|
client := mocks.NewClient(t)
|
||||||
|
client.On("UpdateLog", mock.Anything, mock.Anything).Return(func(_ context.Context, req *connect_go.Request[runnerv1.UpdateLogRequest]) (*connect_go.Response[runnerv1.UpdateLogResponse], error) {
|
||||||
|
t.Logf("Received UpdateLog: %s", req.Msg.String())
|
||||||
|
return connect_go.NewResponse(&runnerv1.UpdateLogResponse{
|
||||||
|
AckIndex: req.Msg.Index + int64(len(req.Msg.Rows)),
|
||||||
|
}), nil
|
||||||
|
})
|
||||||
|
client.On("UpdateTask", mock.Anything, mock.Anything).Return(func(_ context.Context, req *connect_go.Request[runnerv1.UpdateTaskRequest]) (*connect_go.Response[runnerv1.UpdateTaskResponse], error) {
|
||||||
|
t.Logf("Received UpdateTask: %s", req.Msg.String())
|
||||||
|
return connect_go.NewResponse(&runnerv1.UpdateTaskResponse{}), nil
|
||||||
|
})
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
taskCtx, err := structpb.NewStruct(map[string]interface{}{})
|
||||||
|
require.NoError(t, err)
|
||||||
|
reporter := NewReporter(ctx, cancel, client, &runnerv1.Task{
|
||||||
|
Context: taskCtx,
|
||||||
|
})
|
||||||
|
defer func() {
|
||||||
|
assert.NoError(t, reporter.Close(""))
|
||||||
|
}()
|
||||||
|
reporter.ResetSteps(5)
|
||||||
|
|
||||||
|
dataStep0 := map[string]interface{}{
|
||||||
|
"stage": "Main",
|
||||||
|
"stepNumber": 0,
|
||||||
|
"raw_output": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.NoError(t, reporter.Fire(&log.Entry{Message: "regular log line", Data: dataStep0}))
|
||||||
|
assert.NoError(t, reporter.Fire(&log.Entry{Message: "::debug::debug log line", Data: dataStep0}))
|
||||||
|
assert.NoError(t, reporter.Fire(&log.Entry{Message: "regular log line", Data: dataStep0}))
|
||||||
|
assert.NoError(t, reporter.Fire(&log.Entry{Message: "::debug::debug log line", Data: dataStep0}))
|
||||||
|
assert.NoError(t, reporter.Fire(&log.Entry{Message: "::debug::debug log line", Data: dataStep0}))
|
||||||
|
assert.NoError(t, reporter.Fire(&log.Entry{Message: "regular log line", Data: dataStep0}))
|
||||||
|
|
||||||
|
assert.Equal(t, int64(3), reporter.state.Steps[0].LogLength)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user