|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "slices" |
| 7 | + |
| 8 | + "github.com/shurcooL/githubv4" |
| 9 | + "github.com/turbot/steampipe-plugin-github/github/models" |
| 10 | + "github.com/turbot/steampipe-plugin-sdk/v5/plugin" |
| 11 | +) |
| 12 | + |
| 13 | +func extractBranchFromHydrateItem(h *plugin.HydrateData) (models.Branch, error) { |
| 14 | + if branch, ok := h.Item.(models.Branch); ok { |
| 15 | + return branch, nil |
| 16 | + } else { |
| 17 | + return models.Branch{}, fmt.Errorf("unable to parse hydrate item %v as a Branch", h.Item) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +func appendBranchColumnIncludes(m *map[string]interface{}, cols []string) { |
| 22 | + protectionIncluded := githubv4.Boolean(slices.Contains(cols, "protected") || slices.Contains(cols, "branch_protection_rule")) |
| 23 | + |
| 24 | + (*m)["includeBranchProtectionRule"] = protectionIncluded |
| 25 | + (*m)["includeAllowsDeletions"] = protectionIncluded |
| 26 | + (*m)["includeAllowsForcePushes"] = protectionIncluded |
| 27 | + (*m)["includeBlocksCreations"] = protectionIncluded |
| 28 | + (*m)["includeCreator"] = protectionIncluded |
| 29 | + (*m)["includeBranchProtectionRuleId"] = protectionIncluded |
| 30 | + (*m)["includeDismissesStaleReviews"] = protectionIncluded |
| 31 | + (*m)["includeIsAdminEnforced"] = protectionIncluded |
| 32 | + (*m)["includeLockAllowsFetchAndMerge"] = protectionIncluded |
| 33 | + (*m)["includeLockBranch"] = protectionIncluded |
| 34 | + (*m)["includePattern"] = protectionIncluded |
| 35 | + (*m)["includeRequireLastPushApproval"] = protectionIncluded |
| 36 | + (*m)["includeRequiredApprovingReviewCount"] = protectionIncluded |
| 37 | + (*m)["includeRequiredDeploymentEnvironments"] = protectionIncluded |
| 38 | + (*m)["includeRequiredStatusChecks"] = protectionIncluded |
| 39 | + (*m)["includeRequiresApprovingReviews"] = protectionIncluded |
| 40 | + (*m)["includeRequiresConversationResolution"] = protectionIncluded |
| 41 | + (*m)["includeRequiresCodeOwnerReviews"] = protectionIncluded |
| 42 | + (*m)["includeRequiresCommitSignatures"] = protectionIncluded |
| 43 | + (*m)["includeRequiresDeployments"] = protectionIncluded |
| 44 | + (*m)["includeRequiresLinearHistory"] = protectionIncluded |
| 45 | + (*m)["includeRequiresStatusChecks"] = protectionIncluded |
| 46 | + (*m)["includeRequiresStrictStatusChecks"] = protectionIncluded |
| 47 | + (*m)["includeRestrictsPushes"] = protectionIncluded |
| 48 | + (*m)["includeRestrictsReviewDismissals"] = protectionIncluded |
| 49 | + (*m)["includeMatchingBranches"] = protectionIncluded |
| 50 | +} |
| 51 | + |
| 52 | +func branchHydrateProtected(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 53 | + branch, err := extractBranchFromHydrateItem(h) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + return branch.BranchProtectionRule.NodeId, nil |
| 58 | +} |
| 59 | + |
| 60 | +func branchHydrateBranchProtectionRule(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 61 | + branch, err := extractBranchFromHydrateItem(h) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + return branch.BranchProtectionRule, nil |
| 66 | +} |
| 67 | + |
| 68 | +func extractBranchProtectionRuleFromHydrateItem(h *plugin.HydrateData) (branchProtectionRow, error) { |
| 69 | + if branchProtectionRule, ok := h.Item.(branchProtectionRow); ok { |
| 70 | + return branchProtectionRule, nil |
| 71 | + } else { |
| 72 | + return branchProtectionRow{}, fmt.Errorf("unable to parse hydrate item %v as a BranchProtectionRule", h.Item) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func appendBranchProtectionRuleColumnIncludes(m *map[string]interface{}, cols []string) { |
| 77 | + (*m)["includeAllowsDeletions"] = githubv4.Boolean(slices.Contains(cols, "allows_deletions")) |
| 78 | + (*m)["includeAllowsForcePushes"] = githubv4.Boolean(slices.Contains(cols, "allows_force_pushes")) |
| 79 | + (*m)["includeBlocksCreations"] = githubv4.Boolean(slices.Contains(cols, "blocks_creations")) |
| 80 | + (*m)["includeCreator"] = githubv4.Boolean(slices.Contains(cols, "creator") || slices.Contains(cols, "creator_login")) |
| 81 | + (*m)["includeBranchProtectionRuleId"] = githubv4.Boolean(slices.Contains(cols, "id")) |
| 82 | + (*m)["includeDismissesStaleReviews"] = githubv4.Boolean(slices.Contains(cols, "dismisses_stale_reviews")) |
| 83 | + (*m)["includeIsAdminEnforced"] = githubv4.Boolean(slices.Contains(cols, "is_admin_enforced")) |
| 84 | + (*m)["includeLockAllowsFetchAndMerge"] = githubv4.Boolean(slices.Contains(cols, "lock_allows_fetch_and_merge")) |
| 85 | + (*m)["includeLockBranch"] = githubv4.Boolean(slices.Contains(cols, "lock_branch")) |
| 86 | + (*m)["includePattern"] = githubv4.Boolean(slices.Contains(cols, "pattern")) |
| 87 | + (*m)["includeRequireLastPushApproval"] = githubv4.Boolean(slices.Contains(cols, "require_last_push_approval")) |
| 88 | + (*m)["includeRequiredApprovingReviewCount"] = githubv4.Boolean(slices.Contains(cols, "required_approving_review_count")) |
| 89 | + (*m)["includeRequiredDeploymentEnvironments"] = githubv4.Boolean(slices.Contains(cols, "required_deployment_environments")) |
| 90 | + (*m)["includeRequiredStatusChecks"] = githubv4.Boolean(slices.Contains(cols, "required_status_checks")) |
| 91 | + (*m)["includeRequiresApprovingReviews"] = githubv4.Boolean(slices.Contains(cols, "requires_approving_reviews")) |
| 92 | + (*m)["includeRequiresConversationResolution"] = githubv4.Boolean(slices.Contains(cols, "requires_conversation_resolution")) |
| 93 | + (*m)["includeRequiresCodeOwnerReviews"] = githubv4.Boolean(slices.Contains(cols, "requires_code_owner_reviews")) |
| 94 | + (*m)["includeRequiresCommitSignatures"] = githubv4.Boolean(slices.Contains(cols, "requires_commit_signatures")) |
| 95 | + (*m)["includeRequiresDeployments"] = githubv4.Boolean(slices.Contains(cols, "requires_deployments")) |
| 96 | + (*m)["includeRequiresLinearHistory"] = githubv4.Boolean(slices.Contains(cols, "requires_linear_history")) |
| 97 | + (*m)["includeRequiresStatusChecks"] = githubv4.Boolean(slices.Contains(cols, "requires_status_checks")) |
| 98 | + (*m)["includeRequiresStrictStatusChecks"] = githubv4.Boolean(slices.Contains(cols, "requires_strict_status_checks")) |
| 99 | + (*m)["includeRestrictsPushes"] = githubv4.Boolean(slices.Contains(cols, "restricts_pushes")) |
| 100 | + (*m)["includeRestrictsReviewDismissals"] = githubv4.Boolean(slices.Contains(cols, "restricts_review_dismissals")) |
| 101 | + (*m)["includeMatchingBranches"] = githubv4.Boolean(slices.Contains(cols, "matching_branches")) |
| 102 | +} |
| 103 | + |
| 104 | +func branchProtectionRuleHydrateAllowsDeletions(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 105 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 106 | + if err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + return brp.AllowsDeletions, nil |
| 110 | +} |
| 111 | + |
| 112 | +func branchProtectionRuleHydrateAllowsForcePushes(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 113 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 114 | + if err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + return brp.AllowsForcePushes, nil |
| 118 | +} |
| 119 | + |
| 120 | +func branchProtectionRuleHydrateBlocksCreations(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 121 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 122 | + if err != nil { |
| 123 | + return nil, err |
| 124 | + } |
| 125 | + return brp.BlocksCreations, nil |
| 126 | +} |
| 127 | + |
| 128 | +func branchProtectionRuleHydrateCreatorLogin(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 129 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 130 | + if err != nil { |
| 131 | + return nil, err |
| 132 | + } |
| 133 | + return brp.CreatorLogin, nil |
| 134 | +} |
| 135 | + |
| 136 | +func branchProtectionRuleHydrateId(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 137 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 138 | + if err != nil { |
| 139 | + return nil, err |
| 140 | + } |
| 141 | + return brp.ID, nil |
| 142 | +} |
| 143 | + |
| 144 | +func branchProtectionRuleHydrateDismissesStaleReviews(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 145 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 146 | + if err != nil { |
| 147 | + return nil, err |
| 148 | + } |
| 149 | + return brp.DismissesStaleReviews, nil |
| 150 | +} |
| 151 | + |
| 152 | +func branchProtectionRuleHydrateIsAdminEnforced(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 153 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 154 | + if err != nil { |
| 155 | + return nil, err |
| 156 | + } |
| 157 | + return brp.IsAdminEnforced, nil |
| 158 | +} |
| 159 | + |
| 160 | +func branchProtectionRuleHydrateLockAllowsFetchAndMerge(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 161 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 162 | + if err != nil { |
| 163 | + return nil, err |
| 164 | + } |
| 165 | + return brp.LockAllowsFetchAndMerge, nil |
| 166 | +} |
| 167 | + |
| 168 | +func branchProtectionRuleHydrateLockBranch(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 169 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 170 | + if err != nil { |
| 171 | + return nil, err |
| 172 | + } |
| 173 | + return brp.LockBranch, nil |
| 174 | +} |
| 175 | + |
| 176 | +func branchProtectionRuleHydratePattern(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 177 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 178 | + if err != nil { |
| 179 | + return nil, err |
| 180 | + } |
| 181 | + return brp.Pattern, nil |
| 182 | +} |
| 183 | + |
| 184 | +func branchProtectionRuleHydrateRequireLastPushApproval(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 185 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 186 | + if err != nil { |
| 187 | + return nil, err |
| 188 | + } |
| 189 | + return brp.RequireLastPushApproval, nil |
| 190 | +} |
| 191 | + |
| 192 | +func branchProtectionRuleHydrateRequiredApprovingReviewCount(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 193 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 194 | + if err != nil { |
| 195 | + return nil, err |
| 196 | + } |
| 197 | + return brp.RequiredApprovingReviewCount, nil |
| 198 | +} |
| 199 | + |
| 200 | +func branchProtectionRuleHydrateRequiredDeploymentEnvironments(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 201 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 202 | + if err != nil { |
| 203 | + return nil, err |
| 204 | + } |
| 205 | + return brp.RequiredDeploymentEnvironments, nil |
| 206 | +} |
| 207 | + |
| 208 | +func branchProtectionRuleHydrateRequiredStatusChecks(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 209 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 210 | + if err != nil { |
| 211 | + return nil, err |
| 212 | + } |
| 213 | + return brp.RequiredStatusChecks, nil |
| 214 | +} |
| 215 | + |
| 216 | +func branchProtectionRuleHydrateRequiresApprovingReviews(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 217 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 218 | + if err != nil { |
| 219 | + return nil, err |
| 220 | + } |
| 221 | + return brp.RequiresApprovingReviews, nil |
| 222 | +} |
| 223 | + |
| 224 | +func branchProtectionRuleHydrateRequiresConversationResolution(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 225 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 226 | + if err != nil { |
| 227 | + return nil, err |
| 228 | + } |
| 229 | + return brp.RequiresConversationResolution, nil |
| 230 | +} |
| 231 | + |
| 232 | +func branchProtectionRuleHydrateRequiresCodeOwnerReviews(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 233 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 234 | + if err != nil { |
| 235 | + return nil, err |
| 236 | + } |
| 237 | + return brp.RequiresCodeOwnerReviews, nil |
| 238 | +} |
| 239 | + |
| 240 | +func branchProtectionRuleHydrateRequiresCommitSignatures(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 241 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 242 | + if err != nil { |
| 243 | + return nil, err |
| 244 | + } |
| 245 | + return brp.RequiresCommitSignatures, nil |
| 246 | +} |
| 247 | + |
| 248 | +func branchProtectionRuleHydrateRequiresDeployments(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 249 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 250 | + if err != nil { |
| 251 | + return nil, err |
| 252 | + } |
| 253 | + return brp.RequiresDeployments, nil |
| 254 | +} |
| 255 | + |
| 256 | +func branchProtectionRuleHydrateRequiresLinearHistory(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 257 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 258 | + if err != nil { |
| 259 | + return nil, err |
| 260 | + } |
| 261 | + return brp.RequiresLinearHistory, nil |
| 262 | +} |
| 263 | + |
| 264 | +func branchProtectionRuleHydrateRequiresStatusChecks(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 265 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 266 | + if err != nil { |
| 267 | + return nil, err |
| 268 | + } |
| 269 | + return brp.RequiresStatusChecks, nil |
| 270 | +} |
| 271 | + |
| 272 | +func branchProtectionRuleHydrateRequiresStrictStatusChecks(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 273 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 274 | + if err != nil { |
| 275 | + return nil, err |
| 276 | + } |
| 277 | + return brp.RequiresStrictStatusChecks, nil |
| 278 | +} |
| 279 | + |
| 280 | +func branchProtectionRuleHydrateRestrictsPushes(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 281 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 282 | + if err != nil { |
| 283 | + return nil, err |
| 284 | + } |
| 285 | + return brp.RestrictsPushes, nil |
| 286 | +} |
| 287 | + |
| 288 | +func branchProtectionRuleHydrateRestrictsReviewDismissals(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 289 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 290 | + if err != nil { |
| 291 | + return nil, err |
| 292 | + } |
| 293 | + return brp.RestrictsReviewDismissals, nil |
| 294 | +} |
| 295 | + |
| 296 | +func branchProtectionRuleHydrateMatchingBranchesTotalCount(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 297 | + brp, err := extractBranchProtectionRuleFromHydrateItem(h) |
| 298 | + if err != nil { |
| 299 | + return nil, err |
| 300 | + } |
| 301 | + return brp.MatchingBranches, nil |
| 302 | +} |
0 commit comments