mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-09-05 18:40:59 +00:00
chore: panic if a random name cannot be created (#853)
If that happens so much will go wrong that there is no point in continuing to do anything. It simplifies the requirements of the caller: it may be a function that is assumed to never error. <!--start release-notes-assistant--> <!--URL:https://code.forgejo.org/forgejo/runner--> - other - [PR](https://code.forgejo.org/forgejo/runner/pulls/853): <!--number 853 --><!--line 0 --><!--description Y2hvcmU6IHBhbmljIGlmIGEgcmFuZG9tIG5hbWUgY2Fubm90IGJlIGNyZWF0ZWQ=-->chore: panic if a random name cannot be created<!--description--> <!--end release-notes-assistant--> Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/853 Reviewed-by: Mathieu Fenniak <mfenniak@noreply.code.forgejo.org> Co-authored-by: Earl Warren <contact@earl-warren.org> Co-committed-by: Earl Warren <contact@earl-warren.org>
This commit is contained in:
parent
987efff021
commit
0ff1ec4c04
4 changed files with 14 additions and 18 deletions
|
@ -167,11 +167,7 @@ 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++ {
|
||||||
key, err := common.RandName(4)
|
key := common.MustRandName(4)
|
||||||
if err != nil {
|
|
||||||
return "", errors.New("Could not generate the run id")
|
|
||||||
}
|
|
||||||
|
|
||||||
_, loaded := h.runs.LoadOrStore(key, data)
|
_, loaded := h.runs.LoadOrStore(key, data)
|
||||||
if !loaded {
|
if !loaded {
|
||||||
// The key was unique and added successfully
|
// The key was unique and added successfully
|
||||||
|
|
|
@ -5,12 +5,21 @@ package common
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RandName(size int) (string, error) {
|
func randName(size int) (string, error) {
|
||||||
randBytes := make([]byte, size)
|
randBytes := make([]byte, size)
|
||||||
if _, err := rand.Read(randBytes); err != nil {
|
if _, err := rand.Read(randBytes); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return hex.EncodeToString(randBytes), nil
|
return hex.EncodeToString(randBytes), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MustRandName(size int) string {
|
||||||
|
name, err := randName(size)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("RandName(%d): %v", size, err))
|
||||||
|
}
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
|
|
@ -39,10 +39,7 @@ func (c GoGitActionCache) Fetch(ctx context.Context, cacheDir, url, ref, token s
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
branchName, err := common.RandName(12)
|
branchName := common.MustRandName(12)
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
var auth transport.AuthMethod
|
var auth transport.AuthMethod
|
||||||
if token != "" {
|
if token != "" {
|
||||||
|
|
|
@ -283,10 +283,7 @@ func (rc *RunContext) startHostEnvironment() common.Executor {
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
cacheDir := rc.ActionCacheDir()
|
cacheDir := rc.ActionCacheDir()
|
||||||
randName, err := common.RandName(8)
|
randName := common.MustRandName(8)
|
||||||
if err != nil {
|
|
||||||
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 {
|
||||||
|
@ -624,10 +621,7 @@ func (rc *RunContext) sh(ctx context.Context, script string) (stdout, stderr str
|
||||||
env[k] = v
|
env[k] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
base, err := common.RandName(8)
|
base := common.MustRandName(8)
|
||||||
if err != nil {
|
|
||||||
return "", "", err
|
|
||||||
}
|
|
||||||
name := base + ".sh"
|
name := base + ".sh"
|
||||||
oldStdout, oldStderr := rc.JobContainer.ReplaceLogWriter(hout, herr)
|
oldStdout, oldStderr := rc.JobContainer.ReplaceLogWriter(hout, herr)
|
||||||
err = rc.JobContainer.Copy(rc.JobContainer.GetActPath(), &container.FileEntry{
|
err = rc.JobContainer.Copy(rc.JobContainer.GetActPath(), &container.FileEntry{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue