You've already forked golang-jenkins
mirror of
https://github.com/newrelic-forks/golang-jenkins.git
synced 2026-06-18 20:15:04 +02:00
Merge remote-tracking branch 'upstream/master' into upstream-merge
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
The MIT License (MIT) Copyright © 2014 Kohei YOSHIDA <kohei.yoshida@gehirn.co.jp>
|
||||
The MIT License (MIT) Copyright © 2014 Kohei YOSHIDA <https://yosida95.com/>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the
|
||||
+1
-1
@@ -35,4 +35,4 @@ Make calls against the desired resources:
|
||||
License
|
||||
-------
|
||||
golang-jenkins is licensed under the MIT LICENSE.
|
||||
See `./LICENSE.rst <./LICENSE.rst>`_.
|
||||
See `./LICENSE <./LICENSE>`_.
|
||||
|
||||
@@ -9,6 +9,7 @@ type ComputerObject struct {
|
||||
|
||||
type Computer struct {
|
||||
Actions []struct{} `json:"actions"`
|
||||
Class string `json:"_class"`
|
||||
DisplayName string `json:"displayName"`
|
||||
Executors []struct{} `json:"executors"`
|
||||
Idle bool `json:"idle"`
|
||||
|
||||
+72
-1
@@ -13,6 +13,16 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type HTTPStatusError struct {
|
||||
URL string
|
||||
Code int
|
||||
Status string
|
||||
}
|
||||
|
||||
func (e *HTTPStatusError) Error() string {
|
||||
return fmt.Sprintf("bad http status: %d: %s", e.Code, e.Status)
|
||||
}
|
||||
|
||||
type Auth struct {
|
||||
Username string
|
||||
ApiToken string
|
||||
@@ -21,15 +31,22 @@ type Auth struct {
|
||||
type Jenkins struct {
|
||||
auth *Auth
|
||||
baseUrl string
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func NewJenkins(auth *Auth, baseUrl string) *Jenkins {
|
||||
return &Jenkins{
|
||||
auth: auth,
|
||||
baseUrl: baseUrl,
|
||||
client: http.DefaultClient,
|
||||
}
|
||||
}
|
||||
|
||||
// SetHTTPClient with timeouts or insecure transport, etc.
|
||||
func (jenkins *Jenkins) SetHTTPClient(client *http.Client) {
|
||||
jenkins.client = client
|
||||
}
|
||||
|
||||
func (jenkins *Jenkins) buildUrl(path string, params url.Values) (requestUrl string) {
|
||||
requestUrl = jenkins.baseUrl + path + "/api/json"
|
||||
if params != nil {
|
||||
@@ -42,11 +59,57 @@ func (jenkins *Jenkins) buildUrl(path string, params url.Values) (requestUrl str
|
||||
return
|
||||
}
|
||||
|
||||
// checkCrumb - checks if `useCrumb` is enabled and if so, retrieves crumb field and value and updates request header
|
||||
func (jenkins *Jenkins) checkCrumb(req *http.Request) (*http.Request, error) {
|
||||
|
||||
// api - store jenkins api useCrumbs response
|
||||
api := struct {
|
||||
UseCrumbs bool `json:"useCrumbs"`
|
||||
}{}
|
||||
|
||||
err := jenkins.get("/api/json", url.Values{"tree": []string{"useCrumbs"}}, &api)
|
||||
if err != nil {
|
||||
return req, err
|
||||
}
|
||||
|
||||
if !api.UseCrumbs {
|
||||
// CSRF Protection is not enabled
|
||||
return req, nil
|
||||
}
|
||||
|
||||
// get crumb field and value
|
||||
crumb := struct {
|
||||
Crumb string `json:"crumb"`
|
||||
CrumbRequestField string `json:"crumbRequestField"`
|
||||
}{}
|
||||
|
||||
err = jenkins.get("/crumbIssuer", nil, &crumb)
|
||||
if err != nil {
|
||||
return req, err
|
||||
}
|
||||
|
||||
// update header
|
||||
req.Header.Set(crumb.CrumbRequestField, crumb.Crumb)
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (jenkins *Jenkins) sendRequest(req *http.Request) (*http.Response, error) {
|
||||
if jenkins.auth != nil {
|
||||
req.SetBasicAuth(jenkins.auth.Username, jenkins.auth.ApiToken)
|
||||
}
|
||||
return http.DefaultClient.Do(req)
|
||||
res, err := jenkins.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return nil, &HTTPStatusError{
|
||||
URL: req.URL.String(),
|
||||
Code: res.StatusCode,
|
||||
Status: res.Status,
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (jenkins *Jenkins) parseXmlResponse(resp *http.Response, body interface{}) (err error) {
|
||||
@@ -123,6 +186,10 @@ func (jenkins *Jenkins) post(path string, params url.Values, body interface{}) (
|
||||
if params != nil {
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
}
|
||||
|
||||
if _, err := jenkins.checkCrumb(req); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp, err := jenkins.sendRequest(req)
|
||||
if err != nil {
|
||||
@@ -148,6 +215,10 @@ func (jenkins *Jenkins) postXml(path string, params url.Values, xmlBody io.Reade
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := jenkins.checkCrumb(req); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req.Header.Add("Content-Type", "application/xml")
|
||||
resp, err := jenkins.sendRequest(req)
|
||||
if err != nil {
|
||||
|
||||
@@ -8,6 +8,33 @@ type Artifact struct {
|
||||
RelativePath string `json:"relativePath"`
|
||||
}
|
||||
|
||||
type ScmAuthor struct {
|
||||
FullName string `json:"fullName"`
|
||||
AbsoluteUrl string `json:"absoluteUrl"`
|
||||
}
|
||||
|
||||
type ScmChangeSetPath struct {
|
||||
EditType string `json:"editType"`
|
||||
File string `json:"File"`
|
||||
}
|
||||
|
||||
type ChangeSetItem struct {
|
||||
AffectedPaths []string `json:"affectedPaths"`
|
||||
CommitId string `json:"commitId"`
|
||||
Timestamp int `json:"timestamp"`
|
||||
Author ScmAuthor `json:"author"`
|
||||
Comment string `json:"comment"`
|
||||
Date string `json:"date"`
|
||||
Id string `json:"id"`
|
||||
Message string `json:"msg"`
|
||||
Paths []ScmChangeSetPath `json:"paths"`
|
||||
}
|
||||
|
||||
type ScmChangeSet struct {
|
||||
Kind string `json:"kind"`
|
||||
Items []ChangeSetItem `json:"items"`
|
||||
}
|
||||
|
||||
type Build struct {
|
||||
Id string `json:"id"`
|
||||
Number int `json:"number"`
|
||||
@@ -26,6 +53,8 @@ type Build struct {
|
||||
|
||||
Artifacts []Artifact `json:"artifacts"`
|
||||
Actions []Action `json:"actions"`
|
||||
|
||||
ChangeSet ScmChangeSet `json:"changeSet"`
|
||||
}
|
||||
|
||||
type UpstreamCause struct {
|
||||
@@ -43,23 +72,37 @@ type Job struct {
|
||||
Color string `json:"color"`
|
||||
|
||||
Buildable bool `json:"buildable"`
|
||||
Builds []Build `json:"builds"`
|
||||
DisplayName string `json:"displayName"`
|
||||
Description string `json:"description"`
|
||||
HealthReport []Health `json:"healthReport"`
|
||||
|
||||
Builds []Build `json:"builds"`
|
||||
LastCompletedBuild Build `json:"lastCompletedBuild"`
|
||||
LastFailedBuild Build `json:"lastFailedBuild"`
|
||||
LastStableBuild Build `json:"lastStableBuild"`
|
||||
LastSuccessfulBuild Build `json:"lastSuccessfulBuild"`
|
||||
LastUnstableBuild Build `json:"lastUnstableBuild"`
|
||||
LastUnsuccessfulBuild Build `json:"lastUnsuccessfulBuild"`
|
||||
LastCompletedBuild Build `json:"lastCompletedBuild"`
|
||||
LastFailedBuild Build `json:"lastFailedBuild"`
|
||||
LastStableBuild Build `json:"lastStableBuild"`
|
||||
LastSuccessfulBuild Build `json:"lastSuccessfulBuild"`
|
||||
LastUnstableBuild Build `json:"lastUnstableBuild"`
|
||||
LastUnsuccessfulBuild Build `json:"lastUnsuccessfulBuild"`
|
||||
|
||||
Property []Property `json:"property"`
|
||||
}
|
||||
|
||||
type Health struct {
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
type Property struct {
|
||||
Parameters []JobParameter `json:"parameterDefinitions"`
|
||||
}
|
||||
|
||||
type JobParameter struct {
|
||||
Default Parameter `json:"defaultParameterValue"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Type string `json:"type"`
|
||||
Choices []string `json:"choices"`
|
||||
}
|
||||
|
||||
type MavenJobItem struct {
|
||||
XMLName struct{} `xml:"maven2-moduleset"`
|
||||
Plugin string `xml:"plugin,attr"`
|
||||
|
||||
@@ -21,6 +21,7 @@ type Item struct {
|
||||
|
||||
type Action struct {
|
||||
Causes []Cause `json:"causes"`
|
||||
Parameter []Parameter `json:"parameters"`
|
||||
ParameterDefinitions []ParameterDefinition `json:"parameterDefinitions"`
|
||||
Parameters []Parameter `json:"parameters"`
|
||||
}
|
||||
@@ -36,6 +37,7 @@ type ParameterDefinition struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// Parameter for a build
|
||||
type Parameter struct {
|
||||
Name string `json:"name"`
|
||||
Value interface{} `json:"value"`
|
||||
|
||||
Reference in New Issue
Block a user