Skip to content

Commit 66870e2

Browse files
yp05327silverwind
authored andcommitted
Return responseText instead of string in some functions (go-gitea#28836)
Follow go-gitea#28796 (comment)
1 parent 8816956 commit 66870e2

File tree

8 files changed

+18
-30
lines changed

8 files changed

+18
-30
lines changed

Diff for: cmd/actions.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ func runGenerateActionsRunnerToken(c *cli.Context) error {
5050
if extra.HasError() {
5151
return handleCliResponseExtra(extra)
5252
}
53-
_, _ = fmt.Printf("%s\n", respText)
53+
_, _ = fmt.Printf("%s\n", respText.Text)
5454
return nil
5555
}

Diff for: cmd/keys.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ func runKeys(c *cli.Context) error {
7878
if extra.Error != nil {
7979
return extra.Error
8080
}
81-
_, _ = fmt.Fprintln(c.App.Writer, strings.TrimSpace(authorizedString))
81+
_, _ = fmt.Fprintln(c.App.Writer, strings.TrimSpace(authorizedString.Text))
8282
return nil
8383
}

Diff for: cmd/mailer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ func runSendMail(c *cli.Context) error {
4545
if extra.HasError() {
4646
return handleCliResponseExtra(extra)
4747
}
48-
_, _ = fmt.Printf("Sent %s email(s) to all users\n", respText)
48+
_, _ = fmt.Printf("Sent %s email(s) to all users\n", respText.Text)
4949
return nil
5050
}

Diff for: modules/private/actions.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,12 @@ type GenerateTokenRequest struct {
1414
}
1515

1616
// GenerateActionsRunnerToken calls the internal GenerateActionsRunnerToken function
17-
func GenerateActionsRunnerToken(ctx context.Context, scope string) (string, ResponseExtra) {
17+
func GenerateActionsRunnerToken(ctx context.Context, scope string) (*ResponseText, ResponseExtra) {
1818
reqURL := setting.LocalURL + "api/internal/actions/generate_actions_runner_token"
1919

2020
req := newInternalRequest(ctx, reqURL, "POST", GenerateTokenRequest{
2121
Scope: scope,
2222
})
2323

24-
resp, extra := requestJSONResp(req, &responseText{})
25-
if extra.HasError() {
26-
return "", extra
27-
}
28-
return resp.Text, extra
24+
return requestJSONResp(req, &ResponseText{})
2925
}

Diff for: modules/private/hook.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func HookPreReceive(ctx context.Context, ownerName, repoName string, opts HookOp
101101
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/pre-receive/%s/%s", url.PathEscape(ownerName), url.PathEscape(repoName))
102102
req := newInternalRequest(ctx, reqURL, "POST", opts)
103103
req.SetReadWriteTimeout(time.Duration(60+len(opts.OldCommitIDs)) * time.Second)
104-
_, extra := requestJSONResp(req, &responseText{})
104+
_, extra := requestJSONResp(req, &ResponseText{})
105105
return extra
106106
}
107107

@@ -130,14 +130,14 @@ func SetDefaultBranch(ctx context.Context, ownerName, repoName, branch string) R
130130
url.PathEscape(branch),
131131
)
132132
req := newInternalRequest(ctx, reqURL, "POST")
133-
_, extra := requestJSONResp(req, &responseText{})
133+
_, extra := requestJSONResp(req, &ResponseText{})
134134
return extra
135135
}
136136

137137
// SSHLog sends ssh error log response
138138
func SSHLog(ctx context.Context, isErr bool, msg string) error {
139139
reqURL := setting.LocalURL + "api/internal/ssh/log"
140140
req := newInternalRequest(ctx, reqURL, "POST", &SSHLogOption{IsError: isErr, Message: msg})
141-
_, extra := requestJSONResp(req, &responseText{})
141+
_, extra := requestJSONResp(req, &ResponseText{})
142142
return extra.Error
143143
}

Diff for: modules/private/key.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,16 @@ func UpdatePublicKeyInRepo(ctx context.Context, keyID, repoID int64) error {
1515
// Ask for running deliver hook and test pull request tasks.
1616
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d/update/%d", keyID, repoID)
1717
req := newInternalRequest(ctx, reqURL, "POST")
18-
_, extra := requestJSONResp(req, &responseText{})
18+
_, extra := requestJSONResp(req, &ResponseText{})
1919
return extra.Error
2020
}
2121

