|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/augmentable-dev/vtab" |
| 10 | + "github.com/rs/zerolog" |
| 11 | + "github.com/shurcooL/githubv4" |
| 12 | + "go.riyazali.net/sqlite" |
| 13 | +) |
| 14 | + |
| 15 | +type fetchOrgAuditLogResults struct { |
| 16 | + AuditLogs []*auditLogEntry |
| 17 | + HasNextPage bool |
| 18 | + EndCursor *githubv4.String |
| 19 | +} |
| 20 | + |
| 21 | +type auditLogEntry struct { |
| 22 | + Typename string `graphql:"__typename"` |
| 23 | + NodeFragment struct { |
| 24 | + Id string |
| 25 | + } `graphql:"... on Node"` |
| 26 | + Entry auditLogEntryContents `graphql:"... on AuditEntry"` |
| 27 | +} |
| 28 | + |
| 29 | +type auditLogEntryContents struct { |
| 30 | + Action string |
| 31 | + Actor struct { |
| 32 | + Type string `graphql:"__typename"` |
| 33 | + } |
| 34 | + ActorLogin string |
| 35 | + ActorIp string |
| 36 | + CreatedAt githubv4.DateTime |
| 37 | + OperationType string |
| 38 | + UserLogin string |
| 39 | +} |
| 40 | + |
| 41 | +func (i *iterOrgAuditLogs) fetchOrgAuditRepos(ctx context.Context, startCursor *githubv4.String) (*fetchOrgAuditLogResults, error) { |
| 42 | + var reposQuery struct { |
| 43 | + Organization struct { |
| 44 | + Login string |
| 45 | + AuditLog struct { |
| 46 | + TotalCount int |
| 47 | + Nodes []*auditLogEntry |
| 48 | + PageInfo struct { |
| 49 | + EndCursor githubv4.String |
| 50 | + HasNextPage bool |
| 51 | + } |
| 52 | + } `graphql:"auditLog(first: $perPage, after: $auditLogCursor, orderBy: $auditLogOrder)"` |
| 53 | + } `graphql:"organization(login: $login)"` |
| 54 | + } |
| 55 | + variables := map[string]interface{}{ |
| 56 | + "login": githubv4.String(i.login), |
| 57 | + "perPage": githubv4.Int(i.PerPage), |
| 58 | + "auditLogCursor": startCursor, |
| 59 | + "auditLogOrder": i.auditOrder, |
| 60 | + } |
| 61 | + |
| 62 | + err := i.Client().Query(ctx, &reposQuery, variables) |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + |
| 67 | + return &fetchOrgAuditLogResults{ |
| 68 | + reposQuery.Organization.AuditLog.Nodes, |
| 69 | + reposQuery.Organization.AuditLog.PageInfo.HasNextPage, |
| 70 | + &reposQuery.Organization.AuditLog.PageInfo.EndCursor, |
| 71 | + }, nil |
| 72 | + |
| 73 | +} |
| 74 | + |
| 75 | +type iterOrgAuditLogs struct { |
| 76 | + *Options |
| 77 | + login string |
| 78 | + affiliations string |
| 79 | + current int |
| 80 | + results *fetchOrgAuditLogResults |
| 81 | + auditOrder *githubv4.AuditLogOrder |
| 82 | +} |
| 83 | + |
| 84 | +func (i *iterOrgAuditLogs) logger() *zerolog.Logger { |
| 85 | + logger := i.Logger.With().Int("per-page", i.PerPage).Str("login", i.login).Logger() |
| 86 | + if i.auditOrder != nil { |
| 87 | + logger = logger.With().Str("order_by", string(*i.auditOrder.Field)).Str("order_dir", string(*i.auditOrder.Direction)).Logger() |
| 88 | + } |
| 89 | + return &logger |
| 90 | +} |
| 91 | + |
| 92 | +func (i *iterOrgAuditLogs) Column(ctx vtab.Context, c int) error { |
| 93 | + current := i.results.AuditLogs[i.current] |
| 94 | + |
| 95 | + switch orgAuditCols[c].Name { |
| 96 | + case "login": |
| 97 | + ctx.ResultText(i.login) |
| 98 | + case "id": |
| 99 | + ctx.ResultText(current.NodeFragment.Id) |
| 100 | + case "entry_type": |
| 101 | + ctx.ResultText(current.Typename) |
| 102 | + case "action": |
| 103 | + ctx.ResultText(current.Entry.Action) |
| 104 | + case "actor_type": |
| 105 | + ctx.ResultText(current.Entry.Actor.Type) |
| 106 | + case "actor_login": |
| 107 | + ctx.ResultText(current.Entry.ActorLogin) |
| 108 | + case "actor_ip": |
| 109 | + ctx.ResultText(current.Entry.ActorIp) |
| 110 | + case "created_at": |
| 111 | + t := current.Entry.CreatedAt |
| 112 | + if t.IsZero() { |
| 113 | + ctx.ResultNull() |
| 114 | + } else { |
| 115 | + ctx.ResultText(t.Format(time.RFC3339Nano)) |
| 116 | + } |
| 117 | + case "operation_type": |
| 118 | + ctx.ResultText(current.Entry.OperationType) |
| 119 | + case "user_login": |
| 120 | + ctx.ResultText(current.Entry.UserLogin) |
| 121 | + } |
| 122 | + return nil |
| 123 | +} |
| 124 | + |
| 125 | +func (i *iterOrgAuditLogs) Next() (vtab.Row, error) { |
| 126 | + i.current += 1 |
| 127 | + |
| 128 | + if i.results == nil || i.current >= len(i.results.AuditLogs) { |
| 129 | + if i.results == nil || i.results.HasNextPage { |
| 130 | + err := i.RateLimiter.Wait(context.Background()) |
| 131 | + if err != nil { |
| 132 | + return nil, err |
| 133 | + } |
| 134 | + |
| 135 | + var cursor *githubv4.String |
| 136 | + if i.results != nil { |
| 137 | + cursor = i.results.EndCursor |
| 138 | + } |
| 139 | + |
| 140 | + l := i.logger().With().Interface("cursor", cursor).Logger() |
| 141 | + l.Info().Msgf("fetching page of org audit entries for %s", i.login) |
| 142 | + results, err := i.fetchOrgAuditRepos(context.Background(), cursor) |
| 143 | + if err != nil { |
| 144 | + return nil, err |
| 145 | + } |
| 146 | + |
| 147 | + i.results = results |
| 148 | + i.current = 0 |
| 149 | + |
| 150 | + } else { |
| 151 | + return nil, io.EOF |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return i, nil |
| 156 | +} |
| 157 | + |
| 158 | +var orgAuditCols = []vtab.Column{ |
| 159 | + {Name: "login", Type: "TEXT", Hidden: true, Filters: []*vtab.ColumnFilter{{Op: sqlite.INDEX_CONSTRAINT_EQ, OmitCheck: true}}}, |
| 160 | + {Name: "id", Type: "TEXT"}, |
| 161 | + {Name: "entry_type", Type: "TEXT"}, |
| 162 | + {Name: "action", Type: "TEXT"}, |
| 163 | + {Name: "actor_type", Type: "TEXT"}, |
| 164 | + {Name: "actor_login", Type: "TEXT"}, |
| 165 | + {Name: "actor_ip", Type: "TEXT"}, |
| 166 | + {Name: "created_at", Type: "DATETIME", OrderBy: vtab.ASC | vtab.DESC}, |
| 167 | + {Name: "operation_type", Type: "TEXT"}, |
| 168 | + {Name: "user_login", Type: "TEXT"}, |
| 169 | +} |
| 170 | + |
| 171 | +func NewOrgAuditModule(opts *Options) sqlite.Module { |
| 172 | + return vtab.NewTableFunc("github_audit_repos", orgAuditCols, func(constraints []*vtab.Constraint, orders []*sqlite.OrderBy) (vtab.Iterator, error) { |
| 173 | + var login, affiliations string |
| 174 | + for _, constraint := range constraints { |
| 175 | + if constraint.Op == sqlite.INDEX_CONSTRAINT_EQ { |
| 176 | + switch constraint.ColIndex { |
| 177 | + case 0: |
| 178 | + login = constraint.Value.Text() |
| 179 | + |
| 180 | + case 1: |
| 181 | + affiliations = strings.ToUpper(constraint.Value.Text()) |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + var auditOrder *githubv4.AuditLogOrder |
| 187 | + // for now we can only support single field order bys |
| 188 | + if len(orders) == 1 { |
| 189 | + order := orders[0] |
| 190 | + switch orgAuditCols[order.ColumnIndex].Name { |
| 191 | + case "created_at": |
| 192 | + createdAt := githubv4.AuditLogOrderFieldCreatedAt |
| 193 | + dir := orderByToGitHubOrder(order.Desc) |
| 194 | + auditOrder = &githubv4.AuditLogOrder{ |
| 195 | + Field: &createdAt, |
| 196 | + } |
| 197 | + auditOrder.Direction = &dir |
| 198 | + } |
| 199 | + } |
| 200 | + iter := &iterOrgAuditLogs{opts, login, affiliations, -1, nil, auditOrder} |
| 201 | + iter.logger().Info().Msgf("starting GitHub audit_log iterator for %s", login) |
| 202 | + return iter, nil |
| 203 | + }, vtab.EarlyOrderByConstraintExit(true)) |
| 204 | +} |
0 commit comments