api: prevent panic on job plan (#17689)

Check for a nil job ID to prevent a panic when calling Jobs().Plan().
This commit is contained in:
Luiz Aoqui 2023-06-23 16:20:52 -04:00 committed by GitHub
parent d62c34b9f9
commit 9aa9779d80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 0 deletions

3
.changelog/17689.txt Normal file
View File

@ -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
```

View File

@ -447,6 +447,9 @@ func (j *Jobs) PlanOpts(job *Job, opts *PlanOptions, q *WriteOptions) (*JobPlanR
if job == nil { if job == nil {
return nil, nil, errors.New("must pass non-nil job") 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 // Setup the request
req := &JobPlanRequest{ req := &JobPlanRequest{

View File

@ -2059,6 +2059,12 @@ func TestJobs_Plan(t *testing.T) {
_, _, err = jobs.Plan(nil, true, nil) _, _, err = jobs.Plan(nil, true, nil)
must.Error(t, err) 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 // Make a plan request
planResp, wm, err := jobs.Plan(job, true, nil) planResp, wm, err := jobs.Plan(job, true, nil)
must.NoError(t, err) must.NoError(t, err)