github.com/google/go-github/v68@v68.0.0/github/repos_rules.go (about) 1 // Copyright 2023 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "context" 10 "encoding/json" 11 "fmt" 12 ) 13 14 // BypassActor represents the bypass actors from a ruleset. 15 type BypassActor struct { 16 ActorID *int64 `json:"actor_id,omitempty"` 17 // Possible values for ActorType are: RepositoryRole, Team, Integration, OrganizationAdmin 18 ActorType *string `json:"actor_type,omitempty"` 19 // Possible values for BypassMode are: always, pull_request 20 BypassMode *string `json:"bypass_mode,omitempty"` 21 } 22 23 // RulesetLink represents a single link object from GitHub ruleset request _links. 24 type RulesetLink struct { 25 HRef *string `json:"href,omitempty"` 26 } 27 28 // RulesetLinks represents the "_links" object in a Ruleset. 29 type RulesetLinks struct { 30 Self *RulesetLink `json:"self,omitempty"` 31 } 32 33 // RulesetRefConditionParameters represents the conditions object for ref_names. 34 type RulesetRefConditionParameters struct { 35 Include []string `json:"include"` 36 Exclude []string `json:"exclude"` 37 } 38 39 // RulesetRepositoryNamesConditionParameters represents the conditions object for repository_names. 40 type RulesetRepositoryNamesConditionParameters struct { 41 Include []string `json:"include"` 42 Exclude []string `json:"exclude"` 43 Protected *bool `json:"protected,omitempty"` 44 } 45 46 // RulesetRepositoryIDsConditionParameters represents the conditions object for repository_ids. 47 type RulesetRepositoryIDsConditionParameters struct { 48 RepositoryIDs []int64 `json:"repository_ids,omitempty"` 49 } 50 51 // RulesetRepositoryPropertyTargetParameters represents a repository_property name and values to be used for targeting. 52 type RulesetRepositoryPropertyTargetParameters struct { 53 Name string `json:"name"` 54 Values []string `json:"property_values"` 55 Source *string `json:"source,omitempty"` 56 } 57 58 // RulesetRepositoryPropertyConditionParameters represents the conditions object for repository_property. 59 type RulesetRepositoryPropertyConditionParameters struct { 60 Include []RulesetRepositoryPropertyTargetParameters `json:"include"` 61 Exclude []RulesetRepositoryPropertyTargetParameters `json:"exclude"` 62 } 63 64 // RulesetConditions represents the conditions object in a ruleset. 65 // Set either RepositoryName or RepositoryID or RepositoryProperty, not more than one. 66 type RulesetConditions struct { 67 RefName *RulesetRefConditionParameters `json:"ref_name,omitempty"` 68 RepositoryName *RulesetRepositoryNamesConditionParameters `json:"repository_name,omitempty"` 69 RepositoryID *RulesetRepositoryIDsConditionParameters `json:"repository_id,omitempty"` 70 RepositoryProperty *RulesetRepositoryPropertyConditionParameters `json:"repository_property,omitempty"` 71 } 72 73 // RulePatternParameters represents the rule pattern parameters. 74 type RulePatternParameters struct { 75 Name *string `json:"name,omitempty"` 76 // If Negate is true, the rule will fail if the pattern matches. 77 Negate *bool `json:"negate,omitempty"` 78 // Possible values for Operator are: starts_with, ends_with, contains, regex 79 Operator string `json:"operator"` 80 Pattern string `json:"pattern"` 81 } 82 83 // RuleFileParameters represents a list of file paths. 84 type RuleFileParameters struct { 85 RestrictedFilePaths *[]string `json:"restricted_file_paths"` 86 } 87 88 // RuleMaxFilePathLengthParameters represents the max_file_path_length rule parameters. 89 type RuleMaxFilePathLengthParameters struct { 90 MaxFilePathLength int `json:"max_file_path_length"` 91 } 92 93 // RuleFileExtensionRestrictionParameters represents the file_extension_restriction rule parameters. 94 type RuleFileExtensionRestrictionParameters struct { 95 RestrictedFileExtensions []string `json:"restricted_file_extensions"` 96 } 97 98 // RuleMaxFileSizeParameters represents the max_file_size rule parameters. 99 type RuleMaxFileSizeParameters struct { 100 MaxFileSize int64 `json:"max_file_size"` 101 } 102 103 // UpdateAllowsFetchAndMergeRuleParameters represents the update rule parameters. 104 type UpdateAllowsFetchAndMergeRuleParameters struct { 105 UpdateAllowsFetchAndMerge bool `json:"update_allows_fetch_and_merge"` 106 } 107 108 // RequiredDeploymentEnvironmentsRuleParameters represents the required_deployments rule parameters. 109 type RequiredDeploymentEnvironmentsRuleParameters struct { 110 RequiredDeploymentEnvironments []string `json:"required_deployment_environments"` 111 } 112 113 // PullRequestRuleParameters represents the pull_request rule parameters. 114 type PullRequestRuleParameters struct { 115 DismissStaleReviewsOnPush bool `json:"dismiss_stale_reviews_on_push"` 116 RequireCodeOwnerReview bool `json:"require_code_owner_review"` 117 RequireLastPushApproval bool `json:"require_last_push_approval"` 118 RequiredApprovingReviewCount int `json:"required_approving_review_count"` 119 RequiredReviewThreadResolution bool `json:"required_review_thread_resolution"` 120 } 121 122 // RuleRequiredStatusChecks represents the RequiredStatusChecks for the RequiredStatusChecksRuleParameters object. 123 type RuleRequiredStatusChecks struct { 124 Context string `json:"context"` 125 IntegrationID *int64 `json:"integration_id,omitempty"` 126 } 127 128 // MergeQueueRuleParameters represents the merge_queue rule parameters. 129 type MergeQueueRuleParameters struct { 130 CheckResponseTimeoutMinutes int `json:"check_response_timeout_minutes"` 131 // Possible values for GroupingStrategy are: ALLGREEN, HEADGREEN 132 GroupingStrategy string `json:"grouping_strategy"` 133 MaxEntriesToBuild int `json:"max_entries_to_build"` 134 MaxEntriesToMerge int `json:"max_entries_to_merge"` 135 // Possible values for MergeMethod are: MERGE, SQUASH, REBASE 136 MergeMethod string `json:"merge_method"` 137 MinEntriesToMerge int `json:"min_entries_to_merge"` 138 MinEntriesToMergeWaitMinutes int `json:"min_entries_to_merge_wait_minutes"` 139 } 140 141 // RequiredStatusChecksRuleParameters represents the required_status_checks rule parameters. 142 type RequiredStatusChecksRuleParameters struct { 143 DoNotEnforceOnCreate *bool `json:"do_not_enforce_on_create,omitempty"` 144 RequiredStatusChecks []RuleRequiredStatusChecks `json:"required_status_checks"` 145 StrictRequiredStatusChecksPolicy bool `json:"strict_required_status_checks_policy"` 146 } 147 148 // RuleRequiredWorkflow represents the Workflow for the RequireWorkflowsRuleParameters object. 149 type RuleRequiredWorkflow struct { 150 Path string `json:"path"` 151 Ref *string `json:"ref,omitempty"` 152 RepositoryID *int64 `json:"repository_id,omitempty"` 153 Sha *string `json:"sha,omitempty"` 154 } 155 156 // RequiredWorkflowsRuleParameters represents the workflows rule parameters. 157 type RequiredWorkflowsRuleParameters struct { 158 DoNotEnforceOnCreate bool `json:"do_not_enforce_on_create,omitempty"` 159 RequiredWorkflows []*RuleRequiredWorkflow `json:"workflows"` 160 } 161 162 // RuleRequiredCodeScanningTool represents a single required code-scanning tool for the RequiredCodeScanningParameters object. 163 type RuleRequiredCodeScanningTool struct { 164 AlertsThreshold string `json:"alerts_threshold"` 165 SecurityAlertsThreshold string `json:"security_alerts_threshold"` 166 Tool string `json:"tool"` 167 } 168 169 // RequiredCodeScanningRuleParameters represents the code_scanning rule parameters. 170 type RequiredCodeScanningRuleParameters struct { 171 RequiredCodeScanningTools []*RuleRequiredCodeScanningTool `json:"code_scanning_tools"` 172 } 173 174 // RepositoryRule represents a GitHub Rule. 175 type RepositoryRule struct { 176 Type string `json:"type"` 177 Parameters *json.RawMessage `json:"parameters,omitempty"` 178 RulesetSourceType string `json:"ruleset_source_type"` 179 RulesetSource string `json:"ruleset_source"` 180 RulesetID int64 `json:"ruleset_id"` 181 } 182 183 // RepositoryRulesetEditedChanges represents the changes made to a repository ruleset. 184 type RepositoryRulesetEditedChanges struct { 185 Name *RepositoryRulesetEditedSource `json:"name,omitempty"` 186 Enforcement *RepositoryRulesetEditedSource `json:"enforcement,omitempty"` 187 Conditions *RepositoryRulesetEditedConditions `json:"conditions,omitempty"` 188 Rules *RepositoryRulesetEditedRules `json:"rules,omitempty"` 189 } 190 191 // RepositoryRulesetEditedSource represents a source change for the ruleset. 192 type RepositoryRulesetEditedSource struct { 193 From *string `json:"from,omitempty"` 194 } 195 196 // RepositoryRulesetEditedSources represents multiple source changes for the ruleset. 197 type RepositoryRulesetEditedSources struct { 198 From []string `json:"from,omitempty"` 199 } 200 201 // RepositoryRulesetEditedConditions holds changes to conditions in a ruleset. 202 type RepositoryRulesetEditedConditions struct { 203 Added []*RepositoryRulesetRefCondition `json:"added,omitempty"` 204 Deleted []*RepositoryRulesetRefCondition `json:"deleted,omitempty"` 205 Updated []*RepositoryRulesetEditedUpdatedConditions `json:"updated,omitempty"` 206 } 207 208 // RepositoryRulesetEditedRules holds changes to rules in a ruleset. 209 type RepositoryRulesetEditedRules struct { 210 Added []*RepositoryRulesetRule `json:"added,omitempty"` 211 Deleted []*RepositoryRulesetRule `json:"deleted,omitempty"` 212 Updated []*RepositoryRulesetUpdatedRules `json:"updated,omitempty"` 213 } 214 215 // RepositoryRulesetRefCondition represents a reference condition for the ruleset. 216 type RepositoryRulesetRefCondition struct { 217 RefName *RulesetRefConditionParameters `json:"ref_name,omitempty"` 218 } 219 220 // RepositoryRulesetEditedUpdatedConditions holds updates to conditions in a ruleset. 221 type RepositoryRulesetEditedUpdatedConditions struct { 222 Condition *RepositoryRulesetRefCondition `json:"condition,omitempty"` 223 Changes *RepositoryRulesetUpdatedConditionsEdited `json:"changes,omitempty"` 224 } 225 226 // RepositoryRulesetUpdatedConditionsEdited holds the edited updates to conditions in a ruleset. 227 type RepositoryRulesetUpdatedConditionsEdited struct { 228 ConditionType *RepositoryRulesetEditedSource `json:"condition_type,omitempty"` 229 Target *RepositoryRulesetEditedSource `json:"target,omitempty"` 230 Include *RepositoryRulesetEditedSources `json:"include,omitempty"` 231 Exclude *RepositoryRulesetEditedSources `json:"exclude,omitempty"` 232 } 233 234 // RepositoryRulesetUpdatedRules holds updates to rules in a ruleset. 235 type RepositoryRulesetUpdatedRules struct { 236 Rule *RepositoryRulesetRule `json:"rule,omitempty"` 237 Changes *RepositoryRulesetEditedRuleChanges `json:"changes,omitempty"` 238 } 239 240 // RepositoryRulesetEditedRuleChanges holds changes made to a rule in a ruleset. 241 type RepositoryRulesetEditedRuleChanges struct { 242 Configuration *RepositoryRulesetEditedSources `json:"configuration,omitempty"` 243 RuleType *RepositoryRulesetEditedSources `json:"rule_type,omitempty"` 244 Pattern *RepositoryRulesetEditedSources `json:"pattern,omitempty"` 245 } 246 247 // RepositoryRuleset represents the structure of a ruleset associated with a GitHub repository. 248 type RepositoryRuleset struct { 249 ID int64 `json:"id"` 250 Name string `json:"name"` 251 // Possible values for target: "branch", "tag", "push" 252 Target *string `json:"target,omitempty"` 253 // Possible values for source type: "Repository", "Organization" 254 SourceType *string `json:"source_type,omitempty"` 255 Source string `json:"source"` 256 // Possible values for enforcement: "disabled", "active", "evaluate" 257 Enforcement string `json:"enforcement"` 258 BypassActors []*BypassActor `json:"bypass_actors,omitempty"` 259 // Possible values for current user can bypass: "always", "pull_requests_only", "never" 260 CurrentUserCanBypass *string `json:"current_user_can_bypass,omitempty"` 261 NodeID *string `json:"node_id,omitempty"` 262 Links *RepositoryRulesetLink `json:"_links,omitempty"` 263 Conditions json.RawMessage `json:"conditions,omitempty"` 264 Rules []*RepositoryRulesetRule `json:"rules,omitempty"` 265 CreatedAt *Timestamp `json:"created_at,omitempty"` 266 UpdatedAt *Timestamp `json:"updated_at,omitempty"` 267 } 268 269 // RepositoryRulesetRule represents individual rules which are present in a repository's ruleset. 270 type RepositoryRulesetRule struct { 271 Creation *RepositoryRulesetRuleType `json:"creation,omitempty"` 272 Update *RepositoryRulesetUpdateRule `json:"update,omitempty"` 273 Deletion *RepositoryRulesetRuleType `json:"deletion,omitempty"` 274 RequiredLinearHistory *RepositoryRulesetRuleType `json:"required_linear_history,omitempty"` 275 MergeQueue *RepositoryRulesetMergeQueueRule `json:"merge_queue,omitempty"` 276 RequiredDeployments *RepositoryRulesetRequiredDeploymentsRule `json:"required_deployments,omitempty"` 277 RequiredSignatures *RepositoryRulesetRuleType `json:"required_signatures,omitempty"` 278 PullRequest *RepositoryRulesetPullRequestRule `json:"pull_request,omitempty"` 279 RequiredStatusChecks *RepositoryRulesetRequiredStatusChecksRule `json:"required_status_checks,omitempty"` 280 NonFastForward *RepositoryRulesetRuleType `json:"non_fast_forward,omitempty"` 281 CommitMessagePattern *RepositoryRulesetPatternRule `json:"commit_message_pattern,omitempty"` 282 CommitAuthorEmailPattern *RepositoryRulesetPatternRule `json:"commit_author_email_pattern,omitempty"` 283 CommitterEmailPattern *RepositoryRulesetPatternRule `json:"committer_email_pattern,omitempty"` 284 BranchNamePattern *RepositoryRulesetPatternRule `json:"branch_name_pattern,omitempty"` 285 TagNamePattern *RepositoryRulesetPatternRule `json:"tag_name_pattern,omitempty"` 286 FilePathRestriction *RepositoryRulesetFilePathRestrictionRule `json:"file_path_restriction,omitempty"` 287 MaxFilePathLength *RepositoryRulesetMaxFilePathLengthRule `json:"max_file_path_length,omitempty"` 288 FileExtensionRestriction *RepositoryRulesetFileExtensionRestrictionRule `json:"file_extension_restriction,omitempty"` 289 MaxFileSize *RepositoryRulesetMaxFileSizeRule `json:"max_file_size,omitempty"` 290 Workflows *RepositoryRulesetWorkflowsRule `json:"workflows,omitempty"` 291 CodeScanning *RepositoryRulesetCodeScanningRule `json:"code_scanning,omitempty"` 292 } 293 294 // RepositoryRulesetLink represents Links associated with a repository's rulesets. These links are used to provide more information about the ruleset. 295 type RepositoryRulesetLink struct { 296 Self *RulesetLink `json:"self,omitempty"` 297 HTML *RulesetLink `json:"html,omitempty"` 298 } 299 300 // RepositoryRulesetRuleType represents the type of a ruleset rule. 301 type RepositoryRulesetRuleType struct { 302 Type string `json:"type"` 303 } 304 305 // RepositoryRulesetUpdateRule defines an update rule for the repository. 306 type RepositoryRulesetUpdateRule struct { 307 // Type can be one of: "update". 308 Type string `json:"type"` 309 Parameters *UpdateAllowsFetchAndMergeRuleParameters `json:"parameters,omitempty"` 310 } 311 312 // RepositoryRulesetMergeQueueRule defines a merge queue rule for the repository. 313 type RepositoryRulesetMergeQueueRule struct { 314 // Type can be one of: "merge_queue". 315 Type string `json:"type"` 316 Parameters *MergeQueueRuleParameters `json:"parameters,omitempty"` 317 } 318 319 // RepositoryRulesetRequiredDeploymentsRule defines a rule for required deployments. 320 type RepositoryRulesetRequiredDeploymentsRule struct { 321 // Type can be one of: "required_deployments". 322 Type string `json:"type"` 323 Parameters *RequiredDeploymentEnvironmentsRuleParameters `json:"parameters,omitempty"` 324 } 325 326 // RepositoryRulesetPullRequestRule defines a rule for pull requests. 327 type RepositoryRulesetPullRequestRule struct { 328 // Type can be one of: "pull_request". 329 330 Type string `json:"type"` 331 Parameters *PullRequestRuleParameters `json:"parameters,omitempty"` 332 } 333 334 // RepositoryRulesetRequiredStatusChecksRule defines a rule for required status checks. 335 type RepositoryRulesetRequiredStatusChecksRule struct { 336 // Type can be one of: "required_status_checks". 337 338 Type string `json:"type"` 339 Parameters *RequiredStatusChecksRuleParameters `json:"parameters,omitempty"` 340 } 341 342 // RepositoryRulesetPatternRule defines a pattern rule for the repository. 343 type RepositoryRulesetPatternRule struct { 344 Type string `json:"type"` 345 Parameters *RulePatternParameters `json:"parameters,omitempty"` 346 } 347 348 // RepositoryRulesetFilePathRestrictionRule defines a file path restriction rule for the repository. 349 type RepositoryRulesetFilePathRestrictionRule struct { 350 // Type can be one of: "file_path_restriction". 351 Type string `json:"type"` 352 Parameters *RuleFileParameters `json:"parameters,omitempty"` 353 } 354 355 // RepositoryRulesetMaxFilePathLengthRule defines a maximum file path length rule for the repository. 356 type RepositoryRulesetMaxFilePathLengthRule struct { 357 // Type can be one of: "max_file_path_length". 358 359 Type string `json:"type"` 360 Parameters *RuleMaxFilePathLengthParameters `json:"parameters,omitempty"` 361 } 362 363 // RepositoryRulesetFileExtensionRestrictionRule defines a file extension restriction rule for the repository. 364 type RepositoryRulesetFileExtensionRestrictionRule struct { 365 // Type can be one of: "file_extension_restriction". 366 Type string `json:"type"` 367 Parameters *RuleFileExtensionRestrictionParameters `json:"parameters,omitempty"` 368 } 369 370 // RepositoryRulesetMaxFileSizeRule defines a maximum file size rule for the repository. 371 type RepositoryRulesetMaxFileSizeRule struct { 372 // Type can be one of: "max_file_size". 373 Type string `json:"type"` 374 Parameters *RuleMaxFileSizeParameters `json:"parameters,omitempty"` 375 } 376 377 // RepositoryRulesetWorkflowsRule defines a workflow rule for the repository. 378 type RepositoryRulesetWorkflowsRule struct { 379 // Type can be one of: "workflows". 380 Type string `json:"type"` 381 Parameters *RequiredWorkflowsRuleParameters `json:"parameters,omitempty"` 382 } 383 384 // RepositoryRulesetCodeScanningRule defines a code scanning rule for the repository. 385 type RepositoryRulesetCodeScanningRule struct { 386 // Type can be one of: "code_scanning". 387 Type string `json:"type"` 388 Parameters *RuleCodeScanningParameters `json:"parameters,omitempty"` 389 } 390 391 // RuleCodeScanningParameters defines parameters for code scanning rules. 392 type RuleCodeScanningParameters struct { 393 CodeScanningTools []*CodeScanningTool `json:"code_scanning_tools,omitempty"` 394 } 395 396 // CodeScanningTool defines a specific tool used for code scanning. 397 type CodeScanningTool struct { 398 AlertsThreshold string `json:"alerts_threshold"` 399 SecurityAlertsThreshold string `json:"security_alerts_threshold"` 400 Tool string `json:"tool"` 401 } 402 403 // UnmarshalJSON implements the json.Unmarshaler interface. 404 // This helps us handle the fact that RepositoryRule parameter field can be of numerous types. 405 func (r *RepositoryRule) UnmarshalJSON(data []byte) error { 406 type rule RepositoryRule 407 var repositoryRule rule 408 if err := json.Unmarshal(data, &repositoryRule); err != nil { 409 return err 410 } 411 412 r.RulesetID = repositoryRule.RulesetID 413 r.RulesetSourceType = repositoryRule.RulesetSourceType 414 r.RulesetSource = repositoryRule.RulesetSource 415 r.Type = repositoryRule.Type 416 417 switch repositoryRule.Type { 418 case "creation", "deletion", "non_fast_forward", "required_linear_history", "required_signatures": 419 r.Parameters = nil 420 case "update": 421 if repositoryRule.Parameters == nil { 422 r.Parameters = nil 423 return nil 424 } 425 params := UpdateAllowsFetchAndMergeRuleParameters{} 426 if err := json.Unmarshal(*repositoryRule.Parameters, ¶ms); err != nil { 427 return err 428 } 429 430 bytes, _ := json.Marshal(params) 431 rawParams := json.RawMessage(bytes) 432 433 r.Parameters = &rawParams 434 case "merge_queue": 435 if repositoryRule.Parameters == nil { 436 r.Parameters = nil 437 return nil 438 } 439 params := MergeQueueRuleParameters{} 440 if err := json.Unmarshal(*repositoryRule.Parameters, ¶ms); err != nil { 441 return err 442 } 443 444 bytes, _ := json.Marshal(params) 445 rawParams := json.RawMessage(bytes) 446 447 r.Parameters = &rawParams 448 case "required_deployments": 449 params := RequiredDeploymentEnvironmentsRuleParameters{} 450 if err := json.Unmarshal(*repositoryRule.Parameters, ¶ms); err != nil { 451 return err 452 } 453 454 bytes, _ := json.Marshal(params) 455 rawParams := json.RawMessage(bytes) 456 457 r.Parameters = &rawParams 458 case "commit_message_pattern", "commit_author_email_pattern", "committer_email_pattern", "branch_name_pattern", "tag_name_pattern": 459 params := RulePatternParameters{} 460 if err := json.Unmarshal(*repositoryRule.Parameters, ¶ms); err != nil { 461 return err 462 } 463 464 bytes, _ := json.Marshal(params) 465 rawParams := json.RawMessage(bytes) 466 467 r.Parameters = &rawParams 468 case "pull_request": 469 params := PullRequestRuleParameters{} 470 if err := json.Unmarshal(*repositoryRule.Parameters, ¶ms); err != nil { 471 return err 472 } 473 474 bytes, _ := json.Marshal(params) 475 rawParams := json.RawMessage(bytes) 476 477 r.Parameters = &rawParams 478 case "required_status_checks": 479 params := RequiredStatusChecksRuleParameters{} 480 if err := json.Unmarshal(*repositoryRule.Parameters, ¶ms); err != nil { 481 return err 482 } 483 484 bytes, _ := json.Marshal(params) 485 rawParams := json.RawMessage(bytes) 486 487 r.Parameters = &rawParams 488 case "workflows": 489 params := RequiredWorkflowsRuleParameters{} 490 if err := json.Unmarshal(*repositoryRule.Parameters, ¶ms); err != nil { 491 return err 492 } 493 494 bytes, _ := json.Marshal(params) 495 rawParams := json.RawMessage(bytes) 496 497 r.Parameters = &rawParams 498 case "file_path_restriction": 499 params := RuleFileParameters{} 500 if err := json.Unmarshal(*repositoryRule.Parameters, ¶ms); err != nil { 501 return err 502 } 503 bytes, _ := json.Marshal(params) 504 rawParams := json.RawMessage(bytes) 505 506 r.Parameters = &rawParams 507 case "code_scanning": 508 params := RequiredCodeScanningRuleParameters{} 509 if err := json.Unmarshal(*repositoryRule.Parameters, ¶ms); err != nil { 510 return err 511 } 512 bytes, _ := json.Marshal(params) 513 rawParams := json.RawMessage(bytes) 514 515 r.Parameters = &rawParams 516 case "max_file_path_length": 517 params := RuleMaxFilePathLengthParameters{} 518 if err := json.Unmarshal(*repositoryRule.Parameters, ¶ms); err != nil { 519 return err 520 } 521 bytes, _ := json.Marshal(params) 522 rawParams := json.RawMessage(bytes) 523 524 r.Parameters = &rawParams 525 case "file_extension_restriction": 526 params := RuleFileExtensionRestrictionParameters{} 527 if err := json.Unmarshal(*repositoryRule.Parameters, ¶ms); err != nil { 528 return err 529 } 530 bytes, _ := json.Marshal(params) 531 rawParams := json.RawMessage(bytes) 532 533 r.Parameters = &rawParams 534 case "max_file_size": 535 params := RuleMaxFileSizeParameters{} 536 if err := json.Unmarshal(*repositoryRule.Parameters, ¶ms); err != nil { 537 return err 538 } 539 bytes, _ := json.Marshal(params) 540 rawParams := json.RawMessage(bytes) 541 542 r.Parameters = &rawParams 543 default: 544 r.Type = "" 545 r.Parameters = nil 546 return fmt.Errorf("RepositoryRule.Type %q is not yet implemented, unable to unmarshal (%#v)", repositoryRule.Type, repositoryRule) 547 } 548 549 return nil 550 } 551 552 // NewMergeQueueRule creates a rule to only allow merges via a merge queue. 553 func NewMergeQueueRule(params *MergeQueueRuleParameters) (rule *RepositoryRule) { 554 if params != nil { 555 bytes, _ := json.Marshal(params) 556 557 rawParams := json.RawMessage(bytes) 558 559 return &RepositoryRule{ 560 Type: "merge_queue", 561 Parameters: &rawParams, 562 } 563 } 564 return &RepositoryRule{ 565 Type: "merge_queue", 566 } 567 } 568 569 // NewCreationRule creates a rule to only allow users with bypass permission to create matching refs. 570 func NewCreationRule() (rule *RepositoryRule) { 571 return &RepositoryRule{ 572 Type: "creation", 573 } 574 } 575 576 // NewUpdateRule creates a rule to only allow users with bypass permission to update matching refs. 577 func NewUpdateRule(params *UpdateAllowsFetchAndMergeRuleParameters) (rule *RepositoryRule) { 578 if params != nil { 579 bytes, _ := json.Marshal(params) 580 581 rawParams := json.RawMessage(bytes) 582 583 return &RepositoryRule{ 584 Type: "update", 585 Parameters: &rawParams, 586 } 587 } 588 return &RepositoryRule{ 589 Type: "update", 590 } 591 } 592 593 // NewDeletionRule creates a rule to only allow users with bypass permissions to delete matching refs. 594 func NewDeletionRule() (rule *RepositoryRule) { 595 return &RepositoryRule{ 596 Type: "deletion", 597 } 598 } 599 600 // NewRequiredLinearHistoryRule creates a rule to prevent merge commits from being pushed to matching branches. 601 func NewRequiredLinearHistoryRule() (rule *RepositoryRule) { 602 return &RepositoryRule{ 603 Type: "required_linear_history", 604 } 605 } 606 607 // NewRequiredDeploymentsRule creates a rule to require environments to be successfully deployed before they can be merged into the matching branches. 608 func NewRequiredDeploymentsRule(params *RequiredDeploymentEnvironmentsRuleParameters) (rule *RepositoryRule) { 609 bytes, _ := json.Marshal(params) 610 611 rawParams := json.RawMessage(bytes) 612 613 return &RepositoryRule{ 614 Type: "required_deployments", 615 Parameters: &rawParams, 616 } 617 } 618 619 // NewRequiredSignaturesRule creates a rule a to require commits pushed to matching branches to have verified signatures. 620 func NewRequiredSignaturesRule() (rule *RepositoryRule) { 621 return &RepositoryRule{ 622 Type: "required_signatures", 623 } 624 } 625 626 // NewPullRequestRule creates a rule to require all commits be made to a non-target branch and submitted via a pull request before they can be merged. 627 func NewPullRequestRule(params *PullRequestRuleParameters) (rule *RepositoryRule) { 628 bytes, _ := json.Marshal(params) 629 630 rawParams := json.RawMessage(bytes) 631 632 return &RepositoryRule{ 633 Type: "pull_request", 634 Parameters: &rawParams, 635 } 636 } 637 638 // NewRequiredStatusChecksRule creates a rule to require which status checks must pass before branches can be merged into a branch rule. 639 func NewRequiredStatusChecksRule(params *RequiredStatusChecksRuleParameters) (rule *RepositoryRule) { 640 bytes, _ := json.Marshal(params) 641 642 rawParams := json.RawMessage(bytes) 643 644 return &RepositoryRule{ 645 Type: "required_status_checks", 646 Parameters: &rawParams, 647 } 648 } 649 650 // NewNonFastForwardRule creates a rule as part to prevent users with push access from force pushing to matching branches. 651 func NewNonFastForwardRule() (rule *RepositoryRule) { 652 return &RepositoryRule{ 653 Type: "non_fast_forward", 654 } 655 } 656 657 // NewCommitMessagePatternRule creates a rule to restrict commit message patterns being pushed to matching branches. 658 func NewCommitMessagePatternRule(params *RulePatternParameters) (rule *RepositoryRule) { 659 bytes, _ := json.Marshal(params) 660 661 rawParams := json.RawMessage(bytes) 662 663 return &RepositoryRule{ 664 Type: "commit_message_pattern", 665 Parameters: &rawParams, 666 } 667 } 668 669 // NewCommitAuthorEmailPatternRule creates a rule to restrict commits with author email patterns being merged into matching branches. 670 func NewCommitAuthorEmailPatternRule(params *RulePatternParameters) (rule *RepositoryRule) { 671 bytes, _ := json.Marshal(params) 672 673 rawParams := json.RawMessage(bytes) 674 675 return &RepositoryRule{ 676 Type: "commit_author_email_pattern", 677 Parameters: &rawParams, 678 } 679 } 680 681 // NewCommitterEmailPatternRule creates a rule to restrict commits with committer email patterns being merged into matching branches. 682 func NewCommitterEmailPatternRule(params *RulePatternParameters) (rule *RepositoryRule) { 683 bytes, _ := json.Marshal(params) 684 685 rawParams := json.RawMessage(bytes) 686 687 return &RepositoryRule{ 688 Type: "committer_email_pattern", 689 Parameters: &rawParams, 690 } 691 } 692 693 // NewBranchNamePatternRule creates a rule to restrict branch patterns from being merged into matching branches. 694 func NewBranchNamePatternRule(params *RulePatternParameters) (rule *RepositoryRule) { 695 bytes, _ := json.Marshal(params) 696 697 rawParams := json.RawMessage(bytes) 698 699 return &RepositoryRule{ 700 Type: "branch_name_pattern", 701 Parameters: &rawParams, 702 } 703 } 704 705 // NewTagNamePatternRule creates a rule to restrict tag patterns contained in non-target branches from being merged into matching branches. 706 func NewTagNamePatternRule(params *RulePatternParameters) (rule *RepositoryRule) { 707 bytes, _ := json.Marshal(params) 708 709 rawParams := json.RawMessage(bytes) 710 711 return &RepositoryRule{ 712 Type: "tag_name_pattern", 713 Parameters: &rawParams, 714 } 715 } 716 717 // NewRequiredWorkflowsRule creates a rule to require which status checks must pass before branches can be merged into a branch rule. 718 func NewRequiredWorkflowsRule(params *RequiredWorkflowsRuleParameters) (rule *RepositoryRule) { 719 bytes, _ := json.Marshal(params) 720 721 rawParams := json.RawMessage(bytes) 722 723 return &RepositoryRule{ 724 Type: "workflows", 725 Parameters: &rawParams, 726 } 727 } 728 729 // NewRequiredCodeScanningRule creates a rule to require which tools must provide code scanning results before the reference is updated. 730 func NewRequiredCodeScanningRule(params *RequiredCodeScanningRuleParameters) (rule *RepositoryRule) { 731 bytes, _ := json.Marshal(params) 732 733 rawParams := json.RawMessage(bytes) 734 735 return &RepositoryRule{ 736 Type: "code_scanning", 737 Parameters: &rawParams, 738 } 739 } 740 741 // NewFilePathRestrictionRule creates a rule to restrict file paths from being pushed to. 742 func NewFilePathRestrictionRule(params *RuleFileParameters) (rule *RepositoryRule) { 743 bytes, _ := json.Marshal(params) 744 745 rawParams := json.RawMessage(bytes) 746 747 return &RepositoryRule{ 748 Type: "file_path_restriction", 749 Parameters: &rawParams, 750 } 751 } 752 753 // NewMaxFilePathLengthRule creates a rule to restrict file paths longer than the limit from being pushed. 754 func NewMaxFilePathLengthRule(params *RuleMaxFilePathLengthParameters) (rule *RepositoryRule) { 755 bytes, _ := json.Marshal(params) 756 757 rawParams := json.RawMessage(bytes) 758 759 return &RepositoryRule{ 760 Type: "max_file_path_length", 761 Parameters: &rawParams, 762 } 763 } 764 765 // NewFileExtensionRestrictionRule creates a rule to restrict file extensions from being pushed to a commit. 766 func NewFileExtensionRestrictionRule(params *RuleFileExtensionRestrictionParameters) (rule *RepositoryRule) { 767 bytes, _ := json.Marshal(params) 768 769 rawParams := json.RawMessage(bytes) 770 771 return &RepositoryRule{ 772 Type: "file_extension_restriction", 773 Parameters: &rawParams, 774 } 775 } 776 777 // NewMaxFileSizeRule creates a rule to restrict file sizes from being pushed to a commit. 778 func NewMaxFileSizeRule(params *RuleMaxFileSizeParameters) (rule *RepositoryRule) { 779 bytes, _ := json.Marshal(params) 780 781 rawParams := json.RawMessage(bytes) 782 783 return &RepositoryRule{ 784 Type: "max_file_size", 785 Parameters: &rawParams, 786 } 787 } 788 789 // Ruleset represents a GitHub ruleset object. 790 type Ruleset struct { 791 ID *int64 `json:"id,omitempty"` 792 Name string `json:"name"` 793 // Possible values for Target are branch, tag, push 794 Target *string `json:"target,omitempty"` 795 // Possible values for SourceType are: Repository, Organization 796 SourceType *string `json:"source_type,omitempty"` 797 Source string `json:"source"` 798 // Possible values for Enforcement are: disabled, active, evaluate 799 Enforcement string `json:"enforcement"` 800 BypassActors []*BypassActor `json:"bypass_actors,omitempty"` 801 NodeID *string `json:"node_id,omitempty"` 802 Links *RulesetLinks `json:"_links,omitempty"` 803 Conditions *RulesetConditions `json:"conditions,omitempty"` 804 Rules []*RepositoryRule `json:"rules,omitempty"` 805 UpdatedAt *Timestamp `json:"updated_at,omitempty"` 806 CreatedAt *Timestamp `json:"created_at,omitempty"` 807 } 808 809 // rulesetNoOmitBypassActors represents a GitHub ruleset object. The struct does not omit bypassActors if the field is nil or an empty array is passed. 810 type rulesetNoOmitBypassActors struct { 811 ID *int64 `json:"id,omitempty"` 812 Name string `json:"name"` 813 // Possible values for Target are branch, tag 814 Target *string `json:"target,omitempty"` 815 // Possible values for SourceType are: Repository, Organization 816 SourceType *string `json:"source_type,omitempty"` 817 Source string `json:"source"` 818 // Possible values for Enforcement are: disabled, active, evaluate 819 Enforcement string `json:"enforcement"` 820 BypassActors []*BypassActor `json:"bypass_actors"` 821 NodeID *string `json:"node_id,omitempty"` 822 Links *RulesetLinks `json:"_links,omitempty"` 823 Conditions *RulesetConditions `json:"conditions,omitempty"` 824 Rules []*RepositoryRule `json:"rules,omitempty"` 825 } 826 827 // GetRulesForBranch gets all the rules that apply to the specified branch. 828 // 829 // GitHub API docs: https://docs.github.com/rest/repos/rules#get-rules-for-a-branch 830 // 831 //meta:operation GET /repos/{owner}/{repo}/rules/branches/{branch} 832 func (s *RepositoriesService) GetRulesForBranch(ctx context.Context, owner, repo, branch string) ([]*RepositoryRule, *Response, error) { 833 u := fmt.Sprintf("repos/%v/%v/rules/branches/%v", owner, repo, branch) 834 835 req, err := s.client.NewRequest("GET", u, nil) 836 if err != nil { 837 return nil, nil, err 838 } 839 840 var rules []*RepositoryRule 841 resp, err := s.client.Do(ctx, req, &rules) 842 if err != nil { 843 return nil, resp, err 844 } 845 846 return rules, resp, nil 847 } 848 849 // GetAllRulesets gets all the rules that apply to the specified repository. 850 // If includesParents is true, rulesets configured at the organization level that apply to the repository will be returned. 851 // 852 // GitHub API docs: https://docs.github.com/rest/repos/rules#get-all-repository-rulesets 853 // 854 //meta:operation GET /repos/{owner}/{repo}/rulesets 855 func (s *RepositoriesService) GetAllRulesets(ctx context.Context, owner, repo string, includesParents bool) ([]*Ruleset, *Response, error) { 856 u := fmt.Sprintf("repos/%v/%v/rulesets?includes_parents=%v", owner, repo, includesParents) 857 858 req, err := s.client.NewRequest("GET", u, nil) 859 if err != nil { 860 return nil, nil, err 861 } 862 863 var ruleset []*Ruleset 864 resp, err := s.client.Do(ctx, req, &ruleset) 865 if err != nil { 866 return nil, resp, err 867 } 868 869 return ruleset, resp, nil 870 } 871 872 // CreateRuleset creates a ruleset for the specified repository. 873 // 874 // GitHub API docs: https://docs.github.com/rest/repos/rules#create-a-repository-ruleset 875 // 876 //meta:operation POST /repos/{owner}/{repo}/rulesets 877 func (s *RepositoriesService) CreateRuleset(ctx context.Context, owner, repo string, rs *Ruleset) (*Ruleset, *Response, error) { 878 u := fmt.Sprintf("repos/%v/%v/rulesets", owner, repo) 879 880 req, err := s.client.NewRequest("POST", u, rs) 881 if err != nil { 882 return nil, nil, err 883 } 884 885 var ruleset *Ruleset 886 resp, err := s.client.Do(ctx, req, &ruleset) 887 if err != nil { 888 return nil, resp, err 889 } 890 891 return ruleset, resp, nil 892 } 893 894 // GetRuleset gets a ruleset for the specified repository. 895 // If includesParents is true, rulesets configured at the organization level that apply to the repository will be returned. 896 // 897 // GitHub API docs: https://docs.github.com/rest/repos/rules#get-a-repository-ruleset 898 // 899 //meta:operation GET /repos/{owner}/{repo}/rulesets/{ruleset_id} 900 func (s *RepositoriesService) GetRuleset(ctx context.Context, owner, repo string, rulesetID int64, includesParents bool) (*Ruleset, *Response, error) { 901 u := fmt.Sprintf("repos/%v/%v/rulesets/%v?includes_parents=%v", owner, repo, rulesetID, includesParents) 902 903 req, err := s.client.NewRequest("GET", u, nil) 904 if err != nil { 905 return nil, nil, err 906 } 907 908 var ruleset *Ruleset 909 resp, err := s.client.Do(ctx, req, &ruleset) 910 if err != nil { 911 return nil, resp, err 912 } 913 914 return ruleset, resp, nil 915 } 916 917 // UpdateRuleset updates a ruleset for the specified repository. 918 // 919 // GitHub API docs: https://docs.github.com/rest/repos/rules#update-a-repository-ruleset 920 // 921 //meta:operation PUT /repos/{owner}/{repo}/rulesets/{ruleset_id} 922 func (s *RepositoriesService) UpdateRuleset(ctx context.Context, owner, repo string, rulesetID int64, rs *Ruleset) (*Ruleset, *Response, error) { 923 u := fmt.Sprintf("repos/%v/%v/rulesets/%v", owner, repo, rulesetID) 924 925 req, err := s.client.NewRequest("PUT", u, rs) 926 if err != nil { 927 return nil, nil, err 928 } 929 930 var ruleset *Ruleset 931 resp, err := s.client.Do(ctx, req, &ruleset) 932 if err != nil { 933 return nil, resp, err 934 } 935 936 return ruleset, resp, nil 937 } 938 939 // UpdateRulesetNoBypassActor updates a ruleset for the specified repository. 940 // 941 // This function is necessary as the UpdateRuleset function does not marshal ByPassActor if passed as nil or an empty array. 942 // 943 // GitHub API docs: https://docs.github.com/rest/repos/rules#update-a-repository-ruleset 944 // 945 //meta:operation PUT /repos/{owner}/{repo}/rulesets/{ruleset_id} 946 func (s *RepositoriesService) UpdateRulesetNoBypassActor(ctx context.Context, owner, repo string, rulesetID int64, rs *Ruleset) (*Ruleset, *Response, error) { 947 u := fmt.Sprintf("repos/%v/%v/rulesets/%v", owner, repo, rulesetID) 948 949 rsNoBypassActor := &rulesetNoOmitBypassActors{} 950 951 if rs != nil { 952 rsNoBypassActor = &rulesetNoOmitBypassActors{ 953 ID: rs.ID, 954 Name: rs.Name, 955 Target: rs.Target, 956 SourceType: rs.SourceType, 957 Source: rs.Source, 958 Enforcement: rs.Enforcement, 959 BypassActors: rs.BypassActors, 960 NodeID: rs.NodeID, 961 Links: rs.Links, 962 Conditions: rs.Conditions, 963 Rules: rs.Rules, 964 } 965 } 966 967 req, err := s.client.NewRequest("PUT", u, rsNoBypassActor) 968 if err != nil { 969 return nil, nil, err 970 } 971 972 var ruleSet *Ruleset 973 resp, err := s.client.Do(ctx, req, &ruleSet) 974 if err != nil { 975 return nil, resp, err 976 } 977 978 return ruleSet, resp, nil 979 } 980 981 // DeleteRuleset deletes a ruleset for the specified repository. 982 // 983 // GitHub API docs: https://docs.github.com/rest/repos/rules#delete-a-repository-ruleset 984 // 985 //meta:operation DELETE /repos/{owner}/{repo}/rulesets/{ruleset_id} 986 func (s *RepositoriesService) DeleteRuleset(ctx context.Context, owner, repo string, rulesetID int64) (*Response, error) { 987 u := fmt.Sprintf("repos/%v/%v/rulesets/%v", owner, repo, rulesetID) 988 989 req, err := s.client.NewRequest("DELETE", u, nil) 990 if err != nil { 991 return nil, err 992 } 993 994 return s.client.Do(ctx, req, nil) 995 }