Skip to content

Commit

Permalink
Merge branch 'master' into use-go1-10-and-1-11
Browse files Browse the repository at this point in the history
  • Loading branch information
bndr authored Oct 9, 2018
2 parents 98c65ad + 7cc3cd7 commit ca2c67f
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 29 deletions.
53 changes: 53 additions & 0 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,30 @@ type BuildResponse struct {
Revision int
} `json:"revision"`
} `json:"changeSet"`
ChangeSets []struct {
Items []struct {
AffectedPaths []string `json:"affectedPaths"`
Author struct {
AbsoluteUrl string `json:"absoluteUrl"`
FullName string `json:"fullName"`
} `json:"author"`
Comment string `json:"comment"`
CommitID string `json:"commitId"`
Date string `json:"date"`
ID string `json:"id"`
Msg string `json:"msg"`
Paths []struct {
EditType string `json:"editType"`
File string `json:"file"`
} `json:"paths"`
Timestamp int64 `json:"timestamp"`
} `json:"items"`
Kind string `json:"kind"`
Revisions []struct {
Module string
Revision int
} `json:"revision"`
} `json:"changeSets"`
Culprits []Culprit `json:"culprits"`
Description interface{} `json:"description"`
Duration int64 `json:"duration"`
Expand All @@ -158,6 +182,12 @@ type BuildResponse struct {
} `json:"runs"`
}

type consoleResponse struct {
Content string
Offset int64
HasMoreText bool
}

