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

fix: when branch name is invalid, report a error message #328

Merged
merged 6 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions controllers/jenkins/pipelinerun/pipelinerun_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"fmt"
"reflect"
"strings"
"time"

"github.com/go-logr/logr"
Expand All @@ -29,6 +30,7 @@ import (
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/retry"
"k8s.io/klog"
Expand Down Expand Up @@ -243,6 +245,9 @@ func getSCMRefName(prSpec *v1alpha3.PipelineRunSpec) (string, error) {
if prSpec.SCM == nil || prSpec.SCM.RefName == "" {
return "", fmt.Errorf("failed to obtain SCM reference name for multi-branch Pipeline")
}
if errs := validation.IsValidLabelValue(prSpec.SCM.RefName); len(errs) != 0 {
return "", fmt.Errorf(strings.Join(errs, "; "))
}
branch = prSpec.SCM.RefName
}
return branch, nil
Expand Down
14 changes: 14 additions & 0 deletions controllers/jenkins/pipelinerun/pipelinerun_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ func Test_getBranch(t *testing.T) {
},
},
want: "main",
}, {
name: "Multi-branch Pipeline and SCM set, but the name is invalid",
args: args{
prSpec: &v1alpha3.PipelineRunSpec{
PipelineSpec: &v1alpha3.PipelineSpec{
Type: v1alpha3.MultiBranchPipelineType,
},
SCM: &v1alpha3.SCM{
RefName: "测试分支",
RefType: "branch",
},
},
},
wantErr: true,
},
}
for _, tt := range tests {
Expand Down
5 changes: 5 additions & 0 deletions pkg/kapis/devops/v1alpha3/pipelinerun/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package pipelinerun
import (
"errors"
"fmt"
"strings"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/util/validation"
"kubesphere.io/devops/pkg/api/devops/v1alpha3"
"kubesphere.io/devops/pkg/apiserver/query"
"kubesphere.io/devops/pkg/client/devops"
Expand All @@ -23,6 +25,9 @@ func buildLabelSelector(queryParam *query.Query, pipelineName, branchName string
}
labelSelector = labelSelector.Add(*rq)
if branchName != "" {
if errs := validation.IsValidLabelValue(branchName); len(errs) != 0 {
return nil, fmt.Errorf(strings.Join(errs, "; "))
}
rq, err = labels.NewRequirement(v1alpha3.SCMRefNameLabelKey, selection.Equals, []string{branchName})
if err != nil {
// should never happen
Expand Down
8 changes: 8 additions & 0 deletions pkg/kapis/devops/v1alpha3/pipelinerun/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ func Test_buildLabelSelector(t *testing.T) {
branchName: "branchA",
},
want: parseSelector(fmt.Sprintf("%s=pipelineA,%s=branchA,a=b", v1alpha3.PipelineNameLabelKey, v1alpha3.SCMRefNameLabelKey)),
}, {
name: "No label selector was provided",
args: args{
queryParam: &query.Query{},
pipelineName: "pipelineA",
branchName: "分支A",
},
wantErr: true,
},
}
for _, tt := range tests {
Expand Down