nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: operator-framework/operator-sdk
Pull request #4971 (2021-06-08 20:20:18)

[v1.8.x] fix(helm-operator): do not send empty patch requests to kube apiserver
This is an automated cherry-pick of #4957

/assign estroz


Patch:
diff --git a/changelog/fragments/empty-patch-requests.yaml b/changelog/fragments/empty-patch-requests.yaml
new file mode 100644
index 00000000..d48a1fd8
--- /dev/null
+++ b/changelog/fragments/empty-patch-requests.yaml
@@ -0,0 +1,4 @@
+entries:
+  - description: >
+      For helm-based operators, empty patch requests generated from the 3-way merge are filtered to avoid unnecessary requests to the kube apiserver.
+    kind: bugfix
diff --git a/internal/helm/release/manager.go b/internal/helm/release/manager.go
index 9489d377..7ee19a0b 100644
--- a/internal/helm/release/manager.go
+++ b/internal/helm/release/manager.go
@@ -322,7 +322,15 @@ func createPatch(existing runtime.Object, expected *resource.Info) ([]byte, apit
 	}
 
 	patch, err := strategicpatch.CreateThreeWayMergePatch(expectedJSON, expectedJSON, existingJSON, patchMeta, true)
-	return patch, apitypes.StrategicMergePatchType, err
+	if err != nil {
+		return nil, apitypes.StrategicMergePatchType, err
+	}
+	// An empty patch could be in the form of "{}" which represents an empty map out of the 3-way merge;
+	// filter them out here too to avoid sending the apiserver empty patch requests.
+	if len(patch) == 0 || bytes.Equal(patch, []byte("{}")) {
+		return nil, apitypes.StrategicMergePatchType, nil
+	}
+	return patch, apitypes.StrategicMergePatchType, nil
 }
 
 func createJSONMergePatch(existingJSON, expectedJSON []byte) ([]byte, error) {


Test patch:
diff --git a/internal/helm/release/manager_test.go b/internal/helm/release/manager_test.go
index 77ccca43..f930374e 100644
--- a/internal/helm/release/manager_test.go
+++ b/internal/helm/release/manager_test.go
@@ -164,7 +164,7 @@ func TestManagerGenerateStrategicMergePatch(t *testing.T) {
 			o2: newTestDeployment([]v1.Container{
 				{Name: "test1", LivenessProbe: nil},
 			}),
-			patch:     `{}`,
+			patch:     ``,
 			patchType: apitypes.StrategicMergePatchType,
 		},
 		{
@@ -197,7 +197,7 @@ func TestManagerGenerateStrategicMergePatch(t *testing.T) {
 				},
 				Spec: appsv1.DeploymentSpec{},
 			},
-			patch:     `{}`,
+			patch:     ``,
 			patchType: apitypes.StrategicMergePatchType,
 		},
 	}
Repository: operator-framework/operator-sdk
Pull request #4972 (2021-06-08 20:20:54)

[v1.7.x] fix(helm-operator): do not send empty patch requests to kube apiserver
This is an automated cherry-pick of #4957

/assign estroz


Patch:
diff --git a/changelog/fragments/empty-patch-requests.yaml b/changelog/fragments/empty-patch-requests.yaml
new file mode 100644
index 00000000..d48a1fd8
--- /dev/null
+++ b/changelog/fragments/empty-patch-requests.yaml
@@ -0,0 +1,4 @@
+entries:
+  - description: >
+      For helm-based operators, empty patch requests generated from the 3-way merge are filtered to avoid unnecessary requests to the kube apiserver.
+    kind: bugfix
diff --git a/internal/helm/release/manager.go b/internal/helm/release/manager.go
index 2d494f42..07641ccc 100644
--- a/internal/helm/release/manager.go
+++ b/internal/helm/release/manager.go
@@ -322,7 +322,15 @@ func createPatch(existing runtime.Object, expected *resource.Info) ([]byte, apit
 	}
 
 	patch, err := strategicpatch.CreateThreeWayMergePatch(expectedJSON, expectedJSON, existingJSON, patchMeta, true)
