Return `responseText` instead of string in some functions (#28836)
Follow https://github.com/go-gitea/gitea/pull/28796#issuecomment-1891727591
This commit is contained in:
parent
4674aea25b
commit
b60a7c3358
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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{})
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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{})
|
||||
}
|
||||
|
|
|
@ -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{})
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue