Repository: google/go-github
Pull request #2620 (2023-01-09 05:35:58)

Omit OpenID Connect customization template claims when none are set
There's a bug in the OpenID Connect customization template in #2615  where `included_claim_keys` is set to `nil` and sent to the GitHub API if it is not set. This results in the API returning a `422 Unprocessable Entity` error.

This PR resolves this by setting the JSON annotation of `IncludeClaimKeys` in `OIDCSubjectClaimCustomTemplate` to `omitempty` and adds a test to verify this.

codecov[bot]: # [Codecov](https://codecov.io/gh/google/go-github/pull/2620?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) Report
> Merging [#2620](https://codecov.io/gh/google/go-github/pull/2620?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (8d022b3) into [master](https://codecov.io/gh/google/go-github/commit/8ec1e49a02f967da3b7c5d4d34571adf5ec7ceaa?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (8ec1e49) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

```diff
@@           Coverage Diff           @@
##           master    #2620   +/-   ##
=======================================
  Coverage   98.01%   98.02%           
=======================================
  Files         128      129    +1     
  Lines       11055    11084   +29     
=======================================
+ Hits        10836    10865   +29     
  Misses        150      150           
  Partials       69       69           
```


| [Impacted Files](https://codecov.io/gh/google/go-github/pull/2620?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) | Coverage Δ | |
|---|---|---|
| [github/actions\_oidc.go](https://codecov.io/gh/google/go-github/pull/2620?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google#diff-Z2l0aHViL2FjdGlvbnNfb2lkYy5nbw==) | `100.00% <100.00%> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google)

F21: Thanks for the quick review @gmlewis ! Pushed another commit to remove the blank line.
gmlewis: Thank you, @valbeat !
Merging.

Patch:
diff --git a/github/actions_oidc.go b/github/actions_oidc.go
index 5132ad3a..b7f2d26a 100644
--- a/github/actions_oidc.go
+++ b/github/actions_oidc.go
@@ -13,7 +13,7 @@ import (
 // OIDCSubjectClaimCustomTemplate represents an OIDC subject claim customization template.
 type OIDCSubjectClaimCustomTemplate struct {
 	UseDefault       *bool    `json:"use_default,omitempty"`
-	IncludeClaimKeys []string `json:"include_claim_keys"`
+	IncludeClaimKeys []string `json:"include_claim_keys,omitempty"`
 }
 
 // GetOrgOIDCSubjectClaimCustomTemplate gets the subject claim customization template for an organization.


Test patch:
diff --git a/github/actions_oidc_test.go b/github/actions_oidc_test.go
index 99a9a5b3..c56dfef3 100644
--- a/github/actions_oidc_test.go
+++ b/github/actions_oidc_test.go
@@ -148,3 +148,34 @@ func TestActionsService_SetRepoOIDCSubjectClaimCustomTemplate(t *testing.T) {
 		return client.Actions.SetRepoOIDCSubjectClaimCustomTemplate(ctx, "o", "r", input)
 	})
 }
+
+func TestActionService_SetRepoOIDCSubjectClaimCustomTemplateToDefault(t *testing.T) {
+	client, mux, _, teardown := setup()
+	defer teardown()
+
+	mux.HandleFunc("/repos/o/r/actions/oidc/customization/sub", func(w http.ResponseWriter, r *http.Request) {
+		testMethod(t, r, "PUT")
+		testHeader(t, r, "Content-Type", "application/json")
+		testBody(t, r, `{"use_default":true}`+"\n")
+		w.WriteHeader(http.StatusCreated)
+	})
+
+	input := &OIDCSubjectClaimCustomTemplate{
+		UseDefault: Bool(true),
+	}
+	ctx := context.Background()
+	_, err := client.Actions.SetRepoOIDCSubjectClaimCustomTemplate(ctx, "o", "r", input)
+	if err != nil {
+		t.Errorf("Actions.SetRepoOIDCSubjectClaimCustomTemplate returned error: %v", err)
+	}
+
+	const methodName = "SetRepoOIDCSubjectClaimCustomTemplate"
+	testBadOptions(t, methodName, func() (err error) {
+		_, err = client.Actions.SetRepoOIDCSubjectClaimCustomTemplate(ctx, "\n", "\n", input)
+		return err
+	})
+
+	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
+		return client.Actions.SetRepoOIDCSubjectClaimCustomTemplate(ctx, "o", "r", input)
+	})
+}