-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enterprise API responses are sometimes returning arrays of strings instead of a string #3488
Comments
Yes, I think we could, upon failure, see if the array could be turned into a comma-separated string and returned that way without breaking the API. The only danger might be if an enterprise name already had a comma in it, like "Glenn's Company, Inc." then the comma-separated list would not be good. Another alternative would be to create a breaking-API change and change that field from type Thoughts? Would you like to write a PR? |
@gmlewis I started looking into using There are some tests that would need modification as well -- I am not against this but I just wanted to make sure this all makes sense before continuing/creating a PR some sample changes to the accessor functions // GetOrg returns the Org field if it's non-nil, zero value otherwise.
func (a *AuditEntry) GetOrg() string {
var org *string
if a == nil || a.Org == nil {
return ""
}
json.Unmarshal([]byte(a.Org), org) // unmarsal the json.rawmessage to string
return *org
}
// GetOrgID returns the OrgID field if it's non-nil, zero value otherwise.
func (a *AuditEntry) GetOrgID() int64 {
var org_id *int64
if a == nil || a.OrgID == nil {
return 0
}
json.Unmarshal([]byte(a.OrgID), org_id) // unmarsal the json.rawmessage to int64
return *org_id
} |
Well, the first thing that strikes me is that I would not write the accessors that way. // GetOrg returns the Org field if it's valid.
func (a *AuditEntry) GetOrg() (org string, ok bool) {
if a == nil || a.Org == nil {
return "", false
}
if err := json.Unmarshal([]byte(a.Org), &org); err != nil {
return "", false
}
return org, true
}
// GetOrgID returns the OrgID field if it's valid.
func (a *AuditEntry) GetOrgID() (orgID int64, ok bool) {
if a == nil || a.OrgID == nil {
return 0, false
}
if err := json.Unmarshal([]byte(a.OrgID), &orgID); err != nil {
return 0, false
}
return orgID, true
} However, to not interfere with the existing auto-generated accessor mechanisms already established, we might need to consider other alternatives. I think we have a |
Hello @gmlewis I modified // skipStructMethods lists "struct.method" combos to skip.
skipStructMethods = map[string]bool{
"RepositoryContent.GetContent": true,
"Client.GetBaseURL": true,
"Client.GetUploadURL": true,
"ErrorResponse.GetResponse": true,
"RateLimitError.GetResponse": true,
"AbuseRateLimitError.GetResponse": true,
"AuditEntry.GetOrgID": true,
"AuditEntry.GetOrg": true,
} and ran I had to modify the non-generated tests in orgID, _ := json.Marshal(Ptr(int64(1)))
want := []*AuditEntry{
{
Timestamp: &Timestamp{timestamp},
DocumentID: Ptr("beeZYapIUe-wKg5-beadb33"),
Action: Ptr("workflows.completed_workflow_run"),
Actor: Ptr("testactor"),
ActorLocation: &ActorLocation{
CountryCode: Ptr("US"),
},
CreatedAt: &Timestamp{timestamp},
HashedToken: Ptr("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="),
Org: json.RawMessage(bytes.Join([][]byte{[]byte(`"o"`), []byte("")}, []byte(""))),
OrgID: orgID, |
It is really hard to do code reviews without context diffs. |
Context
go version go1.24.0 darwin/arm64
Github Enterprise REST API
go-github versions v66 and v69 tested with
The issue
While pulling logs from the GH enterprise REST API via
client.Enterprise.GetAuditLog(ctx, enterprise-name, &opts)
it seems that there are some issues with actual API response vs the Github provided response schema for/enterprises/{enterprise}/audit-log
The docs state org should be a string
https://docs.github.com/en/enterprise-cloud@latest/rest/enterprise-admin/audit-log?apiVersion=2022-11-28#get-the-audit-log-for-an-enterprise
However, while pulling audit logs from an enterprise account the org field may sometimes be an array of strings.
This is causing the following error when trying to unmarshal the response into a struct
json: cannot unmarshal array into Go struct field entryAlias.org of type string
This error is present in v66 and v69, I've tested and was able to replicate the error with both versions.
Is this something this package may be able to flex? I wonder if the array can be turned into a string with
strings.Join()
when an array is detected during the type switch in this function https://github.com/google/go-github/blob/master/github/github.go#L1024-L1045Specifically it is failing here
as it cannot decode the body properly.
The text was updated successfully, but these errors were encountered: