2021-12-08 00:35:04 +01:00
|
|
|
package cloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-tfe"
|
|
|
|
)
|
|
|
|
|
|
|
|
type taskResultSummary struct {
|
2021-12-15 18:02:48 +01:00
|
|
|
unreachable bool
|
2021-12-08 00:35:04 +01:00
|
|
|
pending int
|
|
|
|
failed int
|
|
|
|
failedMandatory int
|
|
|
|
passed int
|
|
|
|
}
|
|
|
|
|
|
|
|
type taskStageReadFunc func(b *Cloud, stopCtx context.Context) (*tfe.TaskStage, error)
|
|
|
|
|
2021-12-15 18:02:48 +01:00
|
|
|
func summarizeTaskResults(taskResults []*tfe.TaskResult) *taskResultSummary {
|
2022-01-04 22:39:43 +01:00
|
|
|
var pendingCount, errCount, errMandatoryCount, passedCount int
|
2021-12-08 00:35:04 +01:00
|
|
|
for _, task := range taskResults {
|
2021-12-15 18:02:48 +01:00
|
|
|
if task.Status == "unreachable" {
|
|
|
|
return &taskResultSummary{
|
|
|
|
unreachable: true,
|
|
|
|
}
|
2022-01-04 22:39:43 +01:00
|
|
|
} else if task.Status == "running" || task.Status == "pendingCountnding" {
|
|
|
|
pendingCount++
|
2021-12-08 00:35:04 +01:00
|
|
|
} else if task.Status == "passed" {
|
2022-01-04 22:39:43 +01:00
|
|
|
passedCount++
|
2021-12-08 00:35:04 +01:00
|
|
|
} else {
|
|
|
|
// Everything else is a failure
|
2022-01-04 22:39:43 +01:00
|
|
|
errCount++
|
2021-12-08 00:35:04 +01:00
|
|
|
if task.WorkspaceTaskEnforcementLevel == "mandatory" {
|
2022-01-04 22:39:43 +01:00
|
|
|
errMandatoryCount++
|
2021-12-08 00:35:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 18:02:48 +01:00
|
|
|
return &taskResultSummary{
|
|
|
|
unreachable: false,
|
2022-01-04 22:39:43 +01:00
|
|
|
pending: pendingCount,
|
|
|
|
failed: errCount,
|
|
|
|
failedMandatory: errMandatoryCount,
|
|
|
|
passed: passedCount,
|
2021-12-08 00:35:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Cloud) runTasksWithTaskResults(context *IntegrationContext, output IntegrationOutputWriter, fetchTaskStage taskStageReadFunc) error {
|
|
|
|
return context.Poll(func(i int) (bool, error) {
|
|
|
|
stage, err := fetchTaskStage(b, context.StopContext)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return false, generalError("Failed to retrieve pre-apply task stage", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
summary := summarizeTaskResults(stage.TaskResults)
|
2021-12-15 18:02:48 +01:00
|
|
|
|
|
|
|
if summary.unreachable {
|
|
|
|
output.Output("Skipping task results.")
|
|
|
|
output.End()
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-12-08 00:35:04 +01:00
|
|
|
if summary.pending > 0 {
|
2021-12-15 18:02:48 +01:00
|
|
|
pendingMessage := "%d tasks still pending, %d passed, %d failed ... "
|
|
|
|
message := fmt.Sprintf(pendingMessage, summary.pending, summary.passed, summary.failed)
|
2021-12-08 00:35:04 +01:00
|
|
|
|
|
|
|
if i%4 == 0 {
|
|
|
|
if i > 0 {
|
2021-12-15 18:02:48 +01:00
|
|
|
output.OutputElapsed(message, len(pendingMessage)) // Up to 2 digits are allowed by the max message allocation
|
2021-12-08 00:35:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// No more tasks pending/running. Print all the results.
|
|
|
|
|
|
|
|
// Track the first task name that is a mandatory enforcement level breach.
|
|
|
|
var firstMandatoryTaskFailed *string = nil
|
|
|
|
|
|
|
|
if i == 0 {
|
|
|
|
output.Output(fmt.Sprintf("All tasks completed! %d passed, %d failed", summary.passed, summary.failed))
|
|
|
|
} else {
|
|
|
|
output.OutputElapsed(fmt.Sprintf("All tasks completed! %d passed, %d failed", summary.passed, summary.failed), 50)
|
|
|
|
}
|
|
|
|
|
|
|
|
output.Output("")
|
|
|
|
|
|
|
|
for _, t := range stage.TaskResults {
|
|
|
|
capitalizedStatus := string(t.Status)
|
|
|
|
capitalizedStatus = strings.ToUpper(capitalizedStatus[:1]) + capitalizedStatus[1:]
|
|
|
|
|
|
|
|
status := "[green]" + capitalizedStatus
|
|
|
|
if t.Status != "passed" {
|
|
|
|
level := string(t.WorkspaceTaskEnforcementLevel)
|
|
|
|
level = strings.ToUpper(level[:1]) + level[1:]
|
|
|
|
status = fmt.Sprintf("[red]%s (%s)", capitalizedStatus, level)
|
|
|
|
|
|
|
|
if t.WorkspaceTaskEnforcementLevel == "mandatory" && firstMandatoryTaskFailed == nil {
|
|
|
|
firstMandatoryTaskFailed = &t.TaskName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
title := fmt.Sprintf(`%s ⸺ %s`, t.TaskName, status)
|
|
|
|
output.SubOutput(title)
|
|
|
|
|
|
|
|
output.SubOutput(fmt.Sprintf("[dim]%s", t.Message))
|
|
|
|
output.SubOutput("")
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a mandatory enforcement level is breached, return an error.
|
|
|
|
var taskErr error = nil
|
|
|
|
var overall string = "[green]Passed"
|
|
|
|
if firstMandatoryTaskFailed != nil {
|
|
|
|
overall = "[red]Failed"
|
2021-12-15 18:09:07 +01:00
|
|
|
if summary.failedMandatory > 1 {
|
|
|
|
taskErr = fmt.Errorf("the run failed because %d mandatory tasks are required to succeed", summary.failedMandatory)
|
|
|
|
} else {
|
|
|
|
taskErr = fmt.Errorf("the run failed because the run task, %s, is required to succeed", *firstMandatoryTaskFailed)
|
|
|
|
}
|
2021-12-08 00:35:04 +01:00
|
|
|
} else if summary.failed > 0 { // we have failures but none of them mandatory
|
|
|
|
overall = "[green]Passed with advisory failures"
|
|
|
|
}
|
|
|
|
|
|
|
|
output.SubOutput("")
|
|
|
|
output.SubOutput("[bold]Overall Result: " + overall)
|
|
|
|
|
|
|
|
output.End()
|
|
|
|
|
|
|
|
return false, taskErr
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Cloud) runTasks(ctx *IntegrationContext, output IntegrationOutputWriter, stageID string) error {
|
|
|
|
return b.runTasksWithTaskResults(ctx, output, func(b *Cloud, stopCtx context.Context) (*tfe.TaskStage, error) {
|
|
|
|
options := tfe.TaskStageReadOptions{
|
|
|
|
Include: "task_results",
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.client.TaskStages.Read(ctx.StopContext, stageID, &options)
|
|
|
|
})
|
|
|
|
}
|