Skip to content

Commit aae07e9

Browse files
Add CanceledAt, RunEvents, and TriggerReason to Run (#1188)
* Adds CanceledAt, RunEvents, TriggerReason fields to Run * Updated changelog --------- Co-authored-by: jpadrianoGo <[email protected]>
1 parent f28c909 commit aae07e9

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Enhancements
44

55
* Adds `Logs` method to `QueryRuns`, which is EXPERIMENTAL, SUBJECT TO CHANGE, and may not be available to all users by @brandonc [#1186](https://github.com/hashicorp/go-tfe/pull/1186)
6+
* Adds `CanceledAt`, `RunEvents`, `TriggerReason` field to `Run` by @jpadrianoGo [#1161](https://github.com/hashicorp/go-tfe/pull/1161)
67
* Adds `CreatedAt` field to `AgentPool` by @jpadrianoGo [#1150](https://github.com/hashicorp/go-tfe/pull/1150)
78
* Adds `CreatedBy` relation to `AgentToken` by @jpadrianoGo [#1149](https://github.com/hashicorp/go-tfe/pull/1149)
89

run.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ type Run struct {
135135
AutoApply bool `jsonapi:"attr,auto-apply,omitempty"`
136136
AllowConfigGeneration *bool `jsonapi:"attr,allow-config-generation,omitempty"`
137137
AllowEmptyApply bool `jsonapi:"attr,allow-empty-apply"`
138+
CanceledAt time.Time `jsonapi:"attr,canceled-at,iso8601"`
138139
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
139140
ForceCancelAvailableAt time.Time `jsonapi:"attr,force-cancel-available-at,iso8601"`
140141
HasChanges bool `jsonapi:"attr,has-changes"`
@@ -153,6 +154,7 @@ type Run struct {
153154
StatusTimestamps *RunStatusTimestamps `jsonapi:"attr,status-timestamps"`
154155
TargetAddrs []string `jsonapi:"attr,target-addrs,omitempty"`
155156
TerraformVersion string `jsonapi:"attr,terraform-version"`
157+
TriggerReason string `jsonapi:"attr,trigger-reason"`
156158
Variables []*RunVariableAttr `jsonapi:"attr,variables"`
157159

158160
// Relations
@@ -163,6 +165,7 @@ type Run struct {
163165
ConfirmedBy *User `jsonapi:"relation,confirmed-by"`
164166
Plan *Plan `jsonapi:"relation,plan"`
165167
PolicyChecks []*PolicyCheck `jsonapi:"relation,policy-checks"`
168+
RunEvents []*RunEvent `jsonapi:"relation,run-events"`
166169
TaskStages []*TaskStage `jsonapi:"relation,task-stages,omitempty"`
167170
Workspace *Workspace `jsonapi:"relation,workspace"`
168171
Comments []*Comment `jsonapi:"relation,comments"`

run_integration_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,96 @@ func TestRunsConfirmedBy(t *testing.T) {
473473
})
474474
}
475475

476+
func TestRunsCanceledAt(t *testing.T) {
477+
client := testClient(t)
478+
479+
ctx := context.Background()
480+
481+
wTest, wTestCleanup := createWorkspace(t, client, nil)
482+
t.Cleanup(wTestCleanup)
483+
484+
// We need to create 2 runs here. The first run will automatically
485+
// be planned so that one cannot be cancelled. The second one will
486+
// be pending until the first one is confirmed or discarded, so we
487+
// can cancel that one.
488+
createRun(t, client, wTest)
489+
rTest, _ := createRun(t, client, wTest)
490+
491+
t.Run("when the run is not canceled", func(t *testing.T) {
492+
r, err := client.Runs.Read(ctx, rTest.ID)
493+
require.NoError(t, err)
494+
495+
assert.Empty(t, r.CanceledAt)
496+
})
497+
498+
t.Run("when the run is canceled", func(t *testing.T) {
499+
err := client.Runs.Cancel(ctx, rTest.ID, RunCancelOptions{})
500+
require.NoError(t, err)
501+
502+
for i := 1; ; i++ {
503+
// Refresh the view of the run
504+
rTest, err = client.Runs.Read(ctx, rTest.ID)
505+
require.NoError(t, err)
506+
507+
// Check if the timestamp is present.
508+
if !rTest.ForceCancelAvailableAt.IsZero() {
509+
break
510+
}
511+
512+
if i > 30 {
513+
t.Fatal("Timeout waiting for run to be canceled")
514+
}
515+
516+
time.Sleep(time.Second)
517+
}
518+
519+
r, err := client.Runs.Read(ctx, rTest.ID)
520+
require.NoError(t, err)
521+
522+
assert.NotEmpty(t, r.CanceledAt)
523+
})
524+
}
525+
526+
func TestRunsRunEvents(t *testing.T) {
527+
client := testClient(t)
528+
ctx := context.Background()
529+
530+
wTest, wTestCleanup := createWorkspace(t, client, nil)
531+
defer wTestCleanup()
532+
533+
_, cvCleanup := createUploadedConfigurationVersion(t, client, wTest)
534+
t.Cleanup(cvCleanup)
535+
536+
options := RunCreateOptions{
537+
Workspace: wTest,
538+
}
539+
540+
r, err := client.Runs.Create(ctx, options)
541+
require.NoError(t, err)
542+
543+
assert.NotEmpty(t, r.RunEvents)
544+
}
545+
546+
func TestRunsTriggerReason(t *testing.T) {
547+
client := testClient(t)
548+
ctx := context.Background()
549+
550+
wTest, wTestCleanup := createWorkspace(t, client, nil)
551+
defer wTestCleanup()
552+
553+
_, cvCleanup := createUploadedConfigurationVersion(t, client, wTest)
554+
t.Cleanup(cvCleanup)
555+
556+
options := RunCreateOptions{
557+
Workspace: wTest,
558+
}
559+
560+
r, err := client.Runs.Create(ctx, options)
561+
require.NoError(t, err)
562+
563+
assert.NotNil(t, r.TriggerReason)
564+
}
565+
476566
func TestRunsApply(t *testing.T) {
477567
client := testClient(t)
478568
ctx := context.Background()

0 commit comments

Comments
 (0)