|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/google/go-github/v55/github" |
| 7 | + |
| 8 | + "github.com/turbot/steampipe-plugin-sdk/v6/grpc/proto" |
| 9 | + "github.com/turbot/steampipe-plugin-sdk/v6/plugin" |
| 10 | + "github.com/turbot/steampipe-plugin-sdk/v6/plugin/transform" |
| 11 | +) |
| 12 | + |
| 13 | +func tableGitHubActionsCache() *plugin.Table { |
| 14 | + return &plugin.Table{ |
| 15 | + Name: "github_actions_cache", |
| 16 | + Description: "Caches are a built-in storage mechanism to speed up jobs in a workflow by persisting data for re-use.", |
| 17 | + List: &plugin.ListConfig{ |
| 18 | + KeyColumns: []*plugin.KeyColumn{ |
| 19 | + {Name: "repository_full_name", Require: plugin.Required}, |
| 20 | + {Name: "key", Require: plugin.Optional}, |
| 21 | + {Name: "ref", Require: plugin.Optional}, |
| 22 | + }, |
| 23 | + ShouldIgnoreError: isNotFoundError([]string{"404"}), |
| 24 | + Hydrate: tableGitHubCacheList, |
| 25 | + }, |
| 26 | + Columns: commonColumns([]*plugin.Column{ |
| 27 | + // Top columns |
| 28 | + {Name: "repository_full_name", Type: proto.ColumnType_STRING, Transform: transform.FromQual("repository_full_name"), Description: "Full name of the repository that contains the cache."}, |
| 29 | + {Name: "id", Type: proto.ColumnType_INT, Description: "Unique ID of the cache."}, |
| 30 | + {Name: "key", Type: proto.ColumnType_STRING, Description: "Developer-defined string identifier of the cache."}, |
| 31 | + |
| 32 | + // Other columns |
| 33 | + {Name: "ref", Type: proto.ColumnType_STRING, Description: "The git reference of the cache."}, |
| 34 | + {Name: "version", Type: proto.ColumnType_STRING, Description: "Hash generated from combination of compression tool, runner OS, and path."}, |
| 35 | + {Name: "last_accessed_at", Type: proto.ColumnType_TIMESTAMP, Transform: transform.FromField("LastAccessedAt").Transform(convertTimestamp), Description: "Time of the most recent cache access."}, |
| 36 | + {Name: "created_at", Type: proto.ColumnType_TIMESTAMP, Transform: transform.FromField("CreatedAt").Transform(convertTimestamp), Description: "Time when the cache was created."}, |
| 37 | + {Name: "size_in_bytes", Type: proto.ColumnType_INT, Description: "Size of the cache in bytes."}, |
| 38 | + }), |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func tableGitHubCacheList(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 43 | + client := connect(ctx, d) |
| 44 | + |
| 45 | + fullName := d.EqualsQuals["repository_full_name"].GetStringValue() |
| 46 | + owner, repo := parseRepoFullName(fullName) |
| 47 | + opts := &github.ActionsCacheListOptions{ |
| 48 | + ListOptions: github.ListOptions{PerPage: 100}, |
| 49 | + } |
| 50 | + if key := d.EqualsQualString("key"); key != "" { |
| 51 | + opts.Key = github.String(key) |
| 52 | + } |
| 53 | + if ref := d.EqualsQualString("ref"); ref != "" { |
| 54 | + opts.Ref = github.String(ref) |
| 55 | + } |
| 56 | + |
| 57 | + limit := d.QueryContext.Limit |
| 58 | + if limit != nil { |
| 59 | + if *limit < int64(opts.PerPage) { |
| 60 | + opts.PerPage = int(*limit) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + for { |
| 65 | + caches, resp, err := client.Actions.ListCaches(ctx, owner, repo, opts) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + |
| 70 | + for _, i := range caches.ActionsCaches { |
| 71 | + if i != nil { |
| 72 | + d.StreamListItem(ctx, i) |
| 73 | + } |
| 74 | + |
| 75 | + // Context can be cancelled due to manual cancellation or the limit has been hit |
| 76 | + if d.RowsRemaining(ctx) == 0 { |
| 77 | + return nil, nil |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if resp.NextPage == 0 { |
| 82 | + break |
| 83 | + } |
| 84 | + |
| 85 | + opts.Page = resp.NextPage |
| 86 | + } |
| 87 | + |
| 88 | + return nil, nil |
| 89 | +} |
0 commit comments