mirror of
https://gitea.com/gitea/act_runner.git
synced 2026-08-13 22:41:54 +02:00
Gitea needs the workflow model and the expression evaluator to parse workflows and to build the task payload this runner consumes, so today it depends on `gitea.com/gitea/runner` just for `act/model` and `act/exprparser`. Both packages now live in `gitea.dev/actionslib` (`pkg/model`, `pkg/exprparser`), the module both sides already share, and this repository consumes them from there. ### Changes - `act/model` and `act/exprparser` are deleted, all imports point at `gitea.dev/actionslib/pkg/...`. - New `act/ghcontext` package: the `GithubContext` helpers that need a git checkout on disk (`SetRef`, `SetSha`, `SetRepositoryAndOwner`) are runner only and would drag a git client plus the act context logger into the shared module, so they stay here as functions, with their tests. Only caller is `RunContext.getGithubContext`. - `act/common.CartesianProduct` moved to the shared model package, `act/model` was its only user. - `act/model/testdata/container-volumes` moved to `act/runner/testdata/container-volumes`, its only user is `runner_test.go`. - `internal/pkg/client.UUIDHeader` / `TokenHeader` now alias `pkg/protocol`, so the header names cannot drift apart from Gitea. ### Notes - No behaviour change intended: the moved files are unchanged apart from the import paths and the split described above. - `go.mod` depends on the released `gitea.dev/actionslib v0.7.0`, which carries both https://gitea.com/gitea/actionslib/pulls/11 and the `model.UsesHash` port in https://gitea.com/gitea/actionslib/pulls/14 that `main` needs after https://gitea.com/gitea/runner/pulls/1150. - Verified with `go build ./...`, `go vet ./...` and `go test ./act/... ./internal/...`; the docker based `act/runner` integration tests (`TestRunEvent`, `TestRunMatrixWithUserDefinedInclusions`) fail identically with and without this change in my environment. Assisted-by: Codet:GPT-5.1-Codex Reviewed-on: https://gitea.com/gitea/runner/pulls/1143 Reviewed-by: silverwind <[email protected]>
139 lines
4.5 KiB
Go
139 lines
4.5 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"
|
|
|
|
"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
|
|
)
|
|
|
|
func withDefaultBranch(ctx context.Context, b string, event map[string]any) map[string]any {
|
|
repoI, ok := event["repository"]
|
|
if !ok {
|
|
repoI = make(map[string]any)
|
|
}
|
|
|
|
repo, ok := repoI.(map[string]any)
|
|
if !ok {
|
|
common.Logger(ctx).Warnf("unable to set default branch to %v", b)
|
|
return event
|
|
}
|
|
|
|
// if the branch is already there return with no changes
|
|
if _, ok = repo["default_branch"]; ok {
|
|
return event
|
|
}
|
|
|
|
repo["default_branch"] = b
|
|
event["repository"] = repo
|
|
|
|
return event
|
|
}
|
|
|
|
// 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, defaultBranch, 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 := findGitRef(ctx, repoPath)
|
|
if err != nil {
|
|
logger.Warningf("unable to get git ref: %v", err)
|
|
} else {
|
|
logger.Debugf("using github ref: %s", ref)
|
|
ghc.Ref = ref
|
|
}
|
|
|
|
// set the branch in the event data
|
|
if defaultBranch != "" {
|
|
ghc.Event = withDefaultBranch(ctx, defaultBranch, ghc.Event)
|
|
} else {
|
|
ghc.Event = withDefaultBranch(ctx, "master", ghc.Event)
|
|
}
|
|
|
|
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 := findGitRevision(ctx, repoPath)
|
|
if err != nil {
|
|
logger.Warningf("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, githubInstance, remoteName, repoPath string) {
|
|
if ghc.Repository == "" {
|
|
repo, err := findGithubRepo(ctx, repoPath, githubInstance, remoteName)
|
|
if err != nil {
|
|
common.Logger(ctx).Warningf("unable to get git repo (githubInstance: %v; remoteName: %v, repoPath: %v): %v", githubInstance, remoteName, repoPath, err)
|
|
return
|
|
}
|
|
ghc.Repository = repo
|
|
}
|
|
ghc.RepositoryOwner = strings.Split(ghc.Repository, "/")[0]
|
|
}
|