Skip to content

Commit 861da51

Browse files
committed
Add a GetJobObj() in jenkins.go, also add job as param in GetBuildFromQueueID()
This will avoid the problem of trying to create job object in GetBuildFromQueueID() from task.Name. task.Name can be different from job name when Job name has a folder structure. Signed-off-by: Hui Luo <[email protected]>
1 parent 9e2483f commit 861da51

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ if err != nil {
4545
panic("Something Went Wrong")
4646
}
4747

48-
queueid, err := jenkins.BuildJob(ctx, "#jobname", nil)
48+
job := jenkins.GetJobObj(ctx, "#jobname")
49+
queueid, err := jenkins.InvokeSimple(ctx, params)
4950
if err != nil {
5051
panic(err)
5152
}
52-
build, err := jenkins.GetBuildFromQueueID(ctx, queueid)
53+
build, err := jenkins.GetBuildFromQueueID(ctx, job, queueid)
5354
if err != nil {
5455
panic(err)
5556
}

jenkins.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,17 +264,22 @@ func (j *Jenkins) DeleteJob(ctx context.Context, name string) (bool, error) {
264264
return job.Delete(ctx)
265265
}
266266

267+
// Get a job object
268+
func (j *Jenkins) GetJobObj(ctx context.Context, name string) *Job {
269+
return &Job{Jenkins: j, Raw: new(JobResponse), Base: "/job/" + name}
270+
}
271+
267272
// Invoke a job.
268273
// First parameter job name, second parameter is optional Build parameters.
269274
// Returns queue id
270275
func (j *Jenkins) BuildJob(ctx context.Context, name string, params map[string]string) (int64, error) {
271-
job := Job{Jenkins: j, Raw: new(JobResponse), Base: "/job/" + name}
276+
job := j.GetJobObj(ctx, name)
272277
return job.InvokeSimple(ctx, params)
273278
}
274279

275280
// A task in queue will be assigned a build number in a job after a few seconds.
276281
// this function will return the build object.
277-
func (j *Jenkins) GetBuildFromQueueID(ctx context.Context, queueid int64) (*Build, error) {
282+
func (j *Jenkins) GetBuildFromQueueID(ctx context.Context, job *Job, queueid int64) (*Build, error) {
278283
task, err := j.GetQueueItem(ctx, queueid)
279284
if err != nil {
280285
return nil, err
@@ -288,12 +293,7 @@ func (j *Jenkins) GetBuildFromQueueID(ctx context.Context, queueid int64) (*Buil
288293
}
289294
}
290295

291-
buildid := task.Raw.Executable.Number
292-
job, err := task.GetJob(ctx)
293-
if err != nil {
294-
return nil, err
295-
}
296-
build, err := job.GetBuild(ctx, buildid)
296+
build, err := job.GetBuild(ctx, task.Raw.Executable.Number)
297297
if err != nil {
298298
return nil, err
299299
}

0 commit comments

Comments
 (0)