mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-23 11:01:49 +01:00
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
8 changed files with 18 additions and 30 deletions
|
@ -50,6 +50,6 @@ func runGenerateActionsRunnerToken(c *cli.Context) error {
|
||||||
if extra.HasError() {
|
if extra.HasError() {
|
||||||
return handleCliResponseExtra(extra)
|
return handleCliResponseExtra(extra)
|
||||||
}
|
}
|
||||||
_, _ = fmt.Printf("%s\n", respText)
|
_, _ = fmt.Printf("%s\n", respText.Text)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,6 +78,6 @@ func runKeys(c *cli.Context) error {
|
||||||
if extra.Error != nil {
|
if extra.Error != nil {
|
||||||
return extra.Error
|
return extra.Error
|
||||||
}
|
}
|
||||||
_, _ = fmt.Fprintln(c.App.Writer, strings.TrimSpace(authorizedString))
|
_, _ = fmt.Fprintln(c.App.Writer, strings.TrimSpace(authorizedString.Text))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,6 +45,6 @@ func runSendMail(c *cli.Context) error {
|
||||||
if extra.HasError() {
|
if extra.HasError() {
|
||||||
return handleCliResponseExtra(extra)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,16 +14,12 @@ type GenerateTokenRequest struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenerateActionsRunnerToken calls the internal GenerateActionsRunnerToken function
|
// 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"
|
reqURL := setting.LocalURL + "api/internal/actions/generate_actions_runner_token"
|
||||||
|
|
||||||
req := newInternalRequest(ctx, reqURL, "POST", GenerateTokenRequest{
|
req := newInternalRequest(ctx, reqURL, "POST", GenerateTokenRequest{
|
||||||
Scope: scope,
|
Scope: scope,
|
||||||
})
|
})
|
||||||
|
|
||||||
resp, extra := requestJSONResp(req, &responseText{})
|
return requestJSONResp(req, &ResponseText{})
|
||||||
if extra.HasError() {
|
|
||||||
return "", extra
|
|
||||||
}
|
|
||||||
return resp.Text, extra
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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))
|
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 := newInternalRequest(ctx, reqURL, "POST", opts)
|
||||||
req.SetReadWriteTimeout(time.Duration(60+len(opts.OldCommitIDs)) * time.Second)
|
req.SetReadWriteTimeout(time.Duration(60+len(opts.OldCommitIDs)) * time.Second)
|
||||||
_, extra := requestJSONResp(req, &responseText{})
|
_, extra := requestJSONResp(req, &ResponseText{})
|
||||||
return extra
|
return extra
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,7 +130,7 @@ func SetDefaultBranch(ctx context.Context, ownerName, repoName, branch string) R
|
||||||
url.PathEscape(branch),
|
url.PathEscape(branch),
|
||||||
)
|
)
|
||||||
req := newInternalRequest(ctx, reqURL, "POST")
|
req := newInternalRequest(ctx, reqURL, "POST")
|
||||||
_, extra := requestJSONResp(req, &responseText{})
|
_, extra := requestJSONResp(req, &ResponseText{})
|
||||||
return extra
|
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 {
|
func SSHLog(ctx context.Context, isErr bool, msg string) error {
|
||||||
reqURL := setting.LocalURL + "api/internal/ssh/log"
|
reqURL := setting.LocalURL + "api/internal/ssh/log"
|
||||||
req := newInternalRequest(ctx, reqURL, "POST", &SSHLogOption{IsError: isErr, Message: msg})
|
req := newInternalRequest(ctx, reqURL, "POST", &SSHLogOption{IsError: isErr, Message: msg})
|
||||||
_, extra := requestJSONResp(req, &responseText{})
|
_, extra := requestJSONResp(req, &ResponseText{})
|
||||||
return extra.Error
|
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.
|
// Ask for running deliver hook and test pull request tasks.
|
||||||
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d/update/%d", keyID, repoID)
|
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d/update/%d", keyID, repoID)
|
||||||
req := newInternalRequest(ctx, reqURL, "POST")
|
req := newInternalRequest(ctx, reqURL, "POST")
|
||||||
_, extra := requestJSONResp(req, &responseText{})
|
_, extra := requestJSONResp(req, &ResponseText{})
|
||||||
return extra.Error
|
return extra.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
// AuthorizedPublicKeyByContent searches content as prefix (leak e-mail part)
|
// AuthorizedPublicKeyByContent searches content as prefix (leak e-mail part)
|
||||||
// and returns public key found.
|
// 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.
|
// Ask for running deliver hook and test pull request tasks.
|
||||||
reqURL := setting.LocalURL + "api/internal/ssh/authorized_keys"
|
reqURL := setting.LocalURL + "api/internal/ssh/authorized_keys"
|
||||||
req := newInternalRequest(ctx, reqURL, "POST")
|
req := newInternalRequest(ctx, reqURL, "POST")
|
||||||
req.Param("content", content)
|
req.Param("content", content)
|
||||||
resp, extra := requestJSONResp(req, &responseText{})
|
return requestJSONResp(req, &ResponseText{})
|
||||||
if extra.HasError() {
|
|
||||||
return "", extra
|
|
||||||
}
|
|
||||||
return resp.Text, extra
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ type Email struct {
|
||||||
// It accepts a list of usernames.
|
// It accepts a list of usernames.
|
||||||
// If DB contains these users it will send the email to them.
|
// 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
|
// 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"
|
reqURL := setting.LocalURL + "api/internal/mail/send"
|
||||||
|
|
||||||
req := newInternalRequest(ctx, reqURL, "POST", Email{
|
req := newInternalRequest(ctx, reqURL, "POST", Email{
|
||||||
|
@ -29,9 +29,5 @@ func SendEmail(ctx context.Context, subject, message string, to []string) (strin
|
||||||
To: to,
|
To: to,
|
||||||
})
|
})
|
||||||
|
|
||||||
resp, extra := requestJSONResp(req, &responseText{})
|
return requestJSONResp(req, &ResponseText{})
|
||||||
if extra.HasError() {
|
|
||||||
return "", extra
|
|
||||||
}
|
|
||||||
return resp.Text, extra
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,8 +12,8 @@ import (
|
||||||
"code.gitea.io/gitea/modules/json"
|
"code.gitea.io/gitea/modules/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
// responseText is used to get the response as text, instead of parsing it as JSON.
|
// ResponseText is used to get the response as text, instead of parsing it as JSON.
|
||||||
type responseText struct {
|
type ResponseText struct {
|
||||||
Text string
|
Text string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,7 +50,7 @@ func (re responseError) Error() string {
|
||||||
// Caller should check the ResponseExtra.HasError() first to see whether the request fails.
|
// 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 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
|
// * 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) {
|
func requestJSONResp[T any](req *httplib.Request, res *T) (ret *T, extra ResponseExtra) {
|
||||||
resp, err := req.Response()
|
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
|
// now, the StatusCode must be 2xx
|
||||||
var v any = res
|
var v any = res
|
||||||
if respText, ok := v.(*responseText); ok {
|
if respText, ok := v.(*ResponseText); ok {
|
||||||
// get the whole response as a text string
|
// get the whole response as a text string
|
||||||
bs, err := io.ReadAll(resp.Body)
|
bs, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
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
|
// 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.
|
// If the request succeeds (200), the argument clientSuccessMsg will be used as ResponseExtra.UserMsg.
|
||||||
func requestJSONClientMsg(req *httplib.Request, clientSuccessMsg string) ResponseExtra {
|
func requestJSONClientMsg(req *httplib.Request, clientSuccessMsg string) ResponseExtra {
|
||||||
_, extra := requestJSONResp(req, &responseText{})
|
_, extra := requestJSONResp(req, &ResponseText{})
|
||||||
if extra.HasError() {
|
if extra.HasError() {
|
||||||
return extra
|
return extra
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue