Skip to content

Commit b56993a

Browse files
committed
get last passing commit hash
Signed-off-by: shmck <[email protected]>
1 parent f6c861f commit b56993a

File tree

5 files changed

+89
-16
lines changed

5 files changed

+89
-16
lines changed

Diff for: src/channel/index.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { openWorkspace, checkWorkspaceEmpty } from '../services/workspace'
1818
import { showOutput } from '../services/testRunner/output'
1919
import { exec } from '../services/node'
2020
import { WORKSPACE_ROOT, TUTORIAL_URL } from '../environment'
21+
import getLastCommitHash from '../services/git/lastHash'
2122

2223
const readFileAsync = promisify(readFile)
2324

@@ -322,11 +323,16 @@ class Channel implements Channel {
322323
return
323324
case 'EDITOR_RUN_RESET':
324325
// reset to timeline
325-
// 1. get last pass commit
326-
// 2. load timeline until last pass commit
326+
const tutorial: TT.Tutorial | null = this.context.tutorial.get()
327+
const position: T.Position = this.context.position.get()
328+
329+
// get last pass commit
330+
const hash = getLastCommitHash(position, tutorial?.levels || [])
331+
332+
// load timeline until last pass commit
333+
// TODO: run reset script
327334

328335
// if tutorial.config.reset.command, run it
329-
const tutorial: TT.Tutorial | null = this.context.tutorial.get()
330336
if (tutorial?.config?.reset?.command) {
331337
await exec({ command: tutorial.config.reset.command })
332338
}

Diff for: src/services/git/lastHash.test.ts

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import * as TT from '../../../typings/tutorial'
2+
import * as T from '../../../typings'
3+
import getLastCommitHash from './lastHash'
4+
5+
describe('lastHash', () => {
6+
it('should grab the last passing hash from a step', () => {
7+
const position: T.Position = { levelId: '1', stepId: '1.2' }
8+
const levels: TT.Level[] = [
9+
{
10+
id: '1',
11+
title: '',
12+
summary: '',
13+
content: '',
14+
steps: [
15+
{
16+
id: '1.1',
17+
content: '',
18+
setup: { commits: ['abcdef1'] },
19+
},
20+
{
21+
id: '1.2',
22+
content: '',
23+
setup: { commits: ['abcdef2'] },
24+
},
25+
],
26+
},
27+
]
28+
const result = getLastCommitHash(position, levels)
29+
expect(result).toBe('abcdef2')
30+
})
31+
it('should grab the last passing hash from a step with several commits', () => {
32+
const position: T.Position = { levelId: '1', stepId: '1.2' }
33+
const levels: TT.Level[] = [
34+
{
35+
id: '1',
36+
title: '',
37+
summary: '',
38+
content: '',
39+
steps: [
40+
{
41+
id: '1.1',
42+
content: '',
43+
setup: { commits: ['abcdef1'] },
44+
},
45+
{
46+
id: '1.2',
47+
content: '',
48+
setup: { commits: ['abcdef2', 'abcdef3'] },
49+
},
50+
],
51+
},
52+
]
53+
const result = getLastCommitHash(position, levels)
54+
expect(result).toBe('abcdef3')
55+
})
56+
})

Diff for: src/services/git/lastHash.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as TT from '../../../typings/tutorial'
2+
import * as T from '../../../typings'
3+
4+
const getLastCommitHash = (position: T.Position, levels: TT.Level[]) => {
5+
// get previous position
6+
const { levelId, stepId } = position
7+
8+
const level: TT.Level | undefined = levels.find((l) => levelId === l.id)
9+
if (!level) {
10+
throw new Error(`No level found matching ${levelId}`)
11+
}
12+
const step = level.steps.find((s) => stepId === s.id)
13+
if (!step) {
14+
throw new Error(`No step found matching ${stepId}`)
15+
}
16+
const commits = step.setup.commits
17+
if (!commits.length) {
18+
throw new Error(`No commits found on step ${stepId}`)
19+
}
20+
return commits[commits.length - 1]
21+
}
22+
23+
export default getLastCommitHash

Diff for: src/services/git/lastPass.ts

-12
This file was deleted.

Diff for: typings/tutorial.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export type Step = {
3434
id: string
3535
content: string
3636
setup: StepActions
37-
solution: Maybe<StepActions>
37+
solution?: Maybe<StepActions>
3838
hints?: string[]
3939
subtasks?: string[]
4040
}

0 commit comments

Comments
 (0)