Repository: google/go-github
Pull request #2849 (2023-07-28 01:58:39)

Fix `old_name` field in `AuditEntry`
Much to my chagrin, the audit log view in the site manager does not follow the shape of the API response.

I previously raised a PR #2843 which was merged yesterday. Unfortunately, it does not work with the payload returned from the API.

My sincere apologies about this.

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/google/go-github/pull/2849?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) Report
> Merging [#2849](https://app.codecov.io/gh/google/go-github/pull/2849?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (30c6cb9) into [master](https://app.codecov.io/gh/google/go-github/commit/c8c34d01f65e98954a092333edf4ecdf3120c36e?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (c8c34d0) will **not change** coverage.
> The diff coverage is `n/a`.

```diff
@@           Coverage Diff           @@
##           master    #2849   +/-   ##
=======================================
  Coverage   98.06%   98.06%           
=======================================
  Files         138      138           
  Lines       12318    12318           
=======================================
  Hits        12080    12080           
  Misses        162      162           
  Partials       76       76           
```


| [Files Changed](https://app.codecov.io/gh/google/go-github/pull/2849?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) | Coverage Δ | |
|---|---|---|
| [github/orgs\_audit\_log.go](https://app.codecov.io/gh/google/go-github/pull/2849?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google#diff-Z2l0aHViL29yZ3NfYXVkaXRfbG9nLmdv) | `100.00% <ø> (ø)` | |


lmartinking: @gmlewis I have one more field to add, which I will do in a other PR
lmartinking: @gmlewis do you want the structs to mirror the shape of the API or would you prefer it flattened? Which was why I had it as a composite member.
gmlewis: > @gmlewis do you want the structs to mirror the shape of the API or would you prefer it flattened? Which was why I had it as a composite member.

We typically do _not_ use composite members in this repo.

Patch:
diff --git a/github/github-accessors.go b/github/github-accessors.go
index 942ec6a9..356487e0 100644
--- a/github/github-accessors.go
+++ b/github/github-accessors.go
@@ -1134,6 +1134,14 @@ func (a *AuditEntry) GetCreatedAt() Timestamp {
 	return *a.CreatedAt
 }
 
+// GetData returns the Data field.
+func (a *AuditEntry) GetData() *AuditEntryData {
+	if a == nil {
+		return nil
+	}
+	return a.Data
+}
+
 // GetDeployKeyFingerprint returns the DeployKeyFingerprint field if it's non-nil, zero value otherwise.
 func (a *AuditEntry) GetDeployKeyFingerprint() string {
 	if a == nil || a.DeployKeyFingerprint == nil {
@@ -1278,14 +1286,6 @@ func (a *AuditEntry) GetOAuthApplicationID() int64 {
 	return *a.OAuthApplicationID
 }
 
-// GetOldName returns the OldName field if it's non-nil, zero value otherwise.
-func (a *AuditEntry) GetOldName() string {
-	if a == nil || a.OldName == nil {
-		return ""
-	}
-	return *a.OldName
-}
-
 // GetOldPermission returns the OldPermission field if it's non-nil, zero value otherwise.
 func (a *AuditEntry) GetOldPermission() string {
 	if a == nil || a.OldPermission == nil {
@@ -1598,6 +1598,14 @@ func (a *AuditEntry) GetWorkflowRunID() int64 {
 	return *a.WorkflowRunID
 }
 
+// GetOldName returns the OldName field if it's non-nil, zero value otherwise.
+func (a *AuditEntryData) GetOldName() string {
+	if a == nil || a.OldName == nil {
+		return ""
+	}
+	return *a.OldName
+}
+
 // GetApp returns the App field.
 func (a *Authorization) GetApp() *AuthorizationApp {
 	if a == nil {
diff --git a/github/orgs_audit_log.go b/github/orgs_audit_log.go
index a8691053..d7de7712 100644
--- a/github/orgs_audit_log.go
+++ b/github/orgs_audit_log.go
@@ -78,7 +78,6 @@ type AuditEntry struct {
 	Message                *string                 `json:"message,omitempty"`
 	Name                   *string                 `json:"name,omitempty"`
 	OAuthApplicationID     *int64                  `json:"oauth_application_id,omitempty"`
-	OldName                *string                 `json:"old_name,omitempty"` // The previous name of the repository, for a name change
 	OldUser                *string                 `json:"old_user,omitempty"`
 	OldPermission          *string                 `json:"old_permission,omitempty"` // The permission level for membership changes, for example `admin` or `read`.
 	OpenSSHPublicKey       *string                 `json:"openssh_public_key,omitempty"`
@@ -122,6 +121,13 @@ type AuditEntry struct {
 	Visibility             *string                 `json:"visibility,omitempty"` // The repository visibility, for example `public` or `private`.
 	WorkflowID             *int64                  `json:"workflow_id,omitempty"`
 	WorkflowRunID          *int64                  `json:"workflow_run_id,omitempty"`
+
+	Data *AuditEntryData `json:"data,omitempty"`
+}
+
+// AuditEntryData represents additional information stuffed into a `data` field.
+type AuditEntryData struct {
+	OldName *string `json:"old_name,omitempty"` // The previous name of the repository, for a name change
 }
 
 // GetAuditLog gets the audit-log entries for an organization.


Test patch:
diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go
index 4eafaf25..40942da4 100644
--- a/github/github-accessors_test.go
+++ b/github/github-accessors_test.go
@@ -1334,6 +1334,13 @@ func TestAuditEntry_GetCreatedAt(tt *testing.T) {
 	a.GetCreatedAt()
 }
 
+func TestAuditEntry_GetData(tt *testing.T) {
+	a := &AuditEntry{}
+	a.GetData()
+	a = nil
+	a.GetData()
+}
+
 func TestAuditEntry_GetDeployKeyFingerprint(tt *testing.T) {
 	var zeroValue string
 	a := &AuditEntry{DeployKeyFingerprint: &zeroValue}
@@ -1514,16 +1521,6 @@ func TestAuditEntry_GetOAuthApplicationID(tt *testing.T) {
 	a.GetOAuthApplicationID()
 }
 
-func TestAuditEntry_GetOldName(tt *testing.T) {
-	var zeroValue string
-	a := &AuditEntry{OldName: &zeroValue}
-	a.GetOldName()
-	a = &AuditEntry{}
-	a.GetOldName()
-	a = nil
-	a.GetOldName()
-}
-
 func TestAuditEntry_GetOldPermission(tt *testing.T) {
 	var zeroValue string
 	a := &AuditEntry{OldPermission: &zeroValue}
@@ -1914,6 +1911,16 @@ func TestAuditEntry_GetWorkflowRunID(tt *testing.T) {
 	a.GetWorkflowRunID()
 }
 
+func TestAuditEntryData_GetOldName(tt *testing.T) {
+	var zeroValue string
+	a := &AuditEntryData{OldName: &zeroValue}
+	a.GetOldName()
+	a = &AuditEntryData{}
+	a.GetOldName()
+	a = nil
+	a.GetOldName()
+}
+
 func TestAuthorization_GetApp(tt *testing.T) {
 	a := &Authorization{}
 	a.GetApp()
diff --git a/github/orgs_audit_log_test.go b/github/orgs_audit_log_test.go
index a5873ee4..2995de50 100644
--- a/github/orgs_audit_log_test.go
+++ b/github/orgs_audit_log_test.go
@@ -256,7 +256,6 @@ func TestAuditEntry_Marshal(t *testing.T) {
 		LimitedAvailability:    Bool(false),
 		Message:                String("m"),
 		Name:                   String("n"),
-		OldName:                String("on"),
 		OldPermission:          String("op"),
 		OldUser:                String("ou"),
 		OpenSSHPublicKey:       String("osshpk"),
@@ -300,6 +299,9 @@ func TestAuditEntry_Marshal(t *testing.T) {
 		Visibility:            String("v"),
 		WorkflowID:            Int64(1),
 		WorkflowRunID:         Int64(1),
+		Data: &AuditEntryData{
+			OldName: String("on"),
+		},
 	}
 
 	want := `{
@@ -346,7 +348,6 @@ func TestAuditEntry_Marshal(t *testing.T) {
 		"limited_availability": false,
 		"message": "m",
 		"name": "n",
-                "old_name": "on",
 		"old_permission": "op",
 		"old_user": "ou",
 		"openssh_public_key": "osshpk",
@@ -393,7 +394,10 @@ func TestAuditEntry_Marshal(t *testing.T) {
 		"user_agent": "ua",
 		"visibility": "v",
 		"workflow_id": 1,
-		"workflow_run_id": 1
+		"workflow_run_id": 1,
+		"data": {
+			"old_name": "on"
+		}
 	}`
 
 	testJSONMarshal(t, u, want)