mirror of
https://github.com/appleboy/drone-jenkins.git
synced 2026-06-04 18:23:57 +08:00
52abef124e
- Add missing .PHONY targets to the Makefile for better build reliability - Ensure HTTP response bodies are always read and closed in Jenkins post requests - Replace custom response parsing with direct JSON unmarshalling in Jenkins post - Set a default value for the Version variable - Move ASCII art to a constant and reuse for CLI help template - Improve dotenv loading error handling and logging in main - Update repository link in CLI help output - Add validation for required CLI parameters and authentication in run function Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
161 lines
3.2 KiB
Go
161 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
type (
|
|
// Auth contain username and token
|
|
Auth struct {
|
|
Username string
|
|
Token string
|
|
}
|
|
|
|
// Jenkins contain Auth and BaseURL
|
|
Jenkins struct {
|
|
Auth *Auth
|
|
BaseURL string
|
|
Token string // Remote trigger token
|
|
Client *http.Client
|
|
}
|
|
)
|
|
|
|
// NewJenkins is initial Jenkins object
|
|
func NewJenkins(auth *Auth, url string, token string, insecure bool) *Jenkins {
|
|
url = strings.TrimRight(url, "/")
|
|
|
|
client := http.DefaultClient
|
|
if insecure {
|
|
client = &http.Client{
|
|
Transport: &http.Transport{
|
|
// #nosec G402 -- InsecureSkipVerify is intentionally configurable by user
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
},
|
|
}
|
|
}
|
|
|
|
return &Jenkins{
|
|
Auth: auth,
|
|
BaseURL: url,
|
|
Token: token,
|
|
Client: client,
|
|
}
|
|
}
|
|
|
|
func (jenkins *Jenkins) buildURL(path string, params url.Values) (requestURL string) {
|
|
requestURL = jenkins.BaseURL + path
|
|
if params != nil {
|
|
queryString := params.Encode()
|
|
if queryString != "" {
|
|
requestURL = requestURL + "?" + queryString
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (jenkins *Jenkins) sendRequest(req *http.Request) (*http.Response, error) {
|
|
if jenkins.Auth != nil {
|
|
req.SetBasicAuth(jenkins.Auth.Username, jenkins.Auth.Token)
|
|
}
|
|
return jenkins.Client.Do(req)
|
|
}
|
|
|
|
func (jenkins *Jenkins) parseResponse(resp *http.Response, body interface{}) (err error) {
|
|
defer resp.Body.Close()
|
|
|
|
data, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
if body == nil {
|
|
return
|
|
}
|
|
|
|
return json.Unmarshal(data, body)
|
|
}
|
|
|
|
func (jenkins *Jenkins) post(path string, params url.Values, body interface{}) (err error) {
|
|
requestURL := jenkins.buildURL(path, params)
|
|
|
|
req, err := http.NewRequestWithContext(context.Background(), "POST", requestURL, nil)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
resp, err := jenkins.sendRequest(req)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
data, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to read response body: %w", err)
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("unexpected response code: %d, body: %s", resp.StatusCode, string(data))
|
|
}
|
|
|
|
if body == nil {
|
|
return nil
|
|
}
|
|
|
|
return json.Unmarshal(data, body)
|
|
}
|
|
|
|
func (jenkins *Jenkins) parseJobPath(job string) string {
|
|
var path string
|
|
|
|
jobs := strings.Split(strings.TrimPrefix(job, "/"), "/")
|
|
|
|
for _, value := range jobs {
|
|
value = strings.Trim(value, " ")
|
|
if len(value) == 0 {
|
|
continue
|
|
}
|
|
|
|
path = fmt.Sprintf("%s/job/%s", path, value)
|
|
}
|
|
|
|
return path
|
|
}
|
|
|
|
func (jenkins *Jenkins) trigger(job string, params url.Values) error {
|
|
// Add remote trigger token to params
|
|
if jenkins.Token != "" {
|
|
if params == nil {
|
|
params = url.Values{}
|
|
}
|
|
params.Set("token", jenkins.Token)
|
|
}
|
|
|
|
var urlPath string
|
|
// Check if params contains build parameters (excluding 'token')
|
|
hasBuildParams := false
|
|
for key := range params {
|
|
if key != "token" {
|
|
hasBuildParams = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if hasBuildParams {
|
|
urlPath = jenkins.parseJobPath(job) + "/buildWithParameters"
|
|
} else {
|
|
urlPath = jenkins.parseJobPath(job) + "/build"
|
|
}
|
|
|
|
// All params (including token) are passed as query parameters
|
|
return jenkins.post(urlPath, params, nil)
|
|
}
|