Verified Commit c351eea4 authored by Raphael Rösch's avatar Raphael Rösch Committed by GitLab
Browse files

feat: add job_inputs to PlayJobOptions

Changelog: Improvements
parent fc4b4cca
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -411,6 +411,7 @@ type CancelJobOptions struct {
// https://docs.gitlab.com/api/jobs/#run-a-job
type PlayJobOptions struct {
	JobVariablesAttributes *[]*JobVariableOptions `url:"job_variables_attributes,omitempty" json:"job_variables_attributes,omitempty"`
	JobInputs              PipelineInputsOption   `url:"job_inputs,omitempty" json:"job_inputs,omitempty"`
}

// JobVariableOptions represents a single job variable.
+68 −0
Original line number Diff line number Diff line
@@ -262,3 +262,71 @@ func TestJobsService_CancelJobWithOptions(t *testing.T) {
	assert.Equal(t, "test_job", job.Name)
	assert.Equal(t, "canceled", job.Status)
}

func TestJobsService_PlayJobWithJobVariablesAttributes(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)

	// GIVEN a job play endpoint
	mux.HandleFunc("/api/v4/projects/1/jobs/1/play", func(w http.ResponseWriter, r *http.Request) {
		// WHEN the play job request is made with job variables attributes
		testMethod(t, r, http.MethodPost)
		testBodyJSON(t, r, map[string]any{
			"job_variables_attributes": []any{
				map[string]any{
					"key":   "ENVIRONMENT",
					"value": "staging",
				},
			},
		})
		// THEN the response should contain the job details
		fmt.Fprint(w, `{"id":1,"name":"test_job","status":"pending"}`)
	})

	job, resp, err := client.Jobs.PlayJob(1, 1, &PlayJobOptions{
		JobVariablesAttributes: Ptr([]*JobVariableOptions{
			{
				Key:   Ptr("ENVIRONMENT"),
				Value: Ptr("staging"),
			},
		}),
	})

	assert.NoError(t, err)
	assert.NotNil(t, resp)
	assert.Equal(t, int64(1), job.ID)
	assert.Equal(t, "test_job", job.Name)
	assert.Equal(t, "pending", job.Status)
}

func TestJobsService_PlayJobWithJobInputs(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)

	// GIVEN a job play endpoint
	mux.HandleFunc("/api/v4/projects/1/jobs/1/play", func(w http.ResponseWriter, r *http.Request) {
		// WHEN the play job request is made with job inputs
		testMethod(t, r, http.MethodPost)
		testBodyJSON(t, r, map[string]any{
			"job_inputs": map[string]any{
				"environment": "staging",
				"version":     "v2.1.0",
			},
		})
		// THEN the response should contain the job details
		fmt.Fprint(w, `{"id":1,"name":"test_job","status":"pending"}`)
	})

	job, resp, err := client.Jobs.PlayJob(1, 1, &PlayJobOptions{
		JobInputs: PipelineInputsOption{
			"environment": NewPipelineInputValue("staging"),
			"version":     NewPipelineInputValue("v2.1.0"),
		},
	})

	assert.NoError(t, err)
	assert.NotNil(t, resp)
	assert.Equal(t, int64(1), job.ID)
	assert.Equal(t, "test_job", job.Name)
	assert.Equal(t, "pending", job.Status)
}