|
12 | 12 | import datadog.trace.civisibility.codeowners.matcher.RangeMatcher; |
13 | 13 | import java.util.ArrayDeque; |
14 | 14 | import java.util.ArrayList; |
15 | | -import java.util.Arrays; |
16 | 15 | import java.util.Collection; |
| 16 | +import java.util.Collections; |
17 | 17 | import java.util.Deque; |
18 | 18 | import javax.annotation.Nullable; |
19 | 19 | import org.slf4j.Logger; |
@@ -46,29 +46,116 @@ public EntryBuilder(CharacterMatcher.Factory characterMatcherFactory, String s) |
46 | 46 | } |
47 | 47 |
|
48 | 48 | public @Nullable Entry parse() { |
| 49 | + return parse(Collections.emptyList()); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Parses the line as a CODEOWNERS entry: a path pattern optionally followed by one or more |
| 54 | + * owners. |
| 55 | + * |
| 56 | + * @param sectionDefaultOwners default owners declared on the enclosing GitLab section header, |
| 57 | + * used when the entry declares no owners of its own. Pass an empty collection for entries |
| 58 | + * that do not belong to a section (e.g. GitHub CODEOWNERS files). |
| 59 | + * @return the parsed entry, or {@code null} for blank lines, comments, section headers, or lines |
| 60 | + * that cannot be parsed. |
| 61 | + */ |
| 62 | + public @Nullable Entry parse(Collection<String> sectionDefaultOwners) { |
49 | 63 | try { |
50 | | - // skip trailing whitespace |
51 | | - while (offset < c.length && Character.isWhitespace(c[offset])) { |
52 | | - offset++; |
53 | | - } |
| 64 | + skipWhitespace(); |
54 | 65 |
|
55 | 66 | if (offset == c.length // empty line |
56 | 67 | || c[offset] == '#' // comment |
57 | | - || c[offset] == '[' // section header |
58 | | - || (c[offset] == '^' |
59 | | - && offset + 1 < c.length |
60 | | - && c[offset + 1] == '[')) { // GitLab optional section header (^[...]) |
| 68 | + || isSectionHeader()) { // GitLab section header (including optional '^[') |
61 | 69 | return null; |
62 | 70 | } |
63 | 71 |
|
64 | 72 | Matcher matcher = parseMatcher(); |
65 | 73 | Collection<String> owners = parseOwners(); |
| 74 | + if (owners.isEmpty()) { |
| 75 | + owners = sectionDefaultOwners; |
| 76 | + } |
66 | 77 | return new Entry(matcher, owners); |
67 | 78 |
|
68 | 79 | } catch (Exception e) { |
69 | | - log.error("error parsing CODEOWNERS pattern: {}", Arrays.toString(c), e); |
| 80 | + log.warn("Skipping malformed CODEOWNERS entry: {}", new String(c), e); |
| 81 | + return null; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * If the line is a GitLab section header, consumes it and returns the parsed {@link |
| 87 | + * SectionHeader} (its name and default owners). Returns {@code null} when the line is not a |
| 88 | + * section header, leaving the cursor at the start of the pattern so the line can be parsed with |
| 89 | + * {@link #parse(Collection)}. |
| 90 | + * |
| 91 | + * <p>Supports the GitLab header syntax {@code ^[Section name][N] @owner}: an optional leading |
| 92 | + * {@code ^} (optional section), a bracketed name that may contain spaces, an optional {@code [N]} |
| 93 | + * required-approvals count, and trailing default owners. |
| 94 | + * |
| 95 | + * @see <a href="https://docs.gitlab.com/user/project/codeowners/reference/">GitLab Code Owners |
| 96 | + * reference</a> |
| 97 | + */ |
| 98 | + public @Nullable SectionHeader parseSectionHeader() { |
| 99 | + skipWhitespace(); |
| 100 | + if (!isSectionHeader()) { |
70 | 101 | return null; |
71 | 102 | } |
| 103 | + if (c[offset] == '^') { |
| 104 | + offset++; // consume the optional-section marker |
| 105 | + } |
| 106 | + offset++; // consume the opening '[' |
| 107 | + int nameStart = offset; |
| 108 | + while (offset < c.length && c[offset] != ']') { |
| 109 | + offset++; // consume the section name (which may contain spaces) |
| 110 | + } |
| 111 | + String name = new String(c, nameStart, offset - nameStart); |
| 112 | + if (offset < c.length) { |
| 113 | + offset++; // consume the closing ']' |
| 114 | + } |
| 115 | + // skip the optional [N] required-approvals count that may immediately follow the name |
| 116 | + if (offset < c.length && c[offset] == '[') { |
| 117 | + while (offset < c.length && c[offset] != ']') { |
| 118 | + offset++; |
| 119 | + } |
| 120 | + if (offset < c.length) { |
| 121 | + offset++; // consume the closing ']' |
| 122 | + } |
| 123 | + } |
| 124 | + return new SectionHeader(name, parseOwners()); |
| 125 | + } |
| 126 | + |
| 127 | + private void skipWhitespace() { |
| 128 | + while (offset < c.length && Character.isWhitespace(c[offset])) { |
| 129 | + offset++; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Determines whether the line, starting at the current cursor (with leading whitespace already |
| 135 | + * skipped), is a section header. Does not move the cursor. |
| 136 | + * |
| 137 | + * <p>A section header begins with {@code [} — or the GitLab optional-section marker {@code ^[} — |
| 138 | + * and the section name's closing {@code ]} is followed by end-of-line, whitespace, a comment |
| 139 | + * ({@code #}), or the optional {@code [N]} approvals count. Requiring that trailing context |
| 140 | + * distinguishes a section header from a path pattern that merely begins with a character class |
| 141 | + * such as {@code [a-z]*.txt}. |
| 142 | + */ |
| 143 | + private boolean isSectionHeader() { |
| 144 | + int i = offset; |
| 145 | + if (i < c.length && c[i] == '^') { |
| 146 | + i++; // optional-section marker |
| 147 | + } |
| 148 | + if (i >= c.length || c[i] != '[') { |
| 149 | + return false; |
| 150 | + } |
| 151 | + while (i < c.length && c[i] != ']') { |
| 152 | + i++; // scan to the section name's closing ']' |
| 153 | + } |
| 154 | + if (i >= c.length) { |
| 155 | + return false; // unterminated brackets: not a well-formed section header |
| 156 | + } |
| 157 | + i++; // move past ']' |
| 158 | + return i >= c.length || Character.isWhitespace(c[i]) || c[i] == '[' || c[i] == '#'; |
72 | 159 | } |
73 | 160 |
|
74 | 161 | private Matcher parseMatcher() { |
|
0 commit comments