Skip to content

Commit af9dbec

Browse files
authored
Merge pull request #10 from form3tech-oss/bmcstdio-commit-message-prefix
Add support for specifying a commit message prefix.
2 parents 3e218d1 + 4d87c5f commit af9dbec

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ The following provider block variables are available for configuration:
2020

2121
| Name | Description |
2222
| ---- | ----------- |
23+
| `commit_message_prefix` | An optional prefix to be added to all commits generated as a result of manipulating files. |
2324
| `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. |
2425
| `github_token` | A GitHub authorisation token with `repo` permissions and having `admin` access to the target repositories. |
2526
| `github_username` | The username to use for commit messages. |

githubfile/provider.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,21 @@ const (
3030
)
3131

3232
const (
33-
githubEmailKey = "github_email"
34-
githubTokenKey = "github_token"
35-
githubUsernameKey = "github_username"
36-
gpgPassphraseKey = "gpg_passphrase"
37-
gpgSecretKeyKey = "gpg_secret_key"
33+
commitMessagePrefixKey = "commit_message_prefix"
34+
githubEmailKey = "github_email"
35+
githubTokenKey = "github_token"
36+
githubUsernameKey = "github_username"
37+
gpgPassphraseKey = "gpg_passphrase"
38+
gpgSecretKeyKey = "gpg_secret_key"
3839
)
3940

4041
type providerConfiguration struct {
41-
githubClient *github.Client
42-
githubEmail string
43-
githubUsername string
44-
gpgPassphrase string
45-
gpgSecretKey string
42+
commitMessagePrefix string
43+
githubClient *github.Client
44+
githubEmail string
45+
githubUsername string
46+
gpgPassphrase string
47+
gpgSecretKey string
4648
}
4749

4850
func Provider() *schema.Provider {
@@ -60,17 +62,25 @@ func Provider() *schema.Provider {
6062
sk = string(v)
6163
}
6264
return &providerConfiguration{
63-
githubClient: github.NewClient(tc),
64-
githubEmail: d.Get(githubEmailKey).(string),
65-
githubUsername: d.Get(githubUsernameKey).(string),
66-
gpgSecretKey: sk,
67-
gpgPassphrase: d.Get(gpgPassphraseKey).(string),
65+
commitMessagePrefix: d.Get(commitMessagePrefixKey).(string),
66+
githubClient: github.NewClient(tc),
67+
githubEmail: d.Get(githubEmailKey).(string),
68+
githubUsername: d.Get(githubUsernameKey).(string),
69+
gpgSecretKey: sk,
70+
gpgPassphrase: d.Get(gpgPassphraseKey).(string),
6871
}, nil
6972
},
7073
ResourcesMap: map[string]*schema.Resource{
7174
resourceFileName: resourceFile(),
7275
},
7376
Schema: map[string]*schema.Schema{
77+
commitMessagePrefixKey: {
78+
Type: schema.TypeString,
79+
DefaultFunc: defaultFuncForKey(commitMessagePrefixKey),
80+
Optional: true,
81+
Sensitive: false,
82+
Description: "An optional prefix to be added to all commits created as a result of manipulating files.",
83+
},
7484
githubEmailKey: {
7585
Type: schema.TypeString,
7686
DefaultFunc: defaultFuncForKey(githubEmailKey),

githubfile/resource_file.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package githubfile
1717
import (
1818
"context"
1919
"fmt"
20+
"strings"
2021
"time"
2122

2223
"github.com/form3tech-oss/go-github-utils/pkg/branch"
@@ -98,7 +99,7 @@ func resourceFileCreateOrUpdate(s string, d *schema.ResourceData, m interface{})
9899
if err := commit.CreateCommit(context.Background(), c.githubClient, &commit.CommitOptions{
99100
RepoOwner: f.repositoryOwner,
100101
RepoName: f.repositoryName,
101-
CommitMessage: fmt.Sprintf(s, f.path),
102+
CommitMessage: formatCommitMessage(c.commitMessagePrefix, s, f.path),
102103
GpgPassphrase: c.gpgPassphrase,
103104
GpgPrivateKey: c.gpgSecretKey,
104105
Username: c.githubUsername,
@@ -150,7 +151,7 @@ func resourceFileDelete(d *schema.ResourceData, m interface{}) error {
150151
if err := commit.CreateCommit(context.Background(), c.githubClient, &commit.CommitOptions{
151152
RepoOwner: f.repositoryOwner,
152153
RepoName: f.repositoryName,
153-
CommitMessage: fmt.Sprintf("Delete %q.", f.path),
154+
CommitMessage: formatCommitMessage(c.commitMessagePrefix, "Delete %q.", f.path),
154155
GpgPassphrase: c.gpgPassphrase,
155156
GpgPrivateKey: c.gpgSecretKey,
156157
Username: c.githubUsername,
@@ -194,3 +195,10 @@ func resourceFileRead(d *schema.ResourceData, m interface{}) error {
194195
func resourceFileUpdate(d *schema.ResourceData, m interface{}) error {
195196
return resourceFileCreateOrUpdate("Update %q.", d, m)
196197
}
198+
199+
func formatCommitMessage(p, m string, args ...interface{}) string {
200+
if p == "" {
201+
return fmt.Sprintf(m, args...)
202+
}
203+
return fmt.Sprintf(strings.TrimSpace(p)+" "+m, args...)
204+
}

0 commit comments

Comments
 (0)