This commit is contained in:
“xHuPo” 2025-05-27 11:48:18 +08:00
parent 942a0bd14e
commit 94d71a1e12
8 changed files with 1014 additions and 0 deletions

299
v3.1/jenkins.go Normal file
View file

@ -0,0 +1,299 @@
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"path"
"strings"
"sync"
"time"
)
// Build represents a Jenkins build
type Build struct {
Actions []Action `json:"actions"`
Number int `json:"number"`
URL string `json:"url"`
}
// Action represents a Jenkins build action
type Action struct {
Class string `json:"_class"`
Parameters []Parameter `json:"parameters,omitempty"`
}
// Parameter represents a Jenkins build parameter
type Parameter struct {
Name string `json:"name"`
Value interface{} `json:"value"`
}
// JenkinsClient is a client for interacting with Jenkins API
type JenkinsClient struct {
Client *http.Client
Config *Jenkins
semaphore chan struct{} // For concurrency control
mu sync.Mutex // For thread safety
}
// JenkinsService defines the interface for Jenkins operations
type JenkinsService interface {
FetchBuild(ctx context.Context, job string) (*Build, error)
TriggerBuild(ctx context.Context, build *Build, params map[string]interface{}) error
FetchBuildWithRetry(ctx context.Context, job string, retries int) (*Build, error)
TriggerBuildWithRetry(ctx context.Context, build *Build, params map[string]interface{}, retries int) error
}
// NewJenkinsClient creates a new Jenkins client
func NewJenkinsClient(cfg *Jenkins) *JenkinsClient {
// Configure HTTP client with reasonable defaults
transport := &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 30 * time.Second,
DisableCompression: false,
TLSHandshakeTimeout: 5 * time.Second,
}
return &JenkinsClient{
Client: &http.Client{
Timeout: 15 * time.Second, // Increased from 10s to 15s
Transport: transport,
},
Config: cfg,
semaphore: make(chan struct{}, 5), // Limit to 5 concurrent requests
}
}
// buildAPIURL constructs the Jenkins API URL for a job
func (c *JenkinsClient) buildAPIURL(job string) string {
return fmt.Sprintf("%s://%s/job/%s/%s/api/json",
c.Config.Schema,
c.Config.URL,
job,
c.Config.Number,
)
}
// FetchBuild retrieves build information from Jenkins
func (c *JenkinsClient) FetchBuild(ctx context.Context, job string) (*Build, error) {
// Acquire semaphore
select {
case c.semaphore <- struct{}{}:
defer func() { <-c.semaphore }()
case <-ctx.Done():
return nil, ctx.Err()
}
api := c.buildAPIURL(job)
log.Printf("[JENKINS] Fetching build info for job: %s from %s", job, api)
req, err := http.NewRequestWithContext(ctx, "GET", api, nil)
if err != nil {
return nil, fmt.Errorf("failed to create GET request for job %s: %w", job, err)
}
req.SetBasicAuth(c.Config.User, c.Config.Token)
req.Header.Set("Accept", "application/json")
resp, err := c.Client.Do(req)
if err != nil {
return nil, fmt.Errorf("request failed for job %s: %w", job, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
bodyBytes, _ := io.ReadAll(io.LimitReader(resp.Body, 1000))
return nil, fmt.Errorf("unexpected status %s for job %s: %s", resp.Status, job, string(bodyBytes))
}
// Limit response body to 1MB to prevent memory issues
body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
if err != nil {
return nil, fmt.Errorf("failed to read response body for job %s: %w", job, err)
}
var build Build
if err := json.Unmarshal(body, &build); err != nil {
return nil, fmt.Errorf("failed to unmarshal response for job %s: %w", job, err)
}
log.Printf("[JENKINS] Successfully fetched build #%d for job: %s", build.Number, job)
return &build, nil
}
// FetchBuildWithRetry attempts to fetch a build with retries
func (c *JenkinsClient) FetchBuildWithRetry(ctx context.Context, job string, retries int) (*Build, error) {
var lastErr error
for attempt := 0; attempt <= retries; attempt++ {
if attempt > 0 {
log.Printf("[JENKINS] Retry attempt %d/%d for fetching job: %s", attempt, retries, job)
// Exponential backoff: 1s, 2s, 4s, ...
backoffDuration := time.Duration(1<<uint(attempt-1)) * time.Second
select {
case <-time.After(backoffDuration):
case <-ctx.Done():
return nil, ctx.Err()
}
}
build, err := c.FetchBuild(ctx, job)
if err == nil {
return build, nil
}
lastErr = err
log.Printf("[JENKINS] Attempt %d failed: %v", attempt, err)
}
return nil, fmt.Errorf("failed to fetch build after %d retries: %w", retries, lastErr)
}
// ExtractBuildParams extracts build parameters as map[string]string
func ExtractBuildParams(build *Build) map[string]string {
params := make(map[string]string)
for _, action := range build.Actions {
if action.Class == "hudson.model.ParametersAction" {
for _, param := range action.Parameters {
params[param.Name] = fmt.Sprintf("%v", param.Value)
}
}
}
return params
}
// MergeParams merges base parameters with default parameters
func MergeParams(base map[string]string, defaults []map[string]interface{}) map[string]string {
result := make(map[string]string)
// Copy base parameters first
for k, v := range base {
result[k] = v
}
// Apply defaults, potentially overriding base values
for _, param := range defaults {
for k, v := range param {
result[k] = fmt.Sprintf("%v", v)
}
}
return result
}
// StringMapToInterfaceMap converts map[string]string to map[string]interface{}
func StringMapToInterfaceMap(m map[string]string) map[string]interface{} {
res := make(map[string]interface{})
for k, v := range m {
res[k] = v
}
return res
}
// IsSubset checks if b is a subset of a
func IsSubset(a map[string]string, b map[string]interface{}) bool {
for k, v := range b {
if a[k] != fmt.Sprintf("%v", v) {
return false
}
}
return true
}
// generateBuildURL constructs the URL for triggering a build with parameters
func (c *JenkinsClient) generateBuildURL(baseURL string, mergedParams map[string]string) string {
values := url.Values{}
for k, v := range mergedParams {
values.Set(k, v)
}
// Parse and modify the build URL
u, err := url.Parse(baseURL)
if err != nil {
log.Printf("[JENKINS] Failed to parse build URL: %v", err)
return ""
}
u.Path = getBuildWithParametersPath(u.Path)
u.RawQuery = values.Encode()
return u.String()
}
// getBuildWithParametersPath modifies the path to use buildWithParameters endpoint
func getBuildWithParametersPath(p string) string {
parts := strings.Split(strings.TrimSuffix(p, "/"), "/")
if len(parts) > 0 {
return path.Join(parts[:len(parts)-1]...) + "/buildWithParameters"
}
return p
}
// TriggerBuild triggers a Jenkins build with the given parameters
func (c *JenkinsClient) TriggerBuild(ctx context.Context, build *Build, params map[string]interface{}) error {
// Acquire semaphore
select {
case c.semaphore <- struct{}{}:
defer func() { <-c.semaphore }()
case <-ctx.Done():
return ctx.Err()
}
// Thread safety for params manipulation
c.mu.Lock()
merged := make(map[string]string)
for k, v := range params {
merged[k] = fmt.Sprintf("%v", v)
}
c.mu.Unlock()
triggerURL := c.generateBuildURL(build.URL, merged)
if triggerURL == "" {
return fmt.Errorf("failed to generate trigger URL")
}
log.Printf("[JENKINS] Triggering build at URL: %s", triggerURL)
req, err := http.NewRequestWithContext(ctx, "POST", triggerURL, nil)
if err != nil {
return fmt.Errorf("failed to create POST request: %w", err)
}
req.SetBasicAuth(c.Config.User, c.Config.Token)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := c.Client.Do(req)
if err != nil {
return fmt.Errorf("trigger request failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
bodyBytes, _ := io.ReadAll(io.LimitReader(resp.Body, 1000))
return fmt.Errorf("trigger failed with status %s: %s", resp.Status, string(bodyBytes))
}
log.Printf("[JENKINS] Successfully triggered build: %s", resp.Status)
return nil
}
// TriggerBuildWithRetry attempts to trigger a build with retries
func (c *JenkinsClient) TriggerBuildWithRetry(ctx context.Context, build *Build, params map[string]interface{}, retries int) error {
var lastErr error
for attempt := 0; attempt <= retries; attempt++ {
if attempt > 0 {
log.Printf("[JENKINS] Retry attempt %d/%d for triggering build", attempt, retries)
// Exponential backoff: 1s, 2s, 4s, ...
backoffDuration := time.Duration(1<<uint(attempt-1)) * time.Second
select {
case <-time.After(backoffDuration):
case <-ctx.Done():
return ctx.Err()
}
}
err := c.TriggerBuild(ctx, build, params)
if err == nil {
return nil
}
lastErr = err
log.Printf("[JENKINS] Attempt %d failed: %v", attempt, err)
}
return fmt.Errorf("failed to trigger build after %d retries: %w", retries, lastErr)
}