Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The following provider block variables are available for configuration:
- `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`)
- `gpg_secret_key` The private GPG key to use to sign commits (optional) (read from env var `$GPG_SECRET_KEY`)
- `gpg_passphrase` The passphrase associated with the aforementioned GPG key (optional) (read from env var `$GPG_PASSPHRASE`)
- `merge_method` Pull request merge method, default to `merge` available (`merge`, `squash`, `rebase`) (read from env var `$GITHUB_MERGE_METHOD`)

### Authentication

Expand Down Expand Up @@ -72,6 +73,6 @@ The above would result in the following content being committed to `.github/CODE

```
# automatically generated by terraform - please do not edit here
* @expert
* @expert
*.java @java-expert @my-org/experts
```
21 changes: 21 additions & 0 deletions codeowners/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ func Provider() *schema.Provider {
DefaultFunc: schema.EnvDefaultFunc("GITHUB_USERNAME", nil),
Sensitive: true,
},
"merge_method": {
Type: schema.TypeString,
Required: true,
Description: "PullRequest merge method. Default: merge, available: merge, squash, rebase",
DefaultFunc: schema.EnvDefaultFunc("GITHUB_MERGE_METHOD", nil),
Sensitive: true,
},
},
ResourcesMap: map[string]*schema.Resource{
"codeowners_file": resourceFile(),
Expand All @@ -68,6 +75,19 @@ type providerConfiguration struct {
ghEmail string
gpgKey string
gpgPassphrase string
mergeMethod string
}

func mergeMethod(mm string) string {
mergeMethods := []string{"merge", "squash", "rebase"}

for _, v := range mergeMethods {
if mm == v {
return mm
}
}

return "merge"
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
Expand All @@ -85,5 +105,6 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
ghUsername: d.Get("username").(string),
gpgKey: d.Get("gpg_secret_key").(string),
gpgPassphrase: d.Get("gpg_passphrase").(string),
mergeMethod: mergeMethod(d.Get("merge_method").(string)),
}, nil
}
6 changes: 6 additions & 0 deletions codeowners/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func testAccPreCheck(t *testing.T) {
}
}

func TestDefaultMergeMethod(t *testing.T) {
if mergeMethod("wat?") != "merge" {
t.Fatal("Unexpected default value for mergeMethod")
}
}

func init() {
testAccProvider = Provider()
testAccProviders = map[string]terraform.ResourceProvider{
Expand Down
9 changes: 5 additions & 4 deletions codeowners/resource_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package codeowners
import (
"context"
"fmt"
"github.com/form3tech-oss/go-github-utils/pkg/branch"
"net/http"
"sort"
"strings"
"time"

"github.com/form3tech-oss/go-github-utils/pkg/branch"
githubcommitutils "github.com/form3tech-oss/go-github-utils/pkg/commit"
githubfileutils "github.com/form3tech-oss/go-github-utils/pkg/file"
"github.com/google/go-github/v28/github"
Expand Down Expand Up @@ -114,7 +114,7 @@ func resourceFileRead(d *schema.ResourceData, m interface{}) error {
}

func resourceFileCreate(d *schema.ResourceData, m interface{}) error {
return resourceFileCreateOrUpdate("Adding CODEOWNERS file", d, m )
return resourceFileCreateOrUpdate("Adding CODEOWNERS file", d, m)
}

func resourceFileCreateOrUpdate(s string, d *schema.ResourceData, m interface{}) error {
Expand Down Expand Up @@ -154,16 +154,16 @@ func resourceFileCreateOrUpdate(s string, d *schema.ResourceData, m interface{})
RetryBackoff: 5 * time.Second,
PullRequestSourceBranchName: fmt.Sprintf("terraform-provider-codeowners-%d", time.Now().UnixNano()),
PullRequestBody: "",
MergeMethod: config.mergeMethod,
}); err != nil {
return err
}

return resourceFileRead(d, m)
}


func resourceFileUpdate(d *schema.ResourceData, m interface{}) error {
return resourceFileCreateOrUpdate("Updating CODEOWNERS file", d, m )
return resourceFileCreateOrUpdate("Updating CODEOWNERS file", d, m)
}

func resourceFileDelete(d *schema.ResourceData, m interface{}) error {
Expand Down Expand Up @@ -215,6 +215,7 @@ func resourceFileDelete(d *schema.ResourceData, m interface{}) error {
RetryBackoff: 5 * time.Second,
PullRequestSourceBranchName: fmt.Sprintf("terraform-provider-codeowners-%d", time.Now().UnixNano()),
PullRequestBody: "",
MergeMethod: config.mergeMethod,
}); err != nil {
return err
}
Expand Down