go.fuchsia.dev/infra@v0.0.0-20240507153436-9b593402251b/cmd/autogardener/commit_tag.go (about)

     1  // Copyright 2022 The Fuchsia Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"regexp"
     9  	"slices"
    10  	"strings"
    11  )
    12  
    13  var (
    14  	commitTagRegex = regexp.MustCompile(`\[([-\w]+)\]`)
    15  
    16  	// Commit tags that will be ignored, because they often appear as substrings
    17  	// of test names but aren't correlated with the area of the codebase that
    18  	// the test applies to with.
    19  	// TODO(olivernewman): Generate this list on-the-fly by querying all test
    20  	// names and ignoring any tag that appears in more than ~10% of test names.
    21  	ignoreList = []string{
    22  		"cm",
    23  		"component",
    24  		"cmx",
    25  		"fuchsia",
    26  		"pkg",
    27  		"roll",
    28  		"test",
    29  		"tests",
    30  	}
    31  )
    32  
    33  // hasMatchingTag determines whether the test name contains any of the given
    34  // commit message's bracket-delimited tags as a substring.
    35  func hasMatchingTag(commitMessage, testName string) bool {
    36  	separator := "_"
    37  
    38  	// Convert all hyphens to underscores so that we don't ignore matches just
    39  	// because of differing separators.
    40  	testName = strings.ReplaceAll(testName, "-", separator)
    41  
    42  	// Ignore tefmocheck failures, which are generally log signatures that only
    43  	// rarely contain keywords that meaningfully correspond to commit tags.
    44  	// This is a bit of a hack, we should ideally have some more general test
    45  	// tag in ResultDB to indicate that this tefmocheck failures are not regular
    46  	// tests.
    47  	if strings.HasPrefix(testName, "testing_failure_mode") {
    48  		return false
    49  	}
    50  	firstLine := strings.Split(commitMessage, "\n")[0]
    51  	tagMatches := commitTagRegex.FindAllStringSubmatch(firstLine, -1)
    52  	for _, match := range tagMatches {
    53  		tag := match[1]
    54  		// Standardize separators.
    55  		for _, c := range []string{" ", "-"} {
    56  			tag = strings.ReplaceAll(tag, c, separator)
    57  		}
    58  		if slices.Contains(ignoreList, tag) {
    59  			continue
    60  		}
    61  		if strings.Contains(testName, tag) {
    62  			return true
    63  		}
    64  	}
    65  	return false
    66  }