mirror of
https://gitea.com/gitea/runner.git
synced 2026-11-04 14:35:32 +00:00
Removes dead code and redundant logic and consolidates tests, so there is less to read and maintain. No behavior change. 1. **act/runner**: inline single-use wrappers, compile regexes once instead of per call, embed the trampoline as a string, drop the unused legacy result of `parseActionCommand` 2. **act/container**: `sanitizeConfig` and `mergeContainerConfigs` stop returning the configs they already mutate in place, drop never-set option fields and the unused `user` parameter of `ExecWithCmdLine` 3. **act/common and cache server**: drop the unused `Trace`/`Tracef` loggers and the short SHA of `FindGitRevision`, `RegisterJob` releases through `RevokeJob` 4. **daemon, poller and reporter**: one shutdown wait for both poll modes, drop an unreachable fetch timeout branch, guard the reporter's last report time with its existing lock instead of an atomic 5. **build**: drop unused Makefile and release workflow variables, collapse the goreleaser ignore list to the same build targets 6. **tests**: fold near-duplicate tests into tables with shared setup Throughout, hand-rolled logic gives way to `cmp.Or`, `strings.Cut`, `slices` and `min`/`max`. Reviewed-on: https://gitea.com/gitea/runner/pulls/1335 Reviewed-by: bircni <[email protected]> Co-authored-by: silverwind <[email protected]>
143 lines
4.7 KiB
Go
143 lines
4.7 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// Copyright 2021 The nektos/act Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// Package ghcontext fills a model.GithubContext from the local git checkout.
|
|
// The pure data parts of the context live in the shared
|
|
// gitea.dev/actionslib/pkg/model package, only the helpers that need a
|
|
// git repository on disk are kept here.
|
|
package ghcontext
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
|
|
"gitea.com/gitea/runner/act/common"
|
|
"gitea.com/gitea/runner/act/common/git"
|
|
|
|
"gitea.dev/actionslib/pkg/model"
|
|
)
|
|
|
|
var (
|
|
findGitRef = git.FindGitRef
|
|
findGitRevision = git.FindGitRevision
|
|
findGithubRepo = git.FindGithubRepo
|
|
)
|
|
|
|
type pinnedKey struct{}
|
|
|
|
// WithPinnedCheckout keeps a run's sha and ref fixed when HEAD moves.
|
|
func WithPinnedCheckout(ctx context.Context) context.Context {
|
|
if ctx.Value(pinnedKey{}) != nil {
|
|
return ctx
|
|
}
|
|
return context.WithValue(ctx, pinnedKey{}, &sync.Map{})
|
|
}
|
|
|
|
func pinned(ctx context.Context, key string, lookup func() (string, error)) (string, error) {
|
|
cache, ok := ctx.Value(pinnedKey{}).(*sync.Map)
|
|
if !ok {
|
|
return lookup()
|
|
}
|
|
cached, _ := cache.LoadOrStore(key, sync.OnceValues(lookup))
|
|
lookupOnce, _ := cached.(func() (string, error))
|
|
return lookupOnce()
|
|
}
|
|
|
|
// SetRef resolves the ref of the context from its event payload, falling back
|
|
// to the ref checked out in repoPath.
|
|
func SetRef(ctx context.Context, ghc *model.GithubContext, repoPath string) {
|
|
logger := common.Logger(ctx)
|
|
|
|
// https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows
|
|
// https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads
|
|
switch ghc.EventName {
|
|
case "pull_request_target":
|
|
ghc.Ref = "refs/heads/" + ghc.BaseRef
|
|
case "pull_request", "pull_request_review", "pull_request_review_comment":
|
|
ghc.Ref = fmt.Sprintf("refs/pull/%.0f/merge", ghc.Event["number"])
|
|
case "deployment", "deployment_status":
|
|
ghc.Ref = model.AsString(model.NestedMapLookup(ghc.Event, "deployment", "ref"))
|
|
case "release":
|
|
ghc.Ref = "refs/tags/" + model.AsString(model.NestedMapLookup(ghc.Event, "release", "tag_name"))
|
|
case "push", "create", "workflow_dispatch":
|
|
ghc.Ref = model.AsString(ghc.Event["ref"])
|
|
default:
|
|
defaultBranch := model.AsString(model.NestedMapLookup(ghc.Event, "repository", "default_branch"))
|
|
if defaultBranch != "" {
|
|
ghc.Ref = "refs/heads/" + defaultBranch
|
|
}
|
|
}
|
|
|
|
if ghc.Ref == "" {
|
|
ref, err := pinned(ctx, "ref:"+repoPath, func() (string, error) { return findGitRef(ctx, repoPath) })
|
|
if err != nil {
|
|
logger.Warnf("unable to get git ref: %v", err)
|
|
} else {
|
|
logger.Debugf("using github ref: %s", ref)
|
|
ghc.Ref = ref
|
|
}
|
|
|
|
repository, exists := ghc.Event["repository"]
|
|
if !exists {
|
|
repository = map[string]any{}
|
|
}
|
|
if repository, ok := repository.(map[string]any); !ok {
|
|
logger.Warn("unable to set default branch to master")
|
|
} else if _, exists := repository["default_branch"]; !exists {
|
|
repository["default_branch"] = "master"
|
|
ghc.Event["repository"] = repository
|
|
}
|
|
|
|
if ghc.Ref == "" {
|
|
ghc.Ref = "refs/heads/" + model.AsString(model.NestedMapLookup(ghc.Event, "repository", "default_branch"))
|
|
}
|
|
}
|
|
}
|
|
|
|
// SetSha resolves the commit of the context from its event payload, falling
|
|
// back to the revision checked out in repoPath.
|
|
func SetSha(ctx context.Context, ghc *model.GithubContext, repoPath string) {
|
|
logger := common.Logger(ctx)
|
|
|
|
// https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows
|
|
// https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads
|
|
switch ghc.EventName {
|
|
case "pull_request_target":
|
|
ghc.Sha = model.AsString(model.NestedMapLookup(ghc.Event, "pull_request", "base", "sha"))
|
|
case "deployment", "deployment_status":
|
|
ghc.Sha = model.AsString(model.NestedMapLookup(ghc.Event, "deployment", "sha"))
|
|
case "push", "create", "workflow_dispatch":
|
|
if deleted, ok := ghc.Event["deleted"].(bool); ok && !deleted {
|
|
ghc.Sha = model.AsString(ghc.Event["after"])
|
|
}
|
|
}
|
|
|
|
if ghc.Sha == "" {
|
|
sha, err := pinned(ctx, "sha:"+repoPath, func() (string, error) {
|
|
return findGitRevision(ctx, repoPath)
|
|
})
|
|
if err != nil {
|
|
logger.Warnf("unable to get git revision: %v", err)
|
|
} else {
|
|
ghc.Sha = sha
|
|
}
|
|
}
|
|
}
|
|
|
|
// SetRepositoryAndOwner resolves the repository of the context from the git
|
|
// remote in repoPath when it is not set yet, and derives its owner.
|
|
func SetRepositoryAndOwner(ctx context.Context, ghc *model.GithubContext, repoPath string) {
|
|
if ghc.Repository == "" {
|
|
repo, err := findGithubRepo(ctx, repoPath)
|
|
if err != nil {
|
|
common.Logger(ctx).Warnf("unable to get git repo (repoPath: %v): %v", repoPath, err)
|
|
return
|
|
}
|
|
ghc.Repository = repo
|
|
}
|
|
ghc.RepositoryOwner, _, _ = strings.Cut(ghc.Repository, "/")
|
|
}
|