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
9 changes: 7 additions & 2 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ func resourceGithubExample() *schema.Resource {
StateContext: resourceGithubExampleImport,
},

// Include SchemaVersion and StateUpgraders if state migrations exist
// Only if required.
CustomizeDiff: diffExample,

// Include SchemaVersion and StateUpgraders if state migrations exist.
SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Expand All @@ -188,8 +191,10 @@ func resourceGithubExample() *schema.Resource {
},
},

Description: "Manages an example GitHub resource.",

Schema: map[string]*schema.Schema{
// Schema definition
// Schema definition.
},
}
}
Expand Down
45 changes: 31 additions & 14 deletions docs/resources/repository_custom_property.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,62 @@
---
page_title: "github_repository_custom_property (Resource) - GitHub"
subcategory: ""
description: |-
Creates and a specific custom property for a GitHub repository
Resource to manage GitHub repository custom properties.
---

# github_repository_custom_property (Resource)

This resource allows you to create and manage a specific custom property for a GitHub repository.
Resource to manage GitHub repository custom properties.
For more information, see the [GitHub API documentation](https://docs.github.com/rest/metadata/custom-properties#create-or-update-repository-custom-property).

## Example Usage

> Note that this assumes there already is a custom property defined on the org level called `my-cool-property` of type `string`

```terraform
# NOTE: This assumes there already is a custom property defined on the org level called `my-cool-string` of type `string`

resource "github_repository" "example" {
name = "example"
description = "My awesome codebase"
}
resource "github_repository_custom_property" "string" {

resource "github_repository_custom_property" "example" {
repository = github_repository.example.name
property_name = "my-cool-property"
property_name = "my-cool-string"
property_type = "string"
property_value = ["test"]
}
```

## Argument Reference
<!-- schema generated by tfplugindocs -->
## Schema

The following arguments are supported:
### Required

- `repository` - (Required) The repository of the environment.
- `property_name` (String) Name of the custom property.
- `property_type` (String) Type of the custom property. Valid values are `string`, `single_select`, `multi_select`, `true_false`, and `url`.
- `property_value` (Set of String) Value of the custom property. For `string`, `single_select`, `true_false`, and `url` property types, this should be a single value. For `multi_select` property types, this can be multiple values.
- `repository` (String) Name of the repository.

- `property_type` - (Required) Type of the custom property. Can be one of `single_select`, `multi_select`, `string`, or `true_false`
### Read-Only

- `property_name` - (Required) Name of the custom property. Note that a pre-requisiste for this resource is that a custom property of this name has already been defined on the organization level

- `property_value` - (Required) Value of the custom property in the form of an array. Properties of type `single_select`, `string`, and `true_false` are represented as a string array of length 1
- `id` (String) The ID of this resource.
- `repository_id` (Number) ID of the repository.

## Import

GitHub Repository Custom Property can be imported using an ID made up of a combination of the names of the organization, repository, custom property separated by a `:` character, e.g.
Import is supported using the following syntax:

In Terraform v1.5.0 and later, the [`import` block](https://developer.hashicorp.com/terraform/language/import) can be used with the `id` attribute, for example:

```terraform
import {
to = github_repository_custom_property.example
id = "organization-name:repo-name:custom-property-name"
}
```

The [`terraform import` command](https://developer.hashicorp.com/terraform/cli/commands/import) can be used, for example:

```shell
terraform import github_repository_custom_property.example organization-name:repo-name:custom-property-name
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {
to = github_repository_custom_property.example
id = "organization-name:repo-name:custom-property-name"
Comment thread
stevehipwell marked this conversation as resolved.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import github_repository_custom_property.example organization-name:repo-name:custom-property-name
Comment thread
stevehipwell marked this conversation as resolved.
13 changes: 13 additions & 0 deletions examples/resources/github_repository_custom_property/resource_1.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# NOTE: This assumes there already is a custom property defined on the org level called `my-cool-string` of type `string`

resource "github_repository" "example" {
name = "example"
description = "My awesome codebase"
}

resource "github_repository_custom_property" "example" {
repository = github_repository.example.name
property_name = "my-cool-string"
property_type = "string"
property_value = ["test"]
}
10 changes: 0 additions & 10 deletions examples/resources/repository_custom_property/example_1.tf

This file was deleted.

106 changes: 106 additions & 0 deletions github/acc_helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package github

import (
"context"
"encoding/json"
"errors"
"fmt"
"testing"

"github.com/google/go-github/v88/github"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
)

const testRandomIDLength = 5

func mustGetTestMockResponse(t *testing.T, uri string, statusCode int, body any) *mockResponse {
resp := &mockResponse{
ExpectedUri: uri,
StatusCode: statusCode,
}

if body != nil {
bodyBytes, err := json.Marshal(body)
if err != nil {
t.Fatalf("failed to marshal mock response body: %v", err)
}
resp.ResponseBody = string(bodyBytes)
}

return resp
}

func mustCreateTestGitHubClient(t *testing.T, baseURL string, opts ...github.ClientOptionsFunc) *github.Client {
client, err := github.NewClient(append([]github.ClientOptionsFunc{github.WithURLs(&baseURL, nil)}, opts...)...)
if err != nil {
t.Fatalf("failed to create GitHub client: %s", err)
}
return client
}

func mustCreateTestOrganizationRepositoryCustomProperty(t *testing.T, valType string, allowed []string) *github.CustomProperty {
t.Helper()

meta, err := getTestMeta()
if err != nil {
t.Fatalf("failed to get test meta: %v", err)
}

randomID := acctest.RandString(testRandomIDLength)
name := fmt.Sprintf("%s%s", testResourcePrefix, randomID)

req := &github.CustomProperty{
PropertyName: &name,
ValueType: github.PropertyValueType(valType),
AllowedValues: allowed,
}

prop, _, err := meta.v3client.Organizations.CreateOrUpdateCustomProperty(t.Context(), meta.name, name, req)
if err != nil {
t.Fatalf("failed to create test organization repository custom property: %v", err)
}

t.Cleanup(func() {
if _, err := meta.v3client.Organizations.RemoveCustomProperty(context.Background(), meta.name, name); err != nil {
if err, ok := errors.AsType[*github.ErrorResponse](err); ok && err.Response.StatusCode == 404 {
return
}
t.Logf("failed to delete test organization repository custom property %s: %v", name, err)
}
})

return prop
}

func mustCreateTestRepository(t *testing.T) *github.Repository {
t.Helper()

meta, err := getTestMeta()
if err != nil {
t.Fatalf("failed to get test meta: %v", err)
}

randomID := acctest.RandString(testRandomIDLength)
name := fmt.Sprintf("%s%s", testResourcePrefix, randomID)

req := &github.Repository{
Name: &name,
AutoInit: new(true),
}

repo, _, err := meta.v3client.Repositories.Create(t.Context(), meta.name, req)
if err != nil {
t.Fatalf("failed to create test repository: %v", err)
}

t.Cleanup(func() {
if _, err := meta.v3client.Repositories.Delete(context.Background(), meta.name, name); err != nil {
if err, ok := errors.AsType[*github.ErrorResponse](err); ok && err.Response.StatusCode == 404 {
return
}
t.Logf("failed to delete test repository %s: %v", name, err)
}
})

return repo
}
2 changes: 1 addition & 1 deletion github/data_source_github_app_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestAccGithubAppTokenDataSource(t *testing.T) {
})
defer ts.Close()

client := mustGitHubClient(t, ts.URL)
client := mustCreateTestGitHubClient(t, ts.URL)

meta := &Owner{
name: owner,
Expand Down
15 changes: 0 additions & 15 deletions github/helpers_test.go

This file was deleted.

Loading