mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-09-15 18:57:01 +00:00
chore(refactor): add common.RandName to keep name generation DRY
This commit is contained in:
parent
7eb547faa5
commit
6e59f129c1
4 changed files with 25 additions and 14 deletions
|
@ -5,7 +5,6 @@ package cacheproxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/hmac"
|
"crypto/hmac"
|
||||||
"crypto/rand"
|
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
|
@ -170,12 +169,10 @@ func (h *Handler) ExternalURL() string {
|
||||||
// The function returns the 32-bit random key which the run will use to identify itself.
|
// The function returns the 32-bit random key which the run will use to identify itself.
|
||||||
func (h *Handler) AddRun(data RunData) (string, error) {
|
func (h *Handler) AddRun(data RunData) (string, error) {
|
||||||
for retries := 0; retries < 3; retries++ {
|
for retries := 0; retries < 3; retries++ {
|
||||||
keyBytes := make([]byte, 4)
|
key, err := common.RandName(4)
|
||||||
_, err := rand.Read(keyBytes)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.New("Could not generate the run id")
|
return "", errors.New("Could not generate the run id")
|
||||||
}
|
}
|
||||||
key := hex.EncodeToString(keyBytes)
|
|
||||||
|
|
||||||
_, loaded := h.runs.LoadOrStore(key, data)
|
_, loaded := h.runs.LoadOrStore(key, data)
|
||||||
if !loaded {
|
if !loaded {
|
||||||
|
|
16
act/common/randname.go
Normal file
16
act/common/randname.go
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
// Copyright 2025 The Forgejo Authors
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/hex"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RandName(size int) (string, error) {
|
||||||
|
randBytes := make([]byte, size)
|
||||||
|
if _, err := rand.Read(randBytes); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return hex.EncodeToString(randBytes), nil
|
||||||
|
}
|
|
@ -3,8 +3,6 @@ package runner
|
||||||
import (
|
import (
|
||||||
"archive/tar"
|
"archive/tar"
|
||||||
"context"
|
"context"
|
||||||
"crypto/rand"
|
|
||||||
"encoding/hex"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
@ -19,6 +17,8 @@ import (
|
||||||
"github.com/go-git/go-git/v5/plumbing/object"
|
"github.com/go-git/go-git/v5/plumbing/object"
|
||||||
"github.com/go-git/go-git/v5/plumbing/transport"
|
"github.com/go-git/go-git/v5/plumbing/transport"
|
||||||
"github.com/go-git/go-git/v5/plumbing/transport/http"
|
"github.com/go-git/go-git/v5/plumbing/transport/http"
|
||||||
|
|
||||||
|
"github.com/nektos/act/pkg/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ActionCache interface {
|
type ActionCache interface {
|
||||||
|
@ -39,11 +39,10 @@ func (c GoGitActionCache) Fetch(ctx context.Context, cacheDir, url, ref, token s
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
tmpBranch := make([]byte, 12)
|
branchName, err := common.RandName(12)
|
||||||
if _, err := rand.Read(tmpBranch); err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
branchName := hex.EncodeToString(tmpBranch)
|
|
||||||
|
|
||||||
var auth transport.AuthMethod
|
var auth transport.AuthMethod
|
||||||
if token != "" {
|
if token != "" {
|
||||||
|
|
|
@ -5,10 +5,8 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"crypto/rand"
|
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"encoding/hex"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -283,9 +281,10 @@ func (rc *RunContext) startHostEnvironment() common.Executor {
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
cacheDir := rc.ActionCacheDir()
|
cacheDir := rc.ActionCacheDir()
|
||||||
randBytes := make([]byte, 8)
|
randName, err := common.RandName(8)
|
||||||
_, _ = rand.Read(randBytes)
|
if err != nil {
|
||||||
randName := hex.EncodeToString(randBytes)
|
return err
|
||||||
|
}
|
||||||
miscpath := filepath.Join(cacheDir, randName)
|
miscpath := filepath.Join(cacheDir, randName)
|
||||||
actPath := filepath.Join(miscpath, "act")
|
actPath := filepath.Join(miscpath, "act")
|
||||||
if err := os.MkdirAll(actPath, 0o777); err != nil {
|
if err := os.MkdirAll(actPath, 0o777); err != nil {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue