github.com/google/go-github/v66@v66.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"` 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"` 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 RequiredWorkflows []*RuleRequiredWorkflow `json:"workflows"` 159 } 160 161 // RepositoryRule represents a GitHub Rule. 162 type RepositoryRule struct { 163 Type string `json:"type"` 164 Parameters *json.RawMessage `json:"parameters,omitempty"` 165 RulesetSourceType string `json:"ruleset_source_type"` 166 RulesetSource string `json:"ruleset_source"` 167 RulesetID int64 `json:"ruleset_id"` 168 } 169 170 // UnmarshalJSON implements the json.Unmarshaler interface. 171 // This helps us handle the fact that RepositoryRule parameter field can be of numerous types. 172 func (r *RepositoryRule) UnmarshalJSON(data []byte) error { 173 type rule RepositoryRule 174 var RepositoryRule rule 175 if err := json.Unmarshal(data, &RepositoryRule); err != nil { 176 return err 177 } 178 179 r.RulesetID = RepositoryRule.RulesetID 180 r.RulesetSourceType = RepositoryRule.RulesetSourceType 181 r.RulesetSource = RepositoryRule.RulesetSource 182 r.Type = RepositoryRule.Type 183 184 switch RepositoryRule.Type { 185 case "creation", "deletion", "non_fast_forward", "required_linear_history", "required_signatures": 186 r.Parameters = nil 187 case "update": 188 if RepositoryRule.Parameters == nil { 189 r.Parameters = nil 190 return nil 191 } 192 params := UpdateAllowsFetchAndMergeRuleParameters{} 193 if err := json.Unmarshal(*RepositoryRule.Parameters, ¶ms); err != nil { 194 return err 195 } 196 197 bytes, _ := json.Marshal(params) 198 rawParams := json.RawMessage(bytes) 199 200 r.Parameters = &rawParams 201 case "merge_queue": 202 if RepositoryRule.Parameters == nil { 203 r.Parameters = nil 204 return nil 205 } 206 params := MergeQueueRuleParameters{} 207 if err := json.Unmarshal(*RepositoryRule.Parameters, ¶ms); err != nil { 208 return err 209 } 210 211 bytes, _ := json.Marshal(params) 212 rawParams := json.RawMessage(bytes) 213 214 r.Parameters = &rawParams 215 case "required_deployments": 216 params := RequiredDeploymentEnvironmentsRuleParameters{} 217 if err := json.Unmarshal(*RepositoryRule.Parameters, ¶ms); err != nil { 218 return err 219 } 220 221 bytes, _ := json.Marshal(params) 222 rawParams := json.RawMessage(bytes) 223 224 r.Parameters = &rawParams 225 case "commit_message_pattern", "commit_author_email_pattern", "committer_email_pattern", "branch_name_pattern", "tag_name_pattern": 226 params := RulePatternParameters{} 227 if err := json.Unmarshal(*RepositoryRule.Parameters, ¶ms); err != nil { 228 return err 229 } 230 231 bytes, _ := json.Marshal(params) 232 rawParams := json.RawMessage(bytes) 233 234 r.Parameters = &rawParams 235 case "pull_request": 236 params := PullRequestRuleParameters{} 237 if err := json.Unmarshal(*RepositoryRule.Parameters, ¶ms); err != nil { 238 return err 239 } 240 241 bytes, _ := json.Marshal(params) 242 rawParams := json.RawMessage(bytes) 243 244 r.Parameters = &rawParams 245 case "required_status_checks": 246 params := RequiredStatusChecksRuleParameters{} 247 if err := json.Unmarshal(*RepositoryRule.Parameters, ¶ms); err != nil { 248 return err 249 } 250 251 bytes, _ := json.Marshal(params) 252 rawParams := json.RawMessage(bytes) 253 254 r.Parameters = &rawParams 255 case "workflows": 256 params := RequiredWorkflowsRuleParameters{} 257 if err := json.Unmarshal(*RepositoryRule.Parameters, ¶ms); err != nil { 258 return err 259 } 260 261 bytes, _ := json.Marshal(params) 262 rawParams := json.RawMessage(bytes) 263 264 r.Parameters = &rawParams 265 case "file_path_restriction": 266 params := RuleFileParameters{} 267 if err := json.Unmarshal(*RepositoryRule.Parameters, ¶ms); err != nil { 268 return err 269 } 270 bytes, _ := json.Marshal(params) 271 rawParams := json.RawMessage(bytes) 272 273 r.Parameters = &rawParams 274 case "max_file_path_length": 275 params := RuleMaxFilePathLengthParameters{} 276 if err := json.Unmarshal(*RepositoryRule.Parameters, ¶ms); err != nil { 277 return err 278 } 279 bytes, _ := json.Marshal(params) 280 rawParams := json.RawMessage(bytes) 281 282 r.Parameters = &rawParams 283 case "file_extension_restriction": 284 params := RuleFileExtensionRestrictionParameters{} 285 if err := json.Unmarshal(*RepositoryRule.Parameters, ¶ms); err != nil { 286 return err 287 } 288 bytes, _ := json.Marshal(params) 289 rawParams := json.RawMessage(bytes) 290 291 r.Parameters = &rawParams 292 case "max_file_size": 293 params := RuleMaxFileSizeParameters{} 294 if err := json.Unmarshal(*RepositoryRule.Parameters, ¶ms); err != nil { 295 return err 296 } 297 bytes, _ := json.Marshal(params) 298 rawParams := json.RawMessage(bytes) 299 300 r.Parameters = &rawParams 301 default: 302 r.Type = "" 303 r.Parameters = nil 304 return fmt.Errorf("RepositoryRule.Type %q is not yet implemented, unable to unmarshal (%#v)", RepositoryRule.Type, RepositoryRule) 305 } 306 307 return nil 308 } 309 310 // NewMergeQueueRule creates a rule to only allow merges via a merge queue. 311 func NewMergeQueueRule(params *MergeQueueRuleParameters) (rule *RepositoryRule) { 312 if params != nil { 313 bytes, _ := json.Marshal(params) 314 315 rawParams := json.RawMessage(bytes) 316 317 return &RepositoryRule{ 318 Type: "merge_queue", 319 Parameters: &rawParams, 320 } 321 } 322 return &RepositoryRule{ 323 Type: "merge_queue", 324 } 325 } 326 327 // NewCreationRule creates a rule to only allow users with bypass permission to create matching refs. 328 func NewCreationRule() (rule *RepositoryRule) { 329 return &RepositoryRule{ 330 Type: "creation", 331 } 332 } 333 334 // NewUpdateRule creates a rule to only allow users with bypass permission to update matching refs. 335 func NewUpdateRule(params *UpdateAllowsFetchAndMergeRuleParameters) (rule *RepositoryRule) { 336 if params != nil { 337 bytes, _ := json.Marshal(params) 338 339 rawParams := json.RawMessage(bytes) 340 341 return &RepositoryRule{ 342 Type: "update", 343 Parameters: &rawParams, 344 } 345 } 346 return &RepositoryRule{ 347 Type: "update", 348 } 349 } 350 351 // NewDeletionRule creates a rule to only allow users with bypass permissions to delete matching refs. 352 func NewDeletionRule() (rule *RepositoryRule) { 353 return &RepositoryRule{ 354 Type: "deletion", 355 } 356 } 357 358 // NewRequiredLinearHistoryRule creates a rule to prevent merge commits from being pushed to matching branches. 359 func NewRequiredLinearHistoryRule() (rule *RepositoryRule) { 360 return &RepositoryRule{ 361 Type: "required_linear_history", 362 } 363 } 364 365 // NewRequiredDeploymentsRule creates a rule to require environments to be successfully deployed before they can be merged into the matching branches. 366 func NewRequiredDeploymentsRule(params *RequiredDeploymentEnvironmentsRuleParameters) (rule *RepositoryRule) { 367 bytes, _ := json.Marshal(params) 368 369 rawParams := json.RawMessage(bytes) 370 371 return &RepositoryRule{ 372 Type: "required_deployments", 373 Parameters: &rawParams, 374 } 375 } 376 377 // NewRequiredSignaturesRule creates a rule a to require commits pushed to matching branches to have verified signatures. 378 func NewRequiredSignaturesRule() (rule *RepositoryRule) { 379 return &RepositoryRule{ 380 Type: "required_signatures", 381 } 382 } 383 384 // 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. 385 func NewPullRequestRule(params *PullRequestRuleParameters) (rule *RepositoryRule) { 386 bytes, _ := json.Marshal(params) 387 388 rawParams := json.RawMessage(bytes) 389 390 return &RepositoryRule{ 391 Type: "pull_request", 392 Parameters: &rawParams, 393 } 394 } 395 396 // NewRequiredStatusChecksRule creates a rule to require which status checks must pass before branches can be merged into a branch rule. 397 func NewRequiredStatusChecksRule(params *RequiredStatusChecksRuleParameters) (rule *RepositoryRule) { 398 bytes, _ := json.Marshal(params) 399 400 rawParams := json.RawMessage(bytes) 401 402 return &RepositoryRule{ 403 Type: "required_status_checks", 404 Parameters: &rawParams, 405 } 406 } 407 408 // NewNonFastForwardRule creates a rule as part to prevent users with push access from force pushing to matching branches. 409 func NewNonFastForwardRule() (rule *RepositoryRule) { 410 return &RepositoryRule{ 411 Type: "non_fast_forward", 412 } 413 } 414 415 // NewCommitMessagePatternRule creates a rule to restrict commit message patterns being pushed to matching branches. 416 func NewCommitMessagePatternRule(params *RulePatternParameters) (rule *RepositoryRule) { 417 bytes, _ := json.Marshal(params) 418 419 rawParams := json.RawMessage(bytes) 420 421 return &RepositoryRule{ 422 Type: "commit_message_pattern", 423 Parameters: &rawParams, 424 } 425 } 426 427 // NewCommitAuthorEmailPatternRule creates a rule to restrict commits with author email patterns being merged into matching branches. 428 func NewCommitAuthorEmailPatternRule(params *RulePatternParameters) (rule *RepositoryRule) { 429 bytes, _ := json.Marshal(params) 430 431 rawParams := json.RawMessage(bytes) 432 433 return &RepositoryRule{ 434 Type: "commit_author_email_pattern", 435 Parameters: &rawParams, 436 } 437 } 438 439 // NewCommitterEmailPatternRule creates a rule to restrict commits with committer email patterns being merged into matching branches. 440 func NewCommitterEmailPatternRule(params *RulePatternParameters) (rule *RepositoryRule) { 441 bytes, _ := json.Marshal(params) 442 443 rawParams := json.RawMessage(bytes) 444 445 return &RepositoryRule{ 446 Type: "committer_email_pattern", 447 Parameters: &rawParams, 448 } 449 } 450 451 // NewBranchNamePatternRule creates a rule to restrict branch patterns from being merged into matching branches. 452 func NewBranchNamePatternRule(params *RulePatternParameters) (rule *RepositoryRule) { 453 bytes, _ := json.Marshal(params) 454 455 rawParams := json.RawMessage(bytes) 456 457 return &RepositoryRule{ 458 Type: "branch_name_pattern", 459 Parameters: &rawParams, 460 } 461 } 462 463 // NewTagNamePatternRule creates a rule to restrict tag patterns contained in non-target branches from being merged into matching branches. 464 func NewTagNamePatternRule(params *RulePatternParameters) (rule *RepositoryRule) { 465 bytes, _ := json.Marshal(params) 466 467 rawParams := json.RawMessage(bytes) 468 469 return &RepositoryRule{ 470 Type: "tag_name_pattern", 471 Parameters: &rawParams, 472 } 473 } 474 475 // NewRequiredWorkflowsRule creates a rule to require which status checks must pass before branches can be merged into a branch rule. 476 func NewRequiredWorkflowsRule(params *RequiredWorkflowsRuleParameters) (rule *RepositoryRule) { 477 bytes, _ := json.Marshal(params) 478 479 rawParams := json.RawMessage(bytes) 480 481 return &RepositoryRule{ 482 Type: "workflows", 483 Parameters: &rawParams, 484 } 485 } 486 487 // NewFilePathRestrictionRule creates a rule to restrict file paths from being pushed to. 488 func NewFilePathRestrictionRule(params *RuleFileParameters) (rule *RepositoryRule) { 489 bytes, _ := json.Marshal(params) 490 491 rawParams := json.RawMessage(bytes) 492 493 return &RepositoryRule{ 494 Type: "file_path_restriction", 495 Parameters: &rawParams, 496 } 497 } 498 499 // NewMaxFilePathLengthRule creates a rule to restrict file paths longer than the limit from being pushed. 500 func NewMaxFilePathLengthRule(params *RuleMaxFilePathLengthParameters) (rule *RepositoryRule) { 501 bytes, _ := json.Marshal(params) 502 503 rawParams := json.RawMessage(bytes) 504 505 return &RepositoryRule{ 506 Type: "max_file_path_length", 507 Parameters: &rawParams, 508 } 509 } 510 511 // NewFileExtensionRestrictionRule creates a rule to restrict file extensions from being pushed to a commit. 512 func NewFileExtensionRestrictionRule(params *RuleFileExtensionRestrictionParameters) (rule *RepositoryRule) { 513 bytes, _ := json.Marshal(params) 514 515 rawParams := json.RawMessage(bytes) 516 517 return &RepositoryRule{ 518 Type: "file_extension_restriction", 519 Parameters: &rawParams, 520 } 521 } 522 523 // NewMaxFileSizeRule creates a rule to restrict file sizes from being pushed to a commit. 524 func NewMaxFileSizeRule(params *RuleMaxFileSizeParameters) (rule *RepositoryRule) { 525 bytes, _ := json.Marshal(params) 526 527 rawParams := json.RawMessage(bytes) 528 529 return &RepositoryRule{ 530 Type: "max_file_size", 531 Parameters: &rawParams, 532 } 533 } 534 535 // Ruleset represents a GitHub ruleset object. 536 type Ruleset struct { 537 ID *int64 `json:"id,omitempty"` 538 Name string `json:"name"` 539 // Possible values for Target are branch, tag, push 540 Target *string `json:"target,omitempty"` 541 // Possible values for SourceType are: Repository, Organization 542 SourceType *string `json:"source_type,omitempty"` 543 Source string `json:"source"` 544 // Possible values for Enforcement are: disabled, active, evaluate 545 Enforcement string `json:"enforcement"` 546 BypassActors []*BypassActor `json:"bypass_actors,omitempty"` 547 NodeID *string `json:"node_id,omitempty"` 548 Links *RulesetLinks `json:"_links,omitempty"` 549 Conditions *RulesetConditions `json:"conditions,omitempty"` 550 Rules []*RepositoryRule `json:"rules,omitempty"` 551 } 552 553 // rulesetNoOmitBypassActors represents a GitHub ruleset object. The struct does not omit bypassActors if the field is nil or an empty array is passed. 554 type rulesetNoOmitBypassActors struct { 555 ID *int64 `json:"id,omitempty"` 556 Name string `json:"name"` 557 // Possible values for Target are branch, tag 558 Target *string `json:"target,omitempty"` 559 // Possible values for SourceType are: Repository, Organization 560 SourceType *string `json:"source_type,omitempty"` 561 Source string `json:"source"` 562 // Possible values for Enforcement are: disabled, active, evaluate 563 Enforcement string `json:"enforcement"` 564 BypassActors []*BypassActor `json:"bypass_actors"` 565 NodeID *string `json:"node_id,omitempty"` 566 Links *RulesetLinks `json:"_links,omitempty"` 567 Conditions *RulesetConditions `json:"conditions,omitempty"` 568 Rules []*RepositoryRule `json:"rules,omitempty"` 569 } 570 571 // GetRulesForBranch gets all the rules that apply to the specified branch. 572 // 573 // GitHub API docs: https://docs.github.com/rest/repos/rules#get-rules-for-a-branch 574 // 575 //meta:operation GET /repos/{owner}/{repo}/rules/branches/{branch} 576 func (s *RepositoriesService) GetRulesForBranch(ctx context.Context, owner, repo, branch string) ([]*RepositoryRule, *Response, error) { 577 u := fmt.Sprintf("repos/%v/%v/rules/branches/%v", owner, repo, branch) 578 579 req, err := s.client.NewRequest("GET", u, nil) 580 if err != nil { 581 return nil, nil, err 582 } 583 584 var rules []*RepositoryRule 585 resp, err := s.client.Do(ctx, req, &rules) 586 if err != nil { 587 return nil, resp, err 588 } 589 590 return rules, resp, nil 591 } 592 593 // GetAllRulesets gets all the rules that apply to the specified repository. 594 // If includesParents is true, rulesets configured at the organization level that apply to the repository will be returned. 595 // 596 // GitHub API docs: https://docs.github.com/rest/repos/rules#get-all-repository-rulesets 597 // 598 //meta:operation GET /repos/{owner}/{repo}/rulesets 599 func (s *RepositoriesService) GetAllRulesets(ctx context.Context, owner, repo string, includesParents bool) ([]*Ruleset, *Response, error) { 600 u := fmt.Sprintf("repos/%v/%v/rulesets?includes_parents=%v", owner, repo, includesParents) 601 602 req, err := s.client.NewRequest("GET", u, nil) 603 if err != nil { 604 return nil, nil, err 605 } 606 607 var ruleset []*Ruleset 608 resp, err := s.client.Do(ctx, req, &ruleset) 609 if err != nil { 610 return nil, resp, err 611 } 612 613 return ruleset, resp, nil 614 } 615 616 // CreateRuleset creates a ruleset for the specified repository. 617 // 618 // GitHub API docs: https://docs.github.com/rest/repos/rules#create-a-repository-ruleset 619 // 620 //meta:operation POST /repos/{owner}/{repo}/rulesets 621 func (s *RepositoriesService) CreateRuleset(ctx context.Context, owner, repo string, rs *Ruleset) (*Ruleset, *Response, error) { 622 u := fmt.Sprintf("repos/%v/%v/rulesets", owner, repo) 623 624 req, err := s.client.NewRequest("POST", u, rs) 625 if err != nil { 626 return nil, nil, err 627 } 628 629 var ruleset *Ruleset 630 resp, err := s.client.Do(ctx, req, &ruleset) 631 if err != nil { 632 return nil, resp, err 633 } 634 635 return ruleset, resp, nil 636 } 637 638 // GetRuleset gets a ruleset for the specified repository. 639 // If includesParents is true, rulesets configured at the organization level that apply to the repository will be returned. 640 // 641 // GitHub API docs: https://docs.github.com/rest/repos/rules#get-a-repository-ruleset 642 // 643 //meta:operation GET /repos/{owner}/{repo}/rulesets/{ruleset_id} 644 func (s *RepositoriesService) GetRuleset(ctx context.Context, owner, repo string, rulesetID int64, includesParents bool) (*Ruleset, *Response, error) { 645 u := fmt.Sprintf("repos/%v/%v/rulesets/%v?includes_parents=%v", owner, repo, rulesetID, includesParents) 646 647 req, err := s.client.NewRequest("GET", u, nil) 648 if err != nil { 649 return nil, nil, err 650 } 651 652 var ruleset *Ruleset 653 resp, err := s.client.Do(ctx, req, &ruleset) 654 if err != nil { 655 return nil, resp, err 656 } 657 658 return ruleset, resp, nil 659 } 660 661 // UpdateRuleset updates a ruleset for the specified repository. 662 // 663 // GitHub API docs: https://docs.github.com/rest/repos/rules#update-a-repository-ruleset 664 // 665 //meta:operation PUT /repos/{owner}/{repo}/rulesets/{ruleset_id} 666 func (s *RepositoriesService) UpdateRuleset(ctx context.Context, owner, repo string, rulesetID int64, rs *Ruleset) (*Ruleset, *Response, error) { 667 u := fmt.Sprintf("repos/%v/%v/rulesets/%v", owner, repo, rulesetID) 668 669 req, err := s.client.NewRequest("PUT", u, rs) 670 if err != nil { 671 return nil, nil, err 672 } 673 674 var ruleset *Ruleset 675 resp, err := s.client.Do(ctx, req, &ruleset) 676 if err != nil { 677 return nil, resp, err 678 } 679 680 return ruleset, resp, nil 681 } 682 683 // UpdateRulesetNoBypassActor updates a ruleset for the specified repository. 684 // 685 // This function is necessary as the UpdateRuleset function does not marshal ByPassActor if passed as nil or an empty array. 686 // 687 // GitHub API docs: https://docs.github.com/rest/repos/rules#update-a-repository-ruleset 688 // 689 //meta:operation PUT /repos/{owner}/{repo}/rulesets/{ruleset_id} 690 func (s *RepositoriesService) UpdateRulesetNoBypassActor(ctx context.Context, owner, repo string, rulesetID int64, rs *Ruleset) (*Ruleset, *Response, error) { 691 u := fmt.Sprintf("repos/%v/%v/rulesets/%v", owner, repo, rulesetID) 692 693 rsNoBypassActor := &rulesetNoOmitBypassActors{} 694 695 if rs != nil { 696 rsNoBypassActor = &rulesetNoOmitBypassActors{ 697 ID: rs.ID, 698 Name: rs.Name, 699 Target: rs.Target, 700 SourceType: rs.SourceType, 701 Source: rs.Source, 702 Enforcement: rs.Enforcement, 703 BypassActors: rs.BypassActors, 704 NodeID: rs.NodeID, 705 Links: rs.Links, 706 Conditions: rs.Conditions, 707 Rules: rs.Rules, 708 } 709 } 710 711 req, err := s.client.NewRequest("PUT", u, rsNoBypassActor) 712 if err != nil { 713 return nil, nil, err 714 } 715 716 var ruleSet *Ruleset 717 resp, err := s.client.Do(ctx, req, &ruleSet) 718 if err != nil { 719 return nil, resp, err 720 } 721 722 return ruleSet, resp, nil 723 } 724 725 // DeleteRuleset deletes a ruleset for the specified repository. 726 // 727 // GitHub API docs: https://docs.github.com/rest/repos/rules#delete-a-repository-ruleset 728 // 729 //meta:operation DELETE /repos/{owner}/{repo}/rulesets/{ruleset_id} 730 func (s *RepositoriesService) DeleteRuleset(ctx context.Context, owner, repo string, rulesetID int64) (*Response, error) { 731 u := fmt.Sprintf("repos/%v/%v/rulesets/%v", owner, repo, rulesetID) 732 733 req, err := s.client.NewRequest("DELETE", u, nil) 734 if err != nil { 735 return nil, err 736 } 737 738 return s.client.Do(ctx, req, nil) 739 }