-	return patch, apitypes.StrategicMergePatchType, err
+	if err != nil {
+		return nil, apitypes.StrategicMergePatchType, err
+	}
+	// An empty patch could be in the form of "{}" which represents an empty map out of the 3-way merge;
+	// filter them out here too to avoid sending the apiserver empty patch requests.
+	if len(patch) == 0 || bytes.Equal(patch, []byte("{}")) {
+		return nil, apitypes.StrategicMergePatchType, nil
+	}
+	return patch, apitypes.StrategicMergePatchType, nil
 }
 
 func createJSONMergePatch(existingJSON, expectedJSON []byte) ([]byte, error) {


Test patch:
diff --git a/internal/helm/release/manager_test.go b/internal/helm/release/manager_test.go
index a7105dd7..466bb693 100644
--- a/internal/helm/release/manager_test.go
+++ b/internal/helm/release/manager_test.go
@@ -163,7 +163,7 @@ func TestManagerGenerateStrategicMergePatch(t *testing.T) {
 			o2: newTestDeployment([]v1.Container{
 				{Name: "test1", LivenessProbe: nil},
 			}),
-			patch:     `{}`,
+			patch:     ``,
 			patchType: apitypes.StrategicMergePatchType,
 		},
 		{
@@ -196,7 +196,7 @@ func TestManagerGenerateStrategicMergePatch(t *testing.T) {
 				},
 				Spec: appsv1.DeploymentSpec{},
 			},
-			patch:     `{}`,
+			patch:     ``,
 			patchType: apitypes.StrategicMergePatchType,
 		},
 	}
Repository: cloudflare/cloudflare-go
Pull request #1554 (2024-03-13 14:56:11)

feat: OpenAPI spec update via Stainless API


Patch:
diff --git a/internal/requestconfig/requestconfig.go b/internal/requestconfig/requestconfig.go
index 7f5fc8730..f94660917 100644
--- a/internal/requestconfig/requestconfig.go
+++ b/internal/requestconfig/requestconfig.go
@@ -23,6 +23,12 @@ import (
 	"github.com/cloudflare/cloudflare-go/v2/internal/apiquery"
 )
 
+func getDefaultHeaders() map[string]string {
+	return map[string]string{
+		"User-Agent": fmt.Sprintf("Cloudflare/Go %s", internal.PackageVersion),
+	}
+}
+
 func getNormalizedOS() string {
 	switch runtime.GOOS {
 	case "ios":
@@ -118,6 +124,9 @@ func NewRequestConfig(ctx context.Context, method string, u string, body interfa
 	}
 
 	req.Header.Set("Accept", "application/json")
+	for k, v := range getDefaultHeaders() {
+		req.Header.Add(k, v)
+	}
 
 	for k, v := range getPlatformProperties() {
 		req.Header.Add(k, v)


Test patch:
diff --git a/client_test.go b/client_test.go
index f541f1974..f1d4c0fe6 100644
--- a/client_test.go
+++ b/client_test.go
@@ -4,11 +4,13 @@ package cloudflare_test
 
 import (
 	"context"
+	"fmt"
 	"net/http"
 	"testing"
 	"time"
 
 	"github.com/cloudflare/cloudflare-go/v2"
+	"github.com/cloudflare/cloudflare-go/v2/internal"
 	"github.com/cloudflare/cloudflare-go/v2/option"
 	"github.com/cloudflare/cloudflare-go/v2/zones"
 )
@@ -21,6 +23,32 @@ func (t *closureTransport) RoundTrip(req *http.Request) (*http.Response, error)
 	return t.fn(req)
 }
 
+func TestUserAgentHeader(t *testing.T) {
+	var userAgent string
+	client := cloudflare.NewClient(
+		option.WithHTTPClient(&http.Client{
+			Transport: &closureTransport{
+				fn: func(req *http.Request) (*http.Response, error) {
+					userAgent = req.Header.Get("User-Agent")
+					return &http.Response{
+						StatusCode: http.StatusOK,
+					}, nil
+				},
+			},
+		}),
+	)
+	client.Zones.New(context.Background(), zones.ZoneNewParams{
+		Account: cloudflare.F(zones.ZoneNewParamsAccount{
+			ID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
+		}),
+		Name: cloudflare.F("example.com"),
+		Type: cloudflare.F(zones.ZoneNewParamsTypeFull),
+	})
+	if userAgent != fmt.Sprintf("Cloudflare/Go %s", internal.PackageVersion) {
+		t.Errorf("Expected User-Agent to be correct, but got: %#v", userAgent)
+	}
+}
+
 func TestRetryAfter(t *testing.T) {
 	attempts := 0
 	client := cloudflare.NewClient(