Skip to content

Commit ab75c3f

Browse files
committed
parse concurrency
1 parent 0f18046 commit ab75c3f

File tree

4 files changed

+125
-75
lines changed

4 files changed

+125
-75
lines changed

modules/actions/workflows.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type DetectedWorkflow struct {
2424
EntryName string
2525
TriggerEvent *jobparser.Event
2626
Content []byte
27+
Concurrency *jobparser.UninterpolatedConcurrency
2728
}
2829

2930
func init() {
@@ -95,6 +96,19 @@ func GetEventsFromContent(content []byte) ([]*jobparser.Event, error) {
9596
return events, nil
9697
}
9798

99+
func GetConcurrencyFromContent(content []byte) (*jobparser.UninterpolatedConcurrency, error) {
100+
workflow, err := model.ReadWorkflow(bytes.NewReader(content))
101+
if err != nil {
102+
return nil, err
103+
}
104+
uc, err := jobparser.ParseRawConcurrency(&workflow.RawConcurrency)
105+
if err != nil {
106+
return nil, err
107+
}
108+
109+
return uc, nil
110+
}
111+
98112
func DetectWorkflows(
99113
gitRepo *git.Repository,
100114
commit *git.Commit,
@@ -121,6 +135,11 @@ func DetectWorkflows(
121135
log.Warn("ignore invalid workflow %q: %v", entry.Name(), err)
122136
continue
123137
}
138+
concurrency, err := GetConcurrencyFromContent(content)
139+
if err != nil {
140+
log.Warn("ignore workflow with invalid concurrency %q: %v", entry.Name(), err)
141+
continue
142+
}
124143
for _, evt := range events {
125144
log.Trace("detect workflow %q for event %#v matching %q", entry.Name(), evt, triggedEvent)
126145
if evt.IsSchedule() {
@@ -137,6 +156,7 @@ func DetectWorkflows(
137156
EntryName: entry.Name(),
138157
TriggerEvent: evt,
139158
Content: content,
159+
Concurrency: concurrency,
140160
}
141161
workflows = append(workflows, dwf)
142162
}

routers/api/actions/runner/utils.go

Lines changed: 5 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,8 @@ import (
1010
actions_model "code.gitea.io/gitea/models/actions"
1111
"code.gitea.io/gitea/models/db"
1212
secret_model "code.gitea.io/gitea/models/secret"
13-
actions_module "code.gitea.io/gitea/modules/actions"
1413
"code.gitea.io/gitea/modules/container"
15-
"code.gitea.io/gitea/modules/git"
16-
"code.gitea.io/gitea/modules/json"
1714
"code.gitea.io/gitea/modules/log"
18-
"code.gitea.io/gitea/modules/setting"
1915
"code.gitea.io/gitea/services/actions"
2016

2117
runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
@@ -65,82 +61,16 @@ func pickTask(ctx context.Context, runner *actions_model.ActionRunner) (*runnerv
6561
}
6662

6763
func generateTaskContext(t *actions_model.ActionTask) *structpb.Struct {
68-
event := map[string]any{}
69-
_ = json.Unmarshal([]byte(t.Job.Run.EventPayload), &event)
70-
71-
// TriggerEvent is added in https://github.com/go-gitea/gitea/pull/25229
72-
// This fallback is for the old ActionRun that doesn't have the TriggerEvent field
73-
// and should be removed in 1.22
74-
eventName := t.Job.Run.TriggerEvent
75-
if eventName == "" {
76-
eventName = t.Job.Run.Event.Event()
77-
}
78-
79-
baseRef := ""
80-
headRef := ""
81-
ref := t.Job.Run.Ref
82-
sha := t.Job.Run.CommitSHA
83-
if pullPayload, err := t.Job.Run.GetPullRequestEventPayload(); err == nil && pullPayload.PullRequest != nil && pullPayload.PullRequest.Base != nil && pullPayload.PullRequest.Head != nil {
84-
baseRef = pullPayload.PullRequest.Base.Ref
85-
headRef = pullPayload.PullRequest.Head.Ref
86-
87-
// if the TriggerEvent is pull_request_target, ref and sha need to be set according to the base of pull request
88-
// In GitHub's documentation, ref should be the branch or tag that triggered workflow. But when the TriggerEvent is pull_request_target,
89-
// the ref will be the base branch.
90-
if t.Job.Run.TriggerEvent == actions_module.GithubEventPullRequestTarget {
91-
ref = git.BranchPrefix + pullPayload.PullRequest.Base.Name
92-
sha = pullPayload.PullRequest.Base.Sha
93-
}
94-
}
95-
96-
refName := git.RefName(ref)
97-
9864
giteaRuntimeToken, err := actions.CreateAuthorizationToken(t.ID, t.Job.RunID, t.JobID)
9965
if err != nil {
10066
log.Error("actions.CreateAuthorizationToken failed: %v", err)
10167
}
10268

103-
taskContext, err := structpb.NewStruct(map[string]any{
104-
// standard contexts, see https://docs.github.com/en/actions/learn-github-actions/contexts#github-context
105-
"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.
106-
"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.
107-
"action_ref": "", // string, For a step executing an action, this is the ref of the action being executed. For example, v2.
108-
"action_repository": "", // string, For a step executing an action, this is the owner and repository name of the action. For example, actions/checkout.
109-
"action_status": "", // string, For a composite action, the current result of the composite action.
110-
"actor": t.Job.Run.TriggerUser.Name, // string, The username of the user that triggered the initial workflow run. If the workflow run is a re-run, this value may differ from github.triggering_actor. Any workflow re-runs will use the privileges of github.actor, even if the actor initiating the re-run (github.triggering_actor) has different privileges.
111-
"api_url": setting.AppURL + "api/v1", // string, The URL of the GitHub REST API.
112-
"base_ref": baseRef, // string, The base_ref or target branch of the pull request in a workflow run. This property is only available when the event that triggers a workflow run is either pull_request or pull_request_target.
113-
"env": "", // string, Path on the runner to the file that sets environment variables from workflow commands. This file is unique to the current step and is a different file for each step in a job. For more information, see "Workflow commands for GitHub Actions."
114-
"event": event, // object, The full event webhook payload. You can access individual properties of the event using this context. This object is identical to the webhook payload of the event that triggered the workflow run, and is different for each event. The webhooks for each GitHub Actions event is linked in "Events that trigger workflows." For example, for a workflow run triggered by the push event, this object contains the contents of the push webhook payload.
115-
"event_name": eventName, // string, The name of the event that triggered the workflow run.
116-
"event_path": "", // string, The path to the file on the runner that contains the full event webhook payload.
117-
"graphql_url": "", // string, The URL of the GitHub GraphQL API.
118-
"head_ref": headRef, // string, The head_ref or source branch of the pull request in a workflow run. This property is only available when the event that triggers a workflow run is either pull_request or pull_request_target.
119-
"job": fmt.Sprint(t.JobID), // string, The job_id of the current job.
120-
"ref": ref, // string, The fully-formed ref of the branch or tag that triggered the workflow run. For workflows triggered by push, this is the branch or tag ref that was pushed. For workflows triggered by pull_request, this is the pull request merge branch. For workflows triggered by release, this is the release tag created. For other triggers, this is the branch or tag ref that triggered the workflow run. This is only set if a branch or tag is available for the event type. The ref given is fully-formed, meaning that for branches the format is refs/heads/<branch_name>, for pull requests it is refs/pull/<pr_number>/merge, and for tags it is refs/tags/<tag_name>. For example, refs/heads/feature-branch-1.
121-
"ref_name": refName.ShortName(), // string, The short ref name of the branch or tag that triggered the workflow run. This value matches the branch or tag name shown on GitHub. For example, feature-branch-1.
122-
"ref_protected": false, // boolean, true if branch protections are configured for the ref that triggered the workflow run.
123-
"ref_type": refName.RefType(), // string, The type of ref that triggered the workflow run. Valid values are branch or tag.
124-
"path": "", // string, Path on the runner to the file that sets system PATH variables from workflow commands. This file is unique to the current step and is a different file for each step in a job. For more information, see "Workflow commands for GitHub Actions."
125-
"repository": t.Job.Run.Repo.OwnerName + "/" + t.Job.Run.Repo.Name, // string, The owner and repository name. For example, Codertocat/Hello-World.
126-
"repository_owner": t.Job.Run.Repo.OwnerName, // string, The repository owner's name. For example, Codertocat.
127-
"repositoryUrl": t.Job.Run.Repo.HTMLURL(), // string, The Git URL to the repository. For example, git://github.com/codertocat/hello-world.git.
128-
"retention_days": "", // string, The number of days that workflow run logs and artifacts are kept.
129-
"run_id": fmt.Sprint(t.Job.RunID), // string, A unique number for each workflow run within a repository. This number does not change if you re-run the workflow run.
130-
"run_number": fmt.Sprint(t.Job.Run.Index), // string, A unique number for each run of a particular workflow in a repository. This number begins at 1 for the workflow's first run, and increments with each new run. This number does not change if you re-run the workflow run.
131-
"run_attempt": fmt.Sprint(t.Job.Attempt), // string, A unique number for each attempt of a particular workflow run in a repository. This number begins at 1 for the workflow run's first attempt, and increments with each re-run.
132-
"secret_source": "Actions", // string, The source of a secret used in a workflow. Possible values are None, Actions, Dependabot, or Codespaces.
133-
"server_url": setting.AppURL, // string, The URL of the GitHub server. For example: https://github.com.
134-
"sha": sha, // string, The commit SHA that triggered the workflow. The value of this commit SHA depends on the event that triggered the workflow. For more information, see "Events that trigger workflows." For example, ffac537e6cbbf934b08745a378932722df287a53.
135-
"token": t.Token, // string, A token to authenticate on behalf of the GitHub App installed on your repository. This is functionally equivalent to the GITHUB_TOKEN secret. For more information, see "Automatic token authentication."
136-
"triggering_actor": "", // string, The username of the user that initiated the workflow run. If the workflow run is a re-run, this value may differ from github.actor. Any workflow re-runs will use the privileges of github.actor, even if the actor initiating the re-run (github.triggering_actor) has different privileges.
137-
"workflow": t.Job.Run.WorkflowID, // string, The name of the workflow. If the workflow file doesn't specify a name, the value of this property is the full path of the workflow file in the repository.
138-
"workspace": "", // string, The default working directory on the runner for steps, and the default location of your repository when using the checkout action.
139-
140-
// additional contexts
141-
"gitea_default_actions_url": setting.Actions.DefaultActionsURL.URL(),
142-
"gitea_runtime_token": giteaRuntimeToken,
143-
})
69+
ghCtx := actions.GenerateGitContext(t.Job.Run, t.Job)
70+
ghCtx["token"] = t.Token
71+
ghCtx["gitea_runtime_token"] = giteaRuntimeToken
72+
73+
taskContext, err := structpb.NewStruct(ghCtx)
14474
if err != nil {
14575
log.Error("structpb.NewStruct failed: %v", err)
14676
}

services/actions/notifier_helper.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ func handleWorkflows(
332332
continue
333333
}
334334

335+
wfGitCtx := jobparser.ToGitContext(GenerateGitContext(run, nil))
336+
concurrencyGroup, concurrencyCancel := jobparser.InterpolateWorkflowConcurrency(dwf.Concurrency, wfGitCtx, vars)
337+
_, _ = concurrencyGroup, concurrencyCancel
338+
// TODO: check concurrencyGroup and concurrencyCancel
335339
jobs, err := jobparser.Parse(dwf.Content, jobparser.WithVars(vars))
336340
if err != nil {
337341
log.Error("jobparser.Parse: %v", err)

0 commit comments

Comments
 (0)