Skip to content

Commit 34b2b07

Browse files
author
Andy Gertjejansen
committed
Merge branch 'master' into feature/add-required-approving-review-count-#89
2 parents 9680382 + 4b32940 commit 34b2b07

10 files changed

+214
-21
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
## 1.3.1 (Unreleased)
1+
## 1.4.0 (Unreleased)
2+
3+
FEATURES:
4+
5+
* **New Resource:** `github_user_invitation_accepter` [GH-161]
26

37
BUG FIXES:
48

github/provider.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ import (
55
"github.com/hashicorp/terraform/terraform"
66
)
77

8-
// Provider returns a terraform.ResourceProvider.
98
func Provider() terraform.ResourceProvider {
109
var p *schema.Provider
11-
// The actual provider
1210
p = &schema.Provider{
1311
Schema: map[string]*schema.Schema{
1412
"token": &schema.Schema{
@@ -38,22 +36,23 @@ func Provider() terraform.ResourceProvider {
3836
},
3937

4038
ResourcesMap: map[string]*schema.Resource{
41-
"github_branch_protection": resourceGithubBranchProtection(),
42-
"github_issue_label": resourceGithubIssueLabel(),
43-
"github_membership": resourceGithubMembership(),
44-
"github_organization_project": resourceGithubOrganizationProject(),
45-
"github_organization_webhook": resourceGithubOrganizationWebhook(),
46-
"github_project_column": resourceGithubProjectColumn(),
47-
"github_repository": resourceGithubRepository(),
48-
"github_repository_collaborator": resourceGithubRepositoryCollaborator(),
49-
"github_repository_deploy_key": resourceGithubRepositoryDeployKey(),
50-
"github_repository_project": resourceGithubRepositoryProject(),
51-
"github_repository_webhook": resourceGithubRepositoryWebhook(),
52-
"github_team": resourceGithubTeam(),
53-
"github_team_membership": resourceGithubTeamMembership(),
54-
"github_team_repository": resourceGithubTeamRepository(),
55-
"github_user_gpg_key": resourceGithubUserGpgKey(),
56-
"github_user_ssh_key": resourceGithubUserSshKey(),
39+
"github_branch_protection": resourceGithubBranchProtection(),
40+
"github_issue_label": resourceGithubIssueLabel(),
41+
"github_membership": resourceGithubMembership(),
42+
"github_organization_project": resourceGithubOrganizationProject(),
43+
"github_organization_webhook": resourceGithubOrganizationWebhook(),
44+
"github_project_column": resourceGithubProjectColumn(),
45+
"github_repository": resourceGithubRepository(),
46+
"github_repository_collaborator": resourceGithubRepositoryCollaborator(),
47+
"github_repository_deploy_key": resourceGithubRepositoryDeployKey(),
48+
"github_repository_project": resourceGithubRepositoryProject(),
49+
"github_repository_webhook": resourceGithubRepositoryWebhook(),
50+
"github_team": resourceGithubTeam(),
51+
"github_team_membership": resourceGithubTeamMembership(),
52+
"github_team_repository": resourceGithubTeamRepository(),
53+
"github_user_gpg_key": resourceGithubUserGpgKey(),
54+
"github_user_invitation_accepter": resourceGithubUserInvitationAccepter(),
55+
"github_user_ssh_key": resourceGithubUserSshKey(),
5756
},
5857

5958
DataSourcesMap: map[string]*schema.Resource{

github/provider_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var testUser string = os.Getenv("GITHUB_TEST_USER")
1919
var testCollaborator string = os.Getenv("GITHUB_TEST_COLLABORATOR")
2020

2121
var testAccProviders map[string]terraform.ResourceProvider
22+
var testAccProviderFactories func(providers *[]*schema.Provider) map[string]terraform.ResourceProviderFactory
2223
var testAccProvider *schema.Provider
2324

2425
func init() {
@@ -27,6 +28,15 @@ func init() {
2728
"github": testAccProvider,
2829
"tls": tls.Provider(),
2930
}
31+
testAccProviderFactories = func(providers *[]*schema.Provider) map[string]terraform.ResourceProviderFactory {
32+
return map[string]terraform.ResourceProviderFactory{
33+
"github": func() (terraform.ResourceProvider, error) {
34+
p := Provider()
35+
*providers = append(*providers, p.(*schema.Provider))
36+
return p, nil
37+
},
38+
}
39+
}
3040
}
3141

3242
func TestProvider(t *testing.T) {

github/resource_github_repository_collaborator.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package github
22

33
import (
44
"context"
5+
"fmt"
56
"log"
67

78
"github.com/google/go-github/github"
@@ -36,6 +37,10 @@ func resourceGithubRepositoryCollaborator() *schema.Resource {
3637
Default: "push",
3738
ValidateFunc: validateValueFunc([]string{"pull", "push", "admin"}),
3839
},
40+
"invitation_id": {
41+
Type: schema.TypeString,
42+
Computed: true,
43+
},
3944
},
4045
}
4146
}
@@ -90,6 +95,7 @@ func resourceGithubRepositoryCollaboratorRead(d *schema.ResourceData, meta inter
9095
d.Set("repository", repoName)
9196
d.Set("username", username)
9297
d.Set("permission", permissionName)
98+
d.Set("invitation_id", fmt.Sprintf("%d", invitation.GetID()))
9399
return nil
94100
}
95101

github/resource_github_repository_collaborator_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package github
33
import (
44
"context"
55
"fmt"
6+
"regexp"
67
"testing"
78

89
"github.com/hashicorp/terraform/helper/acctest"
@@ -13,6 +14,7 @@ import (
1314
const expectedPermission string = "admin"
1415

1516
func TestAccGithubRepositoryCollaborator_basic(t *testing.T) {
17+
resourceName := "github_repository_collaborator.test_repo_collaborator"
1618
repoName := fmt.Sprintf("tf-acc-test-collab-%s", acctest.RandString(5))
1719

1820
resource.Test(t, resource.TestCase{
@@ -23,8 +25,10 @@ func TestAccGithubRepositoryCollaborator_basic(t *testing.T) {
2325
{
2426
Config: testAccGithubRepositoryCollaboratorConfig(repoName),
2527
Check: resource.ComposeTestCheckFunc(
26-
testAccCheckGithubRepositoryCollaboratorExists("github_repository_collaborator.test_repo_collaborator"),
27-
testAccCheckGithubRepositoryCollaboratorPermission("github_repository_collaborator.test_repo_collaborator"),
28+
testAccCheckGithubRepositoryCollaboratorExists(resourceName),
29+
testAccCheckGithubRepositoryCollaboratorPermission(resourceName),
30+
resource.TestCheckResourceAttr(resourceName, "permission", expectedPermission),
31+
resource.TestMatchResourceAttr(resourceName, "invitation_id", regexp.MustCompile(`^[0-9]+$`)),
2832
),
2933
},
3034
},
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"strconv"
8+
9+
"github.com/hashicorp/terraform/helper/schema"
10+
)
11+
12+
func resourceGithubUserInvitationAccepter() *schema.Resource {
13+
return &schema.Resource{
14+
Create: resourceGithubUserInvitationAccepterCreate,
15+
Read: schema.Noop, // Nothing to read as invitation is removed after it's accepted
16+
Delete: schema.Noop, // Nothing to remove as invitation is removed after it's accepted
17+
18+
Schema: map[string]*schema.Schema{
19+
"invitation_id": {
20+
Type: schema.TypeString,
21+
Required: true,
22+
ForceNew: true,
23+
},
24+
},
25+
}
26+
}
27+
28+
func resourceGithubUserInvitationAccepterCreate(d *schema.ResourceData, meta interface{}) error {
29+
client := meta.(*Organization).client
30+
31+
invitationIdString := d.Get("invitation_id").(string)
32+
invitationId, err := strconv.Atoi(invitationIdString)
33+
if err != nil {
34+
return fmt.Errorf("Failed to parse invitation ID: %s", err)
35+
}
36+
ctx := context.Background()
37+
38+
log.Printf("[DEBUG] Accepting invitation: %d", invitationId)
39+
_, err = client.Users.AcceptInvitation(ctx, int64(invitationId))
40+
if err != nil {
41+
return err
42+
}
43+
44+
d.SetId(invitationIdString)
45+
46+
return nil
47+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package github
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"regexp"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform/helper/acctest"
10+
"github.com/hashicorp/terraform/helper/resource"
11+
"github.com/hashicorp/terraform/helper/schema"
12+
"github.com/hashicorp/terraform/terraform"
13+
)
14+
15+
func TestAccGithubUserInvitationAccepter_basic(t *testing.T) {
16+
resourceName := "github_repository_collaborator.test"
17+
repoName := fmt.Sprintf("tf-acc-test-collab-%s", acctest.RandString(5))
18+
19+
inviteeToken := os.Getenv("GITHUB_TEST_COLLABORATOR_TOKEN")
20+
if inviteeToken == "" {
21+
t.Skip("GITHUB_TEST_COLLABORATOR_TOKEN was not provided, skipping test")
22+
}
23+
24+
var providers []*schema.Provider
25+
26+
resource.Test(t, resource.TestCase{
27+
PreCheck: func() { testAccPreCheck(t) },
28+
ProviderFactories: testAccProviderFactories(&providers),
29+
CheckDestroy: testAccCheckGithubUserInvitationAccepterDestroy,
30+
Steps: []resource.TestStep{
31+
{
32+
Config: testAccGithubUserInvitationAccepterConfig(inviteeToken, repoName),
33+
Check: resource.ComposeTestCheckFunc(
34+
resource.TestCheckResourceAttr(resourceName, "permission", "push"),
35+
resource.TestMatchResourceAttr(resourceName, "invitation_id", regexp.MustCompile(`^[0-9]+$`)),
36+
),
37+
},
38+
},
39+
})
40+
}
41+
42+
func testAccCheckGithubUserInvitationAccepterDestroy(s *terraform.State) error {
43+
return nil
44+
}
45+
46+
func testAccGithubUserInvitationAccepterConfig(inviteeToken, repoName string) string {
47+
return fmt.Sprintf(`
48+
provider "github" {
49+
alias = "main"
50+
}
51+
52+
provider "github" {
53+
alias = "invitee"
54+
token = "%s"
55+
}
56+
57+
resource "github_repository" "test" {
58+
provider = "github.main"
59+
name = "%s"
60+
}
61+
62+
resource "github_repository_collaborator" "test" {
63+
provider = "github.main"
64+
repository = "${github_repository.test.name}"
65+
username = "%s"
66+
permission = "push"
67+
}
68+
69+
resource "github_user_invitation_accepter" "test" {
70+
provider = "github.invitee"
71+
invitation_id = "${github_repository_collaborator.test.invitation_id}"
72+
}
73+
`, inviteeToken, repoName, testCollaborator)
74+
}

website/docs/r/repository_collaborator.html.markdown

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ The following arguments are supported:
4444
* `permission` - (Optional) The permission of the outside collaborator for the repository.
4545
Must be one of `pull`, `push`, or `admin`. Defaults to `push`.
4646

47+
## Attribute Reference
48+
49+
In addition to the above arguments, the following attributes are exported:
50+
51+
* `invitation_id` - ID of the invitation to be used in [`github_user_invitation_accepter`](./user_invitation_accepter.html)
4752

4853
## Import
4954

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
layout: "github"
3+
page_title: "GitHub: github_user_invitation_accepter"
4+
sidebar_current: "docs-github-resource-user-invitation-accepter"
5+
description: |-
6+
Provides a resource to manage GitHub repository collaborator invitations.
7+
---
8+
9+
# github_user_invitation_accepter
10+
11+
Provides a resource to manage GitHub repository collaborator invitations.
12+
13+
## Example Usage
14+
15+
```hcl
16+
resource "github_repository" "example" {
17+
name = "example-repo"
18+
}
19+
20+
resource "github_repository_collaborator" "example" {
21+
repository = "${github_repository.example.name}"
22+
username = "example-username"
23+
permission = "push"
24+
}
25+
26+
provider "github" {
27+
alias = "invitee"
28+
token = "${var.invitee_token}"
29+
}
30+
31+
resource "github_user_invitation_accepter" "example" {
32+
provider = "github.invitee"
33+
invitation_id = "${github_repository_collaborator.example.invitation_id}"
34+
}
35+
```
36+
37+
## Argument Reference
38+
39+
The following arguments are supported:
40+
41+
* `invitation_id` - (Required) ID of the invitation to accept

website/github.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@
7979
<li<%= sidebar_current("docs-github-resource-user-gpg-key") %>>
8080
<a href="/docs/providers/github/r/user_gpg_key.html">github_user_gpg_key</a>
8181
</li>
82+
<li<%= sidebar_current("docs-github-resource-user-invitation-accepter") %>>
83+
<a href="/docs/providers/github/r/user_invitation_accepter.html">github_user_invitation_accepter</a>
84+
</li>
8285
<li<%= sidebar_current("docs-github-resource-user-ssh-key") %>>
8386
<a href="/docs/providers/github/r/user_ssh_key.html">github_user_ssh_key</a>
8487
</li>

0 commit comments

Comments
 (0)