mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
* Another actions test Signed-off-by: Chris Cummer <chriscummer@me.com> * Add BuildTest action Signed-off-by: Chris Cummer <chriscummer@me.com> * Remove lint check for the time being (so many issues) Signed-off-by: Chris Cummer <chriscummer@me.com> * Fix issues found by errcheck Signed-off-by: Chris Cummer <chriscummer@me.com> * Fix errors found by staticcheck Signed-off-by: Chris Cummer <chriscummer@me.com> * Fix issues found by goimports Signed-off-by: Chris Cummer <chriscummer@me.com> * Comment out the action for the time being Signed-off-by: Chris Cummer <chriscummer@me.com> * Fix shadowed variables Signed-off-by: Chris Cummer <chriscummer@me.com> * go mod tidy Signed-off-by: Chris Cummer <chriscummer@me.com> * Remove buildtest.yml Signed-off-by: Chris Cummer <chriscummer@me.com> * go mod tidy Signed-off-by: Chris Cummer <chriscummer@me.com>
68 lines
1.4 KiB
Go
68 lines
1.4 KiB
Go
package jenkins
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"net/http"
|
|
"net/url"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/wtfutil/wtf/utils"
|
|
)
|
|
|
|
func (widget *Widget) Create(jenkinsURL string, username string, apiKey string) (*View, error) {
|
|
const apiSuffix = "api/json?pretty=true"
|
|
view := &View{}
|
|
parsedSuffix, err := url.Parse(apiSuffix)
|
|
if err != nil {
|
|
return view, err
|
|
}
|
|
|
|
parsedJenkinsURL, err := url.Parse(ensureLastSlash(jenkinsURL))
|
|
if err != nil {
|
|
return view, err
|
|
}
|
|
jenkinsAPIURL := parsedJenkinsURL.ResolveReference(parsedSuffix)
|
|
|
|
req, _ := http.NewRequest("GET", jenkinsAPIURL.String(), nil)
|
|
req.SetBasicAuth(username, apiKey)
|
|
|
|
httpClient := &http.Client{Transport: &http.Transport{
|
|
TLSClientConfig: &tls.Config{
|
|
InsecureSkipVerify: !widget.settings.verifyServerCertificate,
|
|
},
|
|
Proxy: http.ProxyFromEnvironment,
|
|
},
|
|
}
|
|
resp, err := httpClient.Do(req)
|
|
|
|
if err != nil {
|
|
return view, err
|
|
}
|
|
|
|
err = utils.ParseJSON(view, resp.Body)
|
|
if err != nil {
|
|
return view, err
|
|
}
|
|
|
|
respJobs := make([]Job, 0, len(view.Jobs)+len(view.ActiveConfigurations))
|
|
respJobs = append(append(respJobs, view.Jobs...), view.ActiveConfigurations...)
|
|
|
|
jobs := make([]Job, 0)
|
|
|
|
var validID = regexp.MustCompile(widget.settings.jobNameRegex)
|
|
for _, job := range respJobs {
|
|
if validID.MatchString(job.Name) {
|
|
jobs = append(jobs, job)
|
|
}
|
|
}
|
|
|
|
view.Jobs = jobs
|
|
|
|
return view, nil
|
|
}
|
|
|
|
func ensureLastSlash(url string) string {
|
|
return strings.TrimRight(url, "/") + "/"
|
|
}
|