Skip to content

Use run-name and evaluate workflow variables #34301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8d799c2
use the correct context data for PR link template in issue card
badhezi Apr 15, 2025
f2a2acf
Merge branch 'main' into main
badhezi Apr 15, 2025
5cc1bda
Merge branch 'main' into main
GiteaBot Apr 16, 2025
54d37d1
Merge branch 'main' into main
GiteaBot Apr 16, 2025
ac25150
Merge branch 'go-gitea:main' into main
badhezi Apr 16, 2025
e11a339
Merge branch 'go-gitea:main' into main
badhezi Apr 20, 2025
104eecc
Merge branch 'go-gitea:main' into main
badhezi Apr 21, 2025
bcc4ade
Merge branch 'go-gitea:main' into main
badhezi Apr 22, 2025
a7aaa79
Merge branch 'go-gitea:main' into main
badhezi Apr 27, 2025
b46d314
Merge branch 'go-gitea:main' into main
badhezi Apr 28, 2025
4e2434b
Merge branch 'go-gitea:main' into main
badhezi Apr 29, 2025
18016d6
save test - revert me later
badhezi Apr 27, 2025
abcb0d5
Support run-name evaluation in action runs
badhezi Apr 28, 2025
27805b3
remove redundant prints
badhezi Apr 28, 2025
7d4f722
lowercase function name
badhezi Apr 28, 2025
73ff39b
revert redundant change
badhezi Apr 28, 2025
5b85309
revert redundant change
badhezi Apr 28, 2025
384f6b1
fix lint errors
badhezi Apr 28, 2025
1df4989
move code to act/jobparser, support scheduled actions
badhezi Apr 29, 2025
9408c76
use pseudo-version of act
badhezi Apr 29, 2025
2fb5f3f
update act
badhezi Apr 29, 2025
883c8eb
fix integration tests
badhezi Apr 29, 2025
4248591
fix error handling and revert changes to test files
badhezi Apr 30, 2025
ba6c9fb
Merge branch 'main' into dev/hezi/fix-run-name
badhezi Apr 30, 2025
11e23c2
Move interpolation logic to jobparser, add helper ToGitHubContext, re…
badhezi May 1, 2025
3a1320c
Merge branch 'main' into dev/hezi/fix-run-name
badhezi May 1, 2025
ae352dc
remove redundant sums
badhezi May 1, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ replace github.com/hashicorp/go-version => github.com/6543/go-version v1.3.1

replace github.com/shurcooL/vfsgen => github.com/lunny/vfsgen v0.0.0-20220105142115-2c99e1ffdfa0

replace github.com/nektos/act => gitea.com/gitea/act v0.261.4
replace github.com/nektos/act => gitea.com/badhezi/act v0.0.0-20250501202946-5f2de4d09b1f

// TODO: the only difference is in `PutObject`: the fork doesn't use `NewVerifyingReader(r, sha256.New(), oid, expectedSize)`, need to figure out why
replace github.com/charmbracelet/git-lfs-transfer => gitea.com/gitea/git-lfs-transfer v0.2.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
gitea.com/gitea/act v0.261.4 h1:Tf9eLlvsYFtKcpuxlMvf9yT3g4Hshb2Beqw6C1STuH8=
gitea.com/gitea/act v0.261.4/go.mod h1:Pg5C9kQY1CEA3QjthjhlrqOC/QOT5NyWNjOjRHw23Ok=
gitea.com/badhezi/act v0.0.0-20250501202946-5f2de4d09b1f h1:aPHa4bpjOgUGJoMkQZ5PvpASmrjHLx55sIu+RtGWzvE=
gitea.com/badhezi/act v0.0.0-20250501202946-5f2de4d09b1f/go.mod h1:Pg5C9kQY1CEA3QjthjhlrqOC/QOT5NyWNjOjRHw23Ok=
gitea.com/gitea/git-lfs-transfer v0.2.0 h1:baHaNoBSRaeq/xKayEXwiDQtlIjps4Ac/Ll4KqLMB40=
gitea.com/gitea/git-lfs-transfer v0.2.0/go.mod h1:UrXUCm3xLQkq15fu7qlXHUMlrhdlXHoi13KH2Dfiits=
gitea.com/gitea/go-xsd-duration v0.0.0-20220703122237-02e73435a078 h1:BAFmdZpRW7zMQZQDClaCWobRj9uL1MR3MzpCVJvc5s4=
Expand Down
16 changes: 16 additions & 0 deletions models/actions/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,19 @@ func calculateDuration(started, stopped timeutil.TimeStamp, status Status) time.
}
return timeSince(s).Truncate(time.Second)
}

