Skip to content

Commit

Permalink
api: prevent panic on job plan (#17689)
Browse files Browse the repository at this point in the history
Check for a nil job ID to prevent a panic when calling Jobs().Plan().
  • Loading branch information
lgfa29 authored and philrenaud committed Jun 26, 2023
1 parent bf315bd commit 78a8d05
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/17689.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
api: Fixed a bug that caused a panic when calling the `Jobs().Plan()` function with a job missing an ID
```
3 changes: 3 additions & 0 deletions api/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,9 @@ func (j *Jobs) PlanOpts(job *Job, opts *PlanOptions, q *WriteOptions) (*JobPlanR
if job == nil {
return nil, nil, errors.New("must pass non-nil job")
}
if job.ID == nil {
return nil, nil, errors.New("job is missing ID")
}

// Setup the request
req := &JobPlanRequest{
Expand Down
6 changes: 6 additions & 0 deletions api/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2059,6 +2059,12 @@ func TestJobs_Plan(t *testing.T) {
_, _, err = jobs.Plan(nil, true, nil)
must.Error(t, err)

// Check that passing a nil job ID fails
invalidJob := testJob()
invalidJob.ID = nil
_, _, err = jobs.Plan(invalidJob, true, nil)
must.Error(t, err)

// Make a plan request
planResp, wm, err := jobs.Plan(job, true, nil)
must.NoError(t, err)
Expand Down

0 comments on commit 78a8d05

Please sign in to comment.