aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoryp05327 <576951401@qq.com>2024-01-19 11:45:23 +0900
committerGitHub <noreply@github.com>2024-01-19 10:45:23 +0800
commitb60a7c3358cdeec3e0a731b68be33b6ba63a6563 (patch)
tree3211199f510d1f9ea2318bb596fab02262058014
parent4674aea25b54baf08594c54f061dee9e44190f02 (diff)
downloadgitea-b60a7c3358cdeec3e0a731b68be33b6ba63a6563.tar.gz
gitea-b60a7c3358cdeec3e0a731b68be33b6ba63a6563.zip
Return `responseText` instead of string in some functions (#28836)
Follow https://github.com/go-gitea/gitea/pull/28796#issuecomment-1891727591
-rw-r--r--cmd/actions.go2
-rw-r--r--cmd/keys.go2
-rw-r--r--cmd/mailer.go2
-rw-r--r--modules/private/actions.go8
-rw-r--r--modules/private/hook.go6
-rw-r--r--modules/private/key.go10
-rw-r--r--modules/private/mail.go8
-rw-r--r--modules/private/request.go10
8 files changed, 18 insertions, 30 deletions
diff --git a/cmd/actions.go b/cmd/actions.go
index 275fd7904e..f582c16c81 100644
--- a/cmd/actions.go
+++ b/cmd/actions.go
@@ -50,6 +50,6 @@ func runGenerateActionsRunnerToken(c *cli.Context) error {
if extra.HasError() {
return handleCliResponseExtra(extra)
}
- _, _ = fmt.Printf("%s\n", respText)
+ _, _ = fmt.Printf("%s\n", respText.Text)
return nil
}
diff --git a/cmd/keys.go b/cmd/keys.go
index 9d5278f109..ceeec48486 100644
--- a/cmd/keys.go
+++ b/cmd/keys.go
@@ -78,6 +78,6 @@ func runKeys(c *cli.Context) error {
if extra.Error != nil {
return extra.Error
}
- _, _ = fmt.Fprintln(c.App.Writer, strings.TrimSpace(authorizedString))
+ _, _ = fmt.Fprintln(c.App.Writer, strings.TrimSpace(authorizedString.Text))
return nil
}
diff --git a/cmd/mailer.go b/cmd/mailer.go
index 646330e85a..0c5f2c8c8d 100644
--- a/cmd/mailer.go
+++ b/cmd/mailer.go
@@ -45,6 +45,6 @@ func runSendMail(c *cli.Context) error {
if extra.HasError() {
return handleCliResponseExtra(extra)
}
- _, _ = fmt.Printf("Sent %s email(s) to all users\n", respText)
+ _, _ = fmt.Printf("Sent %s email(s) to all users\n", respText.Text)
return nil
}
diff --git a/modules/private/actions.go b/modules/private/actions.go
index a22833632e..311a283650 100644
--- a/modules/private/actions.go
+++ b/modules/private/actions.go
@@ -14,16 +14,12 @@ type GenerateTokenRequest struct {
}
// GenerateActionsRunnerToken calls the internal GenerateActionsRunnerToken function
-func GenerateActionsRunnerToken(ctx context.Context, scope string) (string, ResponseExtra) {
+func GenerateActionsRunnerToken(ctx context.Context, scope string) (*ResponseText, ResponseExtra) {
reqURL := setting.LocalURL + "api/internal/actions/generate_actions_runner_token"
req := newInternalRequest(ctx, reqURL, "POST", GenerateTokenRequest{
Scope: scope,
})
- resp, extra := requestJSONResp(req, &responseText{})
- if extra.HasError() {
- return "", extra
- }
- return resp.Text, extra
+ return requestJSONResp(req, &ResponseText{})
}
diff --git a/modules/private/hook.go b/modules/private/hook.go
index 23e03896e4..cab8c81224 100644
--- a/modules/private/hook.go
+++ b/modules/private/hook.go
@@ -101,7 +101,7 @@ func HookPreReceive(ctx context.Context, ownerName, repoName string, opts HookOp
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/pre-receive/%s/%s", url.PathEscape(ownerName), url.PathEscape(repoName))
req := newInternalRequest(ctx, reqURL, "POST", opts)
req.SetReadWriteTimeout(time.Duration(60+len(opts.OldCommitIDs)) * time.Second)
- _, extra := requestJSONResp(req, &responseText{})
+ _, extra := requestJSONResp(req, &ResponseText{})
return extra
}
@@ -130,7 +130,7 @@ func SetDefaultBranch(ctx context.Context, ownerName, repoName, branch string) R
url.PathEscape(branch),
)
req := newInternalRequest(ctx, reqURL, "POST")
- _, extra := requestJSONResp(req, &responseText{})
+ _, extra := requestJSONResp(req, &ResponseText{})
return extra
}
@@ -138,6 +138,6 @@ func SetDefaultBranch(ctx context.Context, ownerName, repoName, branch string) R
func SSHLog(ctx context.Context, isErr bool, msg string) error {
reqURL := setting.LocalURL + "api/internal/ssh/log"
req := newInternalRequest(ctx, reqURL, "POST", &SSHLogOption{IsError: isErr, Message: msg})
- _, extra := requestJSONResp(req, &responseText{})
+ _, extra := requestJSONResp(req, &ResponseText{})
return extra.Error
}
diff --git a/modules/private/key.go b/modules/private/key.go
index 08762bd401..dcd1714856 100644
--- a/modules/private/key.go
+++ b/modules/private/key.go
@@ -15,20 +15,16 @@ func UpdatePublicKeyInRepo(ctx context.Context, keyID, repoID int64) error {
// Ask for running deliver hook and test pull request tasks.
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d/update/%d", keyID, repoID)
req := newInternalRequest(ctx, reqURL, "POST")
- _, extra := requestJSONResp(req, &responseText{})
+ _, extra := requestJSONResp(req, &ResponseText{})
return extra.Error
}
// AuthorizedPublicKeyByContent searches content as prefix (leak e-mail part)
// and returns public key found.
-func AuthorizedPublicKeyByContent(ctx context.Context, content string) (string, ResponseExtra) {
+func AuthorizedPublicKeyByContent(ctx context.Context, content string) (*ResponseText, ResponseExtra) {
// Ask for running deliver hook and test pull request tasks.
reqURL := setting.LocalURL + "api/internal/ssh/authorized_keys"
req := newInternalRequest(ctx, reqURL, "POST")
req.Param("content", content)
- resp, extra := requestJSONResp(req, &responseText{})
- if extra.HasError() {
- return "", extra
- }
- return resp.Text, extra
+ return requestJSONResp(req, &ResponseText{})
}
diff --git a/modules/private/mail.go b/modules/private/mail.go
index ac55d6fe4d..08de5b7e28 100644
--- a/modules/private/mail.go
+++ b/modules/private/mail.go
@@ -20,7 +20,7 @@ type Email struct {
// It accepts a list of usernames.
// If DB contains these users it will send the email to them.
// If to list == nil, it's supposed to send emails to every user present in DB
-func SendEmail(ctx context.Context, subject, message string, to []string) (string, ResponseExtra) {
+func SendEmail(ctx context.Context, subject, message string, to []string) (*ResponseText, ResponseExtra) {
reqURL := setting.LocalURL + "api/internal/mail/send"
req := newInternalRequest(ctx, reqURL, "POST", Email{
@@ -29,9 +29,5 @@ func SendEmail(ctx context.Context, subject, message string, to []string) (strin
To: to,
})
- resp, extra := requestJSONResp(req, &responseText{})
- if extra.HasError() {
- return "", extra
- }
- return resp.Text, extra
+ return requestJSONResp(req, &ResponseText{})
}
diff --git a/modules/private/request.go b/modules/private/request.go
index 2bc43b972d..58cd261239 100644
--- a/modules/private/request.go
+++ b/modules/private/request.go
@@ -12,8 +12,8 @@ import (
"code.gitea.io/gitea/modules/json"
)
-// responseText is used to get the response as text, instead of parsing it as JSON.
-type responseText struct {
+// ResponseText is used to get the response as text, instead of parsing it as JSON.
+type ResponseText struct {
Text string
}
@@ -50,7 +50,7 @@ func (re responseError) Error() string {
// Caller should check the ResponseExtra.HasError() first to see whether the request fails.
//
// * If the "res" is a struct pointer, the response will be parsed as JSON
-// * If the "res" is responseText pointer, the response will be stored as text in it
+// * If the "res" is ResponseText pointer, the response will be stored as text in it
// * If the "res" is responseCallback pointer, the callback function should set the ResponseExtra fields accordingly
func requestJSONResp[T any](req *httplib.Request, res *T) (ret *T, extra ResponseExtra) {
resp, err := req.Response()
@@ -81,7 +81,7 @@ func requestJSONResp[T any](req *httplib.Request, res *T) (ret *T, extra Respons
// now, the StatusCode must be 2xx
var v any = res
- if respText, ok := v.(*responseText); ok {
+ if respText, ok := v.(*ResponseText); ok {
// get the whole response as a text string
bs, err := io.ReadAll(resp.Body)
if err != nil {
@@ -119,7 +119,7 @@ func requestJSONResp[T any](req *httplib.Request, res *T) (ret *T, extra Respons
// requestJSONClientMsg sends a request to the gitea server, server only responds text message status=200 with "success" body
// If the request succeeds (200), the argument clientSuccessMsg will be used as ResponseExtra.UserMsg.
func requestJSONClientMsg(req *httplib.Request, clientSuccessMsg string) ResponseExtra {
- _, extra := requestJSONResp(req, &responseText{})
+ _, extra := requestJSONResp(req, &ResponseText{})
if extra.HasError() {
return extra
}