-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Make runs-on support variable expression #29468
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
Merged
lunny
merged 8 commits into
go-gitea:main
from
sillyguodong:feat/support_vars_in_runs_on
Mar 8, 2024
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c7c62c4
Make runs-on support variable expression
sillyguodong 14f2606
move to service pkg
sillyguodong 93982f5
chore: mv function
sillyguodong 43651df
chore: upgrade go mod
sillyguodong eba48db
chore: return err when get vars and secrets
sillyguodong 194d74f
chore: return err and lint error
sillyguodong 1590032
Merge branch 'main' into feat/support_vars_in_runs_on
GiteaBot 4606c8b
Merge branch 'main' into feat/support_vars_in_runs_on
GiteaBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package actions | ||
|
||
import ( | ||
"context" | ||
|
||
actions_model "code.gitea.io/gitea/models/actions" | ||
"code.gitea.io/gitea/models/db" | ||
secret_model "code.gitea.io/gitea/models/secret" | ||
"code.gitea.io/gitea/modules/log" | ||
secret_module "code.gitea.io/gitea/modules/secret" | ||
"code.gitea.io/gitea/modules/setting" | ||
) | ||
|
||
func GetSecretsOfTask(ctx context.Context, task *actions_model.ActionTask) map[string]string { | ||
secrets := map[string]string{} | ||
|
||
secrets["GITHUB_TOKEN"] = task.Token | ||
secrets["GITEA_TOKEN"] = task.Token | ||
|
||
if task.Job.Run.IsForkPullRequest && task.Job.Run.TriggerEvent != GithubEventPullRequestTarget { | ||
// ignore secrets for fork pull request, except GITHUB_TOKEN and GITEA_TOKEN which are automatically generated. | ||
// for the tasks triggered by pull_request_target event, they could access the secrets because they will run in the context of the base branch | ||
// see the documentation: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target | ||
return secrets | ||
} | ||
|
||
ownerSecrets, err := db.Find[secret_model.Secret](ctx, secret_model.FindSecretsOptions{OwnerID: task.Job.Run.Repo.OwnerID}) | ||
if err != nil { | ||
log.Error("find secrets of owner %v: %v", task.Job.Run.Repo.OwnerID, err) | ||
// go on | ||
} | ||
repoSecrets, err := db.Find[secret_model.Secret](ctx, secret_model.FindSecretsOptions{RepoID: task.Job.Run.RepoID}) | ||
if err != nil { | ||
log.Error("find secrets of repo %v: %v", task.Job.Run.RepoID, err) | ||
// go on | ||
} | ||
|
||
for _, secret := range append(ownerSecrets, repoSecrets...) { | ||
if v, err := secret_module.DecryptSecret(setting.SecretKey, secret.Data); err != nil { | ||
log.Error("decrypt secret %v %q: %v", secret.ID, secret.Name, err) | ||
// go on | ||
} else { | ||
secrets[secret.Name] = v | ||
} | ||
} | ||
|
||
return secrets | ||
} | ||
|
||
func GetVariablesOfRun(ctx context.Context, run *actions_model.ActionRun) map[string]string { | ||
variables := map[string]string{} | ||
|
||
// Global | ||
globalVariables, err := db.Find[actions_model.ActionVariable](ctx, actions_model.FindVariablesOpts{}) | ||
if err != nil { | ||
log.Error("find global variables: %v", err) | ||
} | ||
|
||
// Org / User level | ||
ownerVariables, err := db.Find[actions_model.ActionVariable](ctx, actions_model.FindVariablesOpts{OwnerID: run.Repo.OwnerID}) | ||
if err != nil { | ||
log.Error("find variables of org: %d, error: %v", run.Repo.OwnerID, err) | ||
} | ||
|
||
// Repo level | ||
repoVariables, err := db.Find[actions_model.ActionVariable](ctx, actions_model.FindVariablesOpts{RepoID: run.RepoID}) | ||
if err != nil { | ||
log.Error("find variables of repo: %d, error: %v", run.RepoID, err) | ||
} | ||
|
||
// Level precedence: Repo > Org / User > Global | ||
for _, v := range append(globalVariables, append(ownerVariables, repoVariables...)...) { | ||
variables[v.Name] = v.Data | ||
} | ||
|
||
return variables | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.