Skip to content

Commit

Permalink
Make org, committer info, and review team configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
cmmarslender committed Mar 21, 2024
1 parent e266065 commit 0c109a4
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 36 deletions.
9 changes: 8 additions & 1 deletion cmd/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ var licenseCmd = &cobra.Command{
Use: "license",
Short: "Updates licenses in repos with license flag",
Run: func(cmd *cobra.Command, args []string) {
content, err := repo.NewContent(viper.GetString("templates"), viper.GetString("github-token"))
content, err := repo.NewContent(
viper.GetString("templates"),
viper.GetString("github-org"),
viper.GetString("committer-name"),
viper.GetString("committer-email"),
viper.GetString("review-team"),
viper.GetString("github-token"),
)
if err != nil {
log.Fatalf("Error creating content manager: %s", err.Error())
}
Expand Down
9 changes: 8 additions & 1 deletion cmd/managedFiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ var managedFilesCmd = &cobra.Command{
Use: "managed-files",
Short: "Updates all managed files across the org",
Run: func(cmd *cobra.Command, args []string) {
content, err := repo.NewContent(viper.GetString("templates"), viper.GetString("github-token"))
content, err := repo.NewContent(
viper.GetString("templates"),
viper.GetString("github-org"),
viper.GetString("committer-name"),
viper.GetString("committer-email"),
viper.GetString("review-team"),
viper.GetString("github-token"),
)
if err != nil {
log.Fatalf("Error creating content manager: %s", err.Error())
}
Expand Down
22 changes: 17 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,34 @@ func Execute() {

func init() {
var (
cfgFile string
templateDir string
githubToken string
signCommits bool
push bool
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")

cobra.CheckErr(viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config")))
cobra.CheckErr(viper.BindPFlag("templates", rootCmd.PersistentFlags().Lookup("templates")))
cobra.CheckErr(viper.BindPFlag("github-org", rootCmd.PersistentFlags().Lookup("github-org")))
cobra.CheckErr(viper.BindPFlag("committer-name", rootCmd.PersistentFlags().Lookup("committer-name")))
cobra.CheckErr(viper.BindPFlag("committer-email", rootCmd.PersistentFlags().Lookup("committer-email")))
cobra.CheckErr(viper.BindPFlag("review-team", rootCmd.PersistentFlags().Lookup("review-team")))
cobra.CheckErr(viper.BindPFlag("github-token", rootCmd.PersistentFlags().Lookup("github-token")))
cobra.CheckErr(viper.BindPFlag("sign-commits", rootCmd.PersistentFlags().Lookup("sign-commits")))
cobra.CheckErr(viper.BindPFlag("push", rootCmd.PersistentFlags().Lookup("push")))
Expand Down
47 changes: 22 additions & 25 deletions internal/repo/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,28 @@ import (
"github.com/spf13/viper"
)

const (
// GitHubOrg the org in GitHub to operate within
GitHubOrg string = "Chia-Network"
// CommitterName User name for git
CommitterName string = "Chia Automation"
// CommitterEmail git email
CommitterEmail string = "automation@chia.net"
// ReviewTeamName The name of the team that should review the PRs
ReviewTeamName string = "content-updater-reviewers"
)

// Content the content manager object
type Content struct {
templates string
githubToken string
githubClient *github.Client
templates string
githubOrg string
committerName string
committerEmail string
reviewTeamName string
githubToken string
githubClient *github.Client
}

// NewContent returns new repo content manager
func NewContent(templates, githubToken string) (*Content, error) {
func NewContent(templates, githubOrg, committerName, committerEmail, reviewTeam, githubToken string) (*Content, error) {
client := github.NewClient(nil).WithAuthToken(githubToken)
return &Content{
templates: templates,
githubToken: githubToken,
githubClient: client,
templates: templates,
githubOrg: githubOrg,
committerName: committerName,
committerEmail: committerEmail,
reviewTeamName: reviewTeam,
githubToken: githubToken,
githubClient: client,
}, nil
}

Expand All @@ -51,7 +48,7 @@ func repoDir(repoName string) string {

func (c *Content) cloneRepo(repoName string) (*git.Repository, *git.Worktree, error) {
_, err := git.PlainClone(repoDir(repoName), false, &git.CloneOptions{
URL: fmt.Sprintf("https://%s@github.com/%s/%s", c.githubToken, GitHubOrg, repoName),
URL: fmt.Sprintf("https://%s@github.com/%s/%s", c.githubToken, c.githubOrg, repoName),
SingleBranch: true,
Depth: 1,
})
Expand Down Expand Up @@ -160,8 +157,8 @@ func (c *Content) commit(w *git.Worktree, repoName string, message string) error
} else {
commitOptions := &git.CommitOptions{
Author: &object.Signature{
Name: CommitterName,
Email: CommitterEmail,
Name: c.committerName,
Email: c.committerEmail,
When: time.Now(),
},
}
Expand Down Expand Up @@ -204,7 +201,7 @@ func (c *Content) pushAndPR(r *git.Repository, repoName, branchName, title strin
}

// Create the pull request
pr, _, err := c.githubClient.PullRequests.Create(context.TODO(), GitHubOrg, repoName, newPR)
pr, _, err := c.githubClient.PullRequests.Create(context.TODO(), c.githubOrg, repoName, newPR)
if err != nil {
return fmt.Errorf("error creating pull request: %s", err)
}
Expand All @@ -223,10 +220,10 @@ func (c *Content) pushAndPR(r *git.Repository, repoName, branchName, title strin
teamReviewer = []string{*opts.AssignGroup} // Use specified AssignGroup
} else if len(opts.AssignUsers) == 0 {
// Fallback to the default team name only if no individual users are specified
teamReviewer = []string{ReviewTeamName}
teamReviewer = []string{c.reviewTeamName}
}
// Requesting review from the specified team and individual users
_, _, err = c.githubClient.PullRequests.RequestReviewers(context.TODO(), GitHubOrg, repoName, pr.GetNumber(), github.ReviewersRequest{
_, _, err = c.githubClient.PullRequests.RequestReviewers(context.TODO(), c.githubOrg, repoName, pr.GetNumber(), github.ReviewersRequest{
TeamReviewers: teamReviewer,
Reviewers: opts.AssignUsers, // Directly use specified individual users
})
Expand All @@ -238,7 +235,7 @@ func (c *Content) pushAndPR(r *git.Repository, repoName, branchName, title strin
}

func (c *Content) ensureGroupMembership(repoName string) error {
_, err := c.githubClient.Teams.AddTeamRepoBySlug(context.TODO(), GitHubOrg, ReviewTeamName, GitHubOrg, repoName, &github.TeamAddTeamRepoOptions{Permission: "push"})
_, err := c.githubClient.Teams.AddTeamRepoBySlug(context.TODO(), c.githubOrg, c.reviewTeamName, c.githubOrg, repoName, &github.TeamAddTeamRepoOptions{Permission: "push"})
return err
}

Expand Down
4 changes: 2 additions & 2 deletions internal/repo/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (c *Content) ManagedFiles(cfg *config.Config) error {
}
for {
opts.Page++
result, resp, err := c.githubClient.Organizations.ListCustomPropertyValues(context.TODO(), GitHubOrg, opts)
result, resp, err := c.githubClient.Organizations.ListCustomPropertyValues(context.TODO(), c.githubOrg, opts)
if err != nil {
return err
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func (c *Content) CheckFiles(repoName string, files []string, cfg *config.Config
}
}

repo, _, err := c.githubClient.Repositories.Get(context.TODO(), GitHubOrg, repoName)
repo, _, err := c.githubClient.Repositories.Get(context.TODO(), c.githubOrg, repoName)
if err != nil {
return fmt.Errorf("error getting repo info: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/repo/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (c *Content) CheckLicenses() error {
}
for {
opts.Page++
result, resp, err := c.githubClient.Organizations.ListCustomPropertyValues(context.TODO(), GitHubOrg, opts)
result, resp, err := c.githubClient.Organizations.ListCustomPropertyValues(context.TODO(), c.githubOrg, opts)
if err != nil {
return err
}
Expand Down Expand Up @@ -129,7 +129,7 @@ func (c *Content) UpdateLicense(repoName string) error {
return err
}

repo, _, err := c.githubClient.Repositories.Get(context.TODO(), GitHubOrg, repoName)
repo, _, err := c.githubClient.Repositories.Get(context.TODO(), c.githubOrg, repoName)
if err != nil {
return fmt.Errorf("error getting repo info: %w", err)
}
Expand Down

0 comments on commit 0c109a4

Please sign in to comment.