chore(): increase test coverage and update dependencies
Some checks are pending
ci/woodpecker/pr/pr Pipeline is pending

This commit is contained in:
2025-12-18 23:12:27 +01:00
parent e779c5a38b
commit 2d1aa62c61
15 changed files with 384 additions and 74 deletions

View File

@@ -3,7 +3,6 @@ package woodpecker
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
@@ -18,7 +17,7 @@ func DecomAgent(cfg *config.Config, agentId int64) error {
apiRoute := fmt.Sprintf("%s/api/agents/%d", cfg.WoodpeckerInstance, agentId)
req, err := http.NewRequest("DELETE", apiRoute, nil)
if err != nil {
return errors.New(fmt.Sprintf("Could not create delete request: %s", err.Error()))
return fmt.Errorf("Could not create delete request: %s", err.Error())
}
req.Header.Set("Accept", "text/plain")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.WoodpeckerApiToken))
@@ -29,7 +28,7 @@ func DecomAgent(cfg *config.Config, agentId int64) error {
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.New(fmt.Sprintf("Could not delete agent: %s", err.Error()))
return fmt.Errorf("Could not delete agent: %s", err.Error())
}
defer resp.Body.Close()
return nil
@@ -39,24 +38,24 @@ func GetAgentIdByName(cfg *config.Config, name string) (int, error) {
apiRoute := fmt.Sprintf("%s/api/agents?page=1&perPage=100", cfg.WoodpeckerInstance)
req, err := http.NewRequest(http.MethodGet, apiRoute, nil)
if err != nil {
return 0, errors.New(fmt.Sprintf("Could not create agent query request: %s", err.Error()))
return 0, fmt.Errorf("Could not create agent query request: %s", err.Error())
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.WoodpeckerApiToken))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return 0, errors.New(fmt.Sprintf("Could not query agent list: %s", err.Error()))
return 0, fmt.Errorf("Could not query agent list: %s", err.Error())
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return 0, errors.New(fmt.Sprintf("Invalid status code from API: %d", resp.StatusCode))
return 0, fmt.Errorf("Invalid status code from API: %d", resp.StatusCode)
}
agentList := new(models.AgentList)
err = json.NewDecoder(resp.Body).Decode(agentList)
if err != nil {
return 0, errors.New(fmt.Sprintf("Could not unmarshal api response: %s", err.Error()))
return 0, fmt.Errorf("Could not unmarshal api response: %s", err.Error())
}
for _, agent := range agentList.Agents {
@@ -67,7 +66,7 @@ func GetAgentIdByName(cfg *config.Config, name string) (int, error) {
return int(agent.ID), nil
}
}
return 0, errors.New(fmt.Sprintf("Agent with name %s is not in server", name))
return 0, fmt.Errorf("Agent with name %s is not in server", name)
}
func ListAgents(cfg *config.Config) (*models.AgentList, error) {
@@ -75,23 +74,23 @@ func ListAgents(cfg *config.Config) (*models.AgentList, error) {
apiRoute := fmt.Sprintf("%s/api/agents?page=1&perPage=100", cfg.WoodpeckerInstance)
req, err := http.NewRequest(http.MethodGet, apiRoute, nil)
if err != nil {
return agentList, errors.New(fmt.Sprintf("Could not create agent query request: %s", err.Error()))
return agentList, fmt.Errorf("Could not create agent query request: %s", err.Error())
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.WoodpeckerApiToken))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return agentList, errors.New(fmt.Sprintf("Could not query agent list: %s", err.Error()))
return agentList, fmt.Errorf("Could not query agent list: %s", err.Error())
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return agentList, errors.New(fmt.Sprintf("Invalid status code from API: %d", resp.StatusCode))
return agentList, fmt.Errorf("Invalid status code from API: %d", resp.StatusCode)
}
err = json.NewDecoder(resp.Body).Decode(agentList)
if err != nil {
return agentList, errors.New(fmt.Sprintf("Could not unmarshal api response: %s", err.Error()))
return agentList, fmt.Errorf("Could not unmarshal api response: %s", err.Error())
}
return agentList, nil
}
@@ -111,24 +110,24 @@ func CreateWoodpeckerAgent(cfg *config.Config) (*models.Agent, error) {
}).Debugf("Sending the following data to %s: %s", apiRoute, jsonBody)
req, err := http.NewRequest(http.MethodPost, apiRoute, bodyReader)
if err != nil {
return nil, errors.New(fmt.Sprintf("Could not create agent request: %s", err.Error()))
return nil, fmt.Errorf("Could not create agent request: %s", err.Error())
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.WoodpeckerApiToken))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, errors.New(fmt.Sprintf("Could not create new Agent: %s", err.Error()))
return nil, fmt.Errorf("Could not create new Agent: %s", err.Error())
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.New(fmt.Sprintf("Invalid status code from API: %d", resp.StatusCode))
return nil, fmt.Errorf("Invalid status code from API: %d", resp.StatusCode)
}
newAgent := new(models.Agent)
err = json.NewDecoder(resp.Body).Decode(newAgent)
if err != nil {
return nil, errors.New(fmt.Sprintf("Could not unmarshal api response: %s", err.Error()))
return nil, fmt.Errorf("Could not unmarshal api response: %s", err.Error())
}
return newAgent, nil