virtual-code-owners 8.1.0 → 8.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -210,11 +210,17 @@ user/team names but doesn't verify their existence in the project.
210
210
 
211
211
  - valid user/team names start with an `@` or are an e-mail address
212
212
  - valid rules have a file pattern and at least one user/team name
213
+ (unless they're in a _section_ that has default owners `[sales related] @ch/sales`
214
+ in which case the rule inherits the default owners of that section)
213
215
  - valid sections headings comply with the syntax described over at [GitLab](https://docs.gitlab.com/ee/user/project/codeowners/reference.html#sections)
214
216
  > different from GitLab's syntax the line `[bla @group` is not interpreted
215
217
  > as a rule, but as an erroneous section heading. This behaviour might change
216
218
  > to be the same as GitLab's in future releases without a major version bump.
217
219
 
220
+ ### Does virtual-code-owners support GitLab style sections?
221
+
222
+ Yes.
223
+
218
224
  ### I want to specify different locations for the files (e.g. because I'm using GitLab)
219
225
 
220
226
  Here you go:
@@ -228,8 +234,6 @@ npx virtual-code-owners \
228
234
 
229
235
  ### Can I just validate VIRTUAL-CODEOWNERS.txt & virtual-teams.yml without generating output?
230
236
 
231
- So _without_ generating any output?
232
-
233
237
  Sure thing. Use `--dryRun`:
234
238
 
235
239
  ```
@@ -241,16 +245,15 @@ npx virtual-code-owners --dryRun
241
245
  It keeps editors and IDE's from messing up your formatting.
242
246
 
243
247
  Various editors assume an ALL_CAPS file name with `#` characters on various lines
244
- to be markdown, and will auto format them as such. This makes for either very ugly
245
- or in worst cases invalid CODEOWNERS files. Usually such autoformatting is not
246
- present on text files.
248
+ to be markdown, and will auto format them as such. Usually such autoformatting is
249
+ not present on text files.
247
250
 
248
- Apparently these editors know about CODEOWNERS, though, so they don't mess with
249
- the formatting of _those_.
251
+ Often these editors know about CODEOWNERS, so they won't confuse _those_ with
252
+ markdown.
250
253
 
251
254
  ### Why does this exist at all? Why not just use GitHub teams?
252
255
 
253
- You should _totally_ use GitHub teams! If you can.
256
+ If you can you should _totally_ use GitHub teams!
254
257
 
255
258
  Organizations sometimes have large mono repositories with many code owners.
256
259
  They or their bureaucracy haven't landed on actually using GitHub teams to
@@ -259,10 +262,9 @@ the organization chart (and hence the GitHub teams). Teams in those organization
259
262
  who want to have clear code ownership can either:
260
263
 
261
264
  - Wrestle the bureaucracy.
262
- Recommended! It might take a while, though - and even though there are good
263
- people on many levels in bureaucracies, it might eventually not pan out
264
- because #reasons.
265
+ Recommended! It will often require patience though, and in the mean time
266
+ you might want to have some clarity on code ownership.
265
267
  - Maintain a CODEOWNERS file with code assigned to large lists of individuals.
266
- An option, but laborious to maintain, even for smaller projects
268
+ That's a lotta work, even for smaller projects
267
269
 
268
270
  This is where `virtual-code-owners` comes in.
@@ -37,7 +37,7 @@ function generateLine(pCSTLine, pTeamMap) {
37
37
  return (
38
38
  (pCSTLine.optional ? "^" : "") +
39
39
  "[" +
40
- pCSTLine.sectionName +
40
+ pCSTLine.name +
41
41
  "]" +
42
42
  (pCSTLine.minApprovers ? `[${pCSTLine.minApprovers}]` : "") +
43
43
  pCSTLine.spaces +
@@ -49,8 +49,13 @@ function transformForYamlAndMinimatch(pOriginalString) {
49
49
  return lReturnValue;
50
50
  }
51
51
  function lineContainsTeamName(pLine, pTeamName) {
52
- return pLine.users.some(
52
+ const lHasTeamNameInRegularUsers = pLine.users.some(
53
53
  (pUser) =>
54
54
  pUser.type === "virtual-team-name" && pUser.bareName === pTeamName,
55
55
  );
56
+ const lHasTeamNameInInheritedUsers = (pLine.inheritedUsers ?? []).some(
57
+ (pUser) =>
58
+ pUser.type === "virtual-team-name" && pUser.bareName === pTeamName,
59
+ );
60
+ return lHasTeamNameInRegularUsers || lHasTeamNameInInheritedUsers;
56
61
  }
@@ -1,8 +1,6 @@
1
- import Ajv from "ajv";
2
1
  import { readFileSync } from "node:fs";
3
- import { EOL } from "node:os";
4
2
  import { parse as parseYaml } from "yaml";
5
- import virtualTeamsSchema from "./virtual-teams.schema.js";
3
+ import { EOL } from "node:os";
6
4
  export default function readTeamMap(pVirtualTeamsFileName) {
7
5
  const lVirtualTeamsAsAString = readFileSync(pVirtualTeamsFileName, {
8
6
  encoding: "utf-8",
@@ -11,22 +9,74 @@ export default function readTeamMap(pVirtualTeamsFileName) {
11
9
  assertTeamMapValid(lTeamMap, pVirtualTeamsFileName);
12
10
  return lTeamMap;
13
11
  }
14
- function assertTeamMapValid(pTeamMap, pVirtualTeamsFileName) {
15
- const ajv = new Ajv({
16
- allErrors: true,
17
- verbose: true,
18
- });
19
- if (!ajv.validate(virtualTeamsSchema, pTeamMap)) {
12
+ function assertTeamMapValid(pTeamMapCandidate, pVirtualTeamsFileName) {
13
+ const [lValid, lError] = validateTeamMap(pTeamMapCandidate);
14
+ if (!lValid) {
20
15
  throw new Error(
21
- `This is not a valid virtual-teams.yml:${EOL}${formatAjvErrors(ajv.errors, pVirtualTeamsFileName)}.\n`,
16
+ `'${pVirtualTeamsFileName}' is not a valid virtual-teams.yml:${EOL} ${lError}`,
22
17
  );
23
18
  }
24
19
  }
25
- function formatAjvErrors(pAjvErrors, pVirtualTeamsFileName) {
26
- return pAjvErrors
27
- .map((pAjvError) => formatAjvError(pAjvError, pVirtualTeamsFileName))
28
- .join(EOL);
20
+ function validateTeamMap(pCandidateTeamMap) {
21
+ if (
22
+ typeof pCandidateTeamMap !== "object" ||
23
+ pCandidateTeamMap === null ||
24
+ Array.isArray(pCandidateTeamMap)
25
+ ) {
26
+ return [false, "The team map is not an object"];
27
+ }
28
+ const lTeamNameResults = Object.keys(pCandidateTeamMap).map(validateTeamName);
29
+ const lErrors = lTeamNameResults.filter((result) => !result[0]);
30
+ if (lErrors.length > 0) {
31
+ return [
32
+ false,
33
+ `These team names are not valid: ${lErrors.map((error) => error[1]).join(", ")}`,
34
+ ];
35
+ }
36
+ const lTeamResults = Object.keys(pCandidateTeamMap).map((pKey) =>
37
+ validateTeam(pCandidateTeamMap[pKey], pKey),
38
+ );
39
+ const lTeamErrors = lTeamResults.filter((result) => !result[0]);
40
+ if (lTeamErrors.length > 0) {
41
+ return [false, lTeamErrors.map((error) => error[1]).join(`, ${EOL} `)];
42
+ }
43
+ return [true];
29
44
  }
30
- function formatAjvError(pAjvError, pVirtualTeamsFileName) {
31
- return `${pVirtualTeamsFileName}: ${pAjvError.instancePath} - ${JSON.stringify(pAjvError.data)} ${pAjvError.message}`;
45
+ function validateTeamName(pTeamNameCandidate) {
46
+ if (typeof pTeamNameCandidate !== "string") {
47
+ return [false, "not a string"];
48
+ }
49
+ if (pTeamNameCandidate === "") {
50
+ return [false, "'' (empty string)"];
51
+ }
52
+ if (pTeamNameCandidate.includes(" ")) {
53
+ return [false, `'${pTeamNameCandidate}' (contains spaces)`];
54
+ }
55
+ return [true];
56
+ }
57
+ function validateTeam(pCandidateTeam, pTeamName) {
58
+ if (!Array.isArray(pCandidateTeam)) {
59
+ return [false, `This team is not an array: '${pTeamName}'`];
60
+ }
61
+ const lTeamMemberResults = pCandidateTeam.map(validateTeamMember);
62
+ const lErrors = lTeamMemberResults.filter((result) => !result[0]);
63
+ if (lErrors.length > 0) {
64
+ return [false, lErrors.map((error) => error[1]).join(", ")];
65
+ }
66
+ return [true];
67
+ }
68
+ function validateTeamMember(pTeamMemberCandidate) {
69
+ if (typeof pTeamMemberCandidate !== "string") {
70
+ return [
71
+ false,
72
+ `This username is not a string: '${pTeamMemberCandidate.toString()}'`,
73
+ ];
74
+ }
75
+ if (!/^[^@][^\s]+$/.test(pTeamMemberCandidate)) {
76
+ return [
77
+ false,
78
+ `This username doesn't match /^[^@][^\\s]+$/: '${pTeamMemberCandidate}'`,
79
+ ];
80
+ }
81
+ return [true];
32
82
  }
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "8.1.0";
1
+ export const VERSION = "8.2.0";
@@ -1,6 +1,14 @@
1
1
  import { EOL } from "node:os";
2
2
  import { isEmailIshUsername } from "../utensils.js";
3
+ let STATE = {
4
+ currentSection: "",
5
+ inheritedUsers: [],
6
+ };
3
7
  export function parse(pVirtualCodeOwnersAsString, pTeamMap = {}) {
8
+ STATE = {
9
+ currentSection: "",
10
+ inheritedUsers: [],
11
+ };
4
12
  return pVirtualCodeOwnersAsString
5
13
  .split(EOL)
6
14
  .map((pUntreatedLine, pLineNo) =>
@@ -9,63 +17,74 @@ export function parse(pVirtualCodeOwnersAsString, pTeamMap = {}) {
9
17
  }
10
18
  function parseLine(pUntreatedLine, pTeamMap, pLineNo) {
11
19
  const lTrimmedLine = pUntreatedLine.trim();
12
- const lCommentSplitLine = lTrimmedLine.split(/\s*#/);
13
- const lRule = lCommentSplitLine[0]?.match(
14
- /^(?<filesPattern>[^\s]+)(?<spaces>\s+)(?<userNames>.*)$/,
15
- );
16
20
  if (lTrimmedLine.startsWith("#!")) {
17
21
  return { type: "ignorable-comment", line: pLineNo, raw: pUntreatedLine };
18
22
  }
19
23
  if (lTrimmedLine.startsWith("#")) {
20
24
  return { type: "comment", line: pLineNo, raw: pUntreatedLine };
21
25
  }
26
+ if (lTrimmedLine === "") {
27
+ return { type: "empty", line: pLineNo, raw: pUntreatedLine };
28
+ }
22
29
  if (lTrimmedLine.startsWith("[") || lTrimmedLine.startsWith("^[")) {
23
30
  return parseSection(pUntreatedLine, pLineNo, pTeamMap);
24
31
  }
25
- if (!lRule?.groups) {
26
- if (lTrimmedLine === "") {
27
- return { type: "empty", line: pLineNo, raw: pUntreatedLine };
32
+ return parseRule(pUntreatedLine, pLineNo, pTeamMap);
33
+ }
34
+ function parseRule(pUntreatedLine, pLineNo, pTeamMap) {
35
+ const lTrimmedLine = pUntreatedLine.trim();
36
+ const lCommentSplitLine = lTrimmedLine.split(/\s*#/);
37
+ const lRule = lCommentSplitLine[0]?.match(
38
+ /^(?<filesPattern>[^\s]+)(?<spaces>\s+)?(?<userNames>.+)?$/,
39
+ );
40
+ const ruleIsValid =
41
+ lRule?.groups &&
42
+ (lRule.groups.userNames || STATE.inheritedUsers.length > 0);
43
+ if (ruleIsValid) {
44
+ let lReturnValue = {
45
+ type: "rule",
46
+ line: pLineNo,
47
+ raw: pUntreatedLine,
48
+ filesPattern: lRule.groups.filesPattern,
49
+ spaces: lRule.groups?.spaces ?? "",
50
+ users: parseUsers(lRule.groups?.userNames ?? "", pTeamMap),
51
+ inlineComment: lCommentSplitLine[1] ?? "",
52
+ };
53
+ if (STATE.currentSection) {
54
+ lReturnValue.inheritedUsers = STATE.inheritedUsers;
55
+ lReturnValue.currentSection = STATE.currentSection;
28
56
  }
29
- return { type: "unknown", line: pLineNo, raw: pUntreatedLine };
57
+ return lReturnValue;
30
58
  }
31
- return {
32
- type: "rule",
33
- line: pLineNo,
34
- filesPattern: lRule.groups.filesPattern,
35
- spaces: lRule.groups.spaces,
36
- users: parseUsers(lRule.groups.userNames, pTeamMap),
37
- inlineComment: lCommentSplitLine[1] ?? "",
38
- raw: pUntreatedLine,
39
- };
59
+ return { type: "unknown", line: pLineNo, raw: pUntreatedLine };
40
60
  }
41
61
  function parseSection(pUntreatedLine, pLineNo, pTeamMap) {
42
62
  const lTrimmedLine = pUntreatedLine.trim();
43
63
  const lCommentSplitLine = lTrimmedLine.split(/\s*#/);
44
64
  const lSection = lCommentSplitLine[0]?.match(
45
- /^(?<optionalIndicator>\^)?\[(?<sectionName>[^\]]+)\](\[(?<minApprovers>[0-9]+)\])?(?<spaces>\s+)(?<userNames>.*)$/,
65
+ /^(?<optionalIndicator>\^)?\[(?<name>[^\]]+)\](\[(?<minApprovers>[0-9]+)\])?(?<spaces>\s+)?(?<userNames>.+)?$/,
46
66
  );
47
67
  if (!lSection?.groups) {
48
- return lTrimmedLine.endsWith("]")
49
- ? {
50
- type: "section-without-users",
51
- line: pLineNo,
52
- raw: pUntreatedLine,
53
- }
54
- : {
55
- type: "unknown",
56
- line: pLineNo,
57
- raw: pUntreatedLine,
58
- };
68
+ return {
69
+ type: "unknown",
70
+ line: pLineNo,
71
+ raw: pUntreatedLine,
72
+ };
59
73
  }
74
+ const lUsers = parseUsers(lSection.groups?.userNames ?? "", pTeamMap);
75
+ STATE = {
76
+ currentSection: lSection.groups.name,
77
+ inheritedUsers: lUsers,
78
+ };
60
79
  const lReturnValue = {
61
80
  type: "section-heading",
62
81
  line: pLineNo,
63
- optional: lSection.groups.optionalIndicator === "^",
64
- sectionName: lSection.groups.sectionName,
65
- spaces: lSection.groups.spaces,
66
- users: parseUsers(lSection.groups.userNames, pTeamMap),
67
- inlineComment: lTrimmedLine.split(/\s*#/)[1] ?? "",
68
82
  raw: pUntreatedLine,
83
+ optional: lSection.groups.optionalIndicator === "^",
84
+ name: lSection.groups.name,
85
+ spaces: lSection.groups?.spaces ?? "",
86
+ users: parseUsers(lSection.groups?.userNames ?? "", pTeamMap),
87
+ inlineComment: lCommentSplitLine[1] ?? "",
69
88
  };
70
89
  if (lSection.groups.minApprovers) {
71
90
  lReturnValue.minApprovers = parseInt(lSection.groups.minApprovers, 10);
@@ -73,7 +92,7 @@ function parseSection(pUntreatedLine, pLineNo, pTeamMap) {
73
92
  return lReturnValue;
74
93
  }
75
94
  function parseUsers(pUserNamesString, pTeamMap) {
76
- const lUserNames = pUserNamesString.split(/\s+/);
95
+ const lUserNames = pUserNamesString ? pUserNamesString.split(/\s+/) : [];
77
96
  return lUserNames.map((pUserName, pIndex) => {
78
97
  const lBareName = getBareUserName(pUserName);
79
98
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "virtual-code-owners",
3
- "version": "8.1.0",
3
+ "version": "8.2.0",
4
4
  "description": "CODEOWNERS with teams for teams that can't use GitHub teams",
5
5
  "type": "module",
6
6
  "bin": {
@@ -26,8 +26,7 @@
26
26
  "url": "https://github.com/sverweij/virtual-code-owners/issues"
27
27
  },
28
28
  "dependencies": {
29
- "ajv": "8.13.0",
30
- "yaml": "2.4.2"
29
+ "yaml": "2.4.3"
31
30
  },
32
31
  "engines": {
33
32
  "node": "^18.11.0||>=20.0.0"
@@ -1,16 +0,0 @@
1
- export default {
2
- $schema: "http://json-schema.org/draft-07/schema#",
3
- title: "virtual teams schema for virtual-code-owners",
4
- description: "a list of teams and their team members",
5
- $id: "org.js.virtual-code-owners/7.0.0",
6
- type: "object",
7
- additionalProperties: {
8
- type: "array",
9
- items: {
10
- type: "string",
11
- description:
12
- "Username or e-mail address of a team member. (Don't prefix usernames with '@')",
13
- pattern: "^[^@][^\\s]+$",
14
- },
15
- },
16
- };