Skip to content

fix: handle array-typed org/org_id in AuditEntry JSON unmarshaling#4204

Open
nghiack7 wants to merge 1 commit intogoogle:masterfrom
nghiack7:fix/audit-entry-org-array-unmarshal
Open

fix: handle array-typed org/org_id in AuditEntry JSON unmarshaling#4204
nghiack7 wants to merge 1 commit intogoogle:masterfrom
nghiack7:fix/audit-entry-org-array-unmarshal

Conversation

@nghiack7
Copy link
Copy Markdown

@nghiack7 nghiack7 commented May 9, 2026

Problem

The GitHub Enterprise audit-log API sometimes returns org as a JSON array of strings and org_id as a JSON array of integers, deviating from the documented scalar types. This causes GetAuditLog / GetAuditLog (Enterprise) to return an unmarshal error:

json: cannot unmarshal array into Go struct field entryAlias.org of type string

Reported on v66 and v69. See #3488.

Root cause

AuditEntry.UnmarshalJSON delegates to json.Unmarshal via a type entryAlias AuditEntry alias. When the API returns "org": ["a", "b"] the decoder fails because Org is typed *string.

Fix

In UnmarshalJSON, capture org and org_id as json.RawMessage before the main decode pass, then normalise each:

  • string arrays are joined with ", " (preserves all org names)
  • int64 arrays use the first element (mirrors how callers would use it)
  • Scalar values pass through unchanged

Two unexported helpers (unmarshalStringOrStringArray, unmarshalInt64OrInt64Array) encapsulate the logic so the core unmarshal flow stays readable.

Tests

Added table-driven tests in orgs_audit_log_test.go:

test payload shape expected result
org_as_scalar_string "org":"myorg" "myorg"
org_as_single_element_array "org":["myorg"] "myorg"
org_as_multi_element_array "org":["org1","org2","org3"] "org1, org2, org3"
org_id_as_scalar "org_id":42 42
org_id_as_array "org_id":[42,43,44] 42 (first element)

All existing audit-log tests continue to pass.

GitHub's Enterprise audit-log API sometimes returns 'org' as a JSON
array of strings and 'org_id' as a JSON array of integers, deviating
from the documented scalar types. This caused an unmarshal error:

  json: cannot unmarshal array into Go struct field ... of type string

Fix UnmarshalJSON to capture 'org' and 'org_id' as json.RawMessage,
then decode each as scalar-or-array:
- string arrays are joined with ', '
- int64 arrays use the first element

Add two helpers (unmarshalStringOrStringArray, unmarshalInt64OrInt64Array)
and table-driven tests covering scalar, single-element array, and
multi-element array payloads.

Fixes google#3488
@google-cla
Copy link
Copy Markdown

google-cla Bot commented May 9, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 9, 2026

Codecov Report

❌ Patch coverage is 68.57143% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.67%. Comparing base (9f9ead3) to head (0820df2).

Files with missing lines Patch % Lines
github/orgs_audit_log.go 68.57% 5 Missing and 6 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4204      +/-   ##
==========================================
- Coverage   93.71%   93.67%   -0.05%     
==========================================
  Files         209      209              
  Lines       19772    19805      +33     
==========================================
+ Hits        18529    18552      +23     
- Misses       1046     1051       +5     
- Partials      197      202       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant