Skip to content
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

feat!: Change AuditEntry.Org and AuditEntry.OrgID to json.RawMessage #3502

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion github/enterprise_audit_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package github

import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
Expand Down Expand Up @@ -60,7 +61,7 @@ func TestEnterpriseService_GetAuditLog(t *testing.T) {
Action: Ptr("workflows.completed_workflow_run"),
Actor: Ptr("testactor"),
CreatedAt: &Timestamp{timestamp},
Org: Ptr("o"),
Org: json.RawMessage(`"o"`),
AdditionalFields: map[string]interface{}{
"completed_at": "2021-03-07T00:35:08.000Z",
"conclusion": "success",
Expand Down
2 changes: 2 additions & 0 deletions github/gen-accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ var (
"ErrorResponse.GetResponse": true,
"RateLimitError.GetResponse": true,
"AbuseRateLimitError.GetResponse": true,
"AuditEntry.GetOrgID": true,
"AuditEntry.GetOrg": true,
}
// skipStructs lists structs to skip.
skipStructs = map[string]bool{
Expand Down
16 changes: 0 additions & 16 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 0 additions & 22 deletions github/github-accessors_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

102 changes: 84 additions & 18 deletions github/orgs_audit_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,24 @@ type ActorLocation struct {
// in AdditionalFields.
// For a list of actions see - https://docs.github.com/github/setting-up-and-managing-organizations-and-teams/reviewing-the-audit-log-for-your-organization#audit-log-actions
type AuditEntry struct {
Action *string `json:"action,omitempty"` // The name of the action that was performed, for example `user.login` or `repo.create`.
Actor *string `json:"actor,omitempty"` // The actor who performed the action.
ActorID *int64 `json:"actor_id,omitempty"`
ActorLocation *ActorLocation `json:"actor_location,omitempty"`
Business *string `json:"business,omitempty"`
BusinessID *int64 `json:"business_id,omitempty"`
CreatedAt *Timestamp `json:"created_at,omitempty"`
DocumentID *string `json:"_document_id,omitempty"`
ExternalIdentityNameID *string `json:"external_identity_nameid,omitempty"`
ExternalIdentityUsername *string `json:"external_identity_username,omitempty"`
HashedToken *string `json:"hashed_token,omitempty"`
Org *string `json:"org,omitempty"`
OrgID *int64 `json:"org_id,omitempty"`
Timestamp *Timestamp `json:"@timestamp,omitempty"` // The time the audit log event occurred, given as a [Unix timestamp](http://en.wikipedia.org/wiki/Unix_time).
TokenID *int64 `json:"token_id,omitempty"`
TokenScopes *string `json:"token_scopes,omitempty"`
User *string `json:"user,omitempty"` // The user that was affected by the action performed (if available).
UserID *int64 `json:"user_id,omitempty"`
Action *string `json:"action,omitempty"` // The name of the action that was performed, for example `user.login` or `repo.create`.
Actor *string `json:"actor,omitempty"` // The actor who performed the action.
ActorID *int64 `json:"actor_id,omitempty"`
ActorLocation *ActorLocation `json:"actor_location,omitempty"`
Business *string `json:"business,omitempty"`
BusinessID *int64 `json:"business_id,omitempty"`
CreatedAt *Timestamp `json:"created_at,omitempty"`
DocumentID *string `json:"_document_id,omitempty"`
ExternalIdentityNameID *string `json:"external_identity_nameid,omitempty"`
ExternalIdentityUsername *string `json:"external_identity_username,omitempty"`
HashedToken *string `json:"hashed_token,omitempty"`
Org json.RawMessage `json:"org,omitempty"`
OrgID json.RawMessage `json:"org_id,omitempty"`
Timestamp *Timestamp `json:"@timestamp,omitempty"` // The time the audit log event occurred, given as a [Unix timestamp](http://en.wikipedia.org/wiki/Unix_time).
TokenID *int64 `json:"token_id,omitempty"`
TokenScopes *string `json:"token_scopes,omitempty"`
User *string `json:"user,omitempty"` // The user that was affected by the action performed (if available).
UserID *int64 `json:"user_id,omitempty"`

// Some events types have a data field that contains additional information about the event.
Data map[string]interface{} `json:"data,omitempty"`
Expand All @@ -56,6 +56,72 @@ type AuditEntry struct {
AdditionalFields map[string]interface{} `json:"-"`
}

// GetOrg returns the Org field, as a string, 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
}

// GetOrgNames returns the Org field, as a slice of strings, if it's valid.
func (a *AuditEntry) GetOrgNames() (names []string, ok bool) {
if a == nil || a.Org == nil {
return nil, false
}
if err := json.Unmarshal([]byte(a.Org), &names); err != nil {
return nil, false
}

return names, true
}

// GetRawOrg returns the Org field as a json.RawMessage.
func (a *AuditEntry) GetRawOrg() json.RawMessage {
if a == nil || a.Org == nil {
return json.RawMessage{}
}

return a.Org
}

// GetOrgID returns the OrgID field, as an int64, 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
}

// GetOrgIDs returns the OrgID field, as a slice of int64, if it's valid.
func (a *AuditEntry) GetOrgIDs() (orgIDs []int64, ok bool) {
if a == nil || a.OrgID == nil {
return nil, false
}
if err := json.Unmarshal([]byte(a.OrgID), &orgIDs); err != nil {
return nil, false
}

return orgIDs, true
}

// GetRawOrgID returns the OrgID field as a json.RawMessage.
func (a *AuditEntry) GetRawOrgID() json.RawMessage {
if a == nil || a.OrgID == nil {
return json.RawMessage{}
}

return a.OrgID
}

func (a *AuditEntry) UnmarshalJSON(data []byte) error {
type entryAlias AuditEntry
var v entryAlias
Expand Down
Loading
Loading