parse.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package clonerefs
  14. import (
  15. "fmt"
  16. "strconv"
  17. "strings"
  18. "k8s.io/test-infra/prow/kube"
  19. )
  20. // ParseRefs parses a human-provided string into the repo
  21. // that should be cloned and the refs that need to be
  22. // checked out once it is. The format is:
  23. // org,repo=base-ref[:base-sha][,pull-id[:pull-sha[:pull-ref]]]...
  24. // For the base ref and pull IDs, a SHA may optionally be
  25. // provided or may be omitted for the latest available SHA.
  26. // Examples:
  27. // kubernetes,test-infra=master
  28. // kubernetes,test-infra=master:abcde12
  29. // kubernetes,test-infra=master:abcde12,34
  30. // kubernetes,test-infra=master:abcde12,34:fghij56
  31. // kubernetes,test-infra=master,34:fghij56
  32. // kubernetes,test-infra=master:abcde12,34:fghij56,78
  33. // gerrit,test-infra=master:abcde12,34:fghij56:refs/changes/00/123/1
  34. func ParseRefs(value string) (*kube.Refs, error) {
  35. gitRef := &kube.Refs{}
  36. values := strings.SplitN(value, "=", 2)
  37. if len(values) != 2 {
  38. return gitRef, fmt.Errorf("refspec %s invalid: does not contain '='", value)
  39. }
  40. info := values[0]
  41. allRefs := values[1]
  42. infoValues := strings.SplitN(info, ",", 2)
  43. if len(infoValues) != 2 {
  44. return gitRef, fmt.Errorf("refspec %s invalid: does not contain 'org,repo' as prefix", value)
  45. }
  46. gitRef.Org = infoValues[0]
  47. gitRef.Repo = infoValues[1]
  48. refValues := strings.Split(allRefs, ",")
  49. if len(refValues) == 1 && refValues[0] == "" {
  50. return gitRef, fmt.Errorf("refspec %s invalid: does not contain any refs", value)
  51. }
  52. baseRefParts := strings.Split(refValues[0], ":")
  53. if len(baseRefParts) != 1 && len(baseRefParts) != 2 {
  54. return gitRef, fmt.Errorf("refspec %s invalid: malformed base ref", refValues[0])
  55. }
  56. gitRef.BaseRef = baseRefParts[0]
  57. if len(baseRefParts) == 2 {
  58. gitRef.BaseSHA = baseRefParts[1]
  59. }
  60. for _, refValue := range refValues[1:] {
  61. refParts := strings.Split(refValue, ":")
  62. if len(refParts) == 0 || len(refParts) > 3 {
  63. return gitRef, fmt.Errorf("refspec %s invalid: malformed pull ref", refValue)
  64. }
  65. pullNumber, err := strconv.Atoi(refParts[0])
  66. if err != nil {
  67. return gitRef, fmt.Errorf("refspec %s invalid: pull request identifier not a number: %v", refValue, err)
  68. }
  69. pullRef := kube.Pull{
  70. Number: pullNumber,
  71. }
  72. if len(refParts) > 1 {
  73. pullRef.SHA = refParts[1]
  74. }
  75. if len(refParts) > 2 {
  76. pullRef.Ref = refParts[2]
  77. }
  78. gitRef.Pulls = append(gitRef.Pulls, pullRef)
  79. }
  80. return gitRef, nil
  81. }