Skip to content

Commit

Permalink
feat: support parameter handling for dynamic URL paths (#35)
Browse files Browse the repository at this point in the history
- Comment out unused form data encoding in `post` method
- Remove redundant error logging in `post` method
- Add conditional URL path selection in `trigger` method based on parameters
- Add `parameter` flag to CLI options in `main.go`
- Include `parameter` in the `run` function configuration
- Import `net/url` package in `plugin.go`
- Add `Parameter` field to `plugin.go` struct
- Parse and add parameters to URL values in `Exec` method
- Pass parsed parameters to `trigger` method in `Exec` function

Signed-off-by: appleboy <appleboy.tw@gmail.com>
  • Loading branch information
appleboy authored Oct 5, 2024
1 parent bb1e9fe commit a7c6b81
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
12 changes: 8 additions & 4 deletions jenkins.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,14 @@ func (jenkins *Jenkins) parseResponse(resp *http.Response, body interface{}) (er

func (jenkins *Jenkins) post(path string, params url.Values, body interface{}) (err error) {
requestURL := jenkins.buildURL(path, params)
// formData := params.Encode()
req, err := http.NewRequest("POST", requestURL, nil)
if err != nil {
fmt.Println(err)
return
}

resp, err := jenkins.sendRequest(req)
if err != nil {
fmt.Println(err)
return
}

Expand Down Expand Up @@ -118,7 +117,12 @@ func (jenkins *Jenkins) parseJobPath(job string) string {
}

func (jenkins *Jenkins) trigger(job string, params url.Values) error {
path := jenkins.parseJobPath(job) + "/build"
var urlPath string
if len(params) == 0 {
urlPath = jenkins.parseJobPath(job) + "/build"
} else {
urlPath = jenkins.parseJobPath(job) + "/buildWithParameters"
}

return jenkins.post(path, params, nil)
return jenkins.post(urlPath, params, nil)
}
16 changes: 11 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func main() {
Usage: "allow insecure server connections when using SSL",
EnvVar: "PLUGIN_INSECURE,JENKINS_INSECURE,INPUT_INSECURE",
},
cli.StringSliceFlag{
Name: "parameter,p",
Usage: "jenkins build parameter",
EnvVar: "PLUGIN_PARAMETER,JENKINS_PARAMETER,INPUT_PARAMETER",
},
}

// Override a template
Expand Down Expand Up @@ -101,11 +106,12 @@ REPOSITORY:

func run(c *cli.Context) error {
plugin := Plugin{
BaseURL: c.String("host"),
Username: c.String("user"),
Token: c.String("token"),
Job: c.StringSlice("job"),
Insecure: c.Bool("insecure"),
BaseURL: c.String("host"),
Username: c.String("user"),
Token: c.String("token"),
Job: c.StringSlice("job"),
Insecure: c.Bool("insecure"),
Parameter: c.StringSlice("parameter"),
}

return plugin.Exec()
Expand Down
22 changes: 16 additions & 6 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ package main
import (
"errors"
"log"
"net/url"
"strings"
)

type (
// Plugin values.
Plugin struct {
BaseURL string
Username string
Token string
Job []string
Insecure bool
BaseURL string
Username string
Token string
Job []string
Insecure bool
Parameter []string
}
)

Expand Down Expand Up @@ -50,8 +52,16 @@ func (p Plugin) Exec() error {

jenkins := NewJenkins(auth, p.BaseURL, p.Insecure)

params := url.Values{}
for _, v := range p.Parameter {
kv := strings.Split(v, "=")
if len(kv) == 2 {
params.Add(kv[0], kv[1])
}
}

for _, v := range jobs {
if err := jenkins.trigger(v, nil); err != nil {
if err := jenkins.trigger(v, params); err != nil {
return err
}
log.Printf("trigger job %s success", v)
Expand Down

0 comments on commit a7c6b81

Please sign in to comment.