// Builds
func (b *Build) Info() *BuildResponse {
return b.Raw
Expand Down Expand Up @@ -213,6 +243,29 @@ func (b *Build) GetConsoleOutput() string {
return content
}

func (b *Build) GetConsoleOutputFromIndex(startID int64) (consoleResponse, error) {
strstart := strconv.FormatInt(startID, 10)
url := b.Base + "/logText/progressiveText"

var console consoleResponse

querymap := make(map[string]string)
querymap["start"] = strstart
rsp, err := b.Jenkins.Requester.Get(url, &console.Content, querymap)
if err != nil {
return console, err
}

textSize := rsp.Header.Get("X-Text-Size")
console.HasMoreText = len(rsp.Header.Get("X-More-Data")) != 0
console.Offset, err = strconv.ParseInt(textSize, 10, 64)
if err != nil {
return console, err
}

return console, err
}

func (b *Build) GetCauses() ([]map[string]interface{}, error) {
_, err := b.Poll()
if err != nil {
Expand Down
30 changes: 20 additions & 10 deletions credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
//CredentialsManager is utility to control credential plugin
//Credentials declared by it can be used in jenkins jobs
type CredentialsManager struct {
J *Jenkins
J *Jenkins
Folder string
}

const baseCredentialsURL = "/credentials/store/system/domain/%s/"
const baseFolderPrefix = "/job/%s"
const baseCredentialsURL = "%s/credentials/store/%s/domain/%s/"
const createCredentialsURL = baseCredentialsURL + "createCredentials"
const deleteCredentialURL = baseCredentialsURL + "credential/%s/delete"
const deleteCredentialURL = baseCredentialsURL + "credential/%s/doDelete"
const configCredentialURL = baseCredentialsURL + "credential/%s/config.xml"
const credentialsListURL = baseCredentialsURL + "api/json"

Expand Down Expand Up @@ -94,12 +96,22 @@ type PrivateKeyFile struct {
Class string `xml:"class,attr"`
}

func (cm CredentialsManager) fillURL(url string, params ...interface{}) string {
var args []interface{}
if cm.Folder != "" {
args = []interface{}{fmt.Sprintf(baseFolderPrefix, cm.Folder), "folder"}
} else {
args = []interface{}{"", "system"}
}
return fmt.Sprintf(url, append(args, params...)...)
}

//List ids if credentials stored inside provided domain
func (cm CredentialsManager) List(domain string) ([]string, error) {

idsResponse := credentialIDs{}
ids := make([]string, 0)
err := cm.handleResponse(cm.J.Requester.Get(fmt.Sprintf(credentialsListURL, domain), &idsResponse, listQuery))
err := cm.handleResponse(cm.J.Requester.Get(cm.fillURL(credentialsListURL, domain), &idsResponse, listQuery))
if err != nil {
return ids, err
}
Expand All @@ -115,7 +127,7 @@ func (cm CredentialsManager) List(domain string) ([]string, error) {
//it will be parsed as xml to creds parameter(creds must be pointer to struct)
func (cm CredentialsManager) GetSingle(domain string, id string, creds interface{}) error {
str := ""
err := cm.handleResponse(cm.J.Requester.Get(fmt.Sprintf(configCredentialURL, domain, id), &str, map[string]string{}))
err := cm.handleResponse(cm.J.Requester.Get(cm.fillURL(configCredentialURL, domain, id), &str, map[string]string{}))
if err != nil {
return err
}
Expand All @@ -125,19 +137,17 @@ func (cm CredentialsManager) GetSingle(domain string, id string, creds interface

//Add credential to given domain, creds must be struct which is parsable to xml
func (cm CredentialsManager) Add(domain string, creds interface{}) error {

return cm.postCredsXML(fmt.Sprintf(createCredentialsURL, domain), creds)
return cm.postCredsXML(cm.fillURL(createCredentialsURL, domain), creds)
}

//Delete credential in given domain with given id
func (cm CredentialsManager) Delete(domain string, id string) error {
return cm.handleResponse(cm.J.Requester.PostXML(fmt.Sprintf(deleteCredentialURL, domain, id), "", cm.J.Raw, map[string]string{}))
return cm.handleResponse(cm.J.Requester.Post(cm.fillURL(deleteCredentialURL, domain, id), nil, cm.J.Raw, map[string]string{}))
}

//Update credential in given domain with given id, creds must be pointer to struct which is parsable to xml
func (cm CredentialsManager) Update(domain string, id string, creds interface{}) error {

return cm.postCredsXML(fmt.Sprintf(configCredentialURL, domain, id), creds)
return cm.postCredsXML(cm.fillURL(configCredentialURL, domain, id), creds)
}

func (cm CredentialsManager) postCredsXML(url string, creds interface{}) error {
Expand Down
2 changes: 1 addition & 1 deletion jenkins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestDeleteNodes(t *testing.T) {
func TestCreateBuilds(t *testing.T) {
jobs, _ := jenkins.GetAllJobs()
for _, item := range jobs {
queueID, _ = item.InvokeSimple(map[string]string{"param1": "param1"})
queueID, _ = item.InvokeSimple(map[string]string{"params1": "param1"})
item.Poll()
isQueued, _ := item.IsQueued()
assert.Equal(t, true, isQueued)
Expand Down
54 changes: 37 additions & 17 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ type JobResponse struct {
LastUnstableBuild JobBuild `json:"lastUnstableBuild"`
LastUnsuccessfulBuild JobBuild `json:"lastUnsuccessfulBuild"`
Name string `json:"name"`
SubJobs []InnerJob `json:"jobs"`
NextBuildNumber int64 `json:"nextBuildNumber"`
Property []struct {
ParameterDefinitions []ParameterDefinition `json:"parameterDefinitions"`
Expand Down Expand Up @@ -201,10 +200,6 @@ func (j *Job) GetAllBuildIds() ([]JobBuild, error) {
return buildsResp.Builds, nil
}

func (j *Job) GetSubJobsMetadata() []InnerJob {
return j.Raw.SubJobs
}

func (j *Job) GetUpstreamJobsMetadata() []InnerJob {
return j.Raw.UpstreamProjects
}
Expand All @@ -213,18 +208,6 @@ func (j *Job) GetDownstreamJobsMetadata() []InnerJob {
return j.Raw.DownstreamProjects
}

func (j *Job) GetSubJobs() ([]*Job, error) {
jobs := make([]*Job, len(j.Raw.SubJobs))
for i, job := range j.Raw.SubJobs {
ji, err := j.Jenkins.GetSubJob(j.GetName(), job.Name)
if err != nil {
return nil, err
}
jobs[i] = ji
}
return jobs, nil
}

func (j *Job) GetInnerJobsMetadata() []InnerJob {
return j.Raw.Jobs
}
Expand Down Expand Up @@ -533,3 +516,40 @@ func (j *Job) History() ([]*History, error) {

return parseBuildHistory(strings.NewReader(s)), nil
}

func (pr *PipelineRun) ProceedInput() (bool, error) {
actions, _ := pr.GetPendingInputActions()
data := url.Values{}
data.Set("inputId", actions[0].ID)
params := make(map[string]string)
data.Set("json", makeJson(params))

href := pr.Base + "/wfapi/inputSubmit"

resp, err := pr.Job.Jenkins.Requester.Post(href, bytes.NewBufferString(data.Encode()), nil, nil)
if err != nil {
return false, err
}
if resp.StatusCode != 200 {
return false, errors.New(strconv.Itoa(resp.StatusCode))
}
return true, nil
}

func (pr *PipelineRun) AbortInput() (bool, error) {
actions, _ := pr.GetPendingInputActions()
data := url.Values{}
params := make(map[string]string)
data.Set("json", makeJson(params))

href := pr.Base + "/input/" + actions[0].ID + "/abort"

resp, err := pr.Job.Jenkins.Requester.Post(href, bytes.NewBufferString(data.Encode()), nil, nil)
if err != nil {
return false, err
}
if resp.StatusCode != 200 {
return false, errors.New(strconv.Itoa(resp.StatusCode))
}
return true, nil
}
2 changes: 1 addition & 1 deletion pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (job *Job) GetPipelineRun(id string) (pr *PipelineRun, err error) {
func (pr *PipelineRun) GetPendingInputActions() (PIAs []PipelineInputAction, err error) {
PIAs = make([]PipelineInputAction, 0, 1)
href := pr.Base + "/wfapi/pendingInputActions"
_, err = pr.Job.Jenkins.Requester.GetJSON(href, PIAs, nil)
_, err = pr.Job.Jenkins.Requester.GetJSON(href, &PIAs, nil)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit ca2c67f

Please sign in to comment.