Repository: cloudflare/cloudflare-go
Pull request #1065 (2022-08-26 21:26:37)

Updates Pages Projects with more stuff
## Description

Adds field, `production_branch`, to pages project and deployments.

## Has your change been tested?

Updated unit tests and they pass

## Types of changes

What sort of change does your code introduce/modify?

- [ ] Bug fix (non-breaking change which fixes an issue)
- [X] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Checklist:

- [ ] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [X] I have added tests to cover my changes.
- [X] All new and existing tests passed.
- [ ] This change is using publicly documented (api.cloudflare.com or developers.cloudflare.com) and stable APIs. soon:tm:



Patch:
diff --git a/.changelog/1065.txt b/.changelog/1065.txt
new file mode 100644
index 000000000..9dfbfcc4f
--- /dev/null
+++ b/.changelog/1065.txt
@@ -0,0 +1,9 @@
+```release-note:enhancement
+pages_project: Add `production_branch` field
+```
+```release-note:enhancement
+pages_project: Add `kv_namespaces`, `durable_object_namespaces`, `r2_buckets`, and `d1_databases` bindings to deployment config
+```
+```release-note:enhancement
+pages_project: Add `preview_deployment_setting`, `preview_branch_includes`, and `preview_branch_excludes` to source config
+```
\ No newline at end of file
diff --git a/pages_project.go b/pages_project.go
index 61fec3b7f..753c1eb8d 100644
--- a/pages_project.go
+++ b/pages_project.go
@@ -8,6 +8,14 @@ import (
 	"time"
 )
 
