@@ -433,19 +433,20 @@ func upgradeDepDecl(ctx context.Context, gh *githubClient, workDir, name string,
433433 return fmt .Errorf ("\" patches\" attribute is not a list" )
434434 }
435435 for patchIndex , patchLabelExpr := range patchesList .List {
436- patchLabel , ok := patchLabelExpr .( * bzl. StringExpr )
437- if ! ok {
438- return fmt .Errorf ("not all patches are string literals" )
436+ patchLabelValue , comments , err := parsePatchesItem ( patchLabelExpr )
437+ if err != nil {
438+ return fmt .Errorf ("parsing expr %#v : %w" , patchLabelExpr , err )
439439 }
440- if ! strings .HasPrefix (patchLabel .Value , "@io_bazel_rules_go//third_party:" ) {
441- return fmt .Errorf ("patch does not start with '@io_bazel_rules_go//third_party:': %s" , patchLabel )
440+
441+ if ! strings .HasPrefix (patchLabelValue , "//third_party:" ) {
442+ return fmt .Errorf ("patch does not start with '//third_party:': %q" , patchLabelValue )
442443 }
443- patchName := patchLabel . Value [len ("@io_bazel_rules_go //third_party:" ):]
444+ patchName := patchLabelValue [len ("//third_party:" ):]
444445 patchPath := filepath .Join (rootDir , "third_party" , patchName )
445446 prevDir := filepath .Join (workDir , name , string ('a' + patchIndex ))
446447 patchDir := filepath .Join (workDir , name , string ('a' + patchIndex + 1 ))
447448 var patchCmd []string
448- for _ , c := range patchLabel . Comment () .Before {
449+ for _ , c := range comments .Before {
449450 words := strings .Fields (strings .TrimPrefix (c .Token , "#" ))
450451 if len (words ) > 0 && words [0 ] == "releaser:patch-cmd" {
451452 patchCmd = words [1 :]
@@ -488,6 +489,34 @@ func upgradeDepDecl(ctx context.Context, gh *githubClient, workDir, name string,
488489 return nil
489490}
490491
492+ func parsePatchesItem (patchLabelExpr bzl.Expr ) (value string , comments * bzl.Comments , err error ) {
493+ switch patchLabel := patchLabelExpr .(type ) {
494+ case * bzl.CallExpr :
495+ // Verify the identifier, should be Label
496+ if ident , ok := patchLabel .X .(* bzl.Ident ); ! ok {
497+ return "" , nil , fmt .Errorf ("invalid identifier while parsing patch label" )
498+ } else if ident .Name != "Label" {
499+ return "" , nil , fmt .Errorf ("invalid patch function: %q" , ident .Name )
500+ }
501+
502+ // Expect 1 String argument with the patch
503+ if len (patchLabel .List ) != 1 {
504+ return "" , nil , fmt .Errorf ("Label expr should have 1 argument, found %d" , len (patchLabel .List ))
505+ }
506+
507+ // Parse patch as a string
508+ patchLabelStr , ok := patchLabel .List [0 ].(* bzl.StringExpr )
509+ if ! ok {
510+ return "" , nil , fmt .Errorf ("Label expr does not contain a string literal" )
511+ }
512+ return patchLabelStr .Value , patchLabel .Comment (), nil
513+ case * bzl.StringExpr :
514+ return strings .TrimPrefix (patchLabel .Value , "@io_bazel_rules_go" ), patchLabel .Comment (), nil
515+ default :
516+ return "" , nil , fmt .Errorf ("not all patches are string literals or Label()" )
517+ }
518+ }
519+
491520// parseUpgradeDepDirective parses a '# releaser:upgrade-dep org repo' directive
492521// and returns the organization and repository name or an error if the directive
493522// was not found or malformed.
0 commit comments