| // Copyright 2022 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package main |
| |
| import ( |
| "regexp" |
| "slices" |
| "strings" |
| ) |
| |
| var ( |
| commitTagRegex = regexp.MustCompile(`\[([-\w]+)\]`) |
| |
| // Commit tags that will be ignored, because they often appear as substrings |
| // of test names but aren't correlated with the area of the codebase that |
| // the test applies to with. |
| // TODO(olivernewman): Generate this list on-the-fly by querying all test |
| // names and ignoring any tag that appears in more than ~10% of test names. |
| ignoreList = []string{ |
| "cm", |
| "component", |
| "cmx", |
| "fuchsia", |
| "pkg", |
| "roll", |
| "test", |
| "tests", |
| } |
| ) |
| |
| // hasMatchingTag determines whether the test name contains any of the given |
| // commit message's bracket-delimited tags as a substring. |
| func hasMatchingTag(commitMessage, testName string) bool { |
| separator := "_" |
| |
| // Convert all hyphens to underscores so that we don't ignore matches just |
| // because of differing separators. |
| testName = strings.ReplaceAll(testName, "-", separator) |
| |
| // Ignore tefmocheck failures, which are generally log signatures that only |
| // rarely contain keywords that meaningfully correspond to commit tags. |
| // This is a bit of a hack, we should ideally have some more general test |
| // tag in ResultDB to indicate that this tefmocheck failures are not regular |
| // tests. |
| if strings.HasPrefix(testName, "testing_failure_mode") { |
| return false |
| } |
| firstLine := strings.Split(commitMessage, "\n")[0] |
| tagMatches := commitTagRegex.FindAllStringSubmatch(firstLine, -1) |
| for _, match := range tagMatches { |
| tag := match[1] |
| // Standardize separators. |
| for _, c := range []string{" ", "-"} { |
| tag = strings.ReplaceAll(tag, c, separator) |
| } |
| if slices.Contains(ignoreList, tag) { |
| continue |
| } |
| if strings.Contains(testName, tag) { |
| return true |
| } |
| } |
| return false |
| } |