diff --git a/github/orgs_rules_test.go b/github/orgs_rules_test.go index 13d4c76da0c..435e7e5827b 100644 --- a/github/orgs_rules_test.go +++ b/github/orgs_rules_test.go @@ -123,9 +123,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoNames(t *testing.T) { "type": "deletion" }, - { - "type": "merge_queue" - }, { "type": "required_linear_history" }, @@ -241,7 +238,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoNames(t *testing.T) UpdateAllowsFetchAndMerge: true, }), NewDeletionRule(), - NewMergeQueueRule(), NewRequiredLinearHistoryRule(), NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{ RequiredDeploymentEnvironments: []string{"test"}, @@ -328,7 +324,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoNames(t *testing.T) UpdateAllowsFetchAndMerge: true, }), NewDeletionRule(), - NewMergeQueueRule(), NewRequiredLinearHistoryRule(), NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{ RequiredDeploymentEnvironments: []string{"test"}, @@ -449,9 +444,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoProperty(t *testing. { "type": "deletion" }, - { - "type": "merge_queue" - }, { "type": "required_linear_history" }, @@ -574,7 +566,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoProperty(t *testing. UpdateAllowsFetchAndMerge: true, }), NewDeletionRule(), - NewMergeQueueRule(), NewRequiredLinearHistoryRule(), NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{ RequiredDeploymentEnvironments: []string{"test"}, @@ -668,7 +659,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoProperty(t *testing. UpdateAllowsFetchAndMerge: true, }), NewDeletionRule(), - NewMergeQueueRule(), NewRequiredLinearHistoryRule(), NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{ RequiredDeploymentEnvironments: []string{"test"}, @@ -781,9 +771,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoIDs(t *testing.T) { { "type": "deletion" }, - { - "type": "merge_queue" - }, { "type": "required_linear_history" }, @@ -897,7 +884,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoIDs(t *testing.T) { UpdateAllowsFetchAndMerge: true, }), NewDeletionRule(), - NewMergeQueueRule(), NewRequiredLinearHistoryRule(), NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{ RequiredDeploymentEnvironments: []string{"test"}, @@ -982,7 +968,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoIDs(t *testing.T) { UpdateAllowsFetchAndMerge: true, }), NewDeletionRule(), - NewMergeQueueRule(), NewRequiredLinearHistoryRule(), NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{ RequiredDeploymentEnvironments: []string{"test"}, diff --git a/github/repos_rules.go b/github/repos_rules.go index 3c6e8f01c92..a90555cc442 100644 --- a/github/repos_rules.go +++ b/github/repos_rules.go @@ -110,6 +110,19 @@ type RuleRequiredStatusChecks struct { IntegrationID *int64 `json:"integration_id,omitempty"` } +// MergeQueueRuleParameters represents the merge_queue rule parameters. +type MergeQueueRuleParameters struct { + CheckResponseTimeoutMinutes int `json:"check_response_timeout_minutes"` + // Possible values for GroupingStrategy are: ALLGREEN, HEADGREEN + GroupingStrategy string `json:"grouping_strategy"` + MaxEntriesToBuild int `json:"max_entries_to_build"` + MaxEntriesToMerge int `json:"max_entries_to_merge"` + // Possible values for MergeMethod are: MERGE, SQUASH, REBASE + MergeMethod string `json:"merge_method"` + MinEntriesToMerge int `json:"min_entries_to_merge"` + MinEntriesToMergeWaitMinutes int `json:"min_entries_to_merge_wait_minutes"` +} + // RequiredStatusChecksRuleParameters represents the required_status_checks rule parameters. type RequiredStatusChecksRuleParameters struct { DoNotEnforceOnCreate bool `json:"do_not_enforce_on_create"` @@ -154,7 +167,7 @@ func (r *RepositoryRule) UnmarshalJSON(data []byte) error { r.Type = RepositoryRule.Type switch RepositoryRule.Type { - case "creation", "deletion", "merge_queue", "non_fast_forward", "required_linear_history", "required_signatures": + case "creation", "deletion", "non_fast_forward", "required_linear_history", "required_signatures": r.Parameters = nil case "update": if RepositoryRule.Parameters == nil { @@ -170,7 +183,20 @@ func (r *RepositoryRule) UnmarshalJSON(data []byte) error { rawParams := json.RawMessage(bytes) r.Parameters = &rawParams + case "merge_queue": + if RepositoryRule.Parameters == nil { + r.Parameters = nil + return nil + } + params := MergeQueueRuleParameters{} + if err := json.Unmarshal(*RepositoryRule.Parameters, ¶ms); err != nil { + return err + } + + bytes, _ := json.Marshal(params) + rawParams := json.RawMessage(bytes) + r.Parameters = &rawParams case "required_deployments": params := RequiredDeploymentEnvironmentsRuleParameters{} if err := json.Unmarshal(*RepositoryRule.Parameters, ¶ms); err != nil { @@ -240,7 +266,17 @@ func (r *RepositoryRule) UnmarshalJSON(data []byte) error { } // NewMergeQueueRule creates a rule to only allow merges via a merge queue. -func NewMergeQueueRule() (rule *RepositoryRule) { +func NewMergeQueueRule(params *MergeQueueRuleParameters) (rule *RepositoryRule) { + if params != nil { + bytes, _ := json.Marshal(params) + + rawParams := json.RawMessage(bytes) + + return &RepositoryRule{ + Type: "merge_queue", + Parameters: &rawParams, + } + } return &RepositoryRule{ Type: "merge_queue", } diff --git a/github/repos_rules_test.go b/github/repos_rules_test.go index 92f7259d0c8..8e123ed0f8e 100644 --- a/github/repos_rules_test.go +++ b/github/repos_rules_test.go @@ -74,6 +74,48 @@ func TestRepositoryRule_UnmarshalJSON(t *testing.T) { Parameters: nil, }, }, + "Valid merge_queue with params": { + data: `{ + "type":"merge_queue", + "parameters":{ + "check_response_timeout_minutes": 35, + "grouping_strategy": "HEADGREEN", + "max_entries_to_build": 8, + "max_entries_to_merge": 4, + "merge_method": "SQUASH", + "min_entries_to_merge": 2, + "min_entries_to_merge_wait_minutes": 13 + } + }`, + want: NewMergeQueueRule(&MergeQueueRuleParameters{ + CheckResponseTimeoutMinutes: 35, + GroupingStrategy: "HEADGREEN", + MaxEntriesToBuild: 8, + MaxEntriesToMerge: 4, + MergeMethod: "SQUASH", + MinEntriesToMerge: 2, + MinEntriesToMergeWaitMinutes: 13, + }), + }, + "Invalid merge_queue with params": { + data: `{ + "type":"merge_queue", + "parameters":{ + "check_response_timeout_minutes": "35", + "grouping_strategy": "HEADGREEN", + "max_entries_to_build": "8", + "max_entries_to_merge": "4", + "merge_method": "SQUASH", + "min_entries_to_merge": "2", + "min_entries_to_merge_wait_minutes": "13" + } + }`, + want: &RepositoryRule{ + Type: "merge_queue", + Parameters: nil, + }, + wantErr: true, + }, "Valid non_fast_forward": { data: `{"type":"non_fast_forward"}`, want: &RepositoryRule{