Skip to content

Commit

Permalink
Merge pull request #22 from form3tech-oss/bmcstdio-commit-message-prefix
Browse files Browse the repository at this point in the history
Add support for specifying a commit message prefix.
  • Loading branch information
bmcustodio authored Sep 16, 2019
2 parents 4290cdf + f1f1e88 commit aa3a1df
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Download the relevant binary from [releases](https://github.com/form3tech-oss/te

The following provider block variables are available for configuration:

- `commit_message_prefix` - An optional prefix to be added to all commits generated as a result of manipulating the `CODEOWNERS` file.
- `github_token` GitHub auth token - see below section. (read from env var `$GITHUB_TOKEN`)
- `username` Username to use in commits (read from env var `$GITHUB_USERNAME`)
- `email` Email to use in commits - this must match the email in your GPG key if you are signing commits (read from env var `$GITHUB_EMAIL`)
Expand Down
28 changes: 18 additions & 10 deletions codeowners/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import (
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"commit_message_prefix": {
Type: schema.TypeString,
Optional: true,
Description: "An optional prefix to be added to all commits generated as a result of manipulating the 'CODEOWNERS' file.",
DefaultFunc: schema.EnvDefaultFunc("COMMIT_MESSAGE_PREFIX", nil),
},
"github_token": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -56,11 +62,12 @@ func Provider() *schema.Provider {
}

type providerConfiguration struct {
client *github.Client
ghUsername string
ghEmail string
gpgKey string
gpgPassphrase string
commitMessagePrefix string
client *github.Client
ghUsername string
ghEmail string
gpgKey string
gpgPassphrase string
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
Expand All @@ -72,10 +79,11 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
tc := oauth2.NewClient(ctx, ts)

return &providerConfiguration{
client: github.NewClient(tc),
ghEmail: d.Get("email").(string),
ghUsername: d.Get("username").(string),
gpgKey: d.Get("gpg_secret_key").(string),
gpgPassphrase: d.Get("gpg_passphrase").(string),
commitMessagePrefix: d.Get("commit_message_prefix").(string),
client: github.NewClient(tc),
ghEmail: d.Get("email").(string),
ghUsername: d.Get("username").(string),
gpgKey: d.Get("gpg_secret_key").(string),
gpgPassphrase: d.Get("gpg_passphrase").(string),
}, nil
}
13 changes: 10 additions & 3 deletions codeowners/resource_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func resourceFileCreate(d *schema.ResourceData, m interface{}) error {
repoOwner: file.RepositoryOwner,
repoName: file.RepositoryName,
branch: file.Branch,
commitMessage: "Adding CODEOWNERS file",
commitMessage: formatCommitMessage(config.commitMessagePrefix, "Adding CODEOWNERS file"),
gpgPassphrase: config.gpgPassphrase,
gpgPrivateKey: config.gpgKey,
username: config.ghUsername,
Expand Down Expand Up @@ -180,7 +180,7 @@ func resourceFileUpdate(d *schema.ResourceData, m interface{}) error {
repoOwner: file.RepositoryOwner,
repoName: file.RepositoryName,
branch: file.Branch,
commitMessage: "Updating CODEOWNERS file",
commitMessage: formatCommitMessage(config.commitMessagePrefix, "Updating CODEOWNERS file"),
gpgPassphrase: config.gpgPassphrase,
gpgPrivateKey: config.gpgKey,
username: config.ghUsername,
Expand Down Expand Up @@ -210,7 +210,7 @@ func resourceFileDelete(d *schema.ResourceData, m interface{}) error {
}

options := &github.RepositoryContentFileOptions{
Message: github.String("Removing CODEOWNERS file"),
Message: github.String(formatCommitMessage(config.commitMessagePrefix, "Removing CODEOWNERS file")),
SHA: codeOwnerContent.SHA,
}
if file.Branch != "" {
Expand Down Expand Up @@ -297,3 +297,10 @@ func expandRuleset(in *schema.Set) Ruleset {
}
return out
}

func formatCommitMessage(p, m string) string {
if p == "" {
return m
}
return strings.TrimSpace(p) + " " + m
}

0 comments on commit aa3a1df

Please sign in to comment.