2222
// AuthorizedPublicKeyByContent searches content as prefix (leak e-mail part)
2323
// and returns public key found.
24-
func AuthorizedPublicKeyByContent(ctx context.Context, content string) (string, ResponseExtra) {
24+
func AuthorizedPublicKeyByContent(ctx context.Context, content string) (*ResponseText, ResponseExtra) {
2525
// Ask for running deliver hook and test pull request tasks.
2626
reqURL := setting.LocalURL + "api/internal/ssh/authorized_keys"
2727
req := newInternalRequest(ctx, reqURL, "POST")
2828
req.Param("content", content)
29-
resp, extra := requestJSONResp(req, &responseText{})
30-
if extra.HasError() {
31-
return "", extra
32-
}
33-
return resp.Text, extra
29+
return requestJSONResp(req, &ResponseText{})
3430
}

Diff for: modules/private/mail.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Email struct {
2020
// It accepts a list of usernames.
2121
// If DB contains these users it will send the email to them.
2222
// If to list == nil, it's supposed to send emails to every user present in DB
23-
func SendEmail(ctx context.Context, subject, message string, to []string) (string, ResponseExtra) {
23+
func SendEmail(ctx context.Context, subject, message string, to []string) (*ResponseText, ResponseExtra) {
2424
reqURL := setting.LocalURL + "api/internal/mail/send"
2525

2626
req := newInternalRequest(ctx, reqURL, "POST", Email{
@@ -29,9 +29,5 @@ func SendEmail(ctx context.Context, subject, message string, to []string) (strin
2929
To: to,
3030
})
3131

32-
resp, extra := requestJSONResp(req, &responseText{})
33-
if extra.HasError() {
34-
return "", extra
35-
}
36-
return resp.Text, extra
32+
return requestJSONResp(req, &ResponseText{})
3733
}

Diff for: modules/private/request.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"code.gitea.io/gitea/modules/json"
1313
)
1414

15-
// responseText is used to get the response as text, instead of parsing it as JSON.
16-
type responseText struct {
15+
// ResponseText is used to get the response as text, instead of parsing it as JSON.
16+
type ResponseText struct {
1717
Text string
1818
}
1919

@@ -50,7 +50,7 @@ func (re responseError) Error() string {
5050
// Caller should check the ResponseExtra.HasError() first to see whether the request fails.
5151
//
5252
// * If the "res" is a struct pointer, the response will be parsed as JSON
53-
// * If the "res" is responseText pointer, the response will be stored as text in it
53+
// * If the "res" is ResponseText pointer, the response will be stored as text in it
5454
// * If the "res" is responseCallback pointer, the callback function should set the ResponseExtra fields accordingly
5555
func requestJSONResp[T any](req *httplib.Request, res *T) (ret *T, extra ResponseExtra) {
5656
resp, err := req.Response()
@@ -81,7 +81,7 @@ func requestJSONResp[T any](req *httplib.Request, res *T) (ret *T, extra Respons
8181

8282
// now, the StatusCode must be 2xx
8383
var v any = res
84-
if respText, ok := v.(*responseText); ok {
84+
if respText, ok := v.(*ResponseText); ok {
8585
// get the whole response as a text string
8686
bs, err := io.ReadAll(resp.Body)
8787
if err != nil {
@@ -119,7 +119,7 @@ func requestJSONResp[T any](req *httplib.Request, res *T) (ret *T, extra Respons
119119
// requestJSONClientMsg sends a request to the gitea server, server only responds text message status=200 with "success" body
120120
// If the request succeeds (200), the argument clientSuccessMsg will be used as ResponseExtra.UserMsg.
121121
func requestJSONClientMsg(req *httplib.Request, clientSuccessMsg string) ResponseExtra {
122-
_, extra := requestJSONResp(req, &responseText{})
122+
_, extra := requestJSONResp(req, &ResponseText{})
123123
if extra.HasError() {
124124
return extra
125125
}

0 commit comments

Comments
 (0)