Skip to content

Commit

Permalink
feat/unify/runspec (#1683)
Browse files Browse the repository at this point in the history
  • Loading branch information
motatoes authored Aug 29, 2024
1 parent 91358c8 commit a91951e
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 235 deletions.
5 changes: 4 additions & 1 deletion backend/services/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/diggerhq/digger/backend/models"
"github.com/diggerhq/digger/backend/utils"
"github.com/diggerhq/digger/libs/digger_config"
"github.com/diggerhq/digger/libs/scheduler"
"github.com/diggerhq/digger/libs/spec"
"github.com/samber/lo"
Expand Down Expand Up @@ -111,7 +112,6 @@ func GetSpecFromJob(job models.DiggerJob) (*spec.Spec, error) {
batch := job.Batch

spec := spec.Spec{
SpecType: spec.SpecTypeApplyBeforeMergeJob,
JobId: job.DiggerJobID,
CommentId: strconv.FormatInt(*batch.CommentId, 10),
Job: jobSpec,
Expand Down Expand Up @@ -140,6 +140,9 @@ func GetSpecFromJob(job models.DiggerJob) (*spec.Spec, error) {
Policy: spec.PolicySpec{
PolicyType: "http",
},
CommentUpdater: spec.CommentUpdaterSpec{
CommentUpdaterType: digger_config.CommentRenderModeBasic,
},
}
return &spec, nil
}
41 changes: 14 additions & 27 deletions cli/cmd/digger/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,20 @@ var defaultCmd = &cobra.Command{
}

var spec_err error
if spec.SpecType == lib_spec.SpecTypeNextJob {
spec_err = spec2.RunSpecNext(
spec,
lib_spec.VCSProviderBasic{},
lib_spec.JobSpecProvider{},
lib_spec.LockProvider{},
lib_spec.ReporterProvider{},
lib_spec.BackendApiProvider{},
lib_spec.BasicPolicyProvider{},
lib_spec.PlanStorageProvider{},
lib_spec.VariablesProvider{},
comment_updater.CommentUpdaterProviderBasic{},
)
} else {
spec_err = spec2.RunSpec(
spec,
lib_spec.VCSProviderBasic{},
lib_spec.JobSpecProvider{},
lib_spec.LockProvider{},
lib_spec.ReporterProvider{},
lib_spec.BackendApiProvider{},
lib_spec.BasicPolicyProvider{},
lib_spec.PlanStorageProvider{},
lib_spec.VariablesProvider{},
comment_updater.CommentUpdaterProviderBasic{},
)
}

spec_err = spec2.RunSpec(
spec,
lib_spec.VCSProviderBasic{},
lib_spec.JobSpecProvider{},
lib_spec.LockProvider{},
lib_spec.ReporterProvider{},
lib_spec.BackendApiProvider{},
lib_spec.BasicPolicyProvider{},
lib_spec.PlanStorageProvider{},
lib_spec.VariablesProvider{},
comment_updater.CommentUpdaterProviderBasic{},
)

if spec_err != nil {
usage.ReportErrorAndExit(spec.VCS.Actor, fmt.Sprintf("error while running spec: %v", err), 1)
}
Expand Down
180 changes: 0 additions & 180 deletions cli/pkg/spec/next.go

This file was deleted.

37 changes: 30 additions & 7 deletions cli/pkg/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"github.com/diggerhq/digger/cli/pkg/usage"
backend2 "github.com/diggerhq/digger/libs/backendapi"
comment_summary "github.com/diggerhq/digger/libs/comment_utils/summary"
"github.com/diggerhq/digger/libs/digger_config"
"github.com/diggerhq/digger/libs/scheduler"
"github.com/diggerhq/digger/libs/spec"
"github.com/samber/lo"
"log"
"os"
"os/exec"
"time"
)

Expand Down Expand Up @@ -43,6 +43,31 @@ func RunSpec(
usage.ReportErrorAndExit(spec.VCS.Actor, fmt.Sprintf("could not get backend api: %v", err), 1)
}

if spec.Job.Commit != "" {
// checking out to the commit ID
log.Printf("fetching commit ID %v", spec.Job.Commit)
fetchCmd := exec.Command("git", "fetch", "origin", spec.Job.Commit)
fetchCmd.Stdout = os.Stdout
fetchCmd.Stderr = os.Stderr
err = fetchCmd.Run()
if err != nil {
msg := fmt.Sprintf("error while fetching commit SHA: %v", err)
reportError(spec, backendApi, msg, err)
usage.ReportErrorAndExit(spec.VCS.Actor, fmt.Sprintf("error while checking out to commit sha: %v", err), 1)
}

log.Printf("checking out to commit ID %v", spec.Job.Commit)
cmd := exec.Command("git", "checkout", spec.Job.Commit)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
msg := fmt.Sprintf("error while checking out to commit SHA: %v", err)
reportError(spec, backendApi, msg, err)
usage.ReportErrorAndExit(spec.VCS.Actor, fmt.Sprintf("error while checking out to commit sha: %v", err), 1)
}
}

job, err := jobProvider.GetJob(spec.Job)
if err != nil {
message := fmt.Sprintf("could not get job: %v", err)
Expand Down Expand Up @@ -79,8 +104,8 @@ func RunSpec(
reportError(spec, backendApi, message, err)
}

// TODO: render mode being passable from the string
commentUpdater, err := commentUpdaterProvider.Get(digger_config.CommentRenderModeBasic)
// TODO: render mode being passable from the spec as a string
commentUpdater, err := commentUpdaterProvider.Get(spec.CommentUpdater.CommentUpdaterType)
if err != nil {
message := fmt.Sprintf("could not get comment updater: %v", err)
reportError(spec, backendApi, message, err)
Expand All @@ -92,13 +117,11 @@ func RunSpec(
reportError(spec, backendApi, message, err)
}

// TODO: make this part purely based on variables providers

// get variables from the variables spec
variablesMap, err := variablesProvider.GetVariables(spec.Variables)
if err != nil {
log.Printf("could not get variables from provider: %v", err)
reporterError(spec, backendApi, err)
msg := fmt.Sprintf("could not get variables from provider: %v", err)
reportError(spec, backendApi, msg, err)
usage.ReportErrorAndExit(spec.VCS.Actor, fmt.Sprintf("could not get variables from provider: %v", err), 1)
}
job.StateEnvVars = lo.Assign(job.StateEnvVars, variablesMap)
Expand Down
3 changes: 2 additions & 1 deletion libs/comment_utils/summary/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ func (c CommentUpdaterProviderBasic) Get(renderMode string) (CommentUpdater, err
if renderMode == digger_config.CommentRenderModeBasic {
return BasicCommentUpdater{}, nil
} else if renderMode == digger_config.CommentRenderModeGroupByModule {

commentUpdater := BasicCommentUpdater{}
return commentUpdater, nil
} else if renderMode == "noop" {
return NoopCommentUpdater{}, nil
} else {
return nil, fmt.Errorf("Unknown comment render mode found: %v", renderMode)
}
Expand Down
22 changes: 12 additions & 10 deletions libs/spec/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ type ReporterSpec struct {
ReportTerraformOutput bool `json:"report_terraform_output"`
}

type CommentUpdaterSpec struct {
CommentUpdaterType string `json:"comment_updater_type"`
}

type LockSpec struct {
LockType string `json:"lock_type"`
LockProvider string `json:"lock_provider"`
Expand Down Expand Up @@ -45,10 +49,7 @@ type VariableSpec struct {

type SpecType string

const SpecTypeNextJob SpecType = "next_run_spec"
const SpecTypeApplyBeforeMergeJob SpecType = "before_merge_spec"
const SpecTypeManualJob SpecType = "manual_job"
const SpecTypeDriftJob SpecType = "drift_job"

type Spec struct {
// TODO: replace these three to be nested into one of the other specs
Expand All @@ -57,11 +58,12 @@ type Spec struct {
CommentId string `json:"comment_id"`
RunName string `json:"run_name"`

Job scheduler.JobJson `json:"job"`
Reporter ReporterSpec `json:"reporter"`
Lock LockSpec `json:"lock"`
Backend BackendSpec `json:"backend"`
VCS VcsSpec `json:"vcs"`
Policy PolicySpec `json:"policy_provider"`
Variables []VariableSpec `json:"variables"`
Job scheduler.JobJson `json:"job"`
Reporter ReporterSpec `json:"reporter"`
CommentUpdater CommentUpdaterSpec `json:"comment_updater"`
Lock LockSpec `json:"lock"`
Backend BackendSpec `json:"backend"`
VCS VcsSpec `json:"vcs"`
Policy PolicySpec `json:"policy_provider"`
Variables []VariableSpec `json:"variables"`
}
Loading

0 comments on commit a91951e

Please sign in to comment.