Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return responseText instead of string in some functions #28836

Merged
merged 3 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion cmd/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion cmd/mailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 2 additions & 6 deletions modules/private/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
6 changes: 3 additions & 3 deletions modules/private/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -130,14 +130,14 @@ 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
}

// SSHLog sends ssh error log response
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
}
10 changes: 3 additions & 7 deletions modules/private/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
8 changes: 2 additions & 6 deletions modules/private/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{})
}
10 changes: 5 additions & 5 deletions modules/private/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down