+type PagesPreviewDeploymentSetting string
+
+const (
+	PagesPreviewAllBranches    PagesPreviewDeploymentSetting = "all"
+	PagesPreviewNoBranches     PagesPreviewDeploymentSetting = "none"
+	PagesPreviewCustomBranches PagesPreviewDeploymentSetting = "custom"
+)
+
 // PagesProject represents a Pages project.
 type PagesProject struct {
 	Name                string                        `json:"name,omitempty"`
@@ -15,11 +23,12 @@ type PagesProject struct {
 	CreatedOn           *time.Time                    `json:"created_on"`
 	SubDomain           string                        `json:"subdomain"`
 	Domains             []string                      `json:"domains,omitempty"`
-	Source              PagesProjectSource            `json:"source"`
+	Source              *PagesProjectSource           `json:"source,omitempty"`
 	BuildConfig         PagesProjectBuildConfig       `json:"build_config"`
 	DeploymentConfigs   PagesProjectDeploymentConfigs `json:"deployment_configs"`
 	LatestDeployment    PagesProjectDeployment        `json:"latest_deployment"`
 	CanonicalDeployment PagesProjectDeployment        `json:"canonical_deployment"`
+	ProductionBranch    string                        `json:"production_branch,omitempty"`
 }
 
 // PagesProjectSource represents the configuration of a Pages project source.
@@ -30,11 +39,15 @@ type PagesProjectSource struct {
 
 // PagesProjectSourceConfig represents the properties use to configure a Pages project source.
 type PagesProjectSourceConfig struct {
-	Owner              string `json:"owner"`
-	RepoName           string `json:"repo_name"`
-	ProductionBranch   string `json:"production_branch"`
-	PRCommentsEnabled  bool   `json:"pr_comments_enabled"`
-	DeploymentsEnabled bool   `json:"deployments_enabled"`
+	Owner                        string                        `json:"owner"`
+	RepoName                     string                        `json:"repo_name"`
+	ProductionBranch             string                        `json:"production_branch"`
+	PRCommentsEnabled            bool                          `json:"pr_comments_enabled"`
+	DeploymentsEnabled           bool                          `json:"deployments_enabled"`
+	ProductionDeploymentsEnabled bool                          `json:"production_deployments_enabled"`
+	PreviewDeploymentSetting     PagesPreviewDeploymentSetting `json:"preview_deployment_setting"`
+	PreviewBranchIncludes        []string                      `json:"preview_branch_includes"`
+	PreviewBranchExcludes        []string                      `json:"preview_branch_excludes"`
 }
 
 // PagesProjectBuildConfig represents the configuration of a Pages project build process.
@@ -57,6 +70,10 @@ type PagesProjectDeploymentConfigEnvironment struct {
 	EnvVars            map[string]PagesProjectDeploymentVar `json:"env_vars"`
 	CompatibilityDate  string                               `json:"compatibility_date,omitempty"`
 	CompatibilityFlags []string                             `json:"compatibility_flags,omitempty"`
+	KvNamespaces       NamespaceBindingMap                  `json:"kv_namespaces,omitempty"`
+	DoNamespaces       NamespaceBindingMap                  `json:"durable_object_namespaces,omitempty"`
+	D1Databases        D1BindingMap                         `json:"d1_databases,omitempty"`
+	R2Bindings         R2BindingMap                         `json:"r2_buckets,omitempty"`
 }
 
 // PagesProjectDeploymentVar represents a deployment environment variable.
@@ -83,6 +100,7 @@ type PagesProjectDeployment struct {
 	Source             PagesProjectSource            `json:"source"`
 	CompatibilityDate  string                        `json:"compatibility_date,omitempty"`
 	CompatibilityFlags []string                      `json:"compatibility_flags,omitempty"`
+	ProductionBranch   string                        `json:"production_branch,omitempty"`
 }
 
 // PagesProjectDeploymentStage represents an individual stage in a Pages project deployment.
@@ -117,6 +135,24 @@ type pagesProjectListResponse struct {
 	ResultInfo `json:"result_info"`
 }
 
+type NamespaceBindingMap map[string]*NamespaceBindingValue
+
+type NamespaceBindingValue struct {
+	Value string `json:"namespace_id"`
+}
+
+type R2BindingMap map[string]*R2BindingValue
+
+type R2BindingValue struct {
+	Name string `json:"name"`
+}
+
+type D1BindingMap map[string]*D1Binding
+
+type D1Binding struct {
+	ID string `json:"id"`
+}
+
 // ListPagesProjects returns all Pages projects for an account.
 //
 // API reference: https://api.cloudflare.com/#pages-project-get-projects


Test patch:
diff --git a/pages_project_test.go b/pages_project_test.go
index 99a9ffd23..0dffd3c30 100644
--- a/pages_project_test.go
+++ b/pages_project_test.go
@@ -21,6 +21,7 @@ const (
           "testdomain.com",
           "testdomain.org"
         ],
+		"production_branch": "main",
         "source": {
           "type": "github",
           "config": {
@@ -28,7 +29,18 @@ const (
             "repo_name": "pages-test",
             "production_branch": "main",
             "pr_comments_enabled": true,
-            "deployments_enabled": true
+            "deployments_enabled": true,
+			"preview_deployment_setting": "custom",
+			"preview_branch_includes": [
+				"release/*",
+				"production",
+				"main"
+			],
+			"preview_branch_excludes": [
+				"dependabot/*",
+				"dev",
+				"*/ignore"
+			]
           }
         },
         "build_config": {
@@ -60,6 +72,26 @@ const (
                 "value": "production"
               }
             },
+			"d1_databases": {
+				"D1_BINDING": { 
+					"id": "a94509c6-0757-43f3-b053-474b0ab10935" 
+				}
+			},
+			"kv_namespaces": {
+				"KV_BINDING": {
+				  "namespace_id": "5eb63bbbe01eeed093cb22bb8f5acdc3"
+				}
+			},
+		  	"durable_object_namespaces": {
+				"DO_BINDING": {
+			  		"namespace_id": "5eb63bbbe01eeed093cb22bb8f5acdc3"
+				}
+		  	},
+			"r2_buckets": {
+				"R2_BINDING": {
+					"name": "some-bucket"
+				}
+			},
 			"compatibility_date": "2022-08-15",
 			"compatibility_flags": ["production_flag"]
           }
@@ -70,6 +102,7 @@ const (
           "project_id": "5a321fc7-3162-7d36-adce-1213996a7",
           "project_name": "pages-test",
           "environment": "preview",
+		  "production_branch": "main",
           "url": "https://c35216d1.pages-test.pages.dev",
           "created_on": "2021-03-09T00:55:03.923456Z",
           "modified_on": "2021-03-09T00:58:59.045655Z",
@@ -128,7 +161,18 @@ const (
               "repo_name": "pages-test",
               "production_branch": "main",
               "pr_comments_enabled": true,
-              "deployments_enabled": true
+              "deployments_enabled": true,
+			  "preview_deployment_setting": "custom",
+			  "preview_branch_includes": [
+				"release/*",
+				"production",
+				"main"
+			  ],
+			  "preview_branch_excludes": [
+				"dependabot/*",
+				"dev",
+				"*/ignore"
+			  ]
             }
           }
         },
@@ -141,6 +185,7 @@ const (
           "url": "https://c35216d1.pages-test.pages.dev",
           "created_on": "2021-03-09T00:55:03.923456Z",
           "modified_on": "2021-03-09T00:58:59.045655Z",
+		  "production_branch": "main",
           "aliases": [
             "https://branchname.pages-test.pages.dev"
           ],
@@ -196,7 +241,18 @@ const (
               "repo_name": "pages-test",
               "production_branch": "main",
               "pr_comments_enabled": true,
-              "deployments_enabled": true
+              "deployments_enabled": true,
+			  "preview_deployment_setting": "custom",
+			  "preview_branch_includes": [
+				"release/*",
+				"production",
+				"main"
+			  ],
+			  "preview_branch_excludes": [
+				"dependabot/*",
+				"dev",
+				"*/ignore"
+			  ]
             }
           }
         }