func (s *ActionSchedule) ToActionRun() *ActionRun {
return &ActionRun{
Title: s.Title,
RepoID: s.RepoID,
Repo: s.Repo,
OwnerID: s.OwnerID,
WorkflowID: s.WorkflowID,
TriggerUserID: s.TriggerUserID,
TriggerUser: s.TriggerUser,
Ref: s.Ref,
CommitSHA: s.CommitSHA,
Event: s.Event,
EventPayload: s.EventPayload,
}
}
25 changes: 23 additions & 2 deletions services/actions/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ import (
"code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"

"github.com/nektos/act/pkg/model"
)

type GiteaContext map[string]any

// GenerateGiteaContext generate the gitea context without token and gitea_runtime_token
// job can be nil when generating a context for parsing workflow-level expressions
func GenerateGiteaContext(run *actions_model.ActionRun, job *actions_model.ActionRunJob) map[string]any {
func GenerateGiteaContext(run *actions_model.ActionRun, job *actions_model.ActionRunJob) GiteaContext {
event := map[string]any{}
_ = json.Unmarshal([]byte(run.EventPayload), &event)

Expand All @@ -42,7 +47,7 @@ func GenerateGiteaContext(run *actions_model.ActionRun, job *actions_model.Actio

refName := git.RefName(ref)

gitContext := map[string]any{
gitContext := GiteaContext{
// standard contexts, see https://docs.github.com/en/actions/learn-github-actions/contexts#github-context
"action": "", // string, The name of the action currently running, or the id of a step. GitHub removes special characters, and uses the name __run when the current step runs a script without an id. If you use the same action more than once in the same job, the name will include a suffix with the sequence number with underscore before it. For example, the first script you run will have the name __run, and the second script will be named __run_2. Similarly, the second invocation of actions/checkout will be actionscheckout2.
"action_path": "", // string, The path where an action is located. This property is only supported in composite actions. You can use this path to access files located in the same repository as the action.
Expand Down Expand Up @@ -160,3 +165,19 @@ func mergeTwoOutputs(o1, o2 map[string]string) map[string]string {
}
return ret
}

func (g *GiteaContext) ToGitHubContext() *model.GithubContext {
ghCtx := &model.GithubContext{}

gitCtxRaw, err := json.Marshal(g)
if err != nil {
log.Error("ToGitHubContext.json.Marshal: %v", err)
}

err = json.Unmarshal(gitCtxRaw, ghCtx)
if err != nil {
log.Error("ToGitHubContext.json.Unmarshal: %v", err)
}

return ghCtx
}
31 changes: 30 additions & 1 deletion services/actions/notifier_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,11 @@ func handleWorkflows(
run := &actions_model.ActionRun{
Title: strings.SplitN(commit.CommitMessage, "\n", 2)[0],
RepoID: input.Repo.ID,
Repo: input.Repo,
OwnerID: input.Repo.OwnerID,
WorkflowID: dwf.EntryName,
TriggerUserID: input.Doer.ID,
TriggerUser: input.Doer,
Ref: ref,
CommitSHA: commit.ID.String(),
IsForkPullRequest: isForkPullRequest,
Expand Down Expand Up @@ -333,12 +335,18 @@ func handleWorkflows(
continue
}

jobs, err := jobparser.Parse(dwf.Content, jobparser.WithVars(vars))
giteaCtx := GenerateGiteaContext(run, nil)

jobs, err := jobparser.Parse(dwf.Content, jobparser.WithVars(vars), jobparser.WithGitContext(giteaCtx.ToGitHubContext()))
if err != nil {
log.Error("jobparser.Parse: %v", err)
continue
}

if len(jobs) > 0 && jobs[0].RunName != "" {
run.Title = jobs[0].RunName
}

// cancel running jobs if the event is push or pull_request_sync
if run.Event == webhook_module.HookEventPush ||
run.Event == webhook_module.HookEventPullRequestSync {
Expand Down Expand Up @@ -508,16 +516,37 @@ func handleSchedules(
run := &actions_model.ActionSchedule{
Title: strings.SplitN(commit.CommitMessage, "\n", 2)[0],
RepoID: input.Repo.ID,
Repo: input.Repo,
OwnerID: input.Repo.OwnerID,
WorkflowID: dwf.EntryName,
TriggerUserID: user_model.ActionsUserID,
TriggerUser: user_model.NewActionsUser(),
Ref: ref,
CommitSHA: commit.ID.String(),
Event: input.Event,
EventPayload: string(p),
Specs: schedules,
Content: dwf.Content,
}

vars, err := actions_model.GetVariablesOfRun(ctx, run.ToActionRun())
if err != nil {
log.Error("GetVariablesOfRun: %v", err)
continue
}

giteaCtx := GenerateGiteaContext(run.ToActionRun(), nil)

jobs, err := jobparser.Parse(dwf.Content, jobparser.WithVars(vars), jobparser.WithGitContext(giteaCtx.ToGitHubContext()))
if err != nil {
log.Error("jobparser.Parse: %v", err)
continue
}

if len(jobs) > 0 && jobs[0].RunName != "" {
run.Title = jobs[0].RunName
}

crons = append(crons, run)
}

Expand Down
65 changes: 39 additions & 26 deletions services/actions/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,22 +192,48 @@ func DispatchActionWorkflow(ctx reqctx.RequestContext, doer *user_model.User, re

// find workflow from commit
var workflows []*jobparser.SingleWorkflow
for _, entry := range entries {
if entry.Name() != workflowID {
continue
}
var entry *git.TreeEntry

content, err := actions.GetContentFromEntry(entry)
if err != nil {
return err
}
workflows, err = jobparser.Parse(content)
if err != nil {
return err
for _, e := range entries {
if e.Name() != workflowID {
continue
}
entry = e
break
}

content, err := actions.GetContentFromEntry(entry)
if err != nil {
return err
}

run := &actions_model.ActionRun{
Title: strings.SplitN(runTargetCommit.CommitMessage, "\n", 2)[0],
RepoID: repo.ID,
Repo: repo,
OwnerID: repo.OwnerID,
WorkflowID: workflowID,
TriggerUserID: doer.ID,
TriggerUser: doer,
Ref: string(refName),
CommitSHA: runTargetCommit.ID.String(),
IsForkPullRequest: false,
Event: "workflow_dispatch",
TriggerEvent: "workflow_dispatch",
Status: actions_model.StatusWaiting,
}

giteaCtx := GenerateGiteaContext(run, nil)

workflows, err = jobparser.Parse(content, jobparser.WithGitContext(giteaCtx.ToGitHubContext()))
if err != nil {
return err
}

if len(workflows) > 0 && workflows[0].RunName != "" {
run.Title = workflows[0].RunName
}

if len(workflows) == 0 {
return util.ErrorWrapLocale(
util.NewNotExistErrorf("workflow %q doesn't exist", workflowID),
Expand Down Expand Up @@ -236,25 +262,12 @@ func DispatchActionWorkflow(ctx reqctx.RequestContext, doer *user_model.User, re
Inputs: inputsWithDefaults,
Sender: convert.ToUserWithAccessMode(ctx, doer, perm.AccessModeNone),
}

var eventPayload []byte
if eventPayload, err = workflowDispatchPayload.JSONPayload(); err != nil {
return fmt.Errorf("JSONPayload: %w", err)
}

run := &actions_model.ActionRun{
Title: strings.SplitN(runTargetCommit.CommitMessage, "\n", 2)[0],
RepoID: repo.ID,
OwnerID: repo.OwnerID,
WorkflowID: workflowID,
TriggerUserID: doer.ID,
Ref: string(refName),
CommitSHA: runTargetCommit.ID.String(),
IsForkPullRequest: false,
Event: "workflow_dispatch",
TriggerEvent: "workflow_dispatch",
EventPayload: string(eventPayload),
Status: actions_model.StatusWaiting,
}
run.EventPayload = string(eventPayload)

// cancel running jobs of the same workflow
if err := CancelPreviousJobs(
Expand Down
Loading