From 636b6a5e19a69ebfaf5edcc1750ae6acb6a27eb7 Mon Sep 17 00:00:00 2001 From: Ze Peng Date: Thu, 15 Aug 2024 09:37:37 -0600 Subject: [PATCH 1/3] add merge queue parameters to ruleset --- github/orgs_rules_test.go | 12 ++++++------ github/repos_rules.go | 40 ++++++++++++++++++++++++++++++++++++-- github/repos_rules_test.go | 23 ++++++++++++++++++++++ 3 files changed, 67 insertions(+), 8 deletions(-) diff --git a/github/orgs_rules_test.go b/github/orgs_rules_test.go index d45469f27e7..dde336a5580 100644 --- a/github/orgs_rules_test.go +++ b/github/orgs_rules_test.go @@ -241,7 +241,7 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoNames(t *testing.T) UpdateAllowsFetchAndMerge: true, }), NewDeletionRule(), - NewMergeQueueRule(), + NewMergeQueueRule(nil), NewRequiredLinearHistoryRule(), NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{ RequiredDeploymentEnvironments: []string{"test"}, @@ -328,7 +328,7 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoNames(t *testing.T) UpdateAllowsFetchAndMerge: true, }), NewDeletionRule(), - NewMergeQueueRule(), + NewMergeQueueRule(nil), NewRequiredLinearHistoryRule(), NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{ RequiredDeploymentEnvironments: []string{"test"}, @@ -572,7 +572,7 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoProperty(t *testing. UpdateAllowsFetchAndMerge: true, }), NewDeletionRule(), - NewMergeQueueRule(), + NewMergeQueueRule(nil), NewRequiredLinearHistoryRule(), NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{ RequiredDeploymentEnvironments: []string{"test"}, @@ -664,7 +664,7 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoProperty(t *testing. UpdateAllowsFetchAndMerge: true, }), NewDeletionRule(), - NewMergeQueueRule(), + NewMergeQueueRule(nil), NewRequiredLinearHistoryRule(), NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{ RequiredDeploymentEnvironments: []string{"test"}, @@ -893,7 +893,7 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoIDs(t *testing.T) { UpdateAllowsFetchAndMerge: true, }), NewDeletionRule(), - NewMergeQueueRule(), + NewMergeQueueRule(nil), NewRequiredLinearHistoryRule(), NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{ RequiredDeploymentEnvironments: []string{"test"}, @@ -978,7 +978,7 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoIDs(t *testing.T) { UpdateAllowsFetchAndMerge: true, }), NewDeletionRule(), - NewMergeQueueRule(), + NewMergeQueueRule(nil), NewRequiredLinearHistoryRule(), NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{ RequiredDeploymentEnvironments: []string{"test"}, diff --git a/github/repos_rules.go b/github/repos_rules.go index 2827f85af1c..36f6c34ab59 100644 --- a/github/repos_rules.go +++ b/github/repos_rules.go @@ -104,6 +104,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 { RequiredStatusChecks []RuleRequiredStatusChecks `json:"required_status_checks"` @@ -147,7 +160,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 { @@ -163,7 +176,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 { @@ -224,7 +250,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 065d3385a3b..58fbd62366e 100644 --- a/github/repos_rules_test.go +++ b/github/repos_rules_test.go @@ -74,6 +74,29 @@ 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, + }), + }, "Valid non_fast_forward": { data: `{"type":"non_fast_forward"}`, want: &RepositoryRule{ From 2c6e794866fb0c4336e4dcf457ecd90fcd01fb2c Mon Sep 17 00:00:00 2001 From: Ze Peng Date: Wed, 28 Aug 2024 14:30:26 -0600 Subject: [PATCH 2/3] remove merge queue from org ruleset test --- github/orgs_rules_test.go | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/github/orgs_rules_test.go b/github/orgs_rules_test.go index dde336a5580..3f2dbd2e6b7 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(nil), NewRequiredLinearHistoryRule(), NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{ RequiredDeploymentEnvironments: []string{"test"}, @@ -328,7 +324,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoNames(t *testing.T) UpdateAllowsFetchAndMerge: true, }), NewDeletionRule(), - NewMergeQueueRule(nil), 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" }, @@ -572,7 +564,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoProperty(t *testing. UpdateAllowsFetchAndMerge: true, }), NewDeletionRule(), - NewMergeQueueRule(nil), NewRequiredLinearHistoryRule(), NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{ RequiredDeploymentEnvironments: []string{"test"}, @@ -664,7 +655,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoProperty(t *testing. UpdateAllowsFetchAndMerge: true, }), NewDeletionRule(), - NewMergeQueueRule(nil), NewRequiredLinearHistoryRule(), NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{ RequiredDeploymentEnvironments: []string{"test"}, @@ -777,9 +767,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoIDs(t *testing.T) { { "type": "deletion" }, - { - "type": "merge_queue" - }, { "type": "required_linear_history" }, @@ -893,7 +880,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoIDs(t *testing.T) { UpdateAllowsFetchAndMerge: true, }), NewDeletionRule(), - NewMergeQueueRule(nil), NewRequiredLinearHistoryRule(), NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{ RequiredDeploymentEnvironments: []string{"test"}, @@ -978,7 +964,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoIDs(t *testing.T) { UpdateAllowsFetchAndMerge: true, }), NewDeletionRule(), - NewMergeQueueRule(nil), NewRequiredLinearHistoryRule(), NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{ RequiredDeploymentEnvironments: []string{"test"}, From 1cf99cbf365f2a9703acf4be1beeec6ccd0c1a70 Mon Sep 17 00:00:00 2001 From: Ze Peng Date: Wed, 28 Aug 2024 14:57:42 -0600 Subject: [PATCH 3/3] add a test with incorrect parameter type --- github/repos_rules_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/github/repos_rules_test.go b/github/repos_rules_test.go index 58fbd62366e..177819437d6 100644 --- a/github/repos_rules_test.go +++ b/github/repos_rules_test.go @@ -97,6 +97,25 @@ func TestRepositoryRule_UnmarshalJSON(t *testing.T) { 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{