@@ -217,9 +273,10 @@ var (
 		BuildConfig:         *expectedPagesProjectBuildConfig,
 		CreatedOn:           &pagesProjectCreatedOn,
 		DeploymentConfigs:   *expectedPagesProjectDeploymentConfigs,
-		Source:              *expectedPagesProjectSource,
+		Source:              expectedPagesProjectSource,
 		ID:                  "5a321fc7-3162-7d36-adce-1213996a7",
 		LatestDeployment:    *expectedPagesProjectDeployment,
+		ProductionBranch:    "main",
 	}
 
 	deploymentCreatedOn, _  = time.Parse(time.RFC3339, "2021-03-09T00:55:03.923456Z")
@@ -252,6 +309,7 @@ var (
 		Stages:             expectedStages,
 		BuildConfig:        *expectedPagesProjectBuildConfig,
 		Source:             *expectedPagesProjectSource,
+		ProductionBranch:   "main",
 	}
 
 	latestDeploymentStageStartedOn, _ = time.Parse(time.RFC3339, "2021-03-09T00:55:03.923456Z")
@@ -328,6 +386,18 @@ var (
 		},
 		CompatibilityDate:  "2022-08-15",
 		CompatibilityFlags: []string{"production_flag"},
+		KvNamespaces: NamespaceBindingMap{
+			"KV_BINDING": &NamespaceBindingValue{Value: "5eb63bbbe01eeed093cb22bb8f5acdc3"},
+		},
+		D1Databases: D1BindingMap{
+			"D1_BINDING": &D1Binding{ID: "a94509c6-0757-43f3-b053-474b0ab10935"},
+		},
+		DoNamespaces: NamespaceBindingMap{
+			"DO_BINDING": &NamespaceBindingValue{Value: "5eb63bbbe01eeed093cb22bb8f5acdc3"},
+		},
+		R2Bindings: R2BindingMap{
+			"R2_BINDING": &R2BindingValue{Name: "some-bucket"},
+		},
 	}
 
 	expectedPagesProjectSource = &PagesProjectSource{
@@ -336,11 +406,14 @@ var (
 	}
 
 	expectedPagesProjectSourceConfig = &PagesProjectSourceConfig{
-		Owner:              "cloudflare",
-		RepoName:           "pages-test",
-		ProductionBranch:   "main",
-		PRCommentsEnabled:  true,
-		DeploymentsEnabled: true,
+		Owner:                    "cloudflare",
+		RepoName:                 "pages-test",
+		ProductionBranch:         "main",
+		PRCommentsEnabled:        true,
+		DeploymentsEnabled:       true,
+		PreviewDeploymentSetting: PagesPreviewCustomBranches,
+		PreviewBranchIncludes:    []string{"release/*", "production", "main"},
+		PreviewBranchExcludes:    []string{"dependabot/*", "dev", "*/ignore"},
 	}
 )