diff --git a/applicationset/generators/pull_request.go b/applicationset/generators/pull_request.go index 287a00be1ae81..9bb7f162e314f 100644 --- a/applicationset/generators/pull_request.go +++ b/applicationset/generators/pull_request.go @@ -7,6 +7,7 @@ import ( "time" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/util/validation" "sigs.k8s.io/controller-runtime/pkg/client" "github.com/gosimple/slug" @@ -73,10 +74,10 @@ func (g *PullRequestGenerator) GenerateParams(appSetGenerator *argoprojiov1alpha params := make([]map[string]interface{}, 0, len(pulls)) // In order to follow the DNS label standard as defined in RFC 1123, - // we need to limit the 'branch' to 50 to give room to append/suffix-ing it - // with 13 more characters. Also, there is the need to clean it as recommended + // we need to limit the 'branch' to 63. + // Also, there is the need to clean it as recommended // here https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-label-names - slug.MaxLength = 50 + slug.MaxLength = 63 // Converting underscores to dashes slug.CustomSub = map[string]string{ @@ -90,10 +91,16 @@ func (g *PullRequestGenerator) GenerateParams(appSetGenerator *argoprojiov1alpha shortSHALength = len(pull.HeadSHA) } + branchSlug := slug.Make(pull.Branch) + errors := validation.IsDNS1123Label(branchSlug) + if len(errors) > 0 { + return nil, fmt.Errorf("invalid branch slug: %v for branch %v: %v", branchSlug, pull.Branch, errors) + } + params = append(params, map[string]interface{}{ "number": strconv.Itoa(pull.Number), "branch": pull.Branch, - "branch_slug": slug.Make(pull.Branch), + "branch_slug": branchSlug, "head_sha": pull.HeadSHA, "head_short_sha": pull.HeadSHA[:shortSHALength], }) diff --git a/applicationset/generators/pull_request_test.go b/applicationset/generators/pull_request_test.go index ef969376d9719..82040d8b3377a 100644 --- a/applicationset/generators/pull_request_test.go +++ b/applicationset/generators/pull_request_test.go @@ -64,13 +64,38 @@ func TestPullRequestGithubGenerateParams(t *testing.T) { { "number": "2", "branch": "feat/areally+long_pull_request_name_to_test_argo_slugification_and_branch_name_shortening_feature", - "branch_slug": "feat-areally-long-pull-request-name-to-test-argo", + "branch_slug": "feat-areally-long-pull-request-name-to-test-argo-slugification", "head_sha": "9b34ff5bd418e57d58891eb0aa0728043ca1e8be", "head_short_sha": "9b34ff5b", }, }, expectedErr: nil, }, + { + selectFunc: func(context.Context, *argoprojiov1alpha1.PullRequestGenerator, *argoprojiov1alpha1.ApplicationSet) (pullrequest.PullRequestService, error) { + return pullrequest.NewFakeService( + ctx, + []*pullrequest.PullRequest{ + &pullrequest.PullRequest{ + Number: 2, + Branch: "abdominoposterior-pneumonoultramicroscopicsilicovolcanoconiosis-longest-branch-name", + HeadSHA: "09ae444a85e105b34abd0f15dbde72938580f970", + }, + }, + nil, + ) + }, + expected: []map[string]interface{}{ + { + "number": "2", + "branch": "abdominoposterior-pneumonoultramicroscopicsilicovolcanoconiosis-longest-branch-name", + "branch_slug": "abdominoposterior-pneumonoultramicroscopicsilicovolcanoconiosis", + "head_sha": "09ae444a85e105b34abd0f15dbde72938580f970", + "head_short_sha": "09ae444a", + }, + }, + expectedErr: nil, + }, { selectFunc: func(context.Context, *argoprojiov1alpha1.PullRequestGenerator, *argoprojiov1alpha1.ApplicationSet) (pullrequest.PullRequestService, error) { return pullrequest.NewFakeService( diff --git a/docs/operator-manual/applicationset/Generators-Pull-Request.md b/docs/operator-manual/applicationset/Generators-Pull-Request.md index de5866aaf00a8..924c8ef631204 100644 --- a/docs/operator-manual/applicationset/Generators-Pull-Request.md +++ b/docs/operator-manual/applicationset/Generators-Pull-Request.md @@ -268,7 +268,7 @@ spec: * `number`: The ID number of the pull request. * `branch`: The name of the branch of the pull request head. -* `branch_slug`: The branch name will be cleaned to be conform to the DNS label standard as defined in [RFC 1123](https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-label-names), and truncated to 50 characters to give room to append/suffix-ing it with 13 more characters. +* `branch_slug`: The branch name will be cleaned to be conform to the DNS label standard as defined in [RFC 1123](https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-label-names). * `head_sha`: This is the SHA of the head of the pull request. * `head_short_sha`: This is the short SHA of the head of the pull request (8 characters long or the length of the head SHA if it's shorter).