Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Feature/markdown as master #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jun 24, 2020
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
parse commits with tests
Signed-off-by: shmck <[email protected]>
  • Loading branch information
ShMcK committed Jun 23, 2020
commit deba9b3c887b2e49724a8354850d1f31936d7d66
47 changes: 27 additions & 20 deletions src/utils/commits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,52 @@ type GetCommitOptions = {

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

export function parseCommits(logs: ListLogSummary<any>) {


export function parseCommits(logs: ListLogSummary<any>): { [hash: string]: string[]} {
// 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])?/
/^(?<init>INIT)|(L?(?<levelId>\d+)[S|\.]?(?<stepId>\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];
const { groups } = matches
let position
if (groups.init) {
position = 'INIT'
} else if (groups.levelId && groups.stepId) {
let stepType
// @deprecated Q
if (!groups.stepType || ['Q', 'T'].includes(groups.stepType)) {
stepType = 'T' // test
// @deprecated A
} else if (!groups.stepType || ['A', 'S'].includes(groups.stepType)) {
stepType = 'S' // solution
}
position = `${groups.levelId}.${groups.stepId}:${stepType}`
} else if (groups.levelId) {
position = groups.levelId
} else {
// add to the list
commits[position].unshift(commit.hash);
console.warn(`No matcher for commit "${commit.message}"`)
}
commits[position] = [...(commits[position] || []), 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);
}
commits.INIT = [...(commits.INIT || []), commit.hash]
positions.unshift("INIT");
}
}
}
return { commits, positions };
// validate order
validateCommitOrder(positions);
return commits;
}

export async function getCommits({
Expand Down Expand Up @@ -95,10 +105,7 @@ export async function getCommits({
// Load all logs
const logs = await git.log();

const { commits, positions } = parseCommits(logs);

// validate order
validateCommitOrder(positions);
const commits = parseCommits(logs);

return commits;
} catch (e) {
Expand Down
151 changes: 151 additions & 0 deletions tests/commitParse.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import { parseCommits } from "../src/utils/commits";

describe("commitParse", () => {
it("should parse out #. commits", () => {
const logs = {
all: [
{
message: "INIT",
hash: "1",
},
{
message: "1. First Level",
hash: "2",
},
{
message: "1.1 First Step",
hash: "3",
},
],
total: 2,
latest: {},
};
const commits = parseCommits(logs);
expect(commits).toEqual({
INIT: ["1"],
"1": ["2"],
"1.1:T": ["3"],
});
});
// @deprecated - remove L#
it("should parse out L# commits", () => {
const logs = {
all: [
{
message: "INIT",
hash: "1",
},
{
message: "L1 First Level",
hash: "2",
},
{
message: "L1S1 First Step",
hash: "3",
},
],
total: 2,
latest: {},
};
const commits = parseCommits(logs);
expect(commits).toEqual({
INIT: ["1"],
"1": ["2"],
"1.1:T": ["3"],
});
});
// @deprecated - remove with QA
it("should parse out #.Q|A commits", () => {
const logs = {
all: [
{
message: "INIT",
hash: "1",
},
{
message: "1. First Level",
hash: "2",
},
{
message: "1.1Q First Step",
hash: "3",
},
{
message: "1.1A First Step Solution",
hash: "4",
},
],
total: 2,
latest: {},
};
const commits = parseCommits(logs);
expect(commits).toEqual({
INIT: ["1"],
"1": ["2"],
"1.1:T": ["3"],
"1.1:S": ["4"],
});
});
it("should parse out #.T|S commits", () => {
const logs = {
all: [
{
message: "INIT",
hash: "1",
},
{
message: "1. First Level",
hash: "2",
},
{
message: "1.1T First Step",
hash: "3",
},
{
message: "1.1S First Step Solution",
hash: "4",
},
],
total: 2,
latest: {},
};
const commits = parseCommits(logs);
expect(commits).toEqual({
INIT: ["1"],
"1": ["2"],
"1.1:T": ["3"],
"1.1:S": ["4"],
});
});
it("should parse out #._|S commits", () => {
const logs = {
all: [
{
message: "INIT",
hash: "1",
},
{
message: "1. First Level",
hash: "2",
},
{
message: "1.1 First Step",
hash: "3",
},
{
message: "1.1S First Step Solution",
hash: "4",
},
],
total: 2,
latest: {},
};
const commits = parseCommits(logs);
expect(commits).toEqual({
INIT: ["1"],
"1": ["2"],
"1.1:T": ["3"],
"1.1:S": ["4"],
});
});
});