package golang import "strings" // Parse takes a diff of a .bzl file defining go_repository rules and returns // the list of impacted go libraries (e.g., @com_github_foo_bar//...) func Parse(diff []string) []string { names := []string{} for i, line := range diff { // removal will come first in diff, so search for that if strings.Contains(line, "- sum") && checkDiffPair(diff, i) { // work our way backwards until we reach the first line with a name in it names = append(names, "@"+findName(diff, i)+"//...") } } return names } // check forward to find matching + line in diff, otherwise the // go_repository is being removed and we can ignore it func checkDiffPair(diff []string, index int) bool { var pairSum = "+ sum" // check both +1 and +2 forward because version might not be changed when // the sum is return strings.Contains(diff[index+1], pairSum) || strings.Contains(diff[index+2], pairSum) } func findName(diff []string, index int) string { for !isNameLine(diff[index]) { // if it isnt the line with the name on it, decrease index by one // to continue working backwards index = index - 1 } // we have found the name line, read name and return it return readName(diff[index]) } // determines if a given line in a go_repository file contains the name func isNameLine(line string) bool { return strings.HasPrefix(strings.TrimSpace(line), "name = ") } // reads the name of a bazel repository from a line in the .bzl file func readName(line string) string { // there should only be two quotes on a single line, so our desired value // will always be the second element: // name = "com_github_pmezard_go_difflib", return strings.Split(line, "\"")[1] }