Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Add merge queue parameters to repository ruleset #3253

Merged
merged 4 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions github/orgs_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoNames(t *testing.T)
{
"type": "deletion"
},
{
"type": "merge_queue"
},
{
"type": "required_linear_history"
},
Expand Down Expand Up @@ -241,7 +238,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoNames(t *testing.T)
UpdateAllowsFetchAndMerge: true,
}),
NewDeletionRule(),
NewMergeQueueRule(),
NewRequiredLinearHistoryRule(),
NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{
RequiredDeploymentEnvironments: []string{"test"},
Expand Down Expand Up @@ -328,7 +324,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoNames(t *testing.T)
UpdateAllowsFetchAndMerge: true,
}),
NewDeletionRule(),
NewMergeQueueRule(),
NewRequiredLinearHistoryRule(),
NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{
RequiredDeploymentEnvironments: []string{"test"},
Expand Down Expand Up @@ -449,9 +444,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoProperty(t *testing.
{
"type": "deletion"
},
{
"type": "merge_queue"
},
{
"type": "required_linear_history"
},
Expand Down Expand Up @@ -574,7 +566,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoProperty(t *testing.
UpdateAllowsFetchAndMerge: true,
}),
NewDeletionRule(),
NewMergeQueueRule(),
NewRequiredLinearHistoryRule(),
NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{
RequiredDeploymentEnvironments: []string{"test"},
Expand Down Expand Up @@ -668,7 +659,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoProperty(t *testing.
UpdateAllowsFetchAndMerge: true,
}),
NewDeletionRule(),
NewMergeQueueRule(),
NewRequiredLinearHistoryRule(),
NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{
RequiredDeploymentEnvironments: []string{"test"},
Expand Down Expand Up @@ -781,9 +771,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoIDs(t *testing.T) {
{
"type": "deletion"
},
{
"type": "merge_queue"
},
{
"type": "required_linear_history"
},
Expand Down Expand Up @@ -897,7 +884,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoIDs(t *testing.T) {
UpdateAllowsFetchAndMerge: true,
}),
NewDeletionRule(),
NewMergeQueueRule(),
NewRequiredLinearHistoryRule(),
NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{
RequiredDeploymentEnvironments: []string{"test"},
Expand Down Expand Up @@ -982,7 +968,6 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoIDs(t *testing.T) {
UpdateAllowsFetchAndMerge: true,
}),
NewDeletionRule(),
NewMergeQueueRule(),
NewRequiredLinearHistoryRule(),
NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{
RequiredDeploymentEnvironments: []string{"test"},
Expand Down
40 changes: 38 additions & 2 deletions github/repos_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ type RuleRequiredStatusChecks struct {
IntegrationID *int64 `json:"integration_id,omitempty"`
}

// MergeQueueRuleParameters represents the merge_queue rule parameters.
type MergeQueueRuleParameters struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zepeng811 - should any of these parameters be optional (and therefore pointers with the omitempty JSON tag added)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gmlewis did a quick test and seems like it must be all or nothing

adding all parameters like this worked:

curl -L \
  -X POST \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer $GITHUB_TOKEN" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  https://api.github.com/repos/$ORG/$REPO/rulesets \
  -d '{"name":"merge queue test ruleset","target":"branch","enforcement":"active","conditions":{"ref_name":{"include":["refs/heads/main","refs/heads/master"],"exclude":["refs/heads/dev*"]}},"rules":[{"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}}]}'

but if any parameter was removed, like:

curl -L \
  -X POST \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer $GITHUB_TOKEN" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  https://api.github.com/repos/$ORG/$REPO/rulesets \
  -d '{"name":"merge queue test ruleset 2","target":"branch","enforcement":"active","conditions":{"ref_name":{"include":["refs/heads/main","refs/heads/master"],"exclude":["refs/heads/dev*"]}},"rules":[{"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}}]}'

then the API will error:

{
  "message": "Invalid request.\n\nInvalid property /rules/0: data matches no possible input. See `documentation_url`.",
  "documentation_url": "https://docs.github.com/rest/repos/rules#create-a-repository-ruleset",
  "status": "422"
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks for testing!

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"`
Expand Down Expand Up @@ -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 {
Expand All @@ -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, &params); 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, &params); err != nil {
Expand Down Expand Up @@ -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",
}
Expand Down
42 changes: 42 additions & 0 deletions github/repos_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Loading