Skip to content

Commit

Permalink
feat(tmc): send metadata in drift syncs (#1151)
Browse files Browse the repository at this point in the history
  • Loading branch information
i4k-tm committed Sep 21, 2023
2 parents f449935 + edac84e commit 3c25c99
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 31 deletions.
13 changes: 10 additions & 3 deletions cloud/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ type (

// DriftStackPayloadRequest is the payload for the drift sync.
DriftStackPayloadRequest struct {
Stack Stack `json:"stack"`
Status stack.Status `json:"drift_status"`
Metadata GitHubMetadata `json:"metadata,omitempty"`
Stack Stack `json:"stack"`
Status stack.Status `json:"drift_status"`
Metadata *DeploymentMetadata `json:"metadata,omitempty"`
}

// DriftStackPayloadRequests is a list of DriftStackPayloadRequest
Expand Down Expand Up @@ -316,6 +316,13 @@ func (d DriftStackPayloadRequest) Validate() error {
if err := d.Stack.Validate(); err != nil {
return err
}
if d.Metadata != nil {
err := d.Metadata.Validate()
if err != nil {
return err
}
}

return d.Status.Validate()
}

Expand Down
40 changes: 32 additions & 8 deletions cmd/terramate/cli/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cli

import (
"context"
stdjson "encoding/json"
"os"
"strings"
"time"
Expand Down Expand Up @@ -37,7 +38,9 @@ type cloudConfig struct {
runUUID string
orgUUID string

meta2id map[string]int
meta2id map[string]int
reviewRequest *cloud.DeploymentReviewRequest
metadata *cloud.DeploymentMetadata
}
}

Expand Down Expand Up @@ -206,24 +209,42 @@ func (c *cli) cloudInfo() {
c.cloud.output.MsgStdOutV("next token refresh in: %s", time.Until(c.cred().ExpireAt()))
}

func (c *cli) tryGithubMetadata() (*cloud.DeploymentReviewRequest, *cloud.DeploymentMetadata, string) {
func (c *cli) detectCloudMetadata() {
logger := log.With().
Str("normalized_repository", c.prj.prettyRepo()).
Str("head_commit", c.prj.headCommit()).
Logger()

if c.prj.prettyRepo() == "local" {
logger.Debug().Msg("skipping review_request and metadata for local repository")
return
}

r, err := repository.Parse(c.prj.prettyRepo())
if err != nil {
logger.Debug().
Msg("repository cannot be normalized: skipping pull request retrievals for commit")

return nil, nil, ""
return
}

if r.Host != github.Domain {
return nil, nil, ""
return
}

defer func() {
if c.cloud.run.metadata != nil {
data, err := stdjson.Marshal(c.cloud.run.metadata)
if err == nil {
logger.Debug().RawJSON("provider_metadata", data).Msg("detected metadata")
} else {
logger.Warn().Err(err).Msg("failed to encode deployment metadata")
}
} else {
logger.Debug().Msg("no provider metadata detected")
}
}()

ghRepo := r.Owner + "/" + r.Name

logger = logger.With().
Expand Down Expand Up @@ -254,14 +275,14 @@ func (c *cli) tryGithubMetadata() (*cloud.DeploymentReviewRequest, *cloud.Deploy
} else {
logger.Warn().Msg("The provided GitHub token does not have permission to read this repository or it does not exists.")
}
return nil, nil, ghRepo
return
}

if errors.IsKind(err, github.ErrUnprocessableEntity) {
logger.Warn().
Msg("The HEAD commit cannot be found in the remote. Did you forget to push?")

return nil, nil, ghRepo
return
}

logger.Warn().
Expand Down Expand Up @@ -319,11 +340,13 @@ func (c *cli) tryGithubMetadata() (*cloud.DeploymentReviewRequest, *cloud.Deploy
metadata.DeploymentCommitCommitterGitDate = commit.Commit.Committer.Date
}

c.cloud.run.metadata = metadata

if len(pulls) == 0 {
logger.Warn().
Msg("no pull request associated with HEAD commit")

return nil, metadata, ghRepo
return
}

pull := pulls[0]
Expand Down Expand Up @@ -363,7 +386,8 @@ func (c *cli) tryGithubMetadata() (*cloud.DeploymentReviewRequest, *cloud.Deploy
metadata.PullRequestUpdatedAt = pull.UpdatedAt
metadata.PullRequestClosedAt = pull.ClosedAt
metadata.PullRequestMergedAt = pull.MergedAt
return reviewRequest, metadata, ghToken

c.cloud.run.reviewRequest = reviewRequest
}

func (c *cli) isCloudSync() bool {
Expand Down
27 changes: 8 additions & 19 deletions cmd/terramate/cli/cloud_sync_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ package cli

import (
"context"
stdjson "encoding/json"
"fmt"
"os"
"strings"

"github.com/cli/go-gh/v2/pkg/repository"
"github.com/rs/zerolog/log"
"github.com/terramate-io/terramate/cloud"
"github.com/terramate-io/terramate/cloud/deployment"
Expand Down Expand Up @@ -37,16 +37,16 @@ func (c *cli) createCloudDeployment(runStacks []ExecContext) {
err error
deploymentCommitSHA string
deploymentURL string
reviewRequest *cloud.DeploymentReviewRequest
metadata *cloud.DeploymentMetadata
ghRepo string
)

if c.prj.isRepo {
if c.prj.prettyRepo() != "local" {
reviewRequest, metadata, ghRepo = c.tryGithubMetadata()
r, err := repository.Parse(c.prj.prettyRepo())
if err != nil {
logger.Debug().
Msg("repository cannot be normalized: skipping pull request retrievals for commit")
} else {
logger.Debug().Msg("skipping review_request for local repository")
ghRepo = r.Owner + "/" + r.Name
}

deploymentCommitSHA = c.prj.headCommit()
Expand All @@ -67,21 +67,10 @@ func (c *cli) createCloudDeployment(runStacks []ExecContext) {
Msg("detected deployment url")
}

if metadata != nil {
data, err := stdjson.Marshal(metadata)
if err == nil {
logger.Debug().RawJSON("provider_metadata", data).Msg("detected provider metadata")
} else {
logger.Warn().Err(err).Msg("failed to encode deployment metadata")
}
} else {
logger.Debug().Msg("no provider metadata detected")
}

payload := cloud.DeploymentStacksPayloadRequest{
ReviewRequest: reviewRequest,
ReviewRequest: c.cloud.run.reviewRequest,
Workdir: prj.PrjAbsPath(c.rootdir(), c.wd()),
Metadata: metadata,
Metadata: c.cloud.run.metadata,
}

for _, run := range runStacks {
Expand Down
3 changes: 2 additions & 1 deletion cmd/terramate/cli/cloud_sync_drift.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func (c *cli) cloudSyncDriftStatus(st *config.Stack, exitCode int, err error) {
MetaDescription: st.Description,
MetaTags: st.Tags,
},
Status: status,
Status: status,
Metadata: c.cloud.run.metadata,
})

if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/terramate/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func (c *cli) runOnStacks() {

if c.parsedArgs.Run.CloudSyncDeployment || c.parsedArgs.Run.CloudSyncDriftStatus {
c.ensureAllStackHaveIDs(orderedStacks)
c.detectCloudMetadata()
}

isSuccessExit := func(exitCode int) bool {
Expand Down

0 comments on commit 3c25c99

Please sign in to comment.