mirror of
https://gitea.com/gitea/runner.git
synced 2026-08-26 22:07:45 +00:00
chore: update deps, adapt lint, use json v2 (#1185)
- Raised go to 1.27 - Adopted json v2 - Sync lint config from gitea - Fixed all issues Co-authored-by: silverwind <me@silverwind.io> Reviewed-on: https://gitea.com/gitea/runner/pulls/1185 Reviewed-by: bircni <bircni@icloud.com> Co-authored-by: Renovate Bot <renovate-bot@gitea.com>
This commit is contained in:
@@ -11,7 +11,7 @@ import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"encoding/json/v2"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -357,8 +357,8 @@ func (h *Handler) Close() error {
|
||||
|
||||
func (h *Handler) openDB() (*bolthold.Store, error) {
|
||||
return bolthold.Open(filepath.Join(h.dir, "bolt.db"), 0o644, &bolthold.Options{
|
||||
Encoder: json.Marshal,
|
||||
Decoder: json.Unmarshal,
|
||||
Encoder: func(value any) ([]byte, error) { return json.Marshal(value) },
|
||||
Decoder: func(data []byte, value any) error { return json.Unmarshal(data, value) },
|
||||
Options: &bbolt.Options{
|
||||
Timeout: 5 * time.Second,
|
||||
NoGrowSync: bbolt.DefaultOptions.NoGrowSync,
|
||||
@@ -422,7 +422,7 @@ func (h *Handler) lookupCache(db *bolthold.Store, repo string, keys []string, ve
|
||||
func (h *Handler) reserve(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||
cred := credFromContext(r.Context())
|
||||
api := &Request{}
|
||||
if err := json.NewDecoder(r.Body).Decode(api); err != nil {
|
||||
if err := json.UnmarshalRead(r.Body, api); err != nil {
|
||||
h.responseJSON(w, r, 400, err)
|
||||
return
|
||||
}
|
||||
@@ -692,7 +692,7 @@ func (h *Handler) ResultsURL(cred JobCredential) string {
|
||||
|
||||
func (h *Handler) internalRegister(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||
var body internalRegisterBody
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
if err := json.UnmarshalRead(r.Body, &body); err != nil {
|
||||
h.responseJSON(w, r, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
@@ -708,7 +708,7 @@ func (h *Handler) internalRegister(w http.ResponseWriter, r *http.Request, _ htt
|
||||
// POST /_internal/revoke
|
||||
func (h *Handler) internalRevoke(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||
var body internalRevokeBody
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
if err := json.UnmarshalRead(r.Body, &body); err != nil {
|
||||
h.responseJSON(w, r, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ package artifactcache
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/json"
|
||||
"encoding/json/v2"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -136,7 +136,7 @@ func TestHandler(t *testing.T) {
|
||||
defer resp.Body.Close()
|
||||
assert.Equal(t, 200, resp.StatusCode)
|
||||
|
||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&first))
|
||||
require.NoError(t, json.UnmarshalRead(resp.Body, &first))
|
||||
assert.NotZero(t, first.CacheID)
|
||||
}
|
||||
{
|
||||
@@ -151,7 +151,7 @@ func TestHandler(t *testing.T) {
|
||||
defer resp.Body.Close()
|
||||
assert.Equal(t, 200, resp.StatusCode)
|
||||
|
||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&second))
|
||||
require.NoError(t, json.UnmarshalRead(resp.Body, &second))
|
||||
assert.NotZero(t, second.CacheID)
|
||||
}
|
||||
|
||||
@@ -204,7 +204,7 @@ func TestHandler(t *testing.T) {
|
||||
got := struct {
|
||||
CacheID uint64 `json:"cacheId"`
|
||||
}{}
|
||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
||||
require.NoError(t, json.UnmarshalRead(resp.Body, &got))
|
||||
id = got.CacheID
|
||||
}
|
||||
{
|
||||
@@ -259,7 +259,7 @@ func TestHandler(t *testing.T) {
|
||||
got := struct {
|
||||
CacheID uint64 `json:"cacheId"`
|
||||
}{}
|
||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
||||
require.NoError(t, json.UnmarshalRead(resp.Body, &got))
|
||||
id = got.CacheID
|
||||
}
|
||||
{
|
||||
@@ -315,7 +315,7 @@ func TestHandler(t *testing.T) {
|
||||
got := struct {
|
||||
CacheID uint64 `json:"cacheId"`
|
||||
}{}
|
||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
||||
require.NoError(t, json.UnmarshalRead(resp.Body, &got))
|
||||
id = got.CacheID
|
||||
}
|
||||
{
|
||||
@@ -362,7 +362,7 @@ func TestHandler(t *testing.T) {
|
||||
got := struct {
|
||||
CacheID uint64 `json:"cacheId"`
|
||||
}{}
|
||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
||||
require.NoError(t, json.UnmarshalRead(resp.Body, &got))
|
||||
id = got.CacheID
|
||||
}
|
||||
|
||||
@@ -413,7 +413,7 @@ func TestHandler(t *testing.T) {
|
||||
got := struct {
|
||||
CacheID uint64 `json:"cacheId"`
|
||||
}{}
|
||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
||||
require.NoError(t, json.UnmarshalRead(resp.Body, &got))
|
||||
id = got.CacheID
|
||||
}
|
||||
{
|
||||
@@ -493,7 +493,7 @@ func TestHandler(t *testing.T) {
|
||||
ArchiveLocation string `json:"archiveLocation"`
|
||||
CacheKey string `json:"cacheKey"`
|
||||
}{}
|
||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
||||
require.NoError(t, json.UnmarshalRead(resp.Body, &got))
|
||||
assert.Equal(t, "hit", got.Result)
|
||||
assert.Equal(t, keys[except], got.CacheKey)
|
||||
|
||||
@@ -528,7 +528,7 @@ func TestHandler(t *testing.T) {
|
||||
ArchiveLocation string `json:"archiveLocation"`
|
||||
CacheKey string `json:"cacheKey"`
|
||||
}{}
|
||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
||||
require.NoError(t, json.UnmarshalRead(resp.Body, &got))
|
||||
assert.Equal(t, "hit", got.Result)
|
||||
assert.Equal(t, key, got.CacheKey)
|
||||
assert.NotEqual(t, strings.ToLower(key), got.CacheKey)
|
||||
@@ -577,7 +577,7 @@ func TestHandler(t *testing.T) {
|
||||
ArchiveLocation string `json:"archiveLocation"`
|
||||
CacheKey string `json:"cacheKey"`
|
||||
}{}
|
||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
||||
require.NoError(t, json.UnmarshalRead(resp.Body, &got))
|
||||
assert.Equal(t, keys[expect], got.CacheKey)
|
||||
|
||||
contentResp, err := testClient.Get(got.ArchiveLocation)
|
||||
@@ -633,7 +633,7 @@ func TestHandler(t *testing.T) {
|
||||
ArchiveLocation string `json:"archiveLocation"`
|
||||
CacheKey string `json:"cacheKey"`
|
||||
}{}
|
||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
||||
require.NoError(t, json.UnmarshalRead(resp.Body, &got))
|
||||
assert.Equal(t, keys[expect], got.CacheKey)
|
||||
|
||||
contentResp, err := testClient.Get(got.ArchiveLocation)
|
||||
@@ -677,7 +677,7 @@ func uploadCacheNormally(t *testing.T, base, key, version string, content []byte
|
||||
got := struct {
|
||||
CacheID uint64 `json:"cacheId"`
|
||||
}{}
|
||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
||||
require.NoError(t, json.UnmarshalRead(resp.Body, &got))
|
||||
id = got.CacheID
|
||||
}
|
||||
{
|
||||
@@ -708,7 +708,7 @@ func uploadCacheNormally(t *testing.T, base, key, version string, content []byte
|
||||
ArchiveLocation string `json:"archiveLocation"`
|
||||
CacheKey string `json:"cacheKey"`
|
||||
}{}
|
||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
||||
require.NoError(t, json.UnmarshalRead(resp.Body, &got))
|
||||
assert.Equal(t, "hit", got.Result)
|
||||
assert.Equal(t, key, got.CacheKey)
|
||||
archiveLocation = got.ArchiveLocation
|
||||
@@ -1197,7 +1197,7 @@ func TestHandler_CrossRepoIsolation(t *testing.T) {
|
||||
var reserved struct {
|
||||
CacheID uint64 `json:"cacheId"`
|
||||
}
|
||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&reserved))
|
||||
require.NoError(t, json.UnmarshalRead(resp.Body, &reserved))
|
||||
resp.Body.Close()
|
||||
require.NotZero(t, reserved.CacheID)
|
||||
|
||||
@@ -1331,7 +1331,7 @@ func TestHandler_ArtifactSignatureDownload(t *testing.T) {
|
||||
var hit struct {
|
||||
ArchiveLocation string `json:"archiveLocation"`
|
||||
}
|
||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&hit))
|
||||
require.NoError(t, json.UnmarshalRead(resp.Body, &hit))
|
||||
resp.Body.Close()
|
||||
|
||||
require.Contains(t, hit.ArchiveLocation, "sig=")
|
||||
|
||||
@@ -5,7 +5,8 @@ package artifactcache
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"encoding/json"
|
||||
"encoding/json/jsontext"
|
||||
"encoding/json/v2"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -128,7 +129,7 @@ func (h *Handler) v2FinalizeCacheEntryUpload(w http.ResponseWriter, r *http.Requ
|
||||
}
|
||||
db.Close() // commitCache needs the store closed
|
||||
|
||||
cache.Size, _ = cmp.Or(req.SizeBytes, req.SizeBytesCamel).Int64()
|
||||
cache.Size = int64(cmp.Or(req.SizeBytes, req.SizeBytesCamel))
|
||||
if err := h.commitCache(cache); err != nil {
|
||||
h.logger.Errorf("finalize cache %d (%s): %v", cache.ID, cache.Key, err)
|
||||
h.twirpNotOK(w, r)
|
||||
@@ -245,10 +246,10 @@ type (
|
||||
}
|
||||
|
||||
v2FinalizeRequest struct {
|
||||
Key string `json:"key"`
|
||||
Version string `json:"version"`
|
||||
SizeBytes json.Number `json:"size_bytes"`
|
||||
SizeBytesCamel json.Number `json:"sizeBytes"`
|
||||
Key string `json:"key"`
|
||||
Version string `json:"version"`
|
||||
SizeBytes twirpInt64 `json:"size_bytes"`
|
||||
SizeBytesCamel twirpInt64 `json:"sizeBytes"`
|
||||
}
|
||||
|
||||
v2DownloadRequest struct {
|
||||
@@ -259,6 +260,31 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
// twirpInt64 accepts its value as the JSON string the mapping prescribes or as a bare number.
|
||||
type twirpInt64 int64
|
||||
|
||||
func (n *twirpInt64) UnmarshalJSONFrom(dec *jsontext.Decoder) error {
|
||||
val, err := dec.ReadValue()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
digits := []byte(val)
|
||||
switch val.Kind() {
|
||||
case 'n': // absent, keep the zero value
|
||||
return nil
|
||||
case '"':
|
||||
if digits, err = jsontext.AppendUnquote(nil, val); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
parsed, err := strconv.ParseInt(string(digits), 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*n = twirpInt64(parsed)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d v2DownloadRequest) keys() []string {
|
||||
restoreKeys := d.RestoreKeys
|
||||
if len(restoreKeys) == 0 {
|
||||
@@ -269,6 +295,6 @@ func (d v2DownloadRequest) keys() []string {
|
||||
|
||||
func decodeTwirpRequest[T any](r *http.Request) (T, error) {
|
||||
var req T
|
||||
err := json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(&req)
|
||||
err := json.UnmarshalRead(io.LimitReader(r.Body, 1<<20), &req)
|
||||
return req, err
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ package artifactcache
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"encoding/json/v2"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -32,7 +32,7 @@ func v2Call(t *testing.T, handler *Handler, client *http.Client, method string,
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
|
||||
got := map[string]any{}
|
||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
||||
require.NoError(t, json.UnmarshalRead(resp.Body, &got))
|
||||
return got
|
||||
}
|
||||
|
||||
@@ -227,7 +227,7 @@ func TestCacheServiceV2Lookups(t *testing.T) {
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
|
||||
got := map[string]any{}
|
||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&got))
|
||||
require.NoError(t, json.UnmarshalRead(resp.Body, &got))
|
||||
assert.Equal(t, "deps-abc", got["cacheKey"])
|
||||
assert.NotEmpty(t, got["archiveLocation"])
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user