Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
setup commit parsing for testing
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
  • Loading branch information
ShMcK committed Jun 23, 2020
commit 692cb660b744920aed3c470c96125d5e9e45cb58
80 changes: 43 additions & 37 deletions src/utils/commits.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from "fs";
import util from "util";
import * as path from "path";
import { ListLogSummary } from "simple-git/typings/response";
import gitP, { SimpleGit } from "simple-git/promise";
import { validateCommitOrder } from "./validateCommits";

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

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

export function parseCommits(logs: ListLogSummary<any>) {
// Filter relevant logs
const commits: CommitLogObject = {};
const positions: string[] = [];

for (const commit of logs.all) {
const matches = commit.message.match(
/^(?<stepId>(?<levelId>L?\d+)([S|\.]\d+))(?<stepType>[Q|A|T|S])?/
);

if (matches && matches.length) {
// Use an object of commit arrays to collect all commits
const position = matches[0];
if (!commits[position]) {
// does not exist, create the list
commits[position] = [commit.hash];
} else {
// add to the list
commits[position].unshift(commit.hash);
}
positions.unshift(position);
} else {
const initMatches = commit.message.match(/^INIT/);
if (initMatches && initMatches.length) {
if (!commits.INIT) {
// does not exist, create the list
commits.INIT = [commit.hash];
} else {
// add to the list
commits.INIT.unshift(commit.hash);
}
positions.unshift("INIT");
}
}
}
return { commits, positions };
}

export async function getCommits({
localDir,
codeBranch,
Expand Down Expand Up @@ -49,48 +88,19 @@ export async function getCommits({
// track the original branch in case of failure
const originalBranch = branches.current;

// Filter relevant logs
const commits: CommitLogObject = {};

try {
// Checkout the code branches
await git.checkout(codeBranch);

// Load all logs
const logs = await git.log();
const positions: string[] = [];

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

if (matches && matches.length) {
// Use an object of commit arrays to collect all commits
const position = matches[0];
if (!commits[position]) {
// does not exist, create the list
commits[position] = [commit.hash];
} else {
// add to the list
commits[position].unshift(commit.hash);
}
positions.unshift(position);
} else {
const initMatches = commit.message.match(/^INIT/);
if (initMatches && initMatches.length) {
if (!commits.INIT) {
// does not exist, create the list
commits.INIT = [commit.hash];
} else {
// add to the list
commits.INIT.unshift(commit.hash);
}
positions.unshift("INIT");
}
}
}
// validate order
validateCommitOrder(positions);

return commits;
} catch (e) {
console.error("Error with checkout or commit matching");
throw new Error(e.message);
Expand All @@ -100,8 +110,4 @@ export async function getCommits({
// cleanup the tmp directory
await rmdir(tmpDir, { recursive: true });
}

console.log(commits);

return commits;
}
8 changes: 8 additions & 0 deletions src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,14 @@ export function parse(params: ParseParams): any {
level.setup.commits = params.commits[level.id];
}

// @deprecated L1 system
if (params.commits[`L${level.id}`]) {
if (!level.setup) {
level.setup = {};
}
level.setup.commits = params.commits[`L${level.id}`];
}

return level;
}
);
Expand Down
6 changes: 4 additions & 2 deletions src/utils/validateCommits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ export function validateCommitOrder(positions: string[]): boolean {
current = { level: 0, step: 0 };
return;
} else {
const levelMatch = position.match(/^L?([0-9]+)Q?$/);
const stepMatch = position.match(/^L?([0-9]+)[S|\.]([0-9]+)[Q|A]?$/);
// @deprecate - remove L|Q
const levelMatch = position.match(/^L?([0-9]+)[Q|T]?$/);
// @deprecate - remove S|Q|A
const stepMatch = position.match(/^L?([0-9]+)[S|\.]([0-9]+)[Q|A|T|S]?$/);
if (levelMatch) {
// allows next level or step
const [_, levelString] = levelMatch;
Expand Down