Skip to content

Commit

Permalink
fix: add func for git origin (#931)
Browse files Browse the repository at this point in the history
* fix: add func for git origin

* move test; deadcode

* timestamp
  • Loading branch information
jakeyheath authored Sep 13, 2023
1 parent 1487106 commit e14bb24
Show file tree
Hide file tree
Showing 51 changed files with 441 additions and 496 deletions.
17 changes: 17 additions & 0 deletions apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/fs"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
Expand Down Expand Up @@ -522,6 +523,21 @@ func removeExtension(path string) string {
return strings.TrimSuffix(path, filepath.Ext(path))
}

func getGitRemoteOriginURL(cwd ...string) string {
dir := "."
if len(cwd) > 0 {
dir = cwd[0]
}
cmd := exec.Command("git", "remote", "get-url", "--push", "origin")
cmd.Dir = dir
out, err := cmd.Output()
if err != nil {
logrus.Warnf("unable to get git output: %s", err)
return ""
}
return strings.TrimSpace(string(out))
}

func applyTemplate(sourceFile io.Reader, commonTemplates fs.FS, dest afero.Fs, path string, overrides interface{}) error {
dir := filepath.Dir(path)
err := dest.MkdirAll(dir, 0775)
Expand All @@ -539,6 +555,7 @@ func applyTemplate(sourceFile io.Reader, commonTemplates fs.FS, dest afero.Fs, p
"cwd": func() string {
return filepath.Dir(writer.Name())
},
"git_origin": getGitRemoteOriginURL,
},
}...)
if e != nil {
Expand Down
16 changes: 16 additions & 0 deletions apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ func init() {
logrus.SetFormatter(formatter)
}

type testGitRemote struct {
path string
expectedRemote string
}

func TestGetGitRemoveOriginURL(t *testing.T) {
r := require.New(t)
tests := []testGitRemote{
{path: ".", expectedRemote: "[email protected]:chanzuckerberg/fogg"},
}
for _, test := range tests {
remote := getGitRemoteOriginURL(test.path)
r.Equal(test.expectedRemote, remote)
}
}

func randomString(n int) string {
var letter = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")

Expand Down
25 changes: 1 addition & 24 deletions config/v2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import (
"bytes"
"encoding/json"
"fmt"
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/chanzuckerberg/fogg/errs"
"github.com/chanzuckerberg/fogg/plugins"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
yaml "gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -54,28 +52,10 @@ func (c *Config) Write(fs afero.Fs, path string) error {
return encoder.Encode(c)
}

func getGitRemoteOriginURL(cwd ...string) string {
dir := "."
if len(cwd) > 0 {
dir = cwd[0]
}
cmd := exec.Command("git", "remote", "get-url", "--push", "origin")
cmd.Dir = dir
out, err := cmd.Output()
if err != nil {
logrus.Warnf("unable to get git output: %s", err)
return ""
}
return strings.TrimSpace(string(out))
}

// TODO: write out functions to generate tag data
func (c *Config) GenerateStamp(fs afero.Fs, configFile string) {
stamp := Stamp{}
stamp.Date = DateAsTag()
stamp.FoggUser = ""
stamp.GitRepository = getGitRemoteOriginURL()
stamp.CommitHash = ""
c.Stamp = stamp
}

Expand All @@ -86,10 +66,7 @@ func DateAsTag() string {
}

type Stamp struct {
Date string
FoggUser string
GitRepository string
CommitHash string
FoggUser string
}

type Config struct {
Expand Down
16 changes: 0 additions & 16 deletions config/v2/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,6 @@ import (
"github.com/stretchr/testify/require"
)

type testGitRemote struct {
path string
expectedRemote string
}

func TestGetGitRemoveOriginURL(t *testing.T) {
r := require.New(t)
tests := []testGitRemote{
{path: ".", expectedRemote: "[email protected]:chanzuckerberg/fogg"},
}
for _, test := range tests {
remote := getGitRemoteOriginURL(test.path)
r.Equal(test.expectedRemote, remote)
}
}

func TestReadConfig(t *testing.T) {
r := require.New(t)

Expand Down
3 changes: 0 additions & 3 deletions plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -1287,10 +1287,7 @@ func resolveComponentCommon(stamp v2.Stamp, commons ...v2.Common) ComponentCommo
TravisCI: travisPlan,
CircleCI: circlePlan,
GitHubActionsCI: githubActionsPlan,
Date: stamp.Date,
FoggUser: stamp.FoggUser,
GitRepository: stamp.GitRepository,
CommitHash: stamp.CommitHash,
}
}

Expand Down
5 changes: 2 additions & 3 deletions templates/templates/component/terraform/fogg.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,10 @@ variable "tags" {
env = "{{ .Env }}"
service = "{{ .Name }}"
owner = "{{ .Owner }}"
date = "{{ .Date }}"
terraformLastApplyTime = timestamp()
terraformWorkspaceDir = "{{ cwd }}"
foggUser = "{{ .FoggUser }}"
gitRepository = "{{ .GitRepository }}"
commitHash = "{{ .CommitHash }}"
gitRepository = "{{ git_origin }}"
managedBy = "terraform"
}
}
Expand Down
19 changes: 9 additions & 10 deletions testdata/auth0_provider_yaml/terraform/accounts/foo/fogg.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions testdata/auth0_provider_yaml/terraform/envs/bar/bam/fogg.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions testdata/auth0_provider_yaml/terraform/global/fogg.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions testdata/bless_provider_yaml/terraform/accounts/foo/fogg.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions testdata/bless_provider_yaml/terraform/envs/bar/bam/fogg.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions testdata/bless_provider_yaml/terraform/global/fogg.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions testdata/circleci/terraform/global/fogg.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions testdata/github_actions/terraform/global/fogg.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions testdata/github_provider_yaml/terraform/accounts/foo/fogg.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e14bb24

Please sign in to comment.