Skip to content

Commit

Permalink
Support Pull Requests (integrations#739)
Browse files Browse the repository at this point in the history
* Support Pull Requests

* fixup! typo in description

Co-authored-by: Jeremy Udit <[email protected]>
  • Loading branch information
marcinwyszynski and Jeremy Udit authored Apr 17, 2021
1 parent 1cf6656 commit 36b9d27
Show file tree
Hide file tree
Showing 11 changed files with 1,175 additions and 0 deletions.
150 changes: 150 additions & 0 deletions github/data_source_github_repository_pull_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package github

import (
"context"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceGithubRepositoryPullRequest() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubRepositoryPullRequestRead,
Schema: map[string]*schema.Schema{
"owner": {
Type: schema.TypeString,
Optional: true,
},
"base_repository": {
Type: schema.TypeString,
Required: true,
},
"number": {
Type: schema.TypeInt,
Required: true,
},
"base_ref": {
Type: schema.TypeString,
Computed: true,
},
"base_sha": {
Type: schema.TypeString,
Computed: true,
},
"body": {
Type: schema.TypeString,
Computed: true,
},
"draft": {
Type: schema.TypeBool,
Computed: true,
},
"head_owner": {
Type: schema.TypeString,
Computed: true,
},
"head_ref": {
Type: schema.TypeString,
Computed: true,
},
"head_repository": {
Type: schema.TypeString,
Computed: true,
},
"head_sha": {
Type: schema.TypeString,
Computed: true,
},
"labels": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
Description: "List of names of labels on the PR",
},
"maintainer_can_modify": {
Type: schema.TypeBool,
Computed: true,
},
"opened_at": {
Type: schema.TypeInt,
Computed: true,
},
"opened_by": {
Type: schema.TypeString,
Computed: true,
Description: "Username of the PR creator",
},
"state": {
Type: schema.TypeString,
Computed: true,
},
"title": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}

func dataSourceGithubRepositoryPullRequestRead(d *schema.ResourceData, meta interface{}) error {
ctx := context.TODO()
client := meta.(*Owner).v3client

owner := meta.(*Owner).name
if expliclitOwner, ok := d.GetOk("owner"); ok {
owner = expliclitOwner.(string)
}

repository := d.Get("base_repository").(string)
number := d.Get("number").(int)

pullRequest, _, err := client.PullRequests.Get(ctx, owner, repository, number)
if err != nil {
return err
}

if head := pullRequest.GetHead(); head != nil {
d.Set("head_ref", head.GetRef())
d.Set("head_sha", head.GetSHA())

if headRepo := head.Repo; headRepo != nil {
d.Set("head_repository", headRepo.GetName())
}

if headUser := head.User; headUser != nil {
d.Set("head_owner", headUser.GetLogin())
}
}

if base := pullRequest.GetBase(); base != nil {
d.Set("base_ref", base.GetRef())
d.Set("base_sha", base.GetSHA())
}

d.Set("body", pullRequest.GetBody())
d.Set("draft", pullRequest.GetDraft())
d.Set("maintainer_can_modify", pullRequest.GetMaintainerCanModify())
d.Set("number", pullRequest.GetNumber())
d.Set("opened_at", pullRequest.GetCreatedAt().Unix())
d.Set("state", pullRequest.GetState())
d.Set("title", pullRequest.GetTitle())
d.Set("updated_at", pullRequest.GetUpdatedAt().Unix())

if user := pullRequest.GetUser(); user != nil {
d.Set("opened_by", user.GetLogin())
}

labels := []string{}
for _, label := range pullRequest.Labels {
labels = append(labels, label.GetName())
}
d.Set("labels", labels)

d.SetId(buildThreePartID(owner, repository, strconv.Itoa(number)))

return nil
}
96 changes: 96 additions & 0 deletions github/data_source_github_repository_pull_request_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package github

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccGithubRepositoryPullRequestDataSource(t *testing.T) {
t.Run("manages the pull request lifecycle", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-%s"
auto_init = true
}
resource "github_branch" "test" {
repository = github_repository.test.name
branch = "test"
source_branch = github_repository.test.default_branch
}
resource "github_repository_file" "test" {
repository = github_repository.test.name
branch = github_branch.test.branch
file = "test"
content = "bar"
}
resource "github_repository_pull_request" "test" {
base_repository = github_repository_file.test.repository
base_ref = github_repository.test.default_branch
head_ref = github_branch.test.branch
title = "test title"
body = "test body"
}
data "github_repository_pull_request" "test" {
base_repository = github_repository_pull_request.test.base_repository
number = github_repository_pull_request.test.number
}
`, randomID)

const resourceName = "data.github_repository_pull_request.test"

check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
resourceName, "base_repository",
fmt.Sprintf("tf-acc-test-%s", randomID),
),
resource.TestCheckResourceAttr(resourceName, "base_ref", "main"),
resource.TestCheckResourceAttr(resourceName, "head_ref", "test"),
resource.TestCheckResourceAttr(resourceName, "title", "test title"),
resource.TestCheckResourceAttr(resourceName, "body", "test body"),
resource.TestCheckResourceAttr(resourceName, "maintainer_can_modify", "false"),
resource.TestCheckResourceAttrSet(resourceName, "base_sha"),
resource.TestCheckResourceAttr(resourceName, "draft", "false"),
resource.TestCheckResourceAttrSet(resourceName, "head_sha"),
resource.TestCheckResourceAttr(resourceName, "labels.#", "0"),
resource.TestCheckResourceAttrSet(resourceName, "number"),
resource.TestCheckResourceAttrSet(resourceName, "opened_at"),
resource.TestCheckResourceAttrSet(resourceName, "opened_by"),
resource.TestCheckResourceAttr(resourceName, "state", "open"),
resource.TestCheckResourceAttrSet(resourceName, "updated_at"),
)

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
},
})
}

t.Run("with an anonymous account", func(t *testing.T) {
t.Skip("anonymous account not supported for this operation")
})

t.Run("with an individual account", func(t *testing.T) {
testCase(t, individual)
})

t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})
})
}
Loading

0 comments on commit 36b9d27

Please sign in to comment.