-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add API for Variables
#29520
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
techknowlogick
merged 25 commits into
go-gitea:main
from
sillyguodong:feat/api_for_variables
Mar 28, 2024
Merged
Add API for Variables
#29520
Changes from 2 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
d20280e
feat: create and update user scope variable
sillyguodong 7fb32af
chore: lint and typo
sillyguodong eacaba1
feat: delete and get user-level var
sillyguodong c0ccc4b
Merge branch 'main' into feat/api_for_variables
sillyguodong a4a0102
chore: generate swagger
sillyguodong ca218ad
feat: org-level var api
sillyguodong b23a836
feat: repo-level var api
sillyguodong e931674
revert Makefile
sillyguodong 6e1548c
lint swagger
sillyguodong a4c633e
chore: add new line at end of swgger file
sillyguodong 512d062
feat: list variables
sillyguodong 73f483c
chore: tab to space
sillyguodong 0dd5c00
Merge branch 'main' into feat/api_for_variables
sillyguodong 473cff7
Merge branch 'main' into feat/api_for_variables
sillyguodong 77624df
Merge branch 'main' into feat/api_for_variables
silverwind 1806ad7
fix: no check null for owner_id and repo_id
sillyguodong ef5f9f5
Merge branch 'main' into feat/api_for_variables
sillyguodong bf807a6
chore: add integration test
sillyguodong da18636
chore: add comment for opts
sillyguodong 6e21046
fix: misspelling
sillyguodong 75d96f1
fix: test case
sillyguodong 48f23c9
chore: mv func to util pkg
sillyguodong dec6da3
chore: re-run actions
sillyguodong a869bcc
Merge branch 'main' into feat/api_for_variables
techknowlogick 1e3224b
Merge branch 'main' into feat/api_for_variables
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
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,21 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package structs | ||
|
||
// CreateVariableOption the option when creating variable | ||
// swagger:model | ||
type CreateVariableOption struct { | ||
// Value of the variable to create | ||
// required: true | ||
Value string `json:"value" binding:"Required"` | ||
} | ||
|
||
// UpdateVariableOption the option when updating variable | ||
type UpdateVariableOption struct { | ||
// New name for the variable. If the field is empty, the variable name won't be updated. | ||
Name string `json:"name"` | ||
// Value of the variable to update | ||
// required: true | ||
Value string `json:"value" binding:"Required"` | ||
} |
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
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
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,78 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package actions | ||
|
||
import ( | ||
"context" | ||
"regexp" | ||
"strings" | ||
|
||
actions_model "code.gitea.io/gitea/models/actions" | ||
"code.gitea.io/gitea/models/db" | ||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/util" | ||
secret_service "code.gitea.io/gitea/services/secrets" | ||
) | ||
|
||
func CreateVariable(ctx context.Context, ownerID, repoID int64, name, data string) (*actions_model.ActionVariable, error) { | ||
if err := secret_service.ValidateName(name); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := envNameCIRegexMatch(name); err != nil { | ||
return nil, err | ||
} | ||
|
||
v, err := actions_model.InsertVariable(ctx, ownerID, repoID, name, ReserveLineBreakForTextarea(data)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return v, nil | ||
} | ||
|
||
func UpdateVariable(ctx context.Context, variableID int64, name, data string) (bool, error) { | ||
if err := secret_service.ValidateName(name); err != nil { | ||
return false, err | ||
} | ||
|
||
if err := envNameCIRegexMatch(name); err != nil { | ||
return false, err | ||
} | ||
|
||
return actions_model.UpdateVariable(ctx, &actions_model.ActionVariable{ | ||
ID: variableID, | ||
Name: strings.ToUpper(name), | ||
Data: ReserveLineBreakForTextarea(data), | ||
}) | ||
} | ||
|
||
func DeleteVariable(ctx context.Context, variableID int64) (int64, error) { | ||
return db.DeleteByBean(ctx, &actions_model.ActionVariable{ID: variableID}) | ||
} | ||
|
||
// some regular expression of `variables` and `secrets` | ||
// reference to: | ||
// https://docs.github.com/en/actions/learn-github-actions/variables#naming-conventions-for-configuration-variables | ||
// https://docs.github.com/en/actions/security-guides/encrypted-secrets#naming-your-secrets | ||
var ( | ||
forbiddenEnvNameCIRx = regexp.MustCompile("(?i)^CI") | ||
) | ||
|
||
func envNameCIRegexMatch(name string) error { | ||
if forbiddenEnvNameCIRx.MatchString(name) { | ||
log.Error("Env Name cannot be ci") | ||
return util.NewInvalidArgumentErrorf("env name cannot be ci") | ||
} | ||
return nil | ||
} | ||
|
||
func ReserveLineBreakForTextarea(input string) string { | ||
// Since the content is from a form which is a textarea, the line endings are \r\n. | ||
// It's a standard behavior of HTML. | ||
// But we want to store them as \n like what GitHub does. | ||
// And users are unlikely to really need to keep the \r. | ||
// Other than this, we should respect the original content, even leading or trailing spaces. | ||
return strings.ReplaceAll(input, "\r\n", "\n") | ||
} | ||
sillyguodong marked this conversation as resolved.
Show resolved
Hide resolved
|
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.