mirror of
https://github.com/kubevela/kubevela.git
synced 2026-08-18 03:56:36 +00:00
ExtractRevision parsed the last hyphen segment as the revision number
without validating the name shape, so a bareword like "5" returned
5,nil and any name lacking a trailing v-prefixed segment silently
mis-parsed instead of erroring. Mirror the existing ExtractRevisionNum
guards: error with ErrBadRevision when there is no delimiter or the
last segment is not v-prefixed. Extend the colocated bad-name test
cases accordingly.
(cherry picked from commit 2f401979c4)
Signed-off-by: Anas Khan <83116240+anxkhn@users.noreply.github.com>
Co-authored-by: Anas Khan <anxkhn28@gmail.com>
This commit is contained in:
co-authored by
Anas Khan
parent
8ee9eb2ebd
commit
27806c09ba
@@ -17,6 +17,7 @@ limitations under the License.
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -53,6 +54,14 @@ var ExtractComponentName = util.ExtractComponentName
|
||||
// ExtractRevision will extract the revision from a revisionName
|
||||
func ExtractRevision(revisionName string) (int, error) {
|
||||
splits := strings.Split(revisionName, "-")
|
||||
// check some bad revision name, eg: 5
|
||||
if len(splits) == 1 {
|
||||
return 0, errors.New(util.ErrBadRevision)
|
||||
}
|
||||
// check some bad revision name, eg: myapp-a1
|
||||
if !strings.HasPrefix(splits[len(splits)-1], "v") {
|
||||
return 0, errors.New(util.ErrBadRevision)
|
||||
}
|
||||
// the revision is the last string without the prefix "v"
|
||||
return strconv.Atoi(strings.TrimPrefix(splits[len(splits)-1], "v"))
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ func TestConstructExtract(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
badRevision := []string{"xx", "yy-", "zz-0.1"}
|
||||
badRevision := []string{"xx", "yy-", "zz-0.1", "5", "myapp-a1"}
|
||||
t.Run(fmt.Sprintf("tests %s for extractRevision", badRevision), func(t *testing.T) {
|
||||
for _, revisionName := range badRevision {
|
||||
_, err := ExtractRevision(revisionName)
|
||||
|
||||
Reference in New Issue
Block a user