Skip to content

Commit

Permalink
Use correct has error check for internal responses (#28796) (#28798)
Browse files Browse the repository at this point in the history
Backport #28796 by @wxiaoguang

`resp != nil` doesn't mean the request really succeeded. Add a comment
for requestJSONResp to clarify the behavior.

Co-authored-by: wxiaoguang <[email protected]>
  • Loading branch information
GiteaBot and wxiaoguang authored Jan 15, 2024
1 parent df694f6 commit cbf3666
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion modules/private/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func GenerateActionsRunnerToken(ctx context.Context, scope string) (string, Resp
})

resp, extra := requestJSONResp(req, &responseText{})
if resp == nil {
if extra.HasError() {
return "", extra
}
return resp.Text, extra
Expand Down
2 changes: 1 addition & 1 deletion modules/private/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func AuthorizedPublicKeyByContent(ctx context.Context, content string) (string,
req := newInternalRequest(ctx, reqURL, "POST")
req.Param("content", content)
resp, extra := requestJSONResp(req, &responseText{})
if resp == nil {
if extra.HasError() {
return "", extra
}
return resp.Text, extra
Expand Down
2 changes: 1 addition & 1 deletion modules/private/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func SendEmail(ctx context.Context, subject, message string, to []string) (strin
})

resp, extra := requestJSONResp(req, &responseText{})
if resp == nil {
if extra.HasError() {
return "", extra
}
return resp.Text, extra
Expand Down
1 change: 1 addition & 0 deletions modules/private/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (re responseError) Error() string {
// requestJSONResp sends a request to the gitea server and then parses the response.
// If the status code is not 2xx, or any error occurs, the ResponseExtra.Error field is guaranteed to be non-nil,
// and the ResponseExtra.UserMsg field will be set to a message for the end user.
// 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
Expand Down

0 comments on commit cbf3666

Please sign in to comment.