Files
plugin-drone-jenkins/plugin.go
T
appleboy bb1e9fe4e0 feat: improve logging and error handling in Jenkins plugin
- Add error handling for unexpected response codes in `post` method of `jenkins.go`
- Import `log` package in `plugin.go`
- Add logging for successful job trigger in `Exec` method of `plugin.go`

Signed-off-by: appleboy <appleboy.tw@gmail.com>
2024-10-05 13:28:14 +08:00

62 lines
972 B
Go

package main
import (
"errors"
"log"
"strings"
)
type (
// Plugin values.
Plugin struct {
BaseURL string
Username string
Token string
Job []string
Insecure bool
}
)
func trimElement(keys []string) []string {
newKeys := []string{}
for _, value := range keys {
value = strings.Trim(value, " ")
if len(value) == 0 {
continue
}
newKeys = append(newKeys, value)
}
return newKeys
}
// Exec executes the plugin.
func (p Plugin) Exec() error {
if len(p.BaseURL) == 0 || len(p.Username) == 0 || len(p.Token) == 0 {
return errors.New("missing jenkins config")
}
jobs := trimElement(p.Job)
if len(jobs) == 0 {
return errors.New("missing jenkins job")
}
auth := &Auth{
Username: p.Username,
Token: p.Token,
}
jenkins := NewJenkins(auth, p.BaseURL, p.Insecure)
for _, v := range jobs {
if err := jenkins.trigger(v, nil); err != nil {
return err
}
log.Printf("trigger job %s success", v)
}
return nil
}