Skip to content

Commit

Permalink
Make default variables part of config vs compiled into the app (#29)
Browse files Browse the repository at this point in the history
* Make default variables part of config vs compiled into the app (working towards not needing releases for template/variable changes)

* Remove cobra vars
  • Loading branch information
cmmarslender authored Oct 16, 2024
1 parent 3665b49 commit 144589f
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 58 deletions.
2 changes: 1 addition & 1 deletion cmd/debugTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var debugTemplateCmd = &cobra.Command{
if err != nil {
log.Fatalln(err.Error())
}
content, err := repo.ProcessTemplate(tmplContent, map[string]string{})
content, err := repo.ProcessTemplate(tmplContent, map[string]string{}, map[string]string{})
if err != nil {
log.Fatalln(err.Error())
}
Expand Down
8 changes: 7 additions & 1 deletion cmd/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/chia-network/repo-content-updater/internal/config"
"github.com/chia-network/repo-content-updater/internal/repo"
)

Expand All @@ -26,7 +27,12 @@ var licenseCmd = &cobra.Command{
log.Fatalf("Error creating content manager: %s", err.Error())
}

err = content.CheckLicenses()
cfg, err := config.LoadConfig(viper.GetString("config"))
if err != nil {
log.Fatalf("error loading config: %s\n", err.Error())
}

err = content.CheckLicenses(cfg)
if err != nil {
log.Fatalln(err.Error())
}
Expand Down
30 changes: 9 additions & 21 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,16 @@ func Execute() {
}

func init() {
var (
cfgFile string
templateDir string
githubOrg string
committerName string
committerEmail string
reviewTeamName string
githubToken string
signCommits bool
push bool
)

cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "config.yaml", "template config file (default is config.yaml)")
rootCmd.PersistentFlags().StringVar(&templateDir, "templates", "templates", "Path to templates defined in the config. Defaults to ./templates")
rootCmd.PersistentFlags().StringVar(&githubOrg, "github-org", "Chia-Network", "The org to process")
rootCmd.PersistentFlags().StringVar(&committerName, "committer-name", "Chia Automation", "The git user to use when making commits")
rootCmd.PersistentFlags().StringVar(&committerEmail, "committer-email", "automation@chia.net", "The git email to use when making commits")
rootCmd.PersistentFlags().StringVar(&reviewTeamName, "review-team", "content-updater-reviewers", "The default team to assigned to the PRs if a repo override is not set")
rootCmd.PersistentFlags().StringVar(&githubToken, "github-token", "", "The token to use to auth to GitHub API and Push to Repos")
rootCmd.PersistentFlags().BoolVar(&signCommits, "sign-commits", true, "Whether or not to sign commits")
rootCmd.PersistentFlags().BoolVar(&push, "push", true, "Whether or not to push and create the pull request")
rootCmd.PersistentFlags().String("config", "config.yaml", "template config file (default is config.yaml)")
rootCmd.PersistentFlags().String("templates", "templates", "Path to templates defined in the config. Defaults to ./templates")
rootCmd.PersistentFlags().String("github-org", "Chia-Network", "The org to process")
rootCmd.PersistentFlags().String("committer-name", "Chia Automation", "The git user to use when making commits")
rootCmd.PersistentFlags().String("committer-email", "automation@chia.net", "The git email to use when making commits")
rootCmd.PersistentFlags().String("review-team", "content-updater-reviewers", "The default team to assigned to the PRs if a repo override is not set")
rootCmd.PersistentFlags().String("github-token", "", "The token to use to auth to GitHub API and Push to Repos")
rootCmd.PersistentFlags().Bool("sign-commits", true, "Whether or not to sign commits")
rootCmd.PersistentFlags().Bool("push", true, "Whether or not to push and create the pull request")

cobra.CheckErr(viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config")))
cobra.CheckErr(viper.BindPFlag("templates", rootCmd.PersistentFlags().Lookup("templates")))
Expand Down
23 changes: 23 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,26 @@ files:
- .prettierrc.cjs
- .prettier.config.cjs
- .prettierrc.toml

variables:
COMPANY_NAME: "Chia Network Inc."
CGO_ENABLED: "0"
DEPENDABOT_GOMOD_PULL_REQUEST_LIMIT: "10"
DEPENDABOT_GOMOD_REBASE_STRATEGY: "auto"
DEPENDABOT_GOMOD_DIRECTORY: "/"
DEPENDABOT_GOMOD_REVIEWERS: "[\"cmmarslender\", \"starttoaster\"]"
DEPENDABOT_PIP_PULL_REQUEST_LIMIT: "10"
DEPENDABOT_PIP_REBASE_STRATEGY: "auto"
DEPENDABOT_PIP_DIRECTORY: "/"
DEPENDABOT_PIP_REVIEWERS: "[\"emlowe\", \"altendky\"]"
DEPENDABOT_ACTIONS_PULL_REQUEST_LIMIT: "10"
DEPENDABOT_ACTIONS_REBASE_STRATEGY: "auto"
DEPENDABOT_ACTIONS_DIRECTORY: "/"
DEPENDABOT_ACTIONS_REVIEWERS: "[\"cmmarslender\", \"Starttoaster\", \"pmaslana\"]"
DEPENDABOT_NPM_PULL_REQUEST_LIMIT: "10"
DEPENDABOT_NPM_REBASE_STRATEGY: "auto"
DEPENDABOT_NPM_DIRECTORY: "/"
DEPENDABOT_NPM_REVIEWERS: "[\"cmmarslender\", \"ChiaMineJP\"]"
DEPENDABOT_CARGO_DIRECTORY: "/"
DEPENDABOT_CARGO_PULL_REQUEST_LIMIT: "10"
DEPENDABOT_CARGO_REBASE_STRATEGY: "auto"
5 changes: 3 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (

// Config is the supported files config
type Config struct {
Groups []Group `yaml:"groups"`
Files []File `yaml:"files"`
Groups []Group `yaml:"groups"`
Files []File `yaml:"files"`
Variables map[string]string `yaml:"variables"`
}

// Group is a defined group of template files to include at once
Expand Down
2 changes: 1 addition & 1 deletion internal/repo/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (c *Content) CheckFiles(repoName string, files []string, cfg *config.Config
if err != nil {
return err
}
content, err := ProcessTemplate(tmplContent, repoConfig.VarOverrides)
content, err := ProcessTemplate(tmplContent, cfg.Variables, repoConfig.VarOverrides)
if err != nil {
return err
}
Expand Down
10 changes: 6 additions & 4 deletions internal/repo/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
"path"

"github.com/google/go-github/v59/github"

"github.com/chia-network/repo-content-updater/internal/config"
)

// CheckLicenses checks all repos for licenses that need to be managed/updated
func (c *Content) CheckLicenses() error {
func (c *Content) CheckLicenses(cfg *config.Config) error {
var reposToCheck []string

opts := &github.ListOptions{
Expand Down Expand Up @@ -40,7 +42,7 @@ func (c *Content) CheckLicenses() error {

for _, repo := range reposToCheck {
log.Printf("Need to check %s\n", repo)
err := c.UpdateLicense(repo)
err := c.UpdateLicense(repo, cfg)
if err != nil {
log.Printf("Error updating %s: %s\n", repo, err.Error())
continue
Expand All @@ -51,7 +53,7 @@ func (c *Content) CheckLicenses() error {
}

// UpdateLicense ensures the license is up to date for the given repo
func (c *Content) UpdateLicense(repoName string) error {
func (c *Content) UpdateLicense(repoName string, cfg *config.Config) error {
defer removeDirIfExists(repoDir(repoName))

r, w, err := c.cloneRepo(repoName)
Expand All @@ -76,7 +78,7 @@ func (c *Content) UpdateLicense(repoName string) error {
if err != nil {
return err
}
content, err := ProcessTemplate(file, repoConfig.VarOverrides)
content, err := ProcessTemplate(file, cfg.Variables, repoConfig.VarOverrides)
if err != nil {
return err
}
Expand Down
34 changes: 10 additions & 24 deletions internal/repo/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,22 @@ import (
)

// ProcessTemplate renders the given template file
func ProcessTemplate(templateContent []byte, overrides map[string]string) ([]byte, error) {
func ProcessTemplate(templateContent []byte, defaultVars map[string]string, overrides map[string]string) ([]byte, error) {
// Compute the SHA256 hash of the template content
hash := sha256.Sum256(templateContent)
hexHash := hex.EncodeToString(hash[:])

notOverridable := map[string]bool{"CURRENT_YEAR": true}
defaultPullRequestLimit := "10"
data := map[string]string{
"CURRENT_YEAR": strconv.Itoa(time.Now().Year()),
"COMPANY_NAME": "Chia Network Inc.",
"CGO_ENABLED": "0",
"DEPENDABOT_GOMOD_PULL_REQUEST_LIMIT": defaultPullRequestLimit,
"DEPENDABOT_GOMOD_REBASE_STRATEGY": "auto",
"DEPENDABOT_GOMOD_DIRECTORY": "/",
"DEPENDABOT_GOMOD_REVIEWERS": "[\"cmmarslender\", \"starttoaster\"]",
"DEPENDABOT_PIP_PULL_REQUEST_LIMIT": defaultPullRequestLimit,
"DEPENDABOT_PIP_REBASE_STRATEGY": "auto",
"DEPENDABOT_PIP_DIRECTORY": "/",
"DEPENDABOT_PIP_REVIEWERS": "[\"emlowe\", \"altendky\"]",
"DEPENDABOT_ACTIONS_PULL_REQUEST_LIMIT": defaultPullRequestLimit,
"DEPENDABOT_ACTIONS_REBASE_STRATEGY": "auto",
"DEPENDABOT_ACTIONS_DIRECTORY": "/",
"DEPENDABOT_ACTIONS_REVIEWERS": "[\"cmmarslender\", \"Starttoaster\", \"pmaslana\"]",
"DEPENDABOT_NPM_PULL_REQUEST_LIMIT": defaultPullRequestLimit,
"DEPENDABOT_NPM_REBASE_STRATEGY": "auto",
"DEPENDABOT_NPM_DIRECTORY": "/",
"DEPENDABOT_NPM_REVIEWERS": "[\"cmmarslender\", \"ChiaMineJP\"]",
"DEPENDABOT_CARGO_DIRECTORY": "/",
"DEPENDABOT_CARGO_PULL_REQUEST_LIMIT": defaultPullRequestLimit,
"DEPENDABOT_CARGO_REBASE_STRATEGY": "auto",
"CURRENT_YEAR": strconv.Itoa(time.Now().Year()),
}

// Merge `defaultVars` into `data`
for key, value := range defaultVars {
if notOverridable[key] {
continue
}
data[key] = value
}

// Merge `overrides` into `data`, with `overrides` taking precedence
Expand Down
8 changes: 4 additions & 4 deletions internal/repo/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ import (
func TestProcessTemplateOverrides(t *testing.T) {
template := []byte(`{{ .CURRENT_YEAR }} {{ .CGO_ENABLED }}`)

result, err := repo.ProcessTemplate(template, map[string]string{})
result, err := repo.ProcessTemplate(template, map[string]string{"CGO_ENABLED": "0"}, map[string]string{})
assert.Nil(t, err)
assert.Equal(t, []byte(fmt.Sprintf("%d 0", time.Now().Year())), result)
assert.Equal(t, string([]byte(fmt.Sprintf("%d 0", time.Now().Year()))), string(result))

// Ensure allowed overrides work
result, err = repo.ProcessTemplate(template, map[string]string{"CGO_ENABLED": "1"})
result, err = repo.ProcessTemplate(template, map[string]string{}, map[string]string{"CGO_ENABLED": "1"})
assert.Nil(t, err)
assert.Equal(t, []byte(fmt.Sprintf("%d 1", time.Now().Year())), result)

// Ensure disallowed overrides dont override
result, err = repo.ProcessTemplate(template, map[string]string{
result, err = repo.ProcessTemplate(template, map[string]string{}, map[string]string{
"CGO_ENABLED": "1",
"CURRENT_YEAR": "1990",
})
Expand Down

0 comments on commit 144589f

Please sign in to comment.