|
| 1 | +import { minimatch } from "minimatch"; |
| 2 | + |
| 3 | +import type { FileDiff } from "../diff-informed"; |
| 4 | + |
| 5 | +import { BuiltInLanguage } from "."; |
| 6 | + |
| 7 | +/** |
| 8 | + * Represents configurations for how to detect whether changes relate to a given analysis. |
| 9 | + */ |
| 10 | +export type ChangePatterns = { |
| 11 | + /** |
| 12 | + * A list of glob patterns. |
| 13 | + * |
| 14 | + * If a changed file has matches one of these, we conclude that files relevant to the |
| 15 | + * analysis have changed. |
| 16 | + * |
| 17 | + * If undefined, all changes are considered relevant. |
| 18 | + */ |
| 19 | + patterns?: string[]; |
| 20 | + |
| 21 | + /** |
| 22 | + * A list of glob patterns to exclude from `patterns`. Allows specific patterns to be excluded |
| 23 | + * that would otherwise be included by `patterns`. |
| 24 | + * |
| 25 | + * If a changed file matches one of `patterns` and `exclude`, then it is not considered |
| 26 | + * relevant to the analysis. |
| 27 | + */ |
| 28 | + exclude?: string[]; |
| 29 | +}; |
| 30 | + |
| 31 | +/** |
| 32 | + * A mapping from all {@link BuildInLanguage}s to a {@link ChangePatterns} configuration. |
| 33 | + */ |
| 34 | +export const CHANGE_PATTERNS: Record<BuiltInLanguage, ChangePatterns> = { |
| 35 | + [BuiltInLanguage.actions]: {}, |
| 36 | + [BuiltInLanguage.cpp]: {}, |
| 37 | + [BuiltInLanguage.csharp]: {}, |
| 38 | + [BuiltInLanguage.go]: {}, |
| 39 | + [BuiltInLanguage.java]: {}, |
| 40 | + [BuiltInLanguage.javascript]: {}, |
| 41 | + [BuiltInLanguage.python]: {}, |
| 42 | + [BuiltInLanguage.ruby]: {}, |
| 43 | + [BuiltInLanguage.rust]: {}, |
| 44 | + [BuiltInLanguage.swift]: {}, |
| 45 | +}; |
| 46 | + |
| 47 | +/** |
| 48 | + * Decides whether {@link val} matches any of {@link patterns}. Returns early |
| 49 | + * as soon as a pattern matches. |
| 50 | + */ |
| 51 | +export function matchPatterns(val: string, patterns: string[]): boolean { |
| 52 | + for (const pattern of patterns) { |
| 53 | + if (minimatch(val, pattern, { matchBase: true })) { |
| 54 | + return true; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return false; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Decides whether {@link diff} matches the configuration in {@link changePatterns}. |
| 63 | + * |
| 64 | + * @param changePatterns The configuration of patterns to match {@link diff}'s path against. |
| 65 | + * @param diff A description of a change. |
| 66 | + */ |
| 67 | +export function relevantChangeFor( |
| 68 | + changePatterns: ChangePatterns, |
| 69 | + diff: FileDiff, |
| 70 | +): boolean { |
| 71 | + // If `patterns` is defined and none match, then the change is not relevant. |
| 72 | + if ( |
| 73 | + changePatterns.patterns !== undefined && |
| 74 | + !matchPatterns(diff.filename, changePatterns.patterns) |
| 75 | + ) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + // At this point we have decided that the change is relevant, either because no |
| 80 | + // patterns are configured (all changes are relevant) or because one of the configured |
| 81 | + // patterns matched. Let's check if `exclude` patterns are configured and if any match. |
| 82 | + // If so, then we don't consider the change relevant after all. |
| 83 | + if ( |
| 84 | + changePatterns.exclude !== undefined && |
| 85 | + matchPatterns(diff.filename, changePatterns.exclude) |
| 86 | + ) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + // The change is relevant and none of the `exclude` patterns matched (or none were configured). |
| 91 | + return true; |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Decides whether at least one of the given {@link diffs} matches the configuration for {@link language} |
| 96 | + * from {@link CHANGE_PATTERNS}. |
| 97 | + * |
| 98 | + * @param language The language to check for changes for. |
| 99 | + * @param diffs All changes to check. |
| 100 | + */ |
| 101 | +export function relevantChangesFor( |
| 102 | + language: BuiltInLanguage, |
| 103 | + diffs: FileDiff[], |
| 104 | +): boolean { |
| 105 | + for (const diff of diffs) { |
| 106 | + if (relevantChangeFor(CHANGE_PATTERNS[language], diff)) { |
| 107 | + return true; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return false; |
| 112 | +} |
0 commit comments