Skip to content

Commit 692cb66

Browse files
committed
setup commit parsing for testing
Signed-off-by: shmck <[email protected]>
1 parent 8558b8e commit 692cb66

File tree

3 files changed

+55
-39
lines changed

3 files changed

+55
-39
lines changed

src/utils/commits.ts

+43-37
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as fs from "fs";
22
import util from "util";
33
import * as path from "path";
4+
import { ListLogSummary } from "simple-git/typings/response";
45
import gitP, { SimpleGit } from "simple-git/promise";
56
import { validateCommitOrder } from "./validateCommits";
67

@@ -15,6 +16,44 @@ type GetCommitOptions = {
1516

1617
export type CommitLogObject = { [position: string]: string[] };
1718

19+
export function parseCommits(logs: ListLogSummary<any>) {
20+
// Filter relevant logs
21+
const commits: CommitLogObject = {};
22+
const positions: string[] = [];
23+
24+
for (const commit of logs.all) {
25+
const matches = commit.message.match(
26+
/^(?<stepId>(?<levelId>L?\d+)([S|\.]\d+))(?<stepType>[Q|A|T|S])?/
27+
);
28+
29+
if (matches && matches.length) {
30+
// Use an object of commit arrays to collect all commits
31+
const position = matches[0];
32+
if (!commits[position]) {
33+
// does not exist, create the list
34+
commits[position] = [commit.hash];
35+
} else {
36+
// add to the list
37+
commits[position].unshift(commit.hash);
38+
}
39+
positions.unshift(position);
40+
} else {
41+
const initMatches = commit.message.match(/^INIT/);
42+
if (initMatches && initMatches.length) {
43+
if (!commits.INIT) {
44+
// does not exist, create the list
45+
commits.INIT = [commit.hash];
46+
} else {
47+
// add to the list
48+
commits.INIT.unshift(commit.hash);
49+
}
50+
positions.unshift("INIT");
51+
}
52+
}
53+
}
54+
return { commits, positions };
55+
}
56+
1857
export async function getCommits({
1958
localDir,
2059
codeBranch,
@@ -49,48 +88,19 @@ export async function getCommits({
4988
// track the original branch in case of failure
5089
const originalBranch = branches.current;
5190

52-
// Filter relevant logs
53-
const commits: CommitLogObject = {};
54-
5591
try {
5692
// Checkout the code branches
5793
await git.checkout(codeBranch);
5894

5995
// Load all logs
6096
const logs = await git.log();
61-
const positions: string[] = [];
6297

63-
for (const commit of logs.all) {
64-
const matches = commit.message.match(
65-
/^(?<stepId>(?<levelId>L?\d+)([S|\.]\d+))(?<stepType>[QA])?/
66-
);
98+
const { commits, positions } = parseCommits(logs);
6799

68-
if (matches && matches.length) {
69-
// Use an object of commit arrays to collect all commits
70-
const position = matches[0];
71-
if (!commits[position]) {
72-
// does not exist, create the list
73-
commits[position] = [commit.hash];
74-
} else {
75-
// add to the list
76-
commits[position].unshift(commit.hash);
77-
}
78-
positions.unshift(position);
79-
} else {
80-
const initMatches = commit.message.match(/^INIT/);
81-
if (initMatches && initMatches.length) {
82-
if (!commits.INIT) {
83-
// does not exist, create the list
84-
commits.INIT = [commit.hash];
85-
} else {
86-
// add to the list
87-
commits.INIT.unshift(commit.hash);
88-
}
89-
positions.unshift("INIT");
90-
}
91-
}
92-
}
100+
// validate order
93101
validateCommitOrder(positions);
102+
103+
return commits;
94104
} catch (e) {
95105
console.error("Error with checkout or commit matching");
96106
throw new Error(e.message);
@@ -100,8 +110,4 @@ export async function getCommits({
100110
// cleanup the tmp directory
101111
await rmdir(tmpDir, { recursive: true });
102112
}
103-
104-
console.log(commits);
105-
106-
return commits;
107113
}

src/utils/parse.ts

+8
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,14 @@ export function parse(params: ParseParams): any {
187187
level.setup.commits = params.commits[level.id];
188188
}
189189

190+
// @deprecated L1 system
191+
if (params.commits[`L${level.id}`]) {
192+
if (!level.setup) {
193+
level.setup = {};
194+
}
195+
level.setup.commits = params.commits[`L${level.id}`];
196+
}
197+
190198
return level;
191199
}
192200
);

src/utils/validateCommits.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ export function validateCommitOrder(positions: string[]): boolean {
1313
current = { level: 0, step: 0 };
1414
return;
1515
} else {
16-
const levelMatch = position.match(/^L?([0-9]+)Q?$/);
17-
const stepMatch = position.match(/^L?([0-9]+)[S|\.]([0-9]+)[Q|A]?$/);
16+
// @deprecate - remove L|Q
17+
const levelMatch = position.match(/^L?([0-9]+)[Q|T]?$/);
18+
// @deprecate - remove S|Q|A
19+
const stepMatch = position.match(/^L?([0-9]+)[S|\.]([0-9]+)[Q|A|T|S]?$/);
1820
if (levelMatch) {
1921
// allows next level or step
2022
const [_, levelString] = levelMatch;

0 commit comments

Comments
 (0)