Skip to content

Commit

Permalink
Add support for specifying a commit message prefix.
Browse files Browse the repository at this point in the history
Signed-off-by: Bruno Miguel Custódio <brunomcustodio@gmail.com>
  • Loading branch information
bmcustodio committed Sep 16, 2019
1 parent 3e218d1 commit 4d87c5f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The following provider block variables are available for configuration:

| Name | Description |
| ---- | ----------- |
| `commit_message_prefix` | An optional prefix to be added to all commits generated as a result of manipulating files. |
| `github_email` | The email address to use for commit messages.<br>If a GPG key is provided, this must match the one which the key corresponds to. |
| `github_token` | A GitHub authorisation token with `repo` permissions and having `admin` access to the target repositories. |
| `github_username` | The username to use for commit messages. |
Expand Down
40 changes: 25 additions & 15 deletions githubfile/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,21 @@ const (
)

const (
githubEmailKey = "github_email"
githubTokenKey = "github_token"
githubUsernameKey = "github_username"
gpgPassphraseKey = "gpg_passphrase"
gpgSecretKeyKey = "gpg_secret_key"
commitMessagePrefixKey = "commit_message_prefix"
githubEmailKey = "github_email"
githubTokenKey = "github_token"
githubUsernameKey = "github_username"
gpgPassphraseKey = "gpg_passphrase"
gpgSecretKeyKey = "gpg_secret_key"
)

type providerConfiguration struct {
githubClient *github.Client
githubEmail string
githubUsername string
gpgPassphrase string
gpgSecretKey string
commitMessagePrefix string
githubClient *github.Client
githubEmail string
githubUsername string
gpgPassphrase string
gpgSecretKey string
}

func Provider() *schema.Provider {
Expand All @@ -60,17 +62,25 @@ func Provider() *schema.Provider {
sk = string(v)
}
return &providerConfiguration{
githubClient: github.NewClient(tc),
githubEmail: d.Get(githubEmailKey).(string),
githubUsername: d.Get(githubUsernameKey).(string),
gpgSecretKey: sk,
gpgPassphrase: d.Get(gpgPassphraseKey).(string),
commitMessagePrefix: d.Get(commitMessagePrefixKey).(string),
githubClient: github.NewClient(tc),
githubEmail: d.Get(githubEmailKey).(string),
githubUsername: d.Get(githubUsernameKey).(string),
gpgSecretKey: sk,
gpgPassphrase: d.Get(gpgPassphraseKey).(string),
}, nil
},
ResourcesMap: map[string]*schema.Resource{
resourceFileName: resourceFile(),
},
Schema: map[string]*schema.Schema{
commitMessagePrefixKey: {
Type: schema.TypeString,
DefaultFunc: defaultFuncForKey(commitMessagePrefixKey),
Optional: true,
Sensitive: false,
Description: "An optional prefix to be added to all commits created as a result of manipulating files.",
},
githubEmailKey: {
Type: schema.TypeString,
DefaultFunc: defaultFuncForKey(githubEmailKey),
Expand Down
12 changes: 10 additions & 2 deletions githubfile/resource_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package githubfile
import (
"context"
"fmt"
"strings"
"time"

"github.com/form3tech-oss/go-github-utils/pkg/branch"
Expand Down Expand Up @@ -98,7 +99,7 @@ func resourceFileCreateOrUpdate(s string, d *schema.ResourceData, m interface{})
if err := commit.CreateCommit(context.Background(), c.githubClient, &commit.CommitOptions{
RepoOwner: f.repositoryOwner,
RepoName: f.repositoryName,
CommitMessage: fmt.Sprintf(s, f.path),
CommitMessage: formatCommitMessage(c.commitMessagePrefix, s, f.path),
GpgPassphrase: c.gpgPassphrase,
GpgPrivateKey: c.gpgSecretKey,
Username: c.githubUsername,
Expand Down Expand Up @@ -150,7 +151,7 @@ func resourceFileDelete(d *schema.ResourceData, m interface{}) error {
if err := commit.CreateCommit(context.Background(), c.githubClient, &commit.CommitOptions{
RepoOwner: f.repositoryOwner,
RepoName: f.repositoryName,
CommitMessage: fmt.Sprintf("Delete %q.", f.path),
CommitMessage: formatCommitMessage(c.commitMessagePrefix, "Delete %q.", f.path),
GpgPassphrase: c.gpgPassphrase,
GpgPrivateKey: c.gpgSecretKey,
Username: c.githubUsername,
Expand Down Expand Up @@ -194,3 +195,10 @@ func resourceFileRead(d *schema.ResourceData, m interface{}) error {
func resourceFileUpdate(d *schema.ResourceData, m interface{}) error {
return resourceFileCreateOrUpdate("Update %q.", d, m)
}

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

0 comments on commit 4d87c5f

Please sign in to comment.