Skip to content

Commit 364ec73

Browse files
sillyguodongsilverwind
authored andcommitted
Add API for Variables (go-gitea#29520)
close go-gitea#27801 --------- Co-authored-by: silverwind <[email protected]>
1 parent 40cdc84 commit 364ec73

File tree

16 files changed

+2137
-109
lines changed

16 files changed

+2137
-109
lines changed

models/actions/variable.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ package actions
66
import (
77
"context"
88
"errors"
9-
"fmt"
109
"strings"
1110

1211
"code.gitea.io/gitea/models/db"
1312
"code.gitea.io/gitea/modules/log"
1413
"code.gitea.io/gitea/modules/timeutil"
15-
"code.gitea.io/gitea/modules/util"
1614

1715
"xorm.io/builder"
1816
)
@@ -55,24 +53,24 @@ type FindVariablesOpts struct {
5553
db.ListOptions
5654
OwnerID int64
5755
RepoID int64
56+
Name string
5857
}
5958

6059
func (opts FindVariablesOpts) ToConds() builder.Cond {
6160
cond := builder.NewCond()
61+
// Since we now support instance-level variables,
62+
// there is no need to check for null values for `owner_id` and `repo_id`
6263
cond = cond.And(builder.Eq{"owner_id": opts.OwnerID})
6364
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
65+
66+
if opts.Name != "" {
67+
cond = cond.And(builder.Eq{"name": strings.ToUpper(opts.Name)})
68+
}
6469
return cond
6570
}
6671

67-
func GetVariableByID(ctx context.Context, variableID int64) (*ActionVariable, error) {
68-
var variable ActionVariable
69-
has, err := db.GetEngine(ctx).Where("id=?", variableID).Get(&variable)
70-
if err != nil {
71-
return nil, err
72-
} else if !has {
73-
return nil, fmt.Errorf("variable with id %d: %w", variableID, util.ErrNotExist)
74-
}
75-
return &variable, nil
72+
func FindVariables(ctx context.Context, opts FindVariablesOpts) ([]*ActionVariable, error) {
73+
return db.Find[ActionVariable](ctx, opts)
7674
}
7775

7876
func UpdateVariable(ctx context.Context, variable *ActionVariable) (bool, error) {
@@ -84,6 +82,13 @@ func UpdateVariable(ctx context.Context, variable *ActionVariable) (bool, error)
8482
return count != 0, err
8583
}
8684

85+
func DeleteVariable(ctx context.Context, id int64) error {
86+
if _, err := db.DeleteByID[ActionVariable](ctx, id); err != nil {
87+
return err
88+
}
89+
return nil
90+
}
91+
8792
func GetVariablesOfRun(ctx context.Context, run *ActionRun) (map[string]string, error) {
8893
variables := map[string]string{}
8994

modules/structs/variable.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package structs
5+
6+
// CreateVariableOption the option when creating variable
7+
// swagger:model
8+
type CreateVariableOption struct {
9+
// Value of the variable to create
10+
//
11+
// required: true
12+
Value string `json:"value" binding:"Required"`
13+
}
14+
15+
// UpdateVariableOption the option when updating variable
16+
// swagger:model
17+
type UpdateVariableOption struct {
18+
// New name for the variable. If the field is empty, the variable name won't be updated.
19+
Name string `json:"name"`
20+
// Value of the variable to update
21+
//
22+
// required: true
23+
Value string `json:"value" binding:"Required"`
24+
}
25+
26+
// ActionVariable return value of the query API
27+
// swagger:model
28+
type ActionVariable struct {
29+
// the owner to which the variable belongs
30+
OwnerID int64 `json:"owner_id"`
31+
// the repository to which the variable belongs
32+
RepoID int64 `json:"repo_id"`
33+
// the name of the variable
34+
Name string `json:"name"`
35+
// the value of the variable
36+
Data string `json:"data"`
37+
}

modules/util/util.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,12 @@ func IfZero[T comparable](v, def T) T {
221221
}
222222
return v
223223
}
224+
225+
func ReserveLineBreakForTextarea(input string) string {
226+
// Since the content is from a form which is a textarea, the line endings are \r\n.
227+
// It's a standard behavior of HTML.
228+
// But we want to store them as \n like what GitHub does.
229+
// And users are unlikely to really need to keep the \r.
230+
// Other than this, we should respect the original content, even leading or trailing spaces.
231+
return strings.ReplaceAll(input, "\r\n", "\n")
232+
}

modules/util/util_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,8 @@ func TestToPointer(t *testing.T) {
235235
val123 := 123
236236
assert.False(t, &val123 == ToPointer(val123))
237237
}
238+
239+
func TestReserveLineBreakForTextarea(t *testing.T) {
240+
assert.Equal(t, ReserveLineBreakForTextarea("test\r\ndata"), "test\ndata")
241+
assert.Equal(t, ReserveLineBreakForTextarea("test\r\ndata\r\n"), "test\ndata\n")
242+
}

routers/api/v1/api.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,15 @@ func Routes() *web.Route {
955955
Delete(user.DeleteSecret)
956956
})
957957

958+
m.Group("/variables", func() {
959+
m.Get("", user.ListVariables)
960+
m.Combo("/{variablename}").
961+
Get(user.GetVariable).
962+
Delete(user.DeleteVariable).
963+
Post(bind(api.CreateVariableOption{}), user.CreateVariable).
964+
Put(bind(api.UpdateVariableOption{}), user.UpdateVariable)
965+
})
966+
958967
m.Group("/runners", func() {
959968
m.Get("/registration-token", reqToken(), user.GetRegistrationToken)
960969
})
@@ -1073,6 +1082,15 @@ func Routes() *web.Route {
10731082
Delete(reqToken(), reqOwner(), repo.DeleteSecret)
10741083
})
10751084

1085+
m.Group("/variables", func() {
1086+
m.Get("", reqToken(), reqOwner(), repo.ListVariables)
1087+
m.Combo("/{variablename}").
1088+
Get(reqToken(), reqOwner(), repo.GetVariable).
1089+
Delete(reqToken(), reqOwner(), repo.DeleteVariable).
1090+
Post(reqToken(), reqOwner(), bind(api.CreateVariableOption{}), repo.CreateVariable).
1091+
Put(reqToken(), reqOwner(), bind(api.UpdateVariableOption{}), repo.UpdateVariable)
1092+
})
1093+
10761094
m.Group("/runners", func() {
10771095
m.Get("/registration-token", reqToken(), reqOwner(), repo.GetRegistrationToken)
10781096
})
@@ -1452,6 +1470,15 @@ func Routes() *web.Route {
14521470
Delete(reqToken(), reqOrgOwnership(), org.DeleteSecret)
14531471
})
14541472

1473+
m.Group("/variables", func() {
1474+
m.Get("", reqToken(), reqOrgOwnership(), org.ListVariables)
1475+
m.Combo("/{variablename}").
1476+
Get(reqToken(), reqOrgOwnership(), org.GetVariable).
1477+
Delete(reqToken(), reqOrgOwnership(), org.DeleteVariable).
1478+
Post(reqToken(), reqOrgOwnership(), bind(api.CreateVariableOption{}), org.CreateVariable).
1479+
Put(reqToken(), reqOrgOwnership(), bind(api.UpdateVariableOption{}), org.UpdateVariable)
1480+
})
1481+
14551482
m.Group("/runners", func() {
14561483
m.Get("/registration-token", reqToken(), reqOrgOwnership(), org.GetRegistrationToken)
14571484
})

0 commit comments

Comments
 (0)