Skip to content

Commit

Permalink
Resolve repository name to node ID if provided (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Udit authored Nov 9, 2020
1 parent 31d9d4f commit 0fd055c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 8 deletions.
2 changes: 1 addition & 1 deletion github/resource_github_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func resourceGithubBranchProtection() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "",
Description: "Node ID or name of repository",
},
PROTECTION_PATTERN: {
Type: schema.TypeString,
Expand Down
4 changes: 2 additions & 2 deletions github/resource_github_branch_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ func TestAccGithubBranchProtection(t *testing.T) {
resource "github_branch_protection" "test" {
repository_id = github_repository.test.node_id
pattern = "main"
repository_id = github_repository.test.name
pattern = "main"
push_restrictions = [
data.github_user.test.node_id,
Expand Down
7 changes: 6 additions & 1 deletion github/util_v4_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package github
import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/shurcooL/githubv4"
)
Expand Down Expand Up @@ -68,7 +69,11 @@ func branchProtectionResourceData(d *schema.ResourceData, meta interface{}) (Bra
data := BranchProtectionResourceData{}

if v, ok := d.GetOk(REPOSITORY_ID); ok {
data.RepositoryID = v.(string)
repoID, err := getRepositoryID(v.(string), meta)
if err != nil {
return data, err
}
data.RepositoryID = repoID.(string)
}

if v, ok := d.GetOk(PROTECTION_PATTERN); ok {
Expand Down
39 changes: 38 additions & 1 deletion github/util_v4_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@ package github

import (
"context"

"github.com/shurcooL/githubv4"
)

func getRepositoryID(name string, meta interface{}) (githubv4.ID, error) {

// Interperet `name` as a node ID and return
exists, err := repositoryNodeIDExists(name, meta)
if exists {
return githubv4.ID(name), nil
}
if err != nil {
return nil, err
}

// Resolve `name` to a node ID and return
var query struct {
Repository struct {
ID githubv4.ID
Expand All @@ -17,10 +29,35 @@ func getRepositoryID(name string, meta interface{}) (githubv4.ID, error) {
}
ctx := context.Background()
client := meta.(*Owner).v4client
err := client.Query(ctx, &query, variables)
err = client.Query(ctx, &query, variables)
if err != nil {
return nil, err
}

return query.Repository.ID, nil
}

func repositoryNodeIDExists(name string, meta interface{}) (bool, error) {
// Quick check for node ID length
if len(name) != 32 {
return false, nil
}

// API check if node ID exists
var query struct {
Node struct {
ID githubv4.ID
} `graphql:"node(id:$id)"`
}
variables := map[string]interface{}{
"id": githubv4.ID(name),
}
ctx := context.Background()
client := meta.(*Owner).v4client
err := client.Query(ctx, &query, variables)
if err != nil {
return false, err
}

return query.Node.ID.(string) == name, nil
}
13 changes: 10 additions & 3 deletions website/docs/r/branch_protection.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ This resource allows you to configure branch protection for repositories in your
# Protect the master branch of the foo repository. Additionally, require that
# the "ci/travis" context to be passing and only allow the engineers team merge
# to the branch.
resource "github_branch_protection" "example" {
repository_id = github_repository.example.node_id
repository_id = github_repository.example.node_id
# also accepts repository name
# repository_id = github_repository.example.name
pattern = "main"
enforce_admins = true
Expand All @@ -37,8 +42,10 @@ resource "github_branch_protection" "example" {
push_restrictions = [
data.github_user.example.node_id,
github_team.example.node_id,
# limited to a list of one type of restriction (user, team, app)
# github_team.example.node_id
]
}
resource "github_user" "example" {
Expand All @@ -60,7 +67,7 @@ resource "github_team_repository" "example" {

The following arguments are supported:

* `repository_id` - (Required) The repository associated with this branch protection rule.
* `repository_id` - (Required) The name or node ID of the repository associated with this branch protection rule.
* `pattern` - (Required) Identifies the protection rule pattern.
* `enforce_admins` - (Optional) Boolean, setting this to `true` enforces status checks for repository administrators.
* `require_signed_commits` - (Optional) Boolean, setting this to `true` requires all commits to be signed with GPG.
Expand Down

0 comments on commit 0fd055c

Please sign in to comment.