Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new runs list options #424

Merged
merged 1 commit into from Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions run.go
Expand Up @@ -83,6 +83,18 @@ const (
RunSourceUI RunSource = "tfe-ui"
)

// RunOperation represents an operation type of run.
type RunOperation string

// List all available run operations.
const (
RunOperationPlanApply RunOperation = "plan_and_apply"
RunOperationPlanOnly RunOperation = "plan_only"
RunOperationRefreshOnly RunOperation = "refresh_only"
RunOperationDestroy RunOperation = "destroy"
RunOperationEmptyApply RunOperation = "empty_apply"
)

// RunList represents a list of runs.
type RunList struct {
*Pagination
Expand Down Expand Up @@ -180,6 +192,27 @@ const (
// RunListOptions represents the options for listing runs.
type RunListOptions struct {
ListOptions

// Optional: Username of user who created the run
// **Note: This API is still in BETA and is subject to change.**
Name string `url:"search[name],omitempty"`

// Optional: Commit SHA for runs triggered via a vcs event
// **Note: This API is still in BETA and is subject to change.**
Commit string `url:"search[commit],omitempty"`

// Optional: Current status of the run
// **Note: This API is still in BETA and is subject to change.**
Status string `url:"filter[status],omitempty"`

// Optional: Source that triggered the run
// **Note: This API is still in BETA and is subject to change.**
Source string `url:"filter[source],omitempty"`

// Optional: Operation type for the run
// **Note: This API is still in BETA and is subject to change.**
Operation string `url:"filter[operation],omitempty"`

// Optional: A list of relations to include. See available resources:
// https://www.terraform.io/docs/cloud/api/run.html#available-related-resources
Include []RunIncludeOpt `url:"include,omitempty"`
Expand Down
76 changes: 76 additions & 0 deletions run_integration_test.go
Expand Up @@ -94,6 +94,82 @@ func TestRunsList(t *testing.T) {
})
}

func TestRunsListQueryParams(t *testing.T) {
mjyocca marked this conversation as resolved.
Show resolved Hide resolved
type testCase struct {
options *RunListOptions
description string
assertion func(tc testCase, rl *RunList, err error)
}

client := testClient(t)
ctx := context.Background()

orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()

workspaceTest, _ := createWorkspace(t, client, orgTest)
createPlannedRun(t, client, workspaceTest)
pendingRun, _ := createRun(t, client, workspaceTest)

currentUser, _ := client.Users.ReadCurrent(ctx)

testCases := []testCase{
mjyocca marked this conversation as resolved.
Show resolved Hide resolved
{
description: "with status query parameter",
options: &RunListOptions{Status: string(RunPending), Include: []RunIncludeOpt{RunWorkspace}},
assertion: func(tc testCase, rl *RunList, err error) {
assert.NoError(t, err)
assert.Equal(t, 1, len(rl.Items))
},
},
{
description: "with source query parameter",
options: &RunListOptions{Source: string(RunSourceAPI), Include: []RunIncludeOpt{RunWorkspace}},
assertion: func(tc testCase, rl *RunList, err error) {
assert.NoError(t, err)
assert.Equal(t, 2, len(rl.Items))
assert.Equal(t, rl.Items[0].Source, RunSourceAPI)
},
},
{
description: "with operation of plan_only parameter",
options: &RunListOptions{Operation: string(RunOperationPlanOnly), Include: []RunIncludeOpt{RunWorkspace}},
assertion: func(tc testCase, rl *RunList, err error) {
assert.NoError(t, err)
assert.Equal(t, 0, len(rl.Items))
},
},
{
description: "with name parameter",
options: &RunListOptions{Name: currentUser.Username, Include: []RunIncludeOpt{RunWorkspace}},
assertion: func(tc testCase, rl *RunList, err error) {
found := []string{}
for _, r := range rl.Items {
found = append(found, r.ID)
}
assert.Equal(t, 2, len(rl.Items))
assert.Contains(t, found, pendingRun.ID)
},
},
{
description: "with name & commit parameter",
options: &RunListOptions{Name: randomString(t), Commit: randomString(t), Include: []RunIncludeOpt{RunWorkspace}},
assertion: func(tc testCase, rl *RunList, err error) {
assert.NoError(t, err)
assert.Equal(t, 0, len(rl.Items))
},
},
}

for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
skipIfBeta(t)
runs, err := client.Runs.List(ctx, workspaceTest.ID, testCase.options)
testCase.assertion(testCase, runs, err)
})
}
}

func TestRunsCreate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
Expand Down