Skip to content
This repository has been archived by the owner on Dec 26, 2023. It is now read-only.

Commit

Permalink
feat(ui): add icon in run widget to show source of run (#563)
Browse files Browse the repository at this point in the history
Render an icon in the run widget to reveal the source of the run (UI,
API, CLI, Github, or Gitlab).
  • Loading branch information
leg100 committed Aug 6, 2023
1 parent 01a2112 commit 2e7a0bd
Show file tree
Hide file tree
Showing 28 changed files with 400 additions and 82 deletions.
20 changes: 13 additions & 7 deletions internal/api/configuration_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,22 @@ func (a *api) createConfigurationVersion(w http.ResponseWriter, r *http.Request)
Error(w, err)
return
}

opts := types.ConfigurationVersionCreateOptions{}
if err := unmarshal(r.Body, &opts); err != nil {
params := types.ConfigurationVersionCreateOptions{}
if err := unmarshal(r.Body, &params); err != nil {
Error(w, err)
return
}
cv, err := a.CreateConfigurationVersion(r.Context(), workspaceID, configversion.ConfigurationVersionCreateOptions{
AutoQueueRuns: opts.AutoQueueRuns,
Speculative: opts.Speculative,
})

opts := configversion.ConfigurationVersionCreateOptions{
AutoQueueRuns: params.AutoQueueRuns,
Speculative: params.Speculative,
Source: configversion.SourceAPI,
}
if r.Header.Get(headerSourceKey) == headerSourceValue {
opts.Source = configversion.SourceTerraform
}

cv, err := a.CreateConfigurationVersion(r.Context(), workspaceID, opts)
if err != nil {
Error(w, err)
return
Expand Down
62 changes: 35 additions & 27 deletions internal/api/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import (
"golang.org/x/exp/slices"
)

const (
headerSourceKey = "X-Terraform-Integration"
headerSourceValue = "cloud"
)

func (a *api) addRunHandlers(r *mux.Router) {
r = otfhttp.APIRouter(r)

Expand Down Expand Up @@ -54,38 +59,41 @@ func (a *api) addRunHandlers(r *mux.Router) {
}

func (a *api) createRun(w http.ResponseWriter, r *http.Request) {
var opts types.RunCreateOptions
if err := unmarshal(r.Body, &opts); err != nil {
var params types.RunCreateOptions
if err := unmarshal(r.Body, &params); err != nil {
Error(w, err)
return
}
if opts.Workspace == nil {
if params.Workspace == nil {
Error(w, &internal.MissingParameterError{Parameter: "workspace"})
return
}
var configurationVersionID *string
if opts.ConfigurationVersion != nil {
configurationVersionID = &opts.ConfigurationVersion.ID
}
vars := make([]run.Variable, len(opts.Variables))
for i, from := range opts.Variables {
vars[i] = run.Variable{Key: from.Key, Value: from.Value}
}
run, err := a.CreateRun(r.Context(), opts.Workspace.ID, run.CreateOptions{
AutoApply: opts.AutoApply,
IsDestroy: opts.IsDestroy,
Refresh: opts.Refresh,
RefreshOnly: opts.RefreshOnly,
Message: opts.Message,
ConfigurationVersionID: configurationVersionID,
TargetAddrs: opts.TargetAddrs,
ReplaceAddrs: opts.ReplaceAddrs,
PlanOnly: opts.PlanOnly,
Source: run.RunSourceAPI,
AllowEmptyApply: opts.AllowEmptyApply,
TerraformVersion: opts.TerraformVersion,
Variables: vars,
})

opts := run.CreateOptions{
AutoApply: params.AutoApply,
IsDestroy: params.IsDestroy,
Refresh: params.Refresh,
RefreshOnly: params.RefreshOnly,
Message: params.Message,
TargetAddrs: params.TargetAddrs,
ReplaceAddrs: params.ReplaceAddrs,
PlanOnly: params.PlanOnly,
Source: run.SourceAPI,
AllowEmptyApply: params.AllowEmptyApply,
TerraformVersion: params.TerraformVersion,
}
if params.ConfigurationVersion != nil {
opts.ConfigurationVersionID = &params.ConfigurationVersion.ID
}
if r.Header.Get(headerSourceKey) == headerSourceValue {
opts.Source = run.SourceTerraform
}
opts.Variables = make([]run.Variable, len(params.Variables))
for i, from := range params.Variables {
opts.Variables[i] = run.Variable{Key: from.Key, Value: from.Value}
}

run, err := a.CreateRun(r.Context(), params.Workspace.ID, opts)
if err != nil {
Error(w, err)
return
Expand Down Expand Up @@ -163,7 +171,7 @@ func (a *api) listRuns(w http.ResponseWriter, r *http.Request) {
// convert comma-separated list of statuses to []RunStatus
statuses := internal.FromStringCSV[internal.RunStatus](params.Status)
// convert comma-separated list of sources to []RunSource
sources := internal.FromStringCSV[run.RunSource](params.Source)
sources := internal.FromStringCSV[run.Source](params.Source)
// split operations CSV
operations := internal.SplitCSV(params.Operation)
var planOnly *bool
Expand Down
8 changes: 8 additions & 0 deletions internal/cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import (
"net/http"
)

const (
Github = "github"
Gitlab = "gitlab"
)

// Kind is the kind of cloud provider, e.g. github, gitlab, etc.
type Kind string

// Cloud is an external provider of various cloud services e.g. identity provider, VCS
// repositories etc.
type Cloud interface {
Expand Down
2 changes: 2 additions & 0 deletions internal/cloud/vcs_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type (
//
// These fields are populated by cloud-specific handlers
//
Cloud Kind

Type VCSEventType
Action VCSAction
Tag string
Expand Down
14 changes: 7 additions & 7 deletions internal/configversion/configuration_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
)

const (
DefaultConfigurationSource = "tfe-api"
DefaultAutoQueueRuns = true
DefaultAutoQueueRuns = true

// List all available configuration version statuses.
ConfigurationErrored ConfigurationStatus = "errored"
Expand All @@ -29,7 +28,7 @@ type (
ID string
CreatedAt time.Time
AutoQueueRuns bool
Source ConfigurationSource
Source Source
Speculative bool
Status ConfigurationStatus
StatusTimestamps []ConfigurationVersionStatusTimestamp
Expand All @@ -43,15 +42,13 @@ type (
ConfigurationVersionCreateOptions struct {
AutoQueueRuns *bool
Speculative *bool
Source Source
*IngressAttributes
}

// ConfigurationStatus represents a configuration version status.
ConfigurationStatus string

// ConfigurationSource represents a source of a configuration version.
ConfigurationSource string

ConfigurationVersionStatusTimestamp struct {
Status ConfigurationStatus
Timestamp time.Time
Expand Down Expand Up @@ -116,11 +113,14 @@ func NewConfigurationVersion(workspaceID string, opts ConfigurationVersionCreate
ID: internal.NewID("cv"),
CreatedAt: internal.CurrentTimestamp(),
AutoQueueRuns: DefaultAutoQueueRuns,
Source: DefaultConfigurationSource,
Source: DefaultSource,
WorkspaceID: workspaceID,
}
cv.updateStatus(ConfigurationPending)

if opts.Source != "" {
cv.Source = opts.Source
}
if opts.AutoQueueRuns != nil {
cv.AutoQueueRuns = *opts.AutoQueueRuns
}
Expand Down
2 changes: 1 addition & 1 deletion internal/configversion/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (result pgRow) toConfigVersion() *ConfigurationVersion {
CreatedAt: result.CreatedAt.Time.UTC(),
AutoQueueRuns: result.AutoQueueRuns,
Speculative: result.Speculative,
Source: ConfigurationSource(result.Source.String),
Source: Source(result.Source.String),
Status: ConfigurationStatus(result.Status.String),
StatusTimestamps: unmarshalStatusTimestampRows(result.ConfigurationVersionStatusTimestamps),
WorkspaceID: result.WorkspaceID.String,
Expand Down
13 changes: 13 additions & 0 deletions internal/configversion/source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package configversion

const (
SourceAPI Source = "tfe-api"
SourceGithub Source = "github"
SourceGitlab Source = "gitlab"
SourceTerraform Source = "terraform+cloud"

DefaultSource = SourceAPI
)

// Source representse of a run.
type Source string
4 changes: 3 additions & 1 deletion internal/github/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ func handle(r *http.Request, secret string) (*cloud.VCSEvent, error) {
}

// convert github event to an OTF event
var to cloud.VCSEvent
to := cloud.VCSEvent{
Cloud: cloud.Github,
}

switch event := rawEvent.(type) {
case *github.PushEvent:
Expand Down
4 changes: 4 additions & 0 deletions internal/github/event_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestEventHandler(t *testing.T) {
"push",
"./testdata/github_push.json",
&cloud.VCSEvent{
Cloud: cloud.Github,
Type: cloud.VCSEventTypePush,
Branch: "master",
DefaultBranch: "master",
Expand All @@ -40,6 +41,7 @@ func TestEventHandler(t *testing.T) {
"pull_request",
"./testdata/github_pull_opened.json",
&cloud.VCSEvent{
Cloud: cloud.Github,
Type: cloud.VCSEventTypePull,
Branch: "pr-2",
DefaultBranch: "master",
Expand All @@ -59,6 +61,7 @@ func TestEventHandler(t *testing.T) {
"pull_request",
"./testdata/github_pull_update.json",
&cloud.VCSEvent{
Cloud: cloud.Github,
Type: cloud.VCSEventTypePull,
Branch: "pr-1",
DefaultBranch: "master",
Expand All @@ -78,6 +81,7 @@ func TestEventHandler(t *testing.T) {
"push",
"./testdata/github_push_tag.json",
&cloud.VCSEvent{
Cloud: cloud.Github,
Type: cloud.VCSEventTypeTag,
Tag: "v1.0.0",
DefaultBranch: "master",
Expand Down
Loading

0 comments on commit 2e7a0bd

Please sign in to comment.