feat: add actions workflow enable and disable

This commit is contained in:
Romain Bertrand 2026-01-18 11:50:02 +01:00
parent c2ee338f1c
commit 4c6de3ad2e
4 changed files with 159 additions and 39 deletions

View file

@ -88,6 +88,13 @@ func (c *Client) GetJSON(path string, result any) error {
// PostJSON performs a POST request to the specified path with JSON body
func (c *Client) PostJSON(path string, body any, result any) error {
_, err := c.DoJSON(http.MethodPost, path, body, result)
return err
}
// DoJSON performs an HTTP request with a JSON body and decodes the JSON response.
// Returns the HTTP status code and any error encountered.
func (c *Client) DoJSON(method string, path string, body any, result any) (int, error) {
baseURL := "https://" + c.hostname
url := baseURL + path
@ -95,14 +102,14 @@ func (c *Client) PostJSON(path string, body any, result any) error {
if body != nil {
bodyBytes, err := json.Marshal(body)
if err != nil {
return fmt.Errorf("failed to marshal request body: %w", err)
return 0, fmt.Errorf("failed to marshal request body: %w", err)
}
bodyReader = bytes.NewReader(bodyBytes)
}
req, err := http.NewRequest(http.MethodPost, url, bodyReader)
req, err := http.NewRequest(method, url, bodyReader)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
return 0, fmt.Errorf("failed to create request: %w", err)
}
// Set authentication header
@ -110,12 +117,14 @@ func (c *Client) PostJSON(path string, body any, result any) error {
req.Header.Set("Authorization", "token "+c.token)
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to perform request: %w", err)
return 0, fmt.Errorf("failed to perform request: %w", err)
}
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil && err == nil {
@ -125,16 +134,16 @@ func (c *Client) PostJSON(path string, body any, result any) error {
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusNoContent {
bodyBytes, _ := io.ReadAll(resp.Body)
return fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(bodyBytes))
return resp.StatusCode, fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(bodyBytes))
}
if result != nil && resp.StatusCode != http.StatusNoContent {
if err := json.NewDecoder(resp.Body).Decode(result); err != nil {
return fmt.Errorf("failed to decode response: %w", err)
return resp.StatusCode, fmt.Errorf("failed to decode response: %w", err)
}
}
return nil
return resp.StatusCode, nil
}
// GetRawLog performs a GET request and returns the raw response body as string