Skip to content

Commit

Permalink
Add --upload flag to atmos describe affected command (#631)
Browse files Browse the repository at this point in the history
* updates

* updates

* updates

* updates

* updates

* updates

* updates

* updates

* updates

* updates

* updates

* updates

* updates
  • Loading branch information
aknysh authored Jun 23, 2024
1 parent 6746ba4 commit 2290855
Show file tree
Hide file tree
Showing 11 changed files with 353 additions and 104 deletions.
1 change: 1 addition & 0 deletions cmd/describe_affected.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func init() {
describeAffectedCmd.PersistentFlags().Bool("include-spacelift-admin-stacks", false, "Include the Spacelift admin stack of any stack that is affected by config changes: atmos describe affected --include-spacelift-admin-stacks=true")
describeAffectedCmd.PersistentFlags().Bool("include-dependents", false, "Include the dependent components and stacks: atmos describe affected --include-dependents=true")
describeAffectedCmd.PersistentFlags().Bool("include-settings", false, "Include the 'settings' section for each affected component: atmos describe affected --include-settings=true")
describeAffectedCmd.PersistentFlags().Bool("upload", false, "Upload the affected components and stacks to a specified HTTP endpoint: atmos describe affected --upload=true")
describeAffectedCmd.PersistentFlags().Bool("clone-target-ref", false, "Clone the target reference with which to compare the current branch: atmos describe affected --clone-target-ref=true\n"+
"If set to 'false' (default), the target reference will be checked out instead\n"+
"This requires that the target reference is already cloned by Git, and the information about it exists in the '.git' directory")
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/arsham/figurine v1.3.0
github.com/bmatcuk/doublestar/v4 v4.6.1
github.com/charmbracelet/bubbles v0.18.0
github.com/charmbracelet/bubbletea v0.26.4
github.com/charmbracelet/bubbletea v0.26.5
github.com/charmbracelet/lipgloss v0.11.0
github.com/elewis787/boa v0.1.2
github.com/fatih/color v1.17.0
Expand All @@ -18,14 +18,15 @@ require (
github.com/google/go-github/v59 v59.0.0
github.com/google/uuid v1.6.0
github.com/hairyhenderson/gomplate/v3 v3.11.8
github.com/hashicorp/go-getter v1.7.4
github.com/hashicorp/go-getter v1.7.5
github.com/hashicorp/hcl v1.0.0
github.com/hashicorp/hcl/v2 v2.21.0
github.com/hashicorp/terraform-config-inspect v0.0.0-20240607080351-271db412dbcb
github.com/hashicorp/terraform-exec v0.21.0
github.com/ivanpirog/coloredcobra v1.0.1
github.com/json-iterator/go v1.1.12
github.com/jwalton/go-supportscolor v1.2.0
github.com/kubescape/go-git-url v0.0.30
github.com/lrstanley/bubblezone v0.0.0-20240616011544-69b11dddf9ae
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.5.0
Expand Down Expand Up @@ -90,6 +91,7 @@ require (
github.com/bytecodealliance/wasmtime-go/v3 v3.0.2 // indirect
github.com/cenkalti/backoff/v3 v3.2.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chainguard-dev/git-urls v1.0.2 // indirect
github.com/charmbracelet/x/ansi v0.1.2 // indirect
github.com/charmbracelet/x/input v0.1.2 // indirect
github.com/charmbracelet/x/term v0.1.1 // indirect
Expand Down Expand Up @@ -254,6 +256,7 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
inet.af/netaddr v0.0.0-20230525184311-b8eac61e914a // indirect
k8s.io/client-go v0.26.2 // indirect
k8s.io/utils v0.0.0-20230220204549-a5ecb0141aa5 // indirect
oras.land/oras-go/v2 v2.3.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
14 changes: 10 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,12 @@ github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghf
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chainguard-dev/git-urls v1.0.2 h1:pSpT7ifrpc5X55n4aTTm7FFUE+ZQHKiqpiwNkJrVcKQ=
github.com/chainguard-dev/git-urls v1.0.2/go.mod h1:rbGgj10OS7UgZlbzdUQIQpT0k/D4+An04HJY7Ol+Y/o=
github.com/charmbracelet/bubbles v0.18.0 h1:PYv1A036luoBGroX6VWjQIE9Syf2Wby2oOl/39KLfy0=
github.com/charmbracelet/bubbles v0.18.0/go.mod h1:08qhZhtIwzgrtBjAcJnij1t1H0ZRjwHyGsy6AL11PSw=
github.com/charmbracelet/bubbletea v0.26.4 h1:2gDkkzLZaTjMl/dQBpNVtnvcCxsh/FCkimep7FC9c40=
github.com/charmbracelet/bubbletea v0.26.4/go.mod h1:P+r+RRA5qtI1DOHNFn0otoNwB4rn+zNAzSj/EXz6xU0=
github.com/charmbracelet/bubbletea v0.26.5 h1:90pqTPElAReb/qQUgSMUresTkfwVr0Wx+zczeHHOgxk=
github.com/charmbracelet/bubbletea v0.26.5/go.mod h1:dz8CWPlfCCGLFbBlTY4N7bjLiyOGDJEnd2Muu7pOWhk=
github.com/charmbracelet/lipgloss v0.11.0 h1:UoAcbQ6Qml8hDwSWs0Y1cB5TEQuZkDPH/ZqwWWYTG4g=
github.com/charmbracelet/lipgloss v0.11.0/go.mod h1:1UdRTH9gYgpcdNN5oBtjbu/IzNKtzVtb7sqN1t9LNn8=
github.com/charmbracelet/x/ansi v0.1.2 h1:6+LR39uG8DE6zAmbu023YlqjJHkYXDF1z36ZwzO4xZY=
Expand Down Expand Up @@ -728,8 +730,8 @@ github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtng
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-getter v1.7.4 h1:3yQjWuxICvSpYwqSayAdKRFcvBl1y/vogCxczWSmix0=
github.com/hashicorp/go-getter v1.7.4/go.mod h1:W7TalhMmbPmsSMdNjD0ZskARur/9GJ17cfHTRtXV744=
github.com/hashicorp/go-getter v1.7.5 h1:dT58k9hQ/vbxNMwoI5+xFYAJuv6152UNvdHokfI5wE4=
github.com/hashicorp/go-getter v1.7.5/go.mod h1:W7TalhMmbPmsSMdNjD0ZskARur/9GJ17cfHTRtXV744=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-hclog v0.14.1/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
github.com/hashicorp/go-hclog v0.16.2/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
Expand Down Expand Up @@ -907,6 +909,8 @@ github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kubescape/go-git-url v0.0.30 h1:PIbg86ae0ftee/p/Tu/6CA1ju6VoJ51G3sQWNHOm6wg=
github.com/kubescape/go-git-url v0.0.30/go.mod h1:3ddc1HEflms1vMhD9owt/3FBES070UaYTUarcjx8jDk=
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
Expand Down Expand Up @@ -1903,6 +1907,8 @@ inet.af/netaddr v0.0.0-20230525184311-b8eac61e914a h1:1XCVEdxrvL6c0TGOhecLuB7U9z
inet.af/netaddr v0.0.0-20230525184311-b8eac61e914a/go.mod h1:e83i32mAQOW1LAqEIweALsuK2Uw4mhQadA5r7b0Wobo=
k8s.io/client-go v0.26.2 h1:s1WkVujHX3kTp4Zn4yGNFK+dlDXy1bAAkIl+cFAiuYI=
k8s.io/client-go v0.26.2/go.mod h1:u5EjOuSyBa09yqqyY7m3abZeovO/7D/WehVVlZ2qcqU=
k8s.io/utils v0.0.0-20230220204549-a5ecb0141aa5 h1:kmDqav+P+/5e1i9tFfHq1qcF3sOrDp+YEkVDAHu7Jwk=
k8s.io/utils v0.0.0-20230220204549-a5ecb0141aa5/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
mvdan.cc/sh/v3 v3.8.0 h1:ZxuJipLZwr/HLbASonmXtcvvC9HXY9d2lXZHnKGjFc8=
mvdan.cc/sh/v3 v3.8.0/go.mod h1:w04623xkgBVo7/IUK89E0g8hBykgEpN0vgOj3RJr6MY=
nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
Expand Down
6 changes: 3 additions & 3 deletions internal/exec/atlantis_generate_repo_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ func ExecuteAtlantisGenerateRepoConfigAffectedOnly(
var err error

if repoPath != "" {
affected, err = ExecuteDescribeAffectedWithTargetRepoPath(cliConfig, repoPath, verbose, false, false)
affected, _, _, _, err = ExecuteDescribeAffectedWithTargetRepoPath(cliConfig, repoPath, verbose, false, false)
} else if cloneTargetRef {
affected, err = ExecuteDescribeAffectedWithTargetRefClone(cliConfig, ref, sha, sshKeyPath, sshKeyPassword, verbose, false, false)
affected, _, _, _, err = ExecuteDescribeAffectedWithTargetRefClone(cliConfig, ref, sha, sshKeyPath, sshKeyPassword, verbose, false, false)
} else {
affected, err = ExecuteDescribeAffectedWithTargetRefCheckout(cliConfig, ref, sha, verbose, false, false)
affected, _, _, _, err = ExecuteDescribeAffectedWithTargetRefCheckout(cliConfig, ref, sha, verbose, false, false)
}

if err != nil {
Expand Down
109 changes: 102 additions & 7 deletions internal/exec/describe_affected.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package exec

import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"os"
"time"

"github.com/go-git/go-git/v5/plumbing"
giturl "github.com/kubescape/go-git-url"
"github.com/spf13/cobra"

cfg "github.com/cloudposse/atmos/pkg/config"
Expand Down Expand Up @@ -94,6 +101,11 @@ func ExecuteDescribeAffectedCmd(cmd *cobra.Command, args []string) error {
return err
}

upload, err := flags.GetBool("upload")
if err != nil {
return err
}

cloneTargetRef, err := flags.GetBool("clone-target-ref")
if err != nil {
return err
Expand All @@ -103,14 +115,26 @@ func ExecuteDescribeAffectedCmd(cmd *cobra.Command, args []string) error {
return errors.New("if the '--repo-path' flag is specified, the '--ref', '--sha', '--ssh-key' and '--ssh-key-password' flags can't be used")
}

// When uploading, always include dependents and settings for all affected components
if upload {
includeDependents = true
includeSettings = true
}

if verbose {
cliConfig.Logs.Level = u.LogLevelTrace
}

var affected []schema.Affected
var headHead, baseHead *plumbing.Reference
var repoUrl string

if repoPath != "" {
affected, err = ExecuteDescribeAffectedWithTargetRepoPath(cliConfig, repoPath, verbose, includeSpaceliftAdminStacks, includeSettings)
affected, headHead, baseHead, repoUrl, err = ExecuteDescribeAffectedWithTargetRepoPath(cliConfig, repoPath, verbose, includeSpaceliftAdminStacks, includeSettings)
} else if cloneTargetRef {
affected, err = ExecuteDescribeAffectedWithTargetRefClone(cliConfig, ref, sha, sshKeyPath, sshKeyPassword, verbose, includeSpaceliftAdminStacks, includeSettings)
affected, headHead, baseHead, repoUrl, err = ExecuteDescribeAffectedWithTargetRefClone(cliConfig, ref, sha, sshKeyPath, sshKeyPassword, verbose, includeSpaceliftAdminStacks, includeSettings)
} else {
affected, err = ExecuteDescribeAffectedWithTargetRefCheckout(cliConfig, ref, sha, verbose, includeSpaceliftAdminStacks, includeSettings)
affected, headHead, baseHead, repoUrl, err = ExecuteDescribeAffectedWithTargetRefCheckout(cliConfig, ref, sha, verbose, includeSpaceliftAdminStacks, includeSettings)
}

if err != nil {
Expand All @@ -125,16 +149,87 @@ func ExecuteDescribeAffectedCmd(cmd *cobra.Command, args []string) error {
}
}

if verbose {
cliConfig.Logs.Level = u.LogLevelTrace
}

u.LogTrace(cliConfig, fmt.Sprintf("\nAffected components and stacks: \n"))

err = printOrWriteToFile(format, file, affected)
if err != nil {
return err
}

// Upload the affected components and stacks to a specified endpoint
// https://www.digitalocean.com/community/tutorials/how-to-make-http-requests-in-go
if upload {
baseUrl := os.Getenv(cfg.AtmosProBaseUrlEnvVarName)
if baseUrl == "" {
baseUrl = cfg.AtmosProDefaultBaseUrl
}
endpoint := os.Getenv(cfg.AtmosProEndpointEnvVarName)
if endpoint == "" {
endpoint = cfg.AtmosProDefaultEndpoint
}
url := fmt.Sprintf("%s/%s", baseUrl, endpoint)

// Parse the repo URL
gitURL, err := giturl.NewGitURL(repoUrl)
if err != nil {
return err
}

body := map[string]any{
"head_sha": headHead.Hash().String(),
"base_sha": baseHead.Hash().String(),
"repo_url": repoUrl,
"repo_name": gitURL.GetRepoName(),
"repo_owner": gitURL.GetOwnerName(),
"repo_host": gitURL.GetHostName(),
"stacks": affected,
}

bodyJson, err := u.ConvertToJSON(body)
if err != nil {
return err
}

u.LogTrace(cliConfig, fmt.Sprintf("\nUploading the affected components and stacks to %s", url))

bodyReader := bytes.NewReader([]byte(bodyJson))
req, err := http.NewRequest(http.MethodPost, url, bodyReader)
if err != nil {
return err
}

req.Header.Set("Content-Type", "application/json")

// Authorization header
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization
token := os.Getenv(cfg.AtmosProTokenEnvVarName)
if token != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
}

client := http.Client{
Timeout: 10 * time.Second,
}

resp, err := client.Do(req)
if err != nil {
return err
}

defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
u.LogError(err)
}
}(resp.Body)

if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusBadRequest {
err = fmt.Errorf("\nError uploading the affected components and stacks to %s\nStatus: %s\n", url, resp.Status)
return err
}

u.LogTrace(cliConfig, fmt.Sprintf("\nUploaded the affected components and stacks to %s\n", url))
}

return nil
}
Loading

0 comments on commit 2290855

Please sign in to comment.