Skip to content

Commit

Permalink
add call for getting queue item
Browse files Browse the repository at this point in the history
  • Loading branch information
mceloud committed Jul 2, 2018
1 parent 3118be9 commit 401552c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
14 changes: 14 additions & 0 deletions jenkins.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,20 @@ func (j *Jenkins) GetQueueUrl() string {
return "/queue"
}

// GetQueueItem returns a single queue Task
func (j *Jenkins) GetQueueItem(id int64) (*Task, error) {
t := &Task{Raw: new(taskResponse), Jenkins: j, Base: j.getQueueItemURL(id)}
_, err := t.Poll()
if err != nil {
return nil, err
}
return t, nil
}

func (j *Jenkins) getQueueItemURL(id int64) string {
return fmt.Sprintf("/queue/item/%d", id)
}

// Get Artifact data by Hash
func (j *Jenkins) GetArtifactData(id string) (*FingerPrintResponse, error) {
fp := FingerPrint{Jenkins: j, Base: "/fingerprint/", Id: id, Raw: new(FingerPrintResponse)}
Expand Down
15 changes: 13 additions & 2 deletions jenkins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

var (
jenkins *Jenkins
queueID int64
)

func TestInit(t *testing.T) {
Expand Down Expand Up @@ -67,7 +68,7 @@ func TestDeleteNodes(t *testing.T) {
func TestCreateBuilds(t *testing.T) {
jobs, _ := jenkins.GetAllJobs()
for _, item := range jobs {
item.InvokeSimple(map[string]string{"param1": "param1"})
queueID, _ = item.InvokeSimple(map[string]string{"param1": "param1"})
item.Poll()
isQueued, _ := item.IsQueued()
assert.Equal(t, true, isQueued)
Expand All @@ -79,6 +80,16 @@ func TestCreateBuilds(t *testing.T) {
}
}

func TestGetQueueItem(t *testing.T) {
task, err := jenkins.GetQueueItem(queueID)
if err != nil {
t.Fatal(err)
}
if task.Raw == nil || task.Raw.ID != queueID {
t.Fatal()
}
}

func TestParseBuildHistory(t *testing.T) {
r, err := os.Open("_tests/build_history.txt")
if err != nil {
Expand Down Expand Up @@ -264,4 +275,4 @@ func getFileAsString(path string) string {
}

return string(buf)
}
}
17 changes: 15 additions & 2 deletions queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Task struct {
Raw *taskResponse
Jenkins *Jenkins
Queue *Queue
Base string
}

type taskResponse struct {
Expand All @@ -49,8 +50,12 @@ type taskResponse struct {
Name string `json:"name"`
URL string `json:"url"`
} `json:"task"`
URL string `json:"url"`
Why string `json:"why"`
URL string `json:"url"`
Why string `json:"why"`
Executable struct {
Number int64 `json:"number"`
URL string `json:"url"`
} `json:"executable"`
}

type generalAction struct {
Expand Down Expand Up @@ -134,3 +139,11 @@ func (q *Queue) Poll() (int, error) {
}
return response.StatusCode, nil
}

func (t *Task) Poll() (int, error) {
response, err := t.Jenkins.Requester.GetJSON(t.Base, t.Raw, nil)
if err != nil {
return 0, err
}
return response.StatusCode, nil
}

0 comments on commit 401552c

Please sign in to comment.