...

Source file src/edge-infra.dev/hack/build/ci/leaf/pkg/go/parse_deps.go

Documentation: edge-infra.dev/hack/build/ci/leaf/pkg/go

     1  package golang
     2  
     3  import "strings"
     4  
     5  // Parse takes a diff of a .bzl file defining go_repository rules and returns
     6  // the list of impacted go libraries (e.g., @com_github_foo_bar//...)
     7  func Parse(diff []string) []string {
     8  	names := []string{}
     9  	for i, line := range diff {
    10  		// removal will come first in diff, so search for that
    11  		if strings.Contains(line, "-        sum") && checkDiffPair(diff, i) {
    12  			// work our way backwards until we reach the first line with a name in it
    13  			names = append(names, "@"+findName(diff, i)+"//...")
    14  		}
    15  	}
    16  
    17  	return names
    18  }
    19  
    20  // check forward to find matching + line in diff, otherwise the
    21  // go_repository is being removed and we can ignore it
    22  func checkDiffPair(diff []string, index int) bool {
    23  	var pairSum = "+        sum"
    24  	// check both +1 and +2 forward because version might not be changed when
    25  	// the sum is
    26  	return strings.Contains(diff[index+1], pairSum) || strings.Contains(diff[index+2], pairSum)
    27  }
    28  
    29  func findName(diff []string, index int) string {
    30  	for !isNameLine(diff[index]) {
    31  		// if it isnt the line with the name on it, decrease index by one
    32  		// to continue working backwards
    33  		index = index - 1
    34  	}
    35  	// we have found the name line, read name and return it
    36  	return readName(diff[index])
    37  }
    38  
    39  // determines if a given line in a go_repository file contains the name
    40  func isNameLine(line string) bool {
    41  	return strings.HasPrefix(strings.TrimSpace(line), "name = ")
    42  }
    43  
    44  // reads the name of a bazel repository from a line in the .bzl file
    45  func readName(line string) string {
    46  	// there should only be two quotes on a single line, so our desired value
    47  	// will always be the second element:
    48  	//          name = "com_github_pmezard_go_difflib",
    49  	return strings.Split(line, "\"")[1]
    50  }
    51  

View as plain text