go-github icon indicating copy to clipboard operation
go-github copied to clipboard

feat!: Add merge queue parameters to repository ruleset

Open zepeng811 opened this issue 5 months ago • 1 comments

Fixes: #3225.

BREAKING CHANGE: NewMergeQueueRule now takes one parameter: *MergeQueueRuleParameters.

Summary

closes: #3225 (I'm the same author as the assignee of the issue @zhpeng811, but submitting the PR using my corporate account)

Details

This PR adds merge queue parameter configurations to the repository branch ruleset.

an attempt was also made for organization ruleset but was later determined that GitHub currently does not support merge queue configurations at the organizational level. Configs related to merge queue in tests was removed as the result.

Testing

General

Ran the required scripts on step 4 of the CONTRIBUTING.md file

Details

❯ script/fmt.sh
❯ script/test.sh
testing .
?       github.com/google/go-github/v63/test/integration        [no test files]
ok      github.com/google/go-github/v63/github  (cached)        coverage: 99.4% of statements
        github.com/google/go-github/v63/test/fields             coverage: 0.0% of statements
testing example
        github.com/google/go-github/v63/example/actionpermissions               coverage: 0.0% of statements
        github.com/google/go-github/v63/example/basicauth               coverage: 0.0% of statements
        github.com/google/go-github/v63/example/codespaces/newusersecretwithxcrypto             coverage: 0.0% of statements
        github.com/google/go-github/v63/example/codespaces/newreposecretwithxcrypto             coverage: 0.0% of statements
        github.com/google/go-github/v63/example/appengine               coverage: 0.0% of statements
        github.com/google/go-github/v63/example/newfilewithappauth              coverage: 0.0% of statements
        github.com/google/go-github/v63/example/listenvironments                coverage: 0.0% of statements
        github.com/google/go-github/v63/example/commitpr                coverage: 0.0% of statements
        github.com/google/go-github/v63/example/migrations              coverage: 0.0% of statements
        github.com/google/go-github/v63/example/newrepo         coverage: 0.0% of statements
        github.com/google/go-github/v63/example/newreposecretwithxcrypto                coverage: 0.0% of statements
        github.com/google/go-github/v63/example/simple          coverage: 0.0% of statements
        github.com/google/go-github/v63/example/ratelimit               coverage: 0.0% of statements
        github.com/google/go-github/v63/example/tagprotection           coverage: 0.0% of statements
        github.com/google/go-github/v63/example/tokenauth               coverage: 0.0% of statements
        github.com/google/go-github/v63/example/topics          coverage: 0.0% of statements
testing scrape
ok      github.com/google/go-github/scrape      (cached)        coverage: 59.4% of statements
        github.com/google/go-github/scrape/example/scrape               coverage: 0.0% of statements
testing tools
ok      tools/metadata  (cached)        coverage: 81.7% of statements
❯ script/lint.sh
linting .
linting example
linting scrape
linting tools
validating generated files

Repository Ruleset

Manual: Tested with the following test code, both with and without including the specific parameters, and verified in the UI.

Expand to view code

func main() {
	client := github.NewClient(nil).WithAuthToken(os.Getenv("GITHUB_TOKEN"))
	org := "<REPLACE_WITH_YOUR_ORG>"
	repo := "<REPLACE_WITH_YOUR_REPO>"

	ctx := context.Background()
	_, _, err := client.Repositories.CreateRuleset(ctx, org, repo, &github.Ruleset{
		Name:        "merge-queue-test-without-param",
		Enforcement: "active",
		Rules: []*github.RepositoryRule{
			github.NewMergeQueueRule(nil),
		},
	})
	if err != nil {
		fmt.Println(err)
	}
	_, _, err = client.Repositories.CreateRuleset(ctx, org, repo, &github.Ruleset{
		Name:        "merge-queue-test-with-param",
		Enforcement: "active",
		Rules: []*github.RepositoryRule{
			github.NewMergeQueueRule(&github.MergeQueueRuleParameters{
				CheckResponseTimeoutMinutes:  35,
				GroupingStrategy:             "HEADGREEN",
				MaxEntriesToBuild:            8,
				MaxEntriesToMerge:            4,
				MergeMethod:                  "SQUASH",
				MinEntriesToMerge:            2,
				MinEntriesToMergeWaitMinutes: 13,
			}),
		},
	})
	if err != nil {
		fmt.Println(err)
	}
}

Unit Test: A unit test already exist for merge queue without parameters, 2 more tests were added (1 success case and 1 failure case with the incorrect parameter type)

Organization Ruleset

Was trying to create a org ruleset with merge queue rule using the following curl

Expand for curl

export ORG_NAME=<REPLACE_WITH_YOUR_ORG_NAME>

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/orgs/$ORG_NAME/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*"]},"repository_name":{"include":["test1","test2"],"exclude":["test3"],"protected":true}},"rules":[{"type":"merge_queue"}]}'

but got the following error:

{
  "message": "Validation Failed",
  "errors": [
    "Invalid rules: 'Merge queue'"
  ],
  "documentation_url": "https://docs.github.com/rest/orgs/rules#create-an-organization-repository-ruleset",
  "status": "422"
}

I cross referenced with the UI page and did not notice a branch rule for merge queue, which conflicts with the REST API documentation for org rulesets, so seems like GitHub currently does not support merge queue configuration on the org level ruleset. Reported the issue to GitHub community: https://github.com/orgs/community/discussions/137097

zepeng811 avatar Aug 28 '24 21:08 zepeng811