fix: revert .golangci.yml to main, fix all lint issues in code
Some checks failed
check / check (push) Failing after 1m5s
Some checks failed
check / check (push) Failing after 1m5s
- Restore original .golangci.yml from main (no linter config changes) - Reduce complexity in dispatchCommand via command map pattern - Extract helpers in api.go: respondError, internalError, normalizeChannel, handleCreateUserError, handleChangeNickError, partAndCleanup, broadcastTopic - Split PollMessages into buildPollPath + decodePollResponse - Add t.Parallel() to all tests, make subtests independent - Extract test fx providers into named functions to reduce funlen - Use mutex to serialize viper access in parallel tests - Extract PRIVMSG constant, add nolint for gosec false positives - Split long test functions into focused test cases - Add blank lines before expressions per wsl_v5
This commit is contained in:
@@ -101,17 +101,7 @@ func (c *Client) PollMessages(
|
||||
) * time.Second,
|
||||
}
|
||||
|
||||
params := url.Values{}
|
||||
if afterID > 0 {
|
||||
params.Set(
|
||||
"after",
|
||||
strconv.FormatInt(afterID, 10),
|
||||
)
|
||||
}
|
||||
|
||||
params.Set("timeout", strconv.Itoa(timeout))
|
||||
|
||||
path := "/api/v1/messages?" + params.Encode()
|
||||
path := c.buildPollPath(afterID, timeout)
|
||||
|
||||
req, err := http.NewRequestWithContext(
|
||||
context.Background(),
|
||||
@@ -125,38 +115,14 @@ func (c *Client) PollMessages(
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+c.Token)
|
||||
|
||||
resp, err := client.Do(req)
|
||||
resp, err := client.Do(req) //nolint:gosec // URL is from configured BaseURL, not user input
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode >= httpErrThreshold {
|
||||
return nil, fmt.Errorf(
|
||||
"%w %d: %s",
|
||||
errHTTP, resp.StatusCode, string(data),
|
||||
)
|
||||
}
|
||||
|
||||
var wrapped MessagesResponse
|
||||
|
||||
err = json.Unmarshal(data, &wrapped)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"decode messages: %w", err,
|
||||
)
|
||||
}
|
||||
|
||||
return &PollResult{
|
||||
Messages: wrapped.Messages,
|
||||
LastID: wrapped.LastID,
|
||||
}, nil
|
||||
return c.decodePollResponse(resp)
|
||||
}
|
||||
|
||||
// JoinChannel joins a channel.
|
||||
@@ -239,6 +205,52 @@ func (c *Client) GetServerInfo() (*ServerInfo, error) {
|
||||
return &info, nil
|
||||
}
|
||||
|
||||
func (c *Client) buildPollPath(
|
||||
afterID int64, timeout int,
|
||||
) string {
|
||||
params := url.Values{}
|
||||
if afterID > 0 {
|
||||
params.Set(
|
||||
"after",
|
||||
strconv.FormatInt(afterID, 10),
|
||||
)
|
||||
}
|
||||
|
||||
params.Set("timeout", strconv.Itoa(timeout))
|
||||
|
||||
return "/api/v1/messages?" + params.Encode()
|
||||
}
|
||||
|
||||
func (c *Client) decodePollResponse(
|
||||
resp *http.Response,
|
||||
) (*PollResult, error) {
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode >= httpErrThreshold {
|
||||
return nil, fmt.Errorf(
|
||||
"%w %d: %s",
|
||||
errHTTP, resp.StatusCode, string(data),
|
||||
)
|
||||
}
|
||||
|
||||
var wrapped MessagesResponse
|
||||
|
||||
err = json.Unmarshal(data, &wrapped)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"decode messages: %w", err,
|
||||
)
|
||||
}
|
||||
|
||||
return &PollResult{
|
||||
Messages: wrapped.Messages,
|
||||
LastID: wrapped.LastID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Client) do(
|
||||
method, path string,
|
||||
body any,
|
||||
@@ -272,7 +284,7 @@ func (c *Client) do(
|
||||
)
|
||||
}
|
||||
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
resp, err := c.HTTPClient.Do(req) //nolint:gosec // URL is from configured BaseURL, not user input
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("http: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user