Skip to content

Commit f79f6a2

Browse files
appleboyKN4CK3R
andauthored
feat(API): add routes and functions for managing user's secrets (#26909)
- Add routes for creating or updating a user's actions secrets in `routers/api/v1/api.go` - Add a new file `routers/api/v1/user/action.go` with functions for creating or updating a user's secrets and deleting a user's secret - Modify the `templates/swagger/v1_json.tmpl` file to include the routes for creating or updating a user's secrets and deleting a user's secret --------- Signed-off-by: Bo-Yi Wu <[email protected]> Co-authored-by: KN4CK3R <[email protected]>
1 parent f064d71 commit f79f6a2

File tree

3 files changed

+190
-0
lines changed

3 files changed

+190
-0
lines changed

routers/api/v1/api.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,13 @@ func Routes() *web.Route {
836836
Post(bind(api.CreateEmailOption{}), user.AddEmail).
837837
Delete(bind(api.DeleteEmailOption{}), user.DeleteEmail)
838838

839+
// create or update a user's actions secrets
840+
m.Group("/actions/secrets", func() {
841+
m.Combo("/{secretname}").
842+
Put(bind(api.CreateOrUpdateSecretOption{}), user.CreateOrUpdateSecret).
843+
Delete(repo.DeleteSecret)
844+
})
845+
839846
m.Get("/followers", user.ListMyFollowers)
840847
m.Group("/following", func() {
841848
m.Get("", user.ListMyFollowing)

routers/api/v1/user/action.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package user
5+
6+
import (
7+
"net/http"
8+
9+
secret_model "code.gitea.io/gitea/models/secret"
10+
"code.gitea.io/gitea/modules/context"
11+
api "code.gitea.io/gitea/modules/structs"
12+
"code.gitea.io/gitea/modules/web"
13+
"code.gitea.io/gitea/routers/web/shared/actions"
14+
)
15+
16+
// create or update one secret of the user scope
17+
func CreateOrUpdateSecret(ctx *context.APIContext) {
18+
// swagger:operation PUT /user/actions/secrets/{secretname} user updateUserSecret
19+
// ---
20+
// summary: Create or Update a secret value in a user scope
21+
// consumes:
22+
// - application/json
23+
// produces:
24+
// - application/json
25+
// parameters:
26+
// - name: secretname
27+
// in: path
28+
// description: name of the secret
29+
// type: string
30+
// required: true
31+
// - name: body
32+
// in: body
33+
// schema:
34+
// "$ref": "#/definitions/CreateOrUpdateSecretOption"
35+
// responses:
36+
// "201":
37+
// description: response when creating a secret
38+
// "204":
39+
// description: response when updating a secret
40+
// "400":
41+
// "$ref": "#/responses/error"
42+
// "404":
43+
// "$ref": "#/responses/notFound"
44+
45+
secretName := ctx.Params(":secretname")
46+
if err := actions.NameRegexMatch(secretName); err != nil {
47+
ctx.Error(http.StatusBadRequest, "CreateOrUpdateSecret", err)
48+
return
49+
}
50+
opt := web.GetForm(ctx).(*api.CreateOrUpdateSecretOption)
51+
isCreated, err := secret_model.CreateOrUpdateSecret(ctx, ctx.Doer.ID, 0, secretName, opt.Data)
52+
if err != nil {
53+
ctx.Error(http.StatusInternalServerError, "CreateOrUpdateSecret", err)
54+
return
55+
}
56+
if isCreated {
57+
ctx.Status(http.StatusCreated)
58+
return
59+
}
60+
61+
ctx.Status(http.StatusNoContent)
62+
}
63+
64+
// DeleteSecret delete one secret of the user scope
65+
func DeleteSecret(ctx *context.APIContext) {
66+
// swagger:operation DELETE /user/actions/secrets/{secretname} user deleteUserSecret
67+
// ---
68+
// summary: Delete a secret in a user scope
69+
// consumes:
70+
// - application/json
71+
// produces:
72+
// - application/json
73+
// parameters:
74+
// - name: secretname
75+
// in: path
76+
// description: name of the secret
77+
// type: string
78+
// required: true
79+
// responses:
80+
// "204":
81+
// description: delete one secret of the user
82+
// "400":
83+
// "$ref": "#/responses/error"
84+
// "404":
85+
// "$ref": "#/responses/notFound"
86+
87+
secretName := ctx.Params(":secretname")
88+
if err := actions.NameRegexMatch(secretName); err != nil {
89+
ctx.Error(http.StatusBadRequest, "DeleteSecret", err)
90+
return
91+
}
92+
err := secret_model.DeleteSecret(
93+
ctx, ctx.Doer.ID, 0, secretName,
94+
)
95+
if secret_model.IsErrSecretNotFound(err) {
96+
ctx.NotFound(err)
97+
return
98+
}
99+
if err != nil {
100+
ctx.Error(http.StatusInternalServerError, "DeleteSecret", err)
101+
return
102+
}
103+
104+
ctx.Status(http.StatusNoContent)
105+
}

templates/swagger/v1_json.tmpl

Lines changed: